KickJava   Java API By Example, From Geeks To Geeks.

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


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.ValueType;
23 import org.outerj.daisy.repository.user.UserManager;
24 import org.outerj.daisy.repository.user.Role;
25 import org.outerj.daisy.repository.schema.*;
26
27 import java.util.Locale JavaDoc;
28
29 public abstract class AbstractSchemaTest extends AbstractDaisyTestCase {
30     protected boolean resetDataStores() {
31         return true;
32     }
33
34     public void testSchema() throws Exception JavaDoc {
35         RepositoryManager repositoryManager = getRepositoryManager();
36         Repository repository = repositoryManager.getRepository(new Credentials("testuser", "testuser"));
37         repository.switchRole(Role.ADMINISTRATOR);
38         RepositorySchema schema = repository.getRepositorySchema();
39
40         UserManager userManager = repository.getUserManager();
41         long testUserId = userManager.getUser("testuser", false).getId();
42
43         //
44
// Test basic operations for PartType
45
//
46

47         // basic operation
48
PartType partType = schema.createPartType("mypart", "");
49         partType.save();
50
51         // error in name
52
try {
53             partType.setName("part type name with spaces");
54             fail("Could set an invalid part name");
55         } catch (Exception JavaDoc e) {}
56
57         try {
58             partType.setName("0partNameStartingWithDigit");
59             fail("Could set an invalid part name");
60         } catch (Exception JavaDoc e) {}
61
62         // retrieve the created PartType by id and by name
63
partType = schema.getPartTypeById(partType.getId(), true);
64         assertNotNull(partType);
65
66         partType = schema.getPartTypeById(partType.getId(), false);
67         assertNotNull(partType);
68
69         partType = schema.getPartTypeByName("mypart", true);
70         assertNotNull(partType);
71
72         partType = schema.getPartTypeByName("mypart", false);
73         assertNotNull(partType);
74
75         try {
76             schema.getPartTypeByName("nonExistingPartType", true);
77             fail("Retrieving non existing part type should throw exception.");
78         } catch (Exception JavaDoc e) {}
79
80         try {
81             schema.getPartTypeByName("nonExistingPartType", false);
82             fail("Retrieving non existing part type should throw exception.");
83         } catch (Exception JavaDoc e) {}
84
85         // by default, deprecated is false
86
assertFalse(partType.isDeprecated());
87
88         // do some updates on the part type
89
partType = schema.getPartTypeById(partType.getId(), true);
90         partType.setMimeTypes("text/xml");
91         partType.setLabel(new Locale JavaDoc("nl"), "mijn part");
92         partType.setDescription(new Locale JavaDoc("nl"), "beschrijving van mijn part");
93         partType.setDeprecated(true);
94         partType.save();
95
96         partType = schema.getPartTypeById(partType.getId(), true);
97         assertEquals("text/xml", partType.getMimeTypes());
98         assertEquals("mijn part", partType.getLabelExact(new Locale JavaDoc("nl")));
99         assertEquals("beschrijving van mijn part", partType.getDescriptionExact(new Locale JavaDoc("nl")));
100         assertEquals("mijn part", partType.getLabel(new Locale JavaDoc("nl","BE")));
101         assertNull(partType.getLabelExact(new Locale JavaDoc("nl", "BE")));
102         assertTrue(partType.isDeprecated());
103         assertEquals(testUserId, partType.getLastModifier());
104
105         // test concurrent modification
106
PartType partTypeConcurrent = schema.getPartTypeById(partType.getId(), true);
107         partType.save();
108         try {
109             partTypeConcurrent.save();
110             fail("Expected a concurrent modification exception.");
111         } catch (Exception JavaDoc e) {}
112
113         PartType partType2 = schema.createPartType("mypart2", "");
114         partType2.save();
115         PartType partType3 = schema.createPartType("mypart3", "");
116         partType3.save();
117
118         assertEquals(3, schema.getAllPartTypes(true).getArray().length);
119         assertEquals(3, schema.getAllPartTypes(false).getArray().length);
120
121         // creating second with same name should fail
122
PartType partTypeSameName = schema.createPartType("mypart", "");
123         try {
124             partTypeSameName.save();
125             fail("Expected exception when creating part with same name as existing part.");
126         } catch (Exception JavaDoc e) {}
127
128         // test readonlyness
129
partType = schema.getPartTypeByName("mypart", false);
130         try {
131             partType.save();
132             fail("Saving read-only part should fail");
133         } catch (Exception JavaDoc e) {}
134
135         // test part type deletion
136
PartType partTypeToBeDeleted = schema.createPartType("PartTypeToBeDeleted", "");
137         partTypeToBeDeleted.save();
138         schema.deletePartType(partTypeToBeDeleted.getId());
139         try {
140             schema.getPartTypeById(partTypeToBeDeleted.getId(), false);
141             fail("Expected PartTypeNotFoundException.");
142         } catch (PartTypeNotFoundException e) {
143         }
144         try {
145             schema.getPartTypeById(partTypeToBeDeleted.getId(), true);
146             fail("Expected PartTypeNotFoundException.");
147         } catch (PartTypeNotFoundException e) {
148         }
149         try {
150             partType.save();
151             fail("Expected exception when saving deleted part.");
152         } catch (Exception JavaDoc e) {
153         }
154
155
156
157         //
158
// Test basic operations for FieldType
159
//
160

161         // basic operation
162
FieldType fieldType = schema.createFieldType("myfield", ValueType.STRING);
163         fieldType.save();
164
165         // error in name
166
try {
167             fieldType.setName("field type name with spaces");
168             fail("Could set an invalid field name");
169         } catch (Exception JavaDoc e) {}
170
171         // retrieve the created FieldType by id and by name
172
fieldType = schema.getFieldTypeById(fieldType.getId(), true);
173         assertNotNull(fieldType);
174
175         fieldType = schema.getFieldTypeById(fieldType.getId(), false);
176         assertNotNull(fieldType);
177
178         fieldType = schema.getFieldTypeByName("myfield", true);
179         assertNotNull(fieldType);
180
181         fieldType = schema.getFieldTypeByName("myfield", false);
182         assertNotNull(fieldType);
183
184         try {
185             schema.getFieldTypeByName("nonExistingFieldType", true);
186             fail("Retrieving non existing field type should throw exception.");
187         } catch (Exception JavaDoc e) {}
188
189         try {
190             schema.getFieldTypeByName("nonExistingFieldType", false);
191             fail("Retrieving non existing field type should throw exception.");
192         } catch (Exception JavaDoc e) {}
193
194         // by default, deprecated is false
195
assertFalse(fieldType.isDeprecated());
196         assertFalse(fieldType.isAclAllowed());
197         // by default, fieldtype size is zero
198
assertEquals(fieldType.getSize(), 0);
199         
200         // do some updates on the field type
201
fieldType = schema.getFieldTypeById(fieldType.getId(), true);
202         fieldType.setLabel(new Locale JavaDoc("nl"), "mijn veld");
203         fieldType.setDescription(new Locale JavaDoc("nl"), "beschrijving van mijn veld");
204         fieldType.setDeprecated(true);
205         fieldType.setAclAllowed(true);
206         fieldType.setSize(10);
207         
208         // add selection list
209
assertNull("no selection list set and yet not null returned", fieldType.getSelectionList());
210         StaticSelectionList selList = fieldType.createStaticSelectionList();
211
212         // first add two items which comply to the valuetype of the field type (and hence the selection list)
213
StaticListItem statListItem1 = selList.createStaticListItem("polski");
214         statListItem1.setLabel(Locale.US, "labeltest");
215         StaticListItem statListItem2 = selList.createStaticListItem("bez");
216         try {
217             // now add an item which does not comply
218
selList.addItem(new Integer JavaDoc(967));
219             assertTrue("could add non-valuetype compliant item to selection list", false);
220         } catch (Exception JavaDoc e1) {}
221         // TODO catch the exception here
222
// and a third one which complies again
223
StaticListItem statListItem3 = selList.createStaticListItem("trudu");
224         
225         fieldType.save();
226
227         fieldType = schema.getFieldTypeById(fieldType.getId(), true);
228         assertEquals("mijn veld", fieldType.getLabelExact(new Locale JavaDoc("nl")));
229         assertEquals("beschrijving van mijn veld", fieldType.getDescriptionExact(new Locale JavaDoc("nl")));
230         assertEquals("mijn veld", fieldType.getLabel(new Locale JavaDoc("nl","BE")));
231         assertNull(fieldType.getLabelExact(new Locale JavaDoc("nl", "BE")));
232         assertTrue(fieldType.isDeprecated());
233         assertTrue(fieldType.isAclAllowed());
234         assertEquals(testUserId, fieldType.getLastModifier());
235         assertEquals(fieldType.getSize(), 10);
236
237         // test static selection list
238
// we'll use the fieldType with valuetype STRING
239
StaticSelectionList fetchedSelList = (StaticSelectionList)fieldType.getSelectionList();
240         assertNotNull(fetchedSelList);
241         ListItem[] selListItems = fetchedSelList.getItems();
242         assertEquals(selListItems.length, 3);
243         // selection list should maintain order
244
ListItem listItem1 = selListItems[0];
245         ListItem listItem2 = selListItems[1];
246         ListItem listItem3 = selListItems[2];
247         
248         assertEquals(statListItem1.getValue(), listItem1.getValue());
249         assertEquals(statListItem2.getValue(), listItem2.getValue());
250         assertEquals(statListItem3.getValue(), listItem3.getValue());
251
252         assertEquals("labeltest", listItem1.getLabel(Locale.US));
253
254         // add a new field to the list
255
fetchedSelList.addItem("DumuziAbsu");
256         fieldType.save();
257         
258         fieldType = schema.getFieldTypeById(fieldType.getId(), true);
259         fetchedSelList = (StaticSelectionList)fieldType.getSelectionList();
260         selListItems = fetchedSelList.getItems();
261         assertEquals(selListItems.length, 4);
262         assertEquals(selListItems[3].getValue(), "DumuziAbsu");
263         
264         // clear the selection list
265
fieldType.clearSelectionList();
266         fieldType.save();
267         fieldType = schema.getFieldTypeById(fieldType.getId(), true);
268         assertNull(fieldType.getSelectionList());
269
270         // test concurrent modification
271
FieldType fieldTypeConcurrent = schema.getFieldTypeById(fieldType.getId(), true);
272         fieldType.save();
273         try {
274             fieldTypeConcurrent.save();
275             fail("Expected a concurrent modification exception.");
276         } catch (Exception JavaDoc e) {}
277
278         FieldType fieldType2 = schema.createFieldType("myfield2", ValueType.STRING);
279         fieldType2.save();
280         FieldType fieldType3 = schema.createFieldType("myfield3", ValueType.STRING);
281         fieldType3.save();
282
283         assertEquals(3, schema.getAllFieldTypes(true).getArray().length);
284         assertEquals(3, schema.getAllFieldTypes(false).getArray().length);
285
286         // creating second with same name should fail
287
FieldType fieldTypeSameName = schema.createFieldType("myfield", ValueType.STRING);
288         try {
289             fieldTypeSameName.save();
290             fail("Expected exception when creating field with same name as existing field.");
291         } catch (Exception JavaDoc e) {}
292
293         // test readonlyness
294
fieldType = schema.getFieldTypeByName("myfield", false);
295         try {
296             fieldType.save();
297             fail("Saving read-only field should fail");
298         } catch (Exception JavaDoc e) {}
299
300         // test that saving/loading of the different ValueTypes goes correctly
301

302         FieldType otherFieldType = schema.createFieldType("datefield", ValueType.DATE);
303         otherFieldType.save();
304         otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true);
305         assertEquals(ValueType.DATE, otherFieldType.getValueType());
306
307         otherFieldType = schema.createFieldType("datetimefield", ValueType.DATETIME);
308         otherFieldType.save();
309         otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true);
310         assertEquals(ValueType.DATETIME, otherFieldType.getValueType());
311
312         otherFieldType = schema.createFieldType("longfield", ValueType.LONG);
313         otherFieldType.save();
314         otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true);
315         assertEquals(ValueType.LONG, otherFieldType.getValueType());
316
317         otherFieldType = schema.createFieldType("doublefield", ValueType.DOUBLE);
318         otherFieldType.save();
319         otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true);
320         assertEquals(ValueType.DOUBLE, otherFieldType.getValueType());
321
322         otherFieldType = schema.createFieldType("decimalfield", ValueType.DECIMAL);
323         otherFieldType.save();
324         otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true);
325         assertEquals(ValueType.DECIMAL, otherFieldType.getValueType());
326
327         otherFieldType = schema.createFieldType("booleanfield", ValueType.BOOLEAN);
328         otherFieldType.save();
329         otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true);
330         assertEquals(ValueType.BOOLEAN, otherFieldType.getValueType());
331
332         otherFieldType = schema.createFieldType("linkfield", ValueType.LINK);
333         otherFieldType.save();
334         otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true);
335         assertEquals(ValueType.LINK, otherFieldType.getValueType());
336
337         // check multivalue property
338
FieldType multiValueFieldType = schema.createFieldType("string_non_multi_value", ValueType.STRING, false);
339         assertEquals(false, multiValueFieldType.isMultiValue());
340         multiValueFieldType.save();
341         multiValueFieldType = schema.getFieldTypeById(multiValueFieldType.getId(), true);
342         assertEquals(false, multiValueFieldType.isMultiValue());
343
344         multiValueFieldType = schema.createFieldType("string_multi_value", ValueType.STRING, true);
345         assertEquals(true, multiValueFieldType.isMultiValue());
346         multiValueFieldType.save();
347         multiValueFieldType = schema.getFieldTypeById(multiValueFieldType.getId(), true);
348         assertEquals(true, multiValueFieldType.isMultiValue());
349
350         // test field type deletion
351
FieldType fieldTypeToBeDeleted = schema.createFieldType("FieldTypeToBeDeleted", ValueType.STRING);
352         fieldTypeToBeDeleted.save();
353         schema.deleteFieldType(fieldTypeToBeDeleted.getId());
354         try {
355             schema.getFieldTypeById(fieldTypeToBeDeleted.getId(), false);
356             fail("Expected FieldTypeNotFoundException.");
357         } catch (FieldTypeNotFoundException e) {
358         }
359         try {
360             schema.getFieldTypeById(fieldTypeToBeDeleted.getId(), true);
361             fail("Expected FieldTypeNotFoundException.");
362         } catch (FieldTypeNotFoundException e) {
363         }
364         try {
365             fieldType.save();
366             fail("Expected exception when saving deleted field.");
367         } catch (Exception JavaDoc e) {
368         }
369
370         //
371
// Test basic operations for DocumentType
372
//
373

374         DocumentType documentType = schema.createDocumentType("mydoctype");
375         documentType.save();
376
377         documentType = schema.getDocumentTypeById(documentType.getId(), true);
378         documentType.addFieldType(fieldType, true);
379         documentType.addFieldType(fieldType2, false);
380         documentType.save();
381         documentType.addPartType(partType, true);
382         documentType.addPartType(partType2, false);
383         documentType.save();
384
385         documentType = schema.getDocumentTypeById(documentType.getId(), true);
386         assertTrue(documentType.getFieldTypeUse(fieldType.getId()).isRequired());
387         assertFalse(documentType.getFieldTypeUse(fieldType2.getId()).isRequired());
388         assertTrue(documentType.getFieldTypeUse(partType.getId()).isRequired());
389         assertFalse(documentType.getFieldTypeUse(partType2.getId()).isRequired());
390
391         assertFalse(documentType.isDeprecated());
392
393         documentType.setDeprecated(true);
394         documentType.setName("altereddoctype");
395         documentType.save();
396
397         documentType = schema.getDocumentTypeById(documentType.getId(), false);
398         assertTrue(documentType.isDeprecated());
399         assertEquals("altereddoctype", documentType.getName());
400
401         DocumentType documentType2 = schema.createDocumentType("mydoctype2");
402         documentType2.save();
403
404         assertEquals(2, schema.getAllDocumentTypes(true).getArray().length);
405         assertEquals(2, schema.getAllDocumentTypes(false).getArray().length);
406
407         DocumentType documentTypeConcurrent = schema.getDocumentTypeById(documentType2.getId(), true);
408         documentType2.save();
409         try {
410             documentTypeConcurrent.save();
411             fail("Expected a concurrent modification exception.");
412         } catch (Exception JavaDoc e) {}
413
414         // creating second with same name should fail
415
DocumentType documentTypeSameName = schema.createDocumentType("mydoctype2");
416         try {
417             documentTypeSameName.save();
418             fail("Expected exception when creating document type with same name as existing one.");
419         } catch (Exception JavaDoc e) {}
420
421         // test readonlyness
422
documentType2 = schema.getDocumentTypeByName("mydoctype2", false);
423         try {
424             documentType2.save();
425             fail("Saving read-only document type should fail");
426         } catch (Exception JavaDoc e) {}
427
428
429         // test document type deletion
430
DocumentType documentTypeToBeDeleted = schema.createDocumentType("DocumentTypeToBeDeleted");
431         documentTypeToBeDeleted.addFieldType(fieldType, true);
432         documentTypeToBeDeleted.addPartType(partType, true);
433         documentTypeToBeDeleted.save();
434
435         // verify that the field and part cannot be deleted now
436
try {
437             schema.deleteFieldType(fieldType.getId());
438             fail("Deleting field type associated with document type should fail.");
439         } catch (Exception JavaDoc e) {}
440         try {
441             schema.deletePartType(partType.getId());
442             fail("Deleting part type associated with document type should fail.");
443         } catch (Exception JavaDoc e) {}
444
445         schema.deleteDocumentType(documentTypeToBeDeleted.getId());
446         try {
447             schema.getDocumentTypeById(documentTypeToBeDeleted.getId(), false);
448             fail("Expected DocumentTypeNotFoundException.");
449         } catch (DocumentTypeNotFoundException e) {
450         }
451         try {
452             schema.getDocumentTypeById(documentTypeToBeDeleted.getId(), true);
453             fail("Expected DocumentTypeNotFoundException.");
454         } catch (DocumentTypeNotFoundException e) {
455         }
456         try {
457             documentType.save();
458             fail("Expected exception when saving deleted document.");
459         } catch (Exception JavaDoc e) {
460         }
461
462         // TODO's for later
463
// - test only users with appropriate rights can do this stuff
464
// - verify that field/part/document types cannot be deleted while in use by documents/versions of documents
465

466     }
467
468     protected abstract RepositoryManager getRepositoryManager() throws Exception JavaDoc;
469 }
470
Popular Tags