001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.test;
017:
018: import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase;
019: import org.outerj.daisy.repository.RepositoryManager;
020: import org.outerj.daisy.repository.Credentials;
021: import org.outerj.daisy.repository.Repository;
022: import org.outerj.daisy.repository.RepositoryException;
023: import org.outerj.daisy.repository.user.Role;
024: import org.outerj.daisy.repository.variant.*;
025:
026: /**
027: * This testcase tests the basic management of the branch and language
028: * entities. For actual testing of the variant functionality of documents,
029: * see {@link AbstractDocVariantTest}.
030: */
031: public abstract class AbstractVariantTest extends AbstractDaisyTestCase {
032: protected boolean resetDataStores() {
033: return true;
034: }
035:
036: protected abstract RepositoryManager getRepositoryManager()
037: throws Exception;
038:
039: protected void moreTests() throws Exception {
040: // allows subclasses to do more tests within the same test run
041: }
042:
043: public void testVariants() throws Exception {
044: RepositoryManager repositoryManager = getRepositoryManager();
045: Repository repository = repositoryManager
046: .getRepository(new Credentials("testuser", "testuser"));
047: repository.switchRole(Role.ADMINISTRATOR);
048: Role userRole = repository.getUserManager().getRole("User",
049: false);
050:
051: VariantManager variantManager = repository.getVariantManager();
052:
053: {
054: Branch branch = variantManager.createBranch("branch1");
055: branch.save();
056:
057: assertEquals(branch.getLastModifier(), repository
058: .getUserId());
059: long lastModified = branch.getLastModified().getTime();
060: assertTrue(lastModified < System.currentTimeMillis()
061: && lastModified > System.currentTimeMillis() - 3000);
062: assertEquals(1, branch.getUpdateCount());
063:
064: branch = variantManager.getBranch(branch.getId(), true);
065: assertEquals("branch1", branch.getName());
066:
067: try {
068: variantManager.getBranch(branch.getId() + 1, true);
069: fail("Getting a non-existing branch should have failed.");
070: } catch (BranchNotFoundException e) {
071: }
072:
073: try {
074: variantManager.getBranch("xyz", true);
075: fail("Getting a non-existing branch should have failed.");
076: } catch (BranchNotFoundException e) {
077: }
078:
079: Branch cachedBranch = variantManager.getBranch(branch
080: .getId(), false);
081: try {
082: cachedBranch.setName("hallo");
083: fail("Modifying a cached/shared object should throw an exception.");
084: } catch (RuntimeException e) {
085: }
086:
087: // test duplicate name test
088: Branch dupbranch = variantManager.createBranch("branch1");
089: try {
090: dupbranch.save();
091: fail("Saving a branch with the same name as an existing branch should fail.");
092: } catch (RepositoryException e) {
093: }
094:
095: // test description
096: branch = variantManager.getBranch("branch1", true);
097: branch.setDescription("beautiful branch");
098: branch.save();
099:
100: branch = variantManager.getBranch(branch.getId(), true);
101: assertEquals("beautiful branch", branch.getDescription());
102:
103: // test concurrent modification check
104: Branch altBranch = variantManager
105: .getBranch("branch1", true);
106: altBranch.save();
107:
108: try {
109: branch.save();
110: fail("Saving branch should fail due to concurrent modification.");
111: } catch (RepositoryException e) {
112: }
113:
114: Branch branch2 = variantManager.createBranch("branch2");
115: branch2.save();
116: Branch branch3 = variantManager.createBranch("branch3");
117: branch3.save();
118:
119: assertEquals(4, variantManager.getAllBranches(true).size());
120: assertEquals(4, variantManager.getAllBranches(false).size());
121:
122: // main branch should always exist
123: Branch mainBranch = variantManager.getBranch(1, true);
124: assertEquals("main", mainBranch.getName());
125: try {
126: mainBranch.save();
127: fail("Saving the branch 'main' should fail.");
128: } catch (RepositoryException e) {
129: }
130:
131: try {
132: variantManager.createBranch("name with spaces");
133: fail("Creating a branch with an invalid name should fail");
134: } catch (IllegalArgumentException e) {
135: }
136:
137: try {
138: branch3.setName("123abc");
139: fail("Setting invalid branch name should fail");
140: } catch (IllegalArgumentException e) {
141: }
142:
143: // deletion
144: Branch branch4 = variantManager.createBranch("branch4");
145: branch4.save();
146: // make sure branch is in cache
147: variantManager.getBranch(branch4.getId(), false);
148: repository.switchRole(userRole.getId());
149: try {
150: variantManager.deleteBranch(branch4.getId());
151: fail("Deletion of branch by non-admin user should have failed.");
152: } catch (RepositoryException e) {
153: }
154: repository.switchRole(Role.ADMINISTRATOR);
155: variantManager.deleteBranch(branch4.getId());
156:
157: try {
158: variantManager.getBranch(branch4.getId(), false);
159: fail("Getting deleted branch should fail.");
160: } catch (BranchNotFoundException e) {
161: }
162:
163: try {
164: variantManager.getBranch(branch4.getId(), true);
165: fail("Getting deleted branch should fail.");
166: } catch (BranchNotFoundException e) {
167: }
168:
169: try {
170: variantManager.deleteBranch(Branch.MAIN_BRANCH_ID);
171: fail("Deleting branch 'main' should have failed.");
172: } catch (RepositoryException e) {
173: }
174:
175: }
176:
177: //
178: // Same tests for languages
179: //
180: {
181: Language language = variantManager
182: .createLanguage("language1");
183: language.save();
184:
185: assertEquals(language.getLastModifier(), repository
186: .getUserId());
187: long lastModified = language.getLastModified().getTime();
188: assertTrue(lastModified < System.currentTimeMillis()
189: && lastModified > System.currentTimeMillis() - 3000);
190: assertEquals(1, language.getUpdateCount());
191:
192: language = variantManager.getLanguage(language.getId(),
193: true);
194: assertEquals("language1", language.getName());
195:
196: try {
197: variantManager.getLanguage(language.getId() + 1, true);
198: fail("Getting a non-existing language should have failed.");
199: } catch (LanguageNotFoundException e) {
200: }
201:
202: try {
203: variantManager.getLanguage("xyz", true);
204: fail("Getting a non-existing language should have failed.");
205: } catch (LanguageNotFoundException e) {
206: }
207:
208: Language cachedLanguage = variantManager.getLanguage(
209: language.getId(), false);
210: try {
211: cachedLanguage.setName("hallo");
212: fail("Modifying a cached/shared object should throw an exception.");
213: } catch (RuntimeException e) {
214: }
215:
216: // test duplicate name test
217: Language duplanguage = variantManager
218: .createLanguage("language1");
219: try {
220: duplanguage.save();
221: fail("Saving a language with the same name as an existing language should fail.");
222: } catch (RepositoryException e) {
223: }
224:
225: // test description
226: language = variantManager.getLanguage("language1", true);
227: language.setDescription("beautiful language");
228: language.save();
229:
230: language = variantManager.getLanguage(language.getId(),
231: true);
232: assertEquals("beautiful language", language
233: .getDescription());
234:
235: // test concurrent modification check
236: Language altLanguage = variantManager.getLanguage(
237: "language1", true);
238: altLanguage.save();
239:
240: try {
241: language.save();
242: fail("Saving language should fail due to concurrent modification.");
243: } catch (RepositoryException e) {
244: }
245:
246: Language language2 = variantManager
247: .createLanguage("language2");
248: language2.save();
249: Language language3 = variantManager
250: .createLanguage("language3");
251: language3.save();
252:
253: assertEquals(4, variantManager.getAllLanguages(true).size());
254: assertEquals(4, variantManager.getAllLanguages(false)
255: .size());
256:
257: // main language should always exist
258: Language defaultLanguage = variantManager.getLanguage(1,
259: true);
260: assertEquals("default", defaultLanguage.getName());
261: try {
262: defaultLanguage.save();
263: fail("Saving the language 'default' should fail.");
264: } catch (RepositoryException e) {
265: }
266:
267: try {
268: variantManager.createLanguage("name with spaces");
269: fail("Creating a language with an invalid name should fail");
270: } catch (IllegalArgumentException e) {
271: }
272:
273: try {
274: language3.setName("123abc");
275: fail("Setting invalid language name should fail");
276: } catch (IllegalArgumentException e) {
277: }
278:
279: // deletion
280: Language language4 = variantManager
281: .createLanguage("language4");
282: language4.save();
283: // make sure language is in cache
284: variantManager.getLanguage(language4.getId(), false);
285: repository.switchRole(userRole.getId());
286: try {
287: variantManager.deleteLanguage(language4.getId());
288: fail("Deletion of language by non-admin user should have failed.");
289: } catch (RepositoryException e) {
290: }
291: repository.switchRole(Role.ADMINISTRATOR);
292: variantManager.deleteLanguage(language4.getId());
293:
294: try {
295: variantManager.getLanguage(language4.getId(), false);
296: fail("Getting deleted language should fail.");
297: } catch (LanguageNotFoundException e) {
298: }
299:
300: try {
301: variantManager.getLanguage(language4.getId(), true);
302: fail("Getting deleted language should fail.");
303: } catch (LanguageNotFoundException e) {
304: }
305:
306: try {
307: variantManager
308: .deleteLanguage(Language.DEFAULT_LANGUAGE_ID);
309: fail("Deleting language 'default' should have failed.");
310: } catch (RepositoryException e) {
311: }
312:
313: }
314:
315: moreTests();
316: }
317: }
|