vgritsenko    2003/12/24 06:10:05

  Modified:    java/tests/src/org/apache/xindice/core DatabaseTest.java
               java/src/org/apache/xindice/core Collection.java
                        CollectionManager.java
  Log:
  Add additional checks into createCollection
  Add more createCollection tests into DatabaseTest
  
  Revision  Changes    Path
  1.2       +58 -18    
xml-xindice/java/tests/src/org/apache/xindice/core/DatabaseTest.java
  
  Index: DatabaseTest.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/tests/src/org/apache/xindice/core/DatabaseTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DatabaseTest.java 22 Dec 2003 02:14:07 -0000      1.1
  +++ DatabaseTest.java 24 Dec 2003 14:10:04 -0000      1.2
  @@ -65,12 +65,15 @@
   import junit.framework.TestCase;
   
   /**
  - * @version CVS $Revision$, $Date$
  + * Tests Xindice Core API (org.apache.xindice.core.Database,
  + * org.apache.xindice.core.Collection) createCollection.
  + *
    * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a>
  + * @version CVS $Revision$, $Date$
    */
   public class DatabaseTest extends TestCase {
   
  -    final String DATABASE =
  +    public static final String DATABASE =
           "<root-collection dbroot=\"db/\" name=\"db\">" +
               "<queryengine>" +
                   "<resolver autoindex=\"false\" 
class=\"org.apache.xindice.core.query.XPathQueryResolver\" />" +
  @@ -78,26 +81,25 @@
               "</queryengine>" +
           "</root-collection>";
   
  -    final String COLLECTIONA =
  +    public static final String COLLECTIONA =
           "<collection compressed=\"true\" name=\"CollectionA\">" +
               "<filer class=\"org.apache.xindice.core.filer.BTreeFiler\" />" +
               "<indexes />" +
           "</collection>";
   
  -    final String COLLECTIONB =
  +    public static final String COLLECTIONB =
           "<collection compressed=\"true\" name=\"CollectionB\">" +
               "<filer class=\"org.apache.xindice.core.filer.BTreeFiler\" />" +
               "<indexes />" +
           "</collection>";
   
  -    final String COLLECTIONC =
  -        "<collection compressed=\"true\" name=\"CollectionC\">" +
  +    public static final String COLLECTIONC =
  +        "<collection compressed=\"true\" name=\"CollectionC\" 
inline-metadata=\"true\">" +
               "<filer class=\"org.apache.xindice.core.filer.BTreeFiler\" />" +
               "<indexes />" +
           "</collection>";
   
  -
  -     private static Database db;
  +     private Database db;
       private Configuration configurationA;
       private Configuration configurationB;
       private Configuration configurationC;
  @@ -114,22 +116,62 @@
           configurationC = new 
Configuration(DOMParser.toDocument(COLLECTIONC));
       }
   
  +    public void testCreateCollectionNullName() throws Exception {
  +        try {
  +            db.createCollection(null, configurationA);
  +            fail("DBException expected");
  +        } catch (DBException e) {
  +            // Ok
  +        }
  +    }
  +
  +    public void testCreateCollectionEmptyName() throws Exception {
  +        try {
  +            db.createCollection("", configurationA);
  +            fail("DBException expected");
  +        } catch (DBException e) {
  +            // Ok
  +        }
  +    }
  +
  +    public void testCreateCollectionNullConfig() throws Exception {
  +        try {
  +            db.createCollection("name", null);
  +            fail("DBException expected");
  +        } catch (DBException e) {
  +            // Ok
  +        }
  +    }
  +
       public void testCreateCollection() throws Exception {
           Collection collection = db.createCollection("CollectionA", 
configurationA);
           assertNotNull("Collection expected", collection);
           assertEquals("Collection name", "CollectionA", collection.getName());
       }
   
  -    public void testFailCreateCollection() throws Exception {
  -        Collection collection = null;
  +    public void testCreateDuplicateCollection() throws Exception {
  +        db.createCollection("CollectionA", configurationA);
           try {
  -            collection = db.createCollection("somepath", configurationA);
  +            db.createCollection("CollectionA", configurationA);
  +            fail("DBException expected");
  +        } catch (DBException e) {
  +            // Ok
  +        }
  +    }
  +
  +    public void testCreateCollectionWrongName() throws Exception {
  +        try {
  +            // Name does not match name in configuration
  +            db.createCollection("somepath", configurationA);
               fail("Exception expected");
           } catch (DBException e) {
               // Expected
           }
       }
   
  +    /**
  +     * Multithreaded getCollection test
  +     */
       public void testMTGetCollection() throws Exception {
           Collection a = db.createCollection("CollectionA", configurationA);
           Collection b = db.createCollection("CollectionB", configurationB);
  @@ -191,15 +233,13 @@
       public void tearDown() throws Exception {
           try {
               db.dropCollection(db.getCollection("CollectionA"));
  -        } catch (Exception e) {
  -        }
  +        } catch (Exception e) {}
           try {
               db.dropCollection(db.getCollection("CollectionB"));
  -        } catch (Exception e) {
  -        }
  +        } catch (Exception e) {}
           try {
               db.dropCollection(db.getCollection("CollectionC"));
  -        } catch (Exception e) {
  -        }
  +        } catch (Exception e) {}
  +        db.close();
       }
   }
  
  
  
  1.42      +3 -2      
xml-xindice/java/src/org/apache/xindice/core/Collection.java
  
  Index: Collection.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/Collection.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- Collection.java   23 Dec 2003 12:20:07 -0000      1.41
  +++ Collection.java   24 Dec 2003 14:10:04 -0000      1.42
  @@ -1204,6 +1204,7 @@
               }
           }
   
  +        // TODO: Move into if(compressed) ?
           flushSymbolTable();
   
           // Temporary until insert and update are separate
  
  
  
  1.22      +21 -13    
xml-xindice/java/src/org/apache/xindice/core/CollectionManager.java
  
  Index: CollectionManager.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/CollectionManager.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- CollectionManager.java    22 Dec 2003 02:10:44 -0000      1.21
  +++ CollectionManager.java    24 Dec 2003 14:10:04 -0000      1.22
  @@ -82,10 +82,11 @@
   
       private static final String COLLECTION = "collection";
       private static final String COLLECTIONS = "collections";
  -    private static final String[] EmptyStrings = new String[0];
  +    private static final String[] EMPTY_STRINGS = new String[0];
       private static final String NAME = "name";
   
  -    private final Map collections = new HashMap(); // Collection
  +    // FIXME: Access to collections and config is not synchronized
  +    private final Map collections = new HashMap();
       private Configuration config = null;
   
   
  @@ -118,6 +119,11 @@
        * @return The newly created Collection
        */
       public Collection createCollection(String path, Configuration cfg) 
throws DBException {
  +        if (path == null || "".equals(path)) {
  +            throw new DBException(FaultCodes.COL_CANNOT_CREATE,
  +                                  "Null or empty collection name");
  +        }
  +
           if (path.indexOf("/") != -1) {
               CollectionManager cm = this;
               StringTokenizer st = new StringTokenizer(path, "/");
  @@ -138,21 +144,21 @@
                                     "Parent Collection '" + path + "' doesn't 
exist");
           }
   
  -        Collection collection = null;
  -        if (CollectionManager.this instanceof Database) {
  -            collection = new Collection((Database) CollectionManager.this);
  -        } else {
  -            collection = new Collection((Collection) CollectionManager.this);
  +        if (path.indexOf('/') != -1) {
  +            throw new DBException(FaultCodes.COL_CANNOT_CREATE, "Name cannot 
contain '/'");
           }
   
  +        Collection collection = new Collection((Collection) this);
           try {
               // Do a name check to see if all is well
               String n = cfg.getAttribute(NAME);
               if (n == null || n.trim().equals("")) {
  -                throw new DBException(FaultCodes.COL_CANNOT_CREATE, "No name 
specified");
  +                throw new DBException(FaultCodes.COL_CANNOT_CREATE,
  +                                      "No name specified in collection 
configuration");
               }
               if (!n.equals(path)) {
  -                throw new DBException(FaultCodes.COL_CANNOT_CREATE, "Name 
does not match with path name");
  +                throw new DBException(FaultCodes.COL_CANNOT_CREATE,
  +                                      "Name does not match with path name");
               }
   
               if (getCollection(n) != null) {
  @@ -166,7 +172,9 @@
               collection.setConfig(cfg);
               collection.create();
               collections.put(n, collection);
  -            log.info("Created a new collection named '" + n + "'");
  +            if (log.isInfoEnabled()) {
  +                log.info("Created a new collection named '" + n + "'");
  +            }
           } catch (DBException e) {
               // Do not wrap DBException
               throw e;
  @@ -269,7 +277,7 @@
        * @return The Collection list
        */
       public final String[] listCollections() throws DBException {
  -        return (String[]) collections.keySet().toArray(EmptyStrings);
  +        return (String[]) collections.keySet().toArray(EMPTY_STRINGS);
       }
   
       /**
  
  
  

Reply via email to