KickJava   Java API By Example, From Geeks To Geeks.

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


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.query.SortOrder;
21 import org.outerj.daisy.repository.comment.Comment;
22 import org.outerj.daisy.repository.comment.CommentManager;
23 import org.outerj.daisy.repository.comment.CommentVisibility;
24 import org.outerj.daisy.repository.acl.*;
25 import org.outerj.daisy.repository.user.UserManager;
26 import org.outerj.daisy.repository.user.Role;
27 import org.outerj.daisy.repository.user.User;
28 import org.outerj.daisy.repository.schema.*;
29
30 import java.util.*;
31 import java.math.BigDecimal JavaDoc;
32
33 public abstract class AbstractDocumentTest extends AbstractDaisyTestCase {
34     protected boolean resetDataStores() {
35         return true;
36     }
37
38     protected abstract RepositoryManager getRepositoryManager() throws Exception JavaDoc;
39
40     public void testDocument() throws Exception JavaDoc {
41         RepositoryManager repositoryManager = getRepositoryManager();
42         Repository adminRepository = repositoryManager.getRepository(new Credentials("testuser", "testuser"));
43         adminRepository.switchRole(Role.ADMINISTRATOR);
44
45         // create a user (without admin rights) which will do all operations
46
UserManager userManager = adminRepository.getUserManager();
47         Role role = userManager.getRole("user", false);
48         User user = userManager.createUser("ordinaryUser");
49         user.addToRole(role);
50         user.setDefaultRole(role);
51         user.setPassword("secret");
52         user.save();
53         Repository repository = repositoryManager.getRepository(new Credentials("ordinaryUser", "secret"));
54
55         User user2 = userManager.createUser("ordinaryUser2");
56         user2.addToRole(role);
57         user2.setDefaultRole(role);
58         user2.setPassword("secret");
59         user2.save();
60         Repository user2Repository = repositoryManager.getRepository(new Credentials("ordinaryUser2", "secret"));
61
62         RepositorySchema schema = adminRepository.getRepositorySchema();
63
64         // Create some field types
65
FieldType stringField1 = schema.createFieldType("StringField1", ValueType.STRING);
66         stringField1.save();
67         FieldType dateField1 = schema.createFieldType("DateField1", ValueType.DATE);
68         dateField1.save();
69         FieldType dateTimeField1 = schema.createFieldType("DateTimeField1", ValueType.DATETIME);
70         dateTimeField1.save();
71         FieldType decimalField1 = schema.createFieldType("DecimalField1", ValueType.DECIMAL);
72         decimalField1.save();
73         FieldType doubleField1 = schema.createFieldType("DoubleField1", ValueType.DOUBLE);
74         doubleField1.save();
75         FieldType longField1 = schema.createFieldType("LongField1", ValueType.LONG);
76         longField1.save();
77         FieldType booleanField1 = schema.createFieldType("BoolenField1", ValueType.BOOLEAN);
78         booleanField1.save();
79         FieldType linkField1 = schema.createFieldType("LinkField1", ValueType.LINK);
80         linkField1.save();
81
82         // Create some part types
83
PartType partType1 = schema.createPartType("PartType1", "text/xml");
84         partType1.save();
85         PartType partType2 = schema.createPartType("PartType2", "");
86         partType2.save();
87         PartType partType3 = schema.createPartType("PartType3", "image/gif,image/jpeg");
88         partType3.save();
89
90         // Create a documenttype
91
DocumentType documentType1 = schema.createDocumentType("DocumentType1");
92         documentType1.addFieldType(stringField1, true);
93         documentType1.addFieldType(dateField1, true);
94         documentType1.addFieldType(dateTimeField1, false);
95         documentType1.addFieldType(decimalField1, false);
96         documentType1.addFieldType(doubleField1, false);
97         documentType1.addFieldType(longField1, false);
98         documentType1.addFieldType(booleanField1, false);
99         documentType1.addFieldType(linkField1, false);
100         documentType1.addPartType(partType1, false);
101         documentType1.addPartType(partType2, true);
102         documentType1.addPartType(partType3, false);
103         documentType1.save();
104
105         // Create a document type without any required fields or parts
106
DocumentType documentType2 = schema.createDocumentType("DocumentType2");
107         documentType2.addFieldType(stringField1, false);
108         documentType2.addFieldType(dateField1, false);
109         documentType2.addFieldType(dateTimeField1, false);
110         documentType2.addFieldType(decimalField1, false);
111         documentType2.addFieldType(doubleField1, false);
112         documentType2.addFieldType(longField1, false);
113         documentType2.addFieldType(booleanField1, false);
114         documentType2.addFieldType(linkField1, false);
115         documentType2.addPartType(partType1, false);
116         documentType2.addPartType(partType2, false);
117         documentType2.addPartType(partType3, false);
118         documentType2.save();
119
120         // Create an ACL
121
// Note: testing more meaningful ACL's is done by the separate ACL testcase.
122
AccessManager accessManager = adminRepository.getAccessManager();
123         Acl acl = accessManager.getStagingAcl();
124         AclObject aclObject = acl.createNewObject("true");
125         AclEntry aclEntry = aclObject.createNewEntry(AclSubjectType.EVERYONE, -1);
126         aclEntry.set(AclPermission.READ_LIVE, AclActionType.GRANT);
127         aclEntry.set(AclPermission.READ, AclActionType.GRANT);
128         aclEntry.set(AclPermission.WRITE, AclActionType.GRANT);
129         aclEntry.set(AclPermission.PUBLISH, AclActionType.GRANT);
130         aclEntry.set(AclPermission.DELETE, AclActionType.GRANT);
131         aclObject.add(aclEntry);
132         aclEntry = aclObject.createNewEntry(AclSubjectType.USER, user2.getId());
133         aclEntry.set(AclPermission.READ_LIVE, AclActionType.GRANT);
134         aclEntry.set(AclPermission.READ, AclActionType.GRANT);
135         aclEntry.set(AclPermission.WRITE, AclActionType.DENY);
136         aclEntry.set(AclPermission.PUBLISH, AclActionType.DENY);
137         aclEntry.set(AclPermission.DELETE, AclActionType.DENY);
138         aclObject.add(aclEntry);
139         acl.add(aclObject);
140         acl.save();
141         accessManager.copyStagingToLive();
142
143         //
144
// Basic document creation
145
//
146
Document document1 = repository.createDocument("Document 1", documentType1.getId());
147
148         // set a field using, once using id, once using name
149
document1.setField(stringField1.getId(), "hello");
150         document1.setField("DateField1", new Date());
151
152         // set a part, once using id, once using name
153
byte[] part2Data = "meaningful data".getBytes();
154         byte[] part3Data = "myimage".getBytes();
155         document1.setPart(partType2.getId(), "application/x-meaningful", part2Data);
156         document1.setPart("PartType3", "image/gif", part3Data);
157
158         document1.save();
159
160         //
161
// Test data is correctly saved and loaded
162
//
163
Document document1Reloaded = repository.getDocument(document1.getId(), true);
164         assertEquals("hello", (String JavaDoc)document1Reloaded.getField("StringField1").getValue());
165         System.out.println(((Date)document1.getField(dateField1.getId()).getValue()).getTime() + " - " + ((Date)document1Reloaded.getField(dateField1.getId()).getValue()).getTime());
166         assertEquals(document1.getField(dateField1.getId()).getValue(), document1Reloaded.getField(dateField1.getId()).getValue());
167         assertEquals(part2Data, document1Reloaded.getPart("PartType2").getData());
168         assertEquals(part3Data, document1Reloaded.getPart(partType3.getId()).getData());
169
170         //
171
// Calling save now on document should not create new version
172
//
173
document1.save();
174         assertEquals(1, document1.getLastVersionId());
175         assertEquals(1, document1.getLastVersion().getId());
176
177         //
178
// Create document with missing field
179
//
180
Document document2 = repository.createDocument("Document 2", documentType1.getId());
181         document2.setField(stringField1.getId(), "yo");
182         document2.setPart(partType2.getId(), "application/x-meaningful", part2Data);
183         try {
184             document2.save();
185             fail("Saving document should have failed due to missing field.");
186         } catch (Exception JavaDoc e) {}
187         // try saving with disabling validation
188
document2.save(false);
189         // now set missing field -- should now save successfully
190
document2.setField(dateField1.getId(), new Date());
191         document2.save(true);
192
193         document2.deletePart(partType2.getId());
194         try {
195             document2.save();
196             fail("Saving document should have failed due to missing part.");
197         } catch (Exception JavaDoc e) {}
198         // set missing part -- should now save succesfully
199
document2.setPart(partType2.getId(), "application/x-meaningful", part2Data);
200         document2.save();
201
202         //
203
// Test setting new data for a part
204
//
205
byte[] newPart2Data = "Apples and Oranges".getBytes();
206         document2.setPart(partType2.getId(), "application/x-meaningful", newPart2Data);
207         document2.save();
208
209         Document document2Reloaded = repository.getDocument(document2.getId(), true);
210         assertEquals(newPart2Data, document2Reloaded.getPart(partType2.getId()).getData());
211
212         //
213
// Test setting data with incorrect mimetype
214
//
215
try {
216             document2.setPart(partType1.getId(), "image/zarba", part3Data);
217             fail("Setting data with incorrect mimetype should fail.");
218         } catch (Exception JavaDoc e) {}
219
220         //
221
// Test out all types of fields
222
//
223
Document document3 = repository.createDocument("Document 3", documentType1.getId());
224         document3.setField(stringField1.getId(), "hello");
225         Date currentDate = getDate(new Date());
226         document3.setField(dateField1.getId(), currentDate);
227         Date currentDateTime = getDateTime(new Date());
228         document3.setField(dateTimeField1.getId(), currentDateTime);
229         document3.setField(decimalField1.getId(), new BigDecimal JavaDoc("33.43539"));
230         document3.setField(doubleField1.getId(), new Double JavaDoc(343.232d));
231         document3.setField(longField1.getId(), new Long JavaDoc(23234235));
232         document3.setField(booleanField1.getId(), Boolean.FALSE);
233         document3.setField(linkField1.getId(), new VariantKey(23, 2, 8));
234         document3.setPart(partType2.getId(), "application/x-meaningful", part2Data);
235         document3.save();
236
237         // test field values are same after reloading document
238
document3 = repository.getDocument(document3.getId(), true);
239         assertEquals(currentDate, document3.getField(dateField1.getId()).getValue());
240         assertEquals(currentDateTime, document3.getField(dateTimeField1.getId()).getValue());
241         assertEquals(new BigDecimal JavaDoc("33.43539"), document3.getField(decimalField1.getId()).getValue());
242         assertEquals(new Double JavaDoc(343.232d), document3.getField(doubleField1.getId()).getValue());
243         assertEquals(new Long JavaDoc(23234235), document3.getField(longField1.getId()).getValue());
244         assertEquals(Boolean.FALSE, document3.getField(booleanField1.getId()).getValue());
245         assertEquals(new VariantKey(23, 2, 8), document3.getField(linkField1.getId()).getValue());
246
247         //
248
// Test setting wrong type of value on a field
249
//
250
Document document12 = repository.createDocument("Document 12", documentType1.getId());
251         try {
252             document12.setField(doubleField1.getId(), new Long JavaDoc(123));
253             fail("Setting wrong object as field value should give an exception.");
254         } catch (Exception JavaDoc e) {}
255
256         //
257
// Test retired and private flags
258
//
259
Document document4 = repository.createDocument("Document 4", documentType1.getId());
260         document4.setField(stringField1.getId(), "hello");
261         document4.setField("DateField1", new Date());
262         document4.setPart(partType2.getId(), "application/x-meaningful", part2Data);
263         document4.save();
264         assertEquals(false, document4.isRetired());
265         assertEquals(false, document4.isPrivate());
266         document4.setRetired(true);
267         document4.setPrivate(true);
268         document4.save();
269         document4 = repository.getDocument(document4.getId(), false);
270         assertEquals(true, document4.isRetired());
271         assertEquals(true, document4.isPrivate());
272
273         //
274
// Test document cache
275
//
276
Document document5 = repository.createDocument("Document 5", documentType1.getId());
277         document5.setField(stringField1.getId(), "hello");
278         document5.setField("DateField1", new Date());
279         document5.setPart(partType2.getId(), "application/x-meaningful", part2Data);
280         document5.save();
281         Document document5ReadOnly = repository.getDocument(document5.getId(), false);
282         try {
283             document5ReadOnly.save();
284             fail("Saving document should have failed because it is not modifiable.");
285         } catch (Exception JavaDoc e) {}
286         // Do a document update and verify that cache has been refreshed
287
document5.setField(stringField1.getId(), "hello!");
288         document5.save();
289         document5ReadOnly = repository.getDocument(document5.getId(), false);
290         assertEquals("hello!", (String JavaDoc)document5ReadOnly.getField(stringField1.getId()).getValue());
291
292         //
293
// Test comments
294
//
295
CommentManager commentManager = repository.getCommentManager();
296         Document document6 = repository.createDocument("Document 6", documentType2.getId());
297         try {
298             commentManager.addComment(document6.getId(), CommentVisibility.PUBLIC, "Hi, I have something to say.");
299             fail("Adding comment to a non-saved document should fail.");
300         } catch (Exception JavaDoc e) {}
301         document6.save();
302         String JavaDoc comment1Text = "Hi, I have something to say.";
303         commentManager.addComment(document6.getId(), CommentVisibility.PUBLIC, comment1Text);
304         commentManager.addComment(document6.getId(), CommentVisibility.PUBLIC, "Hi, I have another thing to say.");
305         commentManager.addComment(document6.getId(), CommentVisibility.PRIVATE, "This document sucks.");
306         commentManager.addComment(document6.getId(), CommentVisibility.EDITORS, "Oh yes.");
307         Comment[] comments = commentManager.getComments(document6.getId()).getArray();
308         assertEquals(4, comments.length);
309         assertEquals(comments[0].getCreatedBy(), user.getId());
310         assertEquals(comments[0].getText(), comment1Text);
311
312         assertEquals(4, commentManager.getComments().getArray().length);
313         assertEquals(1, commentManager.getComments(CommentVisibility.PRIVATE).getArray().length);
314         assertEquals(2, commentManager.getComments(CommentVisibility.PUBLIC).getArray().length);
315         assertEquals(1, commentManager.getComments(CommentVisibility.EDITORS).getArray().length);
316
317         // another user should not see the private or editors-only comments
318
assertEquals(2, user2Repository.getCommentManager().getComments(document6.getId()).getArray().length);
319
320         // admin user should see all comments except private comments
321
assertEquals(3, adminRepository.getCommentManager().getComments(document6.getId()).getArray().length);
322
323         commentManager.addComment(document6.getId(), CommentVisibility.PUBLIC, "This document is o so nice.");
324         // first user should see new comment without re-fetching document
325
assertEquals(5, commentManager.getComments(document6.getId()).getArray().length);
326
327         // test deleting comments
328
commentManager.deleteComment(document6.getId(), comments[0].getId());
329         assertEquals(4, commentManager.getComments(document6.getId()).getArray().length);
330
331         try {
332             commentManager.deleteComment(document6.getId(), 10);
333             fail("Deleting non-existing comment should give exception.");
334         } catch (Exception JavaDoc e) {}
335
336         // user without write rights should be able to make a private comment and delete it again
337
user2Repository.getCommentManager().addComment(document6.getId(), CommentVisibility.PRIVATE, "This is a comment.");
338         Comment[] user2Comments = user2Repository.getCommentManager().getComments(document6.getId()).getArray();
339         user2Repository.getCommentManager().deleteComment(document6.getId(), user2Comments[user2Comments.length - 1].getId());
340
341         // ... but should not be able to delete other comments
342
try {
343             user2Repository.getCommentManager().deleteComment(document6.getId(), user2Comments[user2Comments.length - 2].getId());
344             fail("User without write rights should not be able to delete non-private comments.");
345         } catch (RepositoryException e) {}
346
347         commentManager.addComment(document6.getId(), CommentVisibility.PRIVATE, "Private comment");
348         Comment[] comments2 = commentManager.getComments(document6.getId()).getArray();
349         try {
350             user2Repository.getCommentManager().deleteComment(document6.getId(), comments2[comments2.length - 1].getId());
351             fail("Non-admin user should not be able to delete other users' private comments.");
352         } catch (RepositoryException e) {}
353
354         // admin user should be able to delete otehr users' private comments
355
adminRepository.getCommentManager().deleteComment(document6.getId(), comments2[comments2.length - 1].getId());
356
357         //
358
// Test customfields
359
//
360
Document document7 = repository.createDocument("Document 7", documentType2.getId());
361         document7.save();
362         document7.setCustomField("some field", "some value");
363         document7.setCustomField("some field 2", "123");
364         document7.save();
365         document7 = repository.getDocument(document7.getId(), true);
366         assertEquals("123", document7.getCustomField("some field 2"));
367         document7.deleteCustomField("some field 2");
368         assertNull(document7.getCustomField("some field 2"));
369         document7.save();
370
371
372
373         //
374
// Test locks
375
//
376
Document document8 = repository.createDocument("Document 8", documentType2.getId());
377         document8.save();
378         LockInfo lockInfo = document8.getLockInfo(false);
379         assertEquals(false, lockInfo.hasLock());
380         document8.lock(1000000, LockType.PESSIMISTIC);
381         // we have a lock, so should be able to save the document
382
// TODO this doesn't make sense anymore since saving is never done if document is not modified
383
document8.save();
384         // another user should not be able to save it...
385
Document document8OtherUser = adminRepository.getDocument(document8.getId(), true);
386         try {
387             document8OtherUser.setCustomField("a", "b");
388             document8OtherUser.save();
389             fail("Saving document should have failed because lock belongs to another user.");
390         } catch (Exception JavaDoc e) {}
391         // .. but if we remove the lock, it should work
392
document8.releaseLock();
393         document8OtherUser.save();
394         // test lock expires
395
document8.lock(1, LockType.PESSIMISTIC);
396         Thread.sleep(1200); // lock creation time is stored only with one-second precision, so take that and some margin
397
lockInfo = document8OtherUser.getLockInfo(true);
398         assertEquals(false, lockInfo.hasLock());
399
400         //
401
// Test versions
402
//
403
Document document9 = repository.createDocument("Document 9", documentType1.getId());
404         document9.setField(stringField1.getId(), "hello");
405         document9.setField("DateField1", new Date());
406         document9.setPart(partType2.getId(), "application/x-meaningful", part2Data);
407         document9.save();
408
409         Version version = document9.getVersion(1);
410         assertEquals(part2Data, version.getPart(partType2.getId()).getData());
411
412         //
413
// Test collections
414
//
415
CollectionManager collectionManager = adminRepository.getCollectionManager();
416         DocumentCollection collection1 = collectionManager.createCollection("Collection1");
417         collection1.save();
418         DocumentCollection collection2 = collectionManager.createCollection("Collection2");
419         collection2.save();
420         DocumentCollection collection3 = collectionManager.createCollection("Collection3");
421         collection3.save();
422
423         Document document10 = repository.createDocument("Document 10", documentType2.getId());
424         document10.addToCollection(collection1);
425         document10.save();
426         assertEquals(1, document10.getCollections().getArray().length);
427         document10 = repository.getDocument(document10.getId(), true);
428         assertEquals(1, document10.getCollections().getArray().length);
429
430         document10.addToCollection(collection2);
431         document10.addToCollection(collection3);
432         document10.save();
433
434         document10 = repository.getDocument(document10.getId(), true);
435         DocumentCollection[] collections = document10.getCollections().getArray();
436         HashSet names = new HashSet();
437         for (int i = 0; i < collections.length; i++) {
438             names.add(collections[i].getName());
439         }
440         assertEquals(true, names.contains("Collection1"));
441         assertEquals(true, names.contains("Collection2"));
442         assertEquals(true, names.contains("Collection3"));
443
444         // delete a collection still belonging to the document, check it is removed after doc loading
445
collectionManager.deleteCollection(collection3.getId());
446         document10 = repository.getDocument(document10.getId(), true);
447         assertEquals(2, document10.getCollections().getArray().length);
448
449         // remove a collection from the document
450
document10.removeFromCollection(collection2);
451         document10.save();
452         document10 = repository.getDocument(document10.getId(), true);
453         assertEquals(1, document10.getCollections().getArray().length);
454
455         // delete a collection still belonging to the document, and then save the document
456
// (the document object has still the collection in it)
457
collectionManager.deleteCollection(collection1.getId());
458         document10.save();
459         document10 = repository.getDocument(document10.getId(), false);
460         assertEquals(0, document10.getCollections().getArray().length);
461
462         // All these collection operations shouldn't have created any new versions
463
assertEquals(1, document10.getVersions().getArray().length);
464
465
466
467         //
468
// Test links
469
//
470
Document document11 = repository.createDocument("Document 11", documentType2.getId());
471         document11.addLink("Google", "http://www.google.be");
472         document11.save();
473         document11 = repository.getDocument(document11.getId(), true);
474         assertEquals(1, document11.getLinks().getArray().length);
475         document11.clearLinks();
476         document11.save();
477         document11 = repository.getDocument(document11.getId(), true);
478         assertEquals(0, document11.getLinks().getArray().length);
479
480
481         //
482
// Test concurrent modification detection
483
//
484
Document document20 = repository.createDocument("Document 20", documentType2.getId());
485         document20.save();
486         Document document20Parallel = repository.getDocument(document20.getId(), true);
487         document20Parallel.setCustomField("p", "t");
488         document20Parallel.save();
489         try {
490             document20.setCustomField("r", "s");
491             document20.save();
492             fail("Saving document should have failed because of concurrent modifications.");
493         } catch (Exception JavaDoc e) {};
494
495         //
496
// Test changing the document type
497
//
498
Document document30 = repository.createDocument("Document 30", documentType1.getId());
499         document30.setField(stringField1.getId(), "value30");
500         document30.setField(dateField1.getId(), new Date());
501         document30.setPart(partType2.getId(), "application/x-meaningful", part2Data);
502         document30.save();
503         assertEquals("document type check", documentType1.getId(), document30.getDocumentTypeId());
504         document30.changeDocumentType(documentType2.getId());
505         document30.save();
506         document30 = repository.getDocument(document30.getId(), true);
507         assertEquals("document type check", documentType2.getId(), document30.getDocumentTypeId());
508
509         //
510
// Test deleting documents
511
//
512
Document document40 = repository.createDocument("Document 40", documentType1.getId());
513         document40.setField(stringField1.getId(), "value40");
514         document40.setField(dateField1.getId(), new Date());
515         document40.setPart(partType2.getId(), "application/x-meaningful", part2Data);
516         document40.save();
517         repository.deleteDocument(document40.getId());
518
519         //
520
// Test changing owner
521
//
522
DocumentType simpleDocType = adminRepository.getRepositorySchema().createDocumentType("SimpleDocType");
523         simpleDocType.save();
524         Document document50 = repository.createDocument("Document 50", "SimpleDocType");
525         document50.save();
526
527         // test current owner is able to change role
528
document50.setOwner(user2.getId());
529         document50.save();
530
531         document50 = repository.getDocument(document50.getId(), false);
532         assertEquals(user2.getId(), document50.getOwner());
533
534         // changing role on non-modifiable document object should not be possible
535
try {
536             document50.setOwner(adminRepository.getUserId());
537             fail("Changing role on non-modifiable document object should not be possible.");
538         } catch (RuntimeException JavaDoc e) {}
539
540         // test non-admin non-owner is not able to change role
541
document50 = repository.getDocument(document50.getId(), true);
542         try {
543             document50.setOwner(adminRepository.getUserId());
544             fail("Ordinary user should not be able to change owner.");
545         } catch (RepositoryRuntimeException e) {}
546
547         // test admin is able to change role
548
document50 = adminRepository.getDocument(document50.getId(), true);
549         document50.setOwner(user.getId());
550
551         try {
552             repository.deleteDocument(document40.getId());
553             fail("Expected a DocumentNotFoundException.");
554         } catch (DocumentNotFoundException e) {}
555
556         //
557
// Test multivalue fields
558
//
559
FieldType mvType1 = schema.createFieldType("mv1", ValueType.STRING, true);
560         mvType1.save();
561         FieldType mvType2 = schema.createFieldType("mv2", ValueType.LONG, true);
562         mvType2.save();
563         DocumentType mvDoctype = schema.createDocumentType("mvdoctype1");
564         mvDoctype.addFieldType(mvType1, true);
565         mvDoctype.addFieldType(mvType2, true);
566         mvDoctype.save();
567
568         Document mvDoc = repository.createDocument("multivalue field test doc", mvDoctype.getId());
569         mvDoc.setField(mvType1.getId(), new String JavaDoc[] {"value1", "value2"});
570         mvDoc.setField("mv2", new Object JavaDoc[] { new Long JavaDoc(12) });
571         mvDoc.save();
572
573         mvDoc = repository.getDocument(mvDoc.getId(), true);
574         Object JavaDoc[] mvValues = (Object JavaDoc[])mvDoc.getField(mvType1.getId()).getValue();
575         assertEquals(2, mvValues.length);
576         assertEquals("value1", mvValues[0]);
577         assertEquals("value2", mvValues[1]);
578         mvValues = (Object JavaDoc[])mvDoc.getField(mvType2.getId()).getValue();
579         assertEquals(1, mvValues.length);
580         assertEquals(new Long JavaDoc(12), mvValues[0]);
581
582         try {
583             mvDoc.setField("mv2", new Object JavaDoc[0]);
584             fail("Zero-length array should not be allowed for the value of a multivalue field.");
585         } catch (DocumentTypeInconsistencyException e) {}
586
587         //
588
// Test part file name and mime type update
589
//
590
Document document60 = repository.createDocument("doc60", documentType2.getId());
591         document60.setPart(partType2.getId(), "application/x-ooktettenstroom", new byte[] {1, 2, 3});
592         document60.setPartFileName(partType2.getId(), "myfilename.data");
593         assertEquals("myfilename.data", document60.getPart(partType2.getId()).getFileName());
594         document60.save();
595         document60 = repository.getDocument(document60.getId(), true);
596         assertEquals("myfilename.data", document60.getPart(partType2.getId()).getFileName());
597
598         document60.setPartFileName(partType2.getId(), "myfilenamenew.data");
599         document60.save();
600         document60 = repository.getDocument(document60.getId(), true);
601         assertEquals("myfilenamenew.data", document60.getPart(partType2.getId()).getFileName());
602         assertEquals(2, document60.getLastVersionId());
603
604         // setting part file name to same value should not cause any update
605
document60.setPartFileName(partType2.getId(), "myfilenamenew.data");
606         document60.save();
607         document60 = repository.getDocument(document60.getId(), true);
608         assertEquals(2, document60.getLastVersionId());
609
610         document60.setPartFileName(partType2.getName(), null);
611         document60.save();
612         document60 = repository.getDocument(document60.getId(), true);
613         assertNull(document60.getPart(partType2.getId()).getFileName());
614         assertEquals(3, document60.getLastVersionId());
615
616         document60.setPartMimeType(partType2.getName(), "application/octet-stream");
617         document60.save();
618         document60 = repository.getDocument(document60.getId(), true);
619         assertEquals("application/octet-stream", document60.getPart(partType2.getId()).getMimeType());
620         assertEquals(4, document60.getLastVersionId());
621
622         //
623
// Test link query selection list
624
// tested here instead of in the SchemaTest since it is more meaningful if documents exist
625
//
626
FieldType myLinkField = schema.createFieldType("myLinkField", ValueType.LINK);
627         myLinkField.createLinkQuerySelectionList("true", false);
628         ListItem[] linkFieldListItems = myLinkField.getSelectionList().getItems();
629         assertTrue(linkFieldListItems.length > 0);
630         assertNotNull(linkFieldListItems[0].getValue());
631         assertNotNull(linkFieldListItems[0].getLabel(Locale.US));
632
633         // Test query selection list
634
stringField1 = schema.getFieldTypeByName("StringField1", true);
635         stringField1.createQuerySelectionList("select $StringField1 where $StringField1 is not null", false, SortOrder.ASCENDING);
636         stringField1.save();
637         ListItem[] queryListItems = stringField1.getSelectionList().getItems();
638         assertTrue(queryListItems.length > 0);
639
640         //
641
// Test the version methods. Not really document related but making a different testcase for this
642
// seems like overkill.
643
//
644
assertNotNull(repository.getClientVersion());
645         assertNotNull(repository.getServerVersion());
646     }
647
648     public void assertEquals(byte[] data1, byte[] data2) {
649         if (data1.length != data2.length)
650             fail("byte arrays not of equals length");
651
652         for (int i = 0; i < data1.length; i++) {
653             if (data1[i] != data2[i])
654                 fail("byte arrays not equal, detected difference at byte " + i);
655         }
656     }
657
658     private Date getDate(Date date) {
659         Calendar calendar = new GregorianCalendar();
660         calendar.setTime(date);
661         calendar.set(Calendar.HOUR_OF_DAY, 0);
662         calendar.set(Calendar.MINUTE, 0);
663         calendar.set(Calendar.SECOND, 0);
664         calendar.set(Calendar.MILLISECOND, 0);
665         return calendar.getTime();
666     }
667
668     private Date getDateTime(Date date) {
669         Calendar calendar = new GregorianCalendar();
670         calendar.setTime(date);
671         calendar.set(Calendar.MILLISECOND, 0);
672         return calendar.getTime();
673     }
674 }
675
Popular Tags