KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > test > AbstractCollectionTest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.test;
17
18 import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase;
19 import org.outerj.daisy.repository.*;
20 import org.outerj.daisy.repository.user.Role;
21
22 public abstract class AbstractCollectionTest extends AbstractDaisyTestCase {
23     protected boolean resetDataStores() {
24         return true;
25     }
26
27     protected abstract RepositoryManager getRepositoryManager() throws Exception JavaDoc;
28
29     public void testCollections() throws Exception JavaDoc {
30         RepositoryManager repositoryManager = getRepositoryManager();
31         Repository repository = repositoryManager.getRepository(new Credentials("testuser", "testuser"));
32         repository.switchRole(Role.ADMINISTRATOR);
33
34         CollectionManager collectionManager = repository.getCollectionManager();
35
36         DocumentCollection collection1 = collectionManager.createCollection("collection1");
37         collection1.save();
38
39         DocumentCollection fetchedCollection1 = collectionManager.getCollection(collection1.getId(), true);
40         assertEquals("collection name is stored correctly", collection1.getName(), fetchedCollection1.getName());
41
42         //some extra tests on the object we just fetched
43
assertTrue("data store id of stored collection is -1 or less", fetchedCollection1.getId() > -1);
44         assertNotNull("last modified date of stored collection is null", fetchedCollection1.getLastModified());
45         assertTrue("last modifier of stored collection is invalid", fetchedCollection1.getLastModifier() >= -1);
46         assertNotNull("xml object returned by stored collection is null", fetchedCollection1.getXml());
47
48         // try to modify the collection
49
collection1.setName(collection1.getName() + "x");
50         collection1.save();
51         assertEquals("collection name change correctly saved", collection1.getName(),
52                 collectionManager.getCollection(collection1.getId(), true).getName());
53
54         // try to retrieve the collection by name
55
assertEquals("retrieving collection by name should give collection with correct id",
56                 collection1.getId(), collectionManager.getCollectionByName(collection1.getName(), true).getId());
57
58         /* at this point there should be at least one collection in the list of all collections,
59          * so the collection list can easily be loaded here.
60          */

61         DocumentCollections collections = collectionManager.getCollections(true);
62         assertNotNull(collections);
63         DocumentCollection[] collArray = collections.getArray();
64         assertNotNull(collArray);
65
66         for (int i = 0; i < collArray.length; i++) {
67             DocumentCollection collection = collArray[i];
68             assertNotNull(collection);
69         }
70
71         //
72
// test proper invalidation of collection caches (if any)
73
//
74
DocumentCollection collection2 = collectionManager.createCollection("collection2");
75         collection2.save();
76
77         // line below should NOT throw an CollectionNotFoundException
78
collectionManager.getCollection(collection2.getId(), false);
79
80         collection2.setName("collection2-modified");
81         collection2.save();
82
83         DocumentCollection collection2Refetched = collectionManager.getCollection(collection2.getId(), false);
84         assertEquals(collection2.getName(), collection2Refetched.getName());
85
86         collectionManager.deleteCollection(collection2.getId());
87         try {
88             collectionManager.getCollection(collection2.getId(), false);
89             fail("Getting a deleted exception should throw an exception.");
90         } catch (CollectionNotFoundException e) {}
91
92         DocumentCollection collection3 = collectionManager.createCollection("a b");
93         collection3.save();
94         assertEquals(collection3.getId(), collectionManager.getCollectionByName("a b", true).getId());
95         assertEquals(collection3.getId(), collectionManager.getCollectionByName("a b", false).getId());
96
97         // NOTE: test of assigning collections to documents are part of the AbstractDocumentTest
98
}
99 }
100
Popular Tags