KickJava   Java API By Example, From Geeks To Geeks.

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


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.RepositoryManager;
20 import org.outerj.daisy.repository.Credentials;
21 import org.outerj.daisy.repository.Repository;
22 import org.outerj.daisy.repository.RepositoryException;
23 import org.outerj.daisy.repository.user.Role;
24 import org.outerj.daisy.repository.variant.*;
25
26 /**
27  * This testcase tests the basic management of the branch and language
28  * entities. For actual testing of the variant functionality of documents,
29  * see {@link AbstractDocVariantTest}.
30  */

31 public abstract class AbstractVariantTest extends AbstractDaisyTestCase {
32     protected boolean resetDataStores() {
33         return true;
34     }
35
36     protected abstract RepositoryManager getRepositoryManager() throws Exception JavaDoc;
37
38     protected void moreTests() throws Exception JavaDoc {
39         // allows subclasses to do more tests within the same test run
40
}
41
42     public void testVariants() throws Exception JavaDoc {
43         RepositoryManager repositoryManager = getRepositoryManager();
44         Repository repository = repositoryManager.getRepository(new Credentials("testuser", "testuser"));
45         repository.switchRole(Role.ADMINISTRATOR);
46         Role userRole = repository.getUserManager().getRole("User", false);
47
48         VariantManager variantManager = repository.getVariantManager();
49
50         {
51             Branch branch = variantManager.createBranch("branch1");
52             branch.save();
53
54             assertEquals(branch.getLastModifier(), repository.getUserId());
55             long lastModified = branch.getLastModified().getTime();
56             assertTrue(lastModified < System.currentTimeMillis() && lastModified > System.currentTimeMillis() - 3000);
57             assertEquals(1, branch.getUpdateCount());
58
59             branch = variantManager.getBranch(branch.getId(), true);
60             assertEquals("branch1", branch.getName());
61
62             try {
63                 variantManager.getBranch(branch.getId() + 1, true);
64                 fail("Getting a non-existing branch should have failed.");
65             } catch (BranchNotFoundException e) {}
66
67             try {
68                 variantManager.getBranch("xyz", true);
69                 fail("Getting a non-existing branch should have failed.");
70             } catch (BranchNotFoundException e) {}
71
72             Branch cachedBranch = variantManager.getBranch(branch.getId(), false);
73             try {
74                 cachedBranch.setName("hallo");
75                 fail("Modifying a cached/shared object should throw an exception.");
76             } catch (RuntimeException JavaDoc e) {}
77
78             // test duplicate name test
79
Branch dupbranch = variantManager.createBranch("branch1");
80             try {
81                 dupbranch.save();
82                 fail("Saving a branch with the same name as an existing branch should fail.");
83             } catch (RepositoryException e) {}
84
85             // test description
86
branch = variantManager.getBranch("branch1", true);
87             branch.setDescription("beautiful branch");
88             branch.save();
89
90             branch = variantManager.getBranch(branch.getId(), true);
91             assertEquals("beautiful branch", branch.getDescription());
92
93             // test concurrent modification check
94
Branch altBranch = variantManager.getBranch("branch1", true);
95             altBranch.save();
96
97             try {
98                 branch.save();
99                 fail("Saving branch should fail due to concurrent modification.");
100             } catch (RepositoryException e) {}
101
102             Branch branch2 = variantManager.createBranch("branch2");
103             branch2.save();
104             Branch branch3 = variantManager.createBranch("branch3");
105             branch3.save();
106
107             assertEquals(4, variantManager.getAllBranches(true).size());
108             assertEquals(4, variantManager.getAllBranches(false).size());
109
110             // main branch should always exist
111
Branch mainBranch = variantManager.getBranch(1, true);
112             assertEquals("main", mainBranch.getName());
113             try {
114                 mainBranch.save();
115                 fail("Saving the branch 'main' should fail.");
116             } catch (RepositoryException e) {}
117
118             try {
119                 variantManager.createBranch("name with spaces");
120                 fail("Creating a branch with an invalid name should fail");
121             } catch (IllegalArgumentException JavaDoc e) {}
122
123             try {
124                 branch3.setName("123abc");
125                 fail("Setting invalid branch name should fail");
126             } catch (IllegalArgumentException JavaDoc e) {}
127
128             // deletion
129
Branch branch4 = variantManager.createBranch("branch4");
130             branch4.save();
131             // make sure branch is in cache
132
variantManager.getBranch(branch4.getId(), false);
133             repository.switchRole(userRole.getId());
134             try {
135                 variantManager.deleteBranch(branch4.getId());
136                 fail("Deletion of branch by non-admin user should have failed.");
137             } catch (RepositoryException e) {}
138             repository.switchRole(Role.ADMINISTRATOR);
139             variantManager.deleteBranch(branch4.getId());
140
141             try {
142                 variantManager.getBranch(branch4.getId(), false);
143                 fail("Getting deleted branch should fail.");
144             } catch (BranchNotFoundException e) {}
145
146             try {
147                 variantManager.getBranch(branch4.getId(), true);
148                 fail("Getting deleted branch should fail.");
149             } catch (BranchNotFoundException e) {}
150
151             try {
152                 variantManager.deleteBranch(Branch.MAIN_BRANCH_ID);
153                 fail("Deleting branch 'main' should have failed.");
154             } catch (RepositoryException e) {}
155
156         }
157
158         //
159
// Same tests for languages
160
//
161
{
162             Language language = variantManager.createLanguage("language1");
163             language.save();
164
165             assertEquals(language.getLastModifier(), repository.getUserId());
166             long lastModified = language.getLastModified().getTime();
167             assertTrue(lastModified < System.currentTimeMillis() && lastModified > System.currentTimeMillis() - 3000);
168             assertEquals(1, language.getUpdateCount());
169
170             language = variantManager.getLanguage(language.getId(), true);
171             assertEquals("language1", language.getName());
172
173             try {
174                 variantManager.getLanguage(language.getId() + 1, true);
175                 fail("Getting a non-existing language should have failed.");
176             } catch (LanguageNotFoundException e) {}
177
178             try {
179                 variantManager.getLanguage("xyz", true);
180                 fail("Getting a non-existing language should have failed.");
181             } catch (LanguageNotFoundException e) {}
182
183             Language cachedLanguage = variantManager.getLanguage(language.getId(), false);
184             try {
185                 cachedLanguage.setName("hallo");
186                 fail("Modifying a cached/shared object should throw an exception.");
187             } catch (RuntimeException JavaDoc e) {}
188
189             // test duplicate name test
190
Language duplanguage = variantManager.createLanguage("language1");
191             try {
192                 duplanguage.save();
193                 fail("Saving a language with the same name as an existing language should fail.");
194             } catch (RepositoryException e) {}
195
196             // test description
197
language = variantManager.getLanguage("language1", true);
198             language.setDescription("beautiful language");
199             language.save();
200
201             language = variantManager.getLanguage(language.getId(), true);
202             assertEquals("beautiful language", language.getDescription());
203
204             // test concurrent modification check
205
Language altLanguage = variantManager.getLanguage("language1", true);
206             altLanguage.save();
207
208             try {
209                 language.save();
210                 fail("Saving language should fail due to concurrent modification.");
211             } catch (RepositoryException e) {}
212
213             Language language2 = variantManager.createLanguage("language2");
214             language2.save();
215             Language language3 = variantManager.createLanguage("language3");
216             language3.save();
217
218             assertEquals(4, variantManager.getAllLanguages(true).size());
219             assertEquals(4, variantManager.getAllLanguages(false).size());
220
221             // main language should always exist
222
Language defaultLanguage = variantManager.getLanguage(1, true);
223             assertEquals("default", defaultLanguage.getName());
224             try {
225                 defaultLanguage.save();
226                 fail("Saving the language 'default' should fail.");
227             } catch (RepositoryException e) {}
228
229             try {
230                 variantManager.createLanguage("name with spaces");
231                 fail("Creating a language with an invalid name should fail");
232             } catch (IllegalArgumentException JavaDoc e) {}
233
234             try {
235                 language3.setName("123abc");
236                 fail("Setting invalid language name should fail");
237             } catch (IllegalArgumentException JavaDoc e) {}
238
239             // deletion
240
Language language4 = variantManager.createLanguage("language4");
241             language4.save();
242             // make sure language is in cache
243
variantManager.getLanguage(language4.getId(), false);
244             repository.switchRole(userRole.getId());
245             try {
246                 variantManager.deleteLanguage(language4.getId());
247                 fail("Deletion of language by non-admin user should have failed.");
248             } catch (RepositoryException e) {}
249             repository.switchRole(Role.ADMINISTRATOR);
250             variantManager.deleteLanguage(language4.getId());
251
252             try {
253                 variantManager.getLanguage(language4.getId(), false);
254                 fail("Getting deleted language should fail.");
255             } catch (LanguageNotFoundException e) {}
256
257             try {
258                 variantManager.getLanguage(language4.getId(), true);
259                 fail("Getting deleted language should fail.");
260             } catch (LanguageNotFoundException e) {}
261
262             try {
263                 variantManager.deleteLanguage(Language.DEFAULT_LANGUAGE_ID);
264                 fail("Deleting language 'default' should have failed.");
265             } catch (RepositoryException e) {}
266
267         }
268
269         moreTests();
270     }
271 }
272
Popular Tags