001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestContentManager.java 3669 2007-02-26 13:51:23Z gbevin $
007: */
008: package com.uwyn.rife.cmf.dam;
009:
010: import com.uwyn.rife.cmf.dam.contentstores.*;
011:
012: import com.uwyn.rife.cmf.Content;
013: import com.uwyn.rife.cmf.ContentInfo;
014: import com.uwyn.rife.cmf.ContentRepository;
015: import com.uwyn.rife.cmf.MimeType;
016: import com.uwyn.rife.cmf.UnsupportedMimeType;
017: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContent;
018: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentFactory;
019: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentInfo;
020: import com.uwyn.rife.cmf.dam.contentmanagers.exceptions.InstallContentErrorException;
021: import com.uwyn.rife.cmf.dam.contentmanagers.exceptions.UnknownContentRepositoryException;
022: import com.uwyn.rife.cmf.dam.contentmanagers.exceptions.UnsupportedMimeTypeException;
023: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
024: import com.uwyn.rife.database.Datasource;
025: import com.uwyn.rife.resources.ResourceFinderClasspath;
026: import com.uwyn.rife.tools.FileUtils;
027: import com.uwyn.rife.tools.InnerClassException;
028: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
029: import java.io.ByteArrayInputStream;
030: import java.io.InputStream;
031: import java.net.URL;
032: import java.util.Arrays;
033: import junit.framework.TestCase;
034:
035: public class TestContentManager extends TestCase {
036: private Datasource mDatasource = new Datasource();
037:
038: public TestContentManager(Datasource datasource,
039: String datasourceName, String name) {
040: super (name);
041:
042: mDatasource = datasource;
043: }
044:
045: public void setUp() throws Exception {
046: DatabaseContentFactory.getInstance(mDatasource).install();
047: }
048:
049: public void tearDown() throws Exception {
050: try {
051: DatabaseContentFactory.getInstance(mDatasource).remove();
052: } catch (Throwable e) {
053: // discart errors
054: }
055: }
056:
057: public void testInstallError() {
058: ContentManager manager = DatabaseContentFactory
059: .getInstance(mDatasource);
060:
061: try {
062: manager.install();
063: fail();
064: } catch (InstallContentErrorException e) {
065: assertNotNull(e.getCause());
066: }
067: }
068:
069: public void testRemoveError() throws Exception {
070: DatabaseContentFactory.getInstance(mDatasource).remove();
071:
072: ContentManager manager = DatabaseContentFactory
073: .getInstance(mDatasource);
074:
075: try {
076: manager.remove();
077: fail();
078: } catch (ContentManagerException e) {
079: assertNotNull(e.getCause());
080: }
081: }
082:
083: public void testStoreContentXhtml() {
084: ContentManager manager = DatabaseContentFactory
085: .getInstance(mDatasource);
086:
087: // text data
088: final String data_text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
089: + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
090: + "<html><head><title>my title</title></head><body></body></html>";
091: Content content = new Content(MimeType.APPLICATION_XHTML,
092: data_text);
093: assertTrue(manager.storeContent("/textcontent", content, null));
094:
095: manager.useContentData("/textcontent", new ContentDataUser() {
096: public Object useContentData(Object contentData)
097: throws InnerClassException {
098: assertEquals(data_text, contentData);
099: return null;
100: }
101: });
102: }
103:
104: public void testStoreContentGif() throws Exception {
105: ContentManager manager = DatabaseContentFactory
106: .getInstance(mDatasource);
107:
108: // image data
109: URL image_resource_gif = ResourceFinderClasspath.getInstance()
110: .getResource("uwyn.gif");
111: URL image_resource_png = ResourceFinderClasspath.getInstance()
112: .getResource("uwyn.png");
113: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
114: final byte[] data_image_png = FileUtils
115: .readBytes(image_resource_png);
116:
117: Content content = new Content(MimeType.IMAGE_PNG,
118: data_image_gif);
119: assertTrue(manager.storeContent("/imagegif", content, null));
120:
121: manager.useContentData("/imagegif", new ContentDataUser() {
122: public Object useContentData(Object contentData)
123: throws InnerClassException {
124: assertTrue(Arrays.equals(data_image_png,
125: (byte[]) contentData));
126: return null;
127: }
128: });
129: }
130:
131: public void testStoreContentTif() throws Exception {
132: ContentManager manager = DatabaseContentFactory
133: .getInstance(mDatasource);
134:
135: // image data
136: URL image_resource_tif = ResourceFinderClasspath.getInstance()
137: .getResource("uwyn.tif");
138: URL image_resource_png = ResourceFinderClasspath.getInstance()
139: .getResource("uwyn-noalpha.png");
140: byte[] data_image_tif = FileUtils.readBytes(image_resource_tif);
141: final byte[] data_image_png = FileUtils
142: .readBytes(image_resource_png);
143:
144: Content content = new Content(MimeType.IMAGE_PNG,
145: data_image_tif);
146: assertTrue(manager.storeContent("/imagetif", content, null));
147:
148: manager.useContentData("/imagetif", new ContentDataUser() {
149: public Object useContentData(Object contentData)
150: throws InnerClassException {
151: assertTrue(Arrays.equals(data_image_png,
152: (byte[]) contentData));
153: return null;
154: }
155: });
156: }
157:
158: public void testStoreContentGifResized() throws Exception {
159: ContentManager manager = DatabaseContentFactory
160: .getInstance(mDatasource);
161:
162: // image data
163: URL image_resource_gif = ResourceFinderClasspath.getInstance()
164: .getResource("uwyn.gif");
165: URL image_resource_png = ResourceFinderClasspath.getInstance()
166: .getResource("uwyn_resized-width_20.png");
167: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
168: final byte[] data_image_png = FileUtils
169: .readBytes(image_resource_png);
170:
171: Content content_image = new Content(MimeType.IMAGE_PNG,
172: data_image_gif);
173: content_image.attribute("width", 20);
174: assertTrue(manager.storeContent("/imagegif", content_image,
175: null));
176:
177: manager.useContentData("/imagegif", new ContentDataUser() {
178: public Object useContentData(Object contentData)
179: throws InnerClassException {
180: assertTrue(Arrays.equals(data_image_png,
181: (byte[]) contentData));
182: return null;
183: }
184: });
185: }
186:
187: public void testStoreContentTifResized() throws Exception {
188: ContentManager manager = DatabaseContentFactory
189: .getInstance(mDatasource);
190:
191: // image data
192: URL image_resource_tif = ResourceFinderClasspath.getInstance()
193: .getResource("uwyn.tif");
194: URL image_resource_png = ResourceFinderClasspath.getInstance()
195: .getResource("uwyn_resized-width_20-noalpha.png");
196: byte[] data_image_tif = FileUtils.readBytes(image_resource_tif);
197: final byte[] data_image_png = FileUtils
198: .readBytes(image_resource_png);
199:
200: Content content = new Content(MimeType.IMAGE_PNG,
201: data_image_tif);
202: content.attribute("width", 20);
203: assertTrue(manager.storeContent("/imagetif", content, null));
204:
205: manager.useContentData("/imagetif", new ContentDataUser() {
206: public Object useContentData(Object contentData)
207: throws InnerClassException {
208: assertTrue(Arrays.equals(data_image_png,
209: (byte[]) contentData));
210: return null;
211: }
212: });
213: }
214:
215: public void testStoreContentRaw() throws Exception {
216: ContentManager manager = DatabaseContentFactory
217: .getInstance(mDatasource);
218:
219: int size = 1024 * 1024 * 4; // 4Mb
220: final byte[] raw = new byte[size];
221: for (int i = 0; i < size; i++) {
222: raw[i] = (byte) (i % 255);
223: }
224:
225: Content content = new Content(MimeType.RAW,
226: new ByteArrayInputStream(raw));
227: assertTrue(manager.storeContent("/rawdata", content, null));
228:
229: manager.useContentData("/rawdata", new ContentDataUser() {
230: public Object useContentData(Object contentData)
231: throws InnerClassException {
232: try {
233: assertTrue(Arrays.equals(raw, FileUtils
234: .readBytes((InputStream) contentData)));
235: } catch (FileUtilsErrorException e) {
236: throwException(e);
237: }
238: return null;
239: }
240: });
241: }
242:
243: public void testStoreContentRepository() {
244: ContentManager manager = DatabaseContentFactory
245: .getInstance(mDatasource);
246:
247: manager.createRepository("mynewrep");
248:
249: // text data
250: final String data_text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
251: + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
252: + "<html><head><title>my title</title></head><body></body></html>";
253: Content content = new Content(MimeType.APPLICATION_XHTML,
254: data_text);
255: assertTrue(manager.storeContent("mynewrep:/textcontent",
256: content, null));
257:
258: manager.useContentData("mynewrep:/textcontent",
259: new ContentDataUser() {
260: public Object useContentData(Object contentData)
261: throws InnerClassException {
262: assertEquals(data_text, contentData);
263: return null;
264: }
265: });
266:
267: manager.useContentData("/textcontent", new ContentDataUser() {
268: public Object useContentData(Object contentData)
269: throws InnerClassException {
270: assertNull(contentData);
271: return null;
272: }
273: });
274:
275: manager.useContentData(ContentRepository.DEFAULT
276: + ":/textcontent", new ContentDataUser() {
277: public Object useContentData(Object contentData)
278: throws InnerClassException {
279: assertNull(contentData);
280: return null;
281: }
282: });
283: }
284:
285: public void testStoreUnknownContentRepository() throws Exception {
286: ContentManager manager = DatabaseContentFactory
287: .getInstance(mDatasource);
288:
289: // text data
290: final String data_text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
291: + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
292: + "<html><head><title>my title</title></head><body></body></html>";
293: Content content = new Content(MimeType.APPLICATION_XHTML,
294: data_text);
295: try {
296: manager
297: .storeContent("mynewrep:/textcontent", content,
298: null);
299: fail();
300: } catch (UnknownContentRepositoryException e) {
301: assertEquals(e.getRepositoryName(), "mynewrep");
302: }
303: }
304:
305: public void testCreateRepositoryIllegalArguments() {
306: ContentManager manager = DatabaseContentFactory
307: .getInstance(mDatasource);
308:
309: try {
310: manager.createRepository(null);
311: fail();
312: } catch (IllegalArgumentException e) {
313: assertTrue(true);
314: }
315:
316: try {
317: manager.createRepository("");
318: fail();
319: } catch (IllegalArgumentException e) {
320: assertTrue(true);
321: }
322: }
323:
324: public void testContainsContentRepository() {
325: ContentManager manager = DatabaseContentFactory
326: .getInstance(mDatasource);
327:
328: assertTrue(manager.containsRepository(""));
329: assertTrue(manager
330: .containsRepository(ContentRepository.DEFAULT));
331: assertFalse(manager.containsRepository("mynewrep"));
332:
333: manager.createRepository("mynewrep");
334: assertTrue(manager.containsRepository("mynewrep"));
335: }
336:
337: public void testContainsRepositoryIllegalArguments() {
338: ContentManager manager = DatabaseContentFactory
339: .getInstance(mDatasource);
340:
341: try {
342: manager.containsRepository(null);
343: fail();
344: } catch (IllegalArgumentException e) {
345: assertTrue(true);
346: }
347: }
348:
349: public void testStoreContentIllegalArguments() {
350: ContentManager manager = DatabaseContentFactory
351: .getInstance(mDatasource);
352:
353: // text data
354: String data_text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
355: + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
356: + "<html><head><title>my title</title></head><body></body></html>";
357: Content content_text = new Content(MimeType.APPLICATION_XHTML,
358: data_text);
359:
360: try {
361: manager.storeContent(null, content_text, null);
362: fail();
363: } catch (IllegalArgumentException e) {
364: assertTrue(true);
365: }
366:
367: try {
368: manager.storeContent("", content_text, null);
369: fail();
370: } catch (IllegalArgumentException e) {
371: assertTrue(true);
372: }
373:
374: try {
375: manager.storeContent("notabsolute", content_text, null);
376: fail();
377: } catch (IllegalArgumentException e) {
378: assertTrue(true);
379: }
380:
381: try {
382: manager.storeContent("default:", content_text, null);
383: fail();
384: } catch (IllegalArgumentException e) {
385: assertTrue(true);
386: }
387:
388: try {
389: manager.storeContent("default:notabsolute", content_text,
390: null);
391: fail();
392: } catch (IllegalArgumentException e) {
393: assertTrue(true);
394: }
395:
396: try {
397: manager.storeContent("/nocontent", null, null);
398: fail();
399: } catch (IllegalArgumentException e) {
400: assertTrue(true);
401: }
402: }
403:
404: public void testStoreContentUnsupportedMimeType() {
405: ContentManager manager = DatabaseContentFactory
406: .getInstance(mDatasource);
407:
408: Content content = new Content(UnsupportedMimeType.UNSUPPORTED,
409: new Object());
410: try {
411: manager.storeContent("/thepath", content, null);
412: fail();
413: } catch (UnsupportedMimeTypeException e) {
414: assertSame(UnsupportedMimeType.UNSUPPORTED, e.getMimeType());
415: }
416: }
417:
418: public void testUseContent() throws Exception {
419: ContentManager manager = DatabaseContentFactory
420: .getInstance(mDatasource);
421:
422: // image data
423: URL image_resource_gif = ResourceFinderClasspath.getInstance()
424: .getResource("uwyn.gif");
425: URL image_resource_png = ResourceFinderClasspath.getInstance()
426: .getResource("uwyn.png");
427: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
428: final byte[] data_image_png = FileUtils
429: .readBytes(image_resource_png);
430:
431: manager.storeContent("/the/logo/of", new Content(
432: MimeType.IMAGE_PNG, data_image_gif), null);
433:
434: manager.useContentData("/the/logo/of", new ContentDataUser() {
435: public Object useContentData(Object contentData)
436: throws InnerClassException {
437: assertTrue(Arrays.equals(data_image_png,
438: (byte[]) contentData));
439: return null;
440: }
441: });
442:
443: assertNull(manager.useContentData("/the/logo/of/uwyn.png",
444: new ContentDataUser() {
445: public Object useContentData(Object contentData)
446: throws InnerClassException {
447: fail();
448: return null;
449: }
450: }));
451:
452: assertNull(manager.useContentData("/the/logo/of/wrongname.png",
453: new ContentDataUser() {
454: public Object useContentData(Object contentData)
455: throws InnerClassException {
456: fail();
457: return null;
458: }
459: }));
460:
461: assertNull(manager.useContentData("/the/wrong/path/uwyn.png",
462: new ContentDataUser() {
463: public Object useContentData(Object contentData)
464: throws InnerClassException {
465: fail();
466: return null;
467: }
468: }));
469: }
470:
471: public void testUseContentName() throws Exception {
472: ContentManager manager = DatabaseContentFactory
473: .getInstance(mDatasource);
474:
475: // image data
476: URL image_resource_gif = ResourceFinderClasspath.getInstance()
477: .getResource("uwyn.gif");
478: URL image_resource_png = ResourceFinderClasspath.getInstance()
479: .getResource("uwyn.png");
480: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
481: final byte[] data_image_png = FileUtils
482: .readBytes(image_resource_png);
483:
484: manager.storeContent("/the/logo/of", new Content(
485: MimeType.IMAGE_PNG, data_image_gif).name("uwyn.png"),
486: null);
487:
488: manager.useContentData("/the/logo/of", new ContentDataUser() {
489: public Object useContentData(Object contentData)
490: throws InnerClassException {
491: assertTrue(Arrays.equals(data_image_png,
492: (byte[]) contentData));
493: return null;
494: }
495: });
496:
497: manager.useContentData("/the/logo/of/uwyn.png",
498: new ContentDataUser() {
499: public Object useContentData(Object contentData)
500: throws InnerClassException {
501: assertTrue(Arrays.equals(data_image_png,
502: (byte[]) contentData));
503: return null;
504: }
505: });
506:
507: assertNull(manager.useContentData("/the/logo/of/wrongname.png",
508: new ContentDataUser() {
509: public Object useContentData(Object contentData)
510: throws InnerClassException {
511: fail();
512: return null;
513: }
514: }));
515:
516: assertNull(manager.useContentData("/the/wrong/path/uwyn.png",
517: new ContentDataUser() {
518: public Object useContentData(Object contentData)
519: throws InnerClassException {
520: fail();
521: return null;
522: }
523: }));
524: }
525:
526: public void testUseContentDataIllegalArguments() throws Exception {
527: ContentManager manager = DatabaseContentFactory
528: .getInstance(mDatasource);
529:
530: try {
531: manager.useContentData(null, new ContentDataUser() {
532: public Object useContentData(Object contentData)
533: throws InnerClassException {
534: return null;
535: }
536: });
537: fail();
538: } catch (IllegalArgumentException e) {
539: assertTrue(true);
540: }
541:
542: try {
543: manager.useContentData("", new ContentDataUser() {
544: public Object useContentData(Object contentData)
545: throws InnerClassException {
546: return null;
547: }
548: });
549: fail();
550: } catch (IllegalArgumentException e) {
551: assertTrue(true);
552: }
553:
554: try {
555: manager.useContentData("notabsolute",
556: new ContentDataUser() {
557: public Object useContentData(Object contentData)
558: throws InnerClassException {
559: return null;
560: }
561: });
562: fail();
563: } catch (IllegalArgumentException e) {
564: assertTrue(true);
565: }
566:
567: try {
568: manager.useContentData("/url", null);
569: fail();
570: } catch (IllegalArgumentException e) {
571: assertTrue(true);
572: }
573: }
574:
575: public void testUseContentDataUnknown() throws Exception {
576: ContentManager manager = DatabaseContentFactory
577: .getInstance(mDatasource);
578:
579: manager.useContentData("/unknown", new ContentDataUser() {
580: public Object useContentData(Object contentData)
581: throws InnerClassException {
582: assertNull(contentData);
583: return null;
584: }
585: });
586: }
587:
588: public void testHasContentData() {
589: ContentManager manager = DatabaseContentFactory
590: .getInstance(mDatasource);
591:
592: assertFalse(manager.hasContentData("/textcontent"));
593:
594: Content content = new Content(MimeType.APPLICATION_XHTML,
595: "<p>some text</p>").fragment(true);
596: manager.storeContent("/textcontent", content, null);
597:
598: assertTrue(manager.hasContentData("/textcontent"));
599: assertFalse(manager.hasContentData("/textcontent/mytext.xhtml"));
600: }
601:
602: public void testHasContentDataName() {
603: ContentManager manager = DatabaseContentFactory
604: .getInstance(mDatasource);
605:
606: assertFalse(manager.hasContentData("/textcontent"));
607:
608: Content content = new Content(MimeType.APPLICATION_XHTML,
609: "<p>some text</p>").name("mytext.xhtml").fragment(true);
610: manager.storeContent("/textcontent", content, null);
611:
612: assertTrue(manager.hasContentData("/textcontent"));
613: assertTrue(manager.hasContentData("/textcontent/mytext.xhtml"));
614: assertFalse(manager
615: .hasContentData("/textcontent/unknowntext.xhtml"));
616: assertFalse(manager
617: .hasContentData("/unknowncontent/mytext.xhtml"));
618: }
619:
620: public void testHasContentDataIllegalArguments() throws Exception {
621: ContentManager manager = DatabaseContentFactory
622: .getInstance(mDatasource);
623:
624: try {
625: manager.hasContentData(null);
626: fail();
627: } catch (IllegalArgumentException e) {
628: assertTrue(true);
629: }
630:
631: try {
632: manager.hasContentData("");
633: fail();
634: } catch (IllegalArgumentException e) {
635: assertTrue(true);
636: }
637:
638: try {
639: manager.hasContentData("notabsolute");
640: fail();
641: } catch (IllegalArgumentException e) {
642: assertTrue(true);
643: }
644: }
645:
646: public void testDeleteContentImage() throws Exception {
647: URL image_resource_gif = ResourceFinderClasspath.getInstance()
648: .getResource("uwyn.gif");
649: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
650:
651: DatabaseContent manager = DatabaseContentFactory
652: .getInstance(mDatasource);
653:
654: assertFalse(manager.hasContentData("/imagecontent"));
655:
656: Content content = new Content(MimeType.IMAGE_PNG,
657: data_image_gif);
658: manager.storeContent("/imagecontent", content, null);
659:
660: DatabaseImageStore store = DatabaseImageStoreFactory
661: .getInstance(mDatasource);
662: DatabaseContentInfo info = manager
663: .getContentInfo("/imagecontent");
664: assertNotNull(info);
665: manager.useContentData("/imagecontent", new ContentDataUser() {
666: public Object useContentData(Object contentData)
667: throws InnerClassException {
668: assertNotNull(contentData);
669: return null;
670: }
671: });
672: assertTrue(manager.hasContentData("/imagecontent"));
673: assertTrue(store.hasContentData(info.getContentId()));
674: assertTrue(manager.deleteContent("/imagecontent"));
675:
676: assertNull(manager.getContentInfo("/imagecontent"));
677: manager.useContentData("/imagecontent", new ContentDataUser() {
678: public Object useContentData(Object contentData)
679: throws InnerClassException {
680: assertNull(contentData);
681: return null;
682: }
683: });
684: assertFalse(manager.hasContentData("/imagecontent"));
685: assertFalse(store.hasContentData(info.getContentId()));
686: assertFalse(manager.deleteContent("/imagecontent"));
687: }
688:
689: public void testDeleteContentText() {
690: DatabaseContent manager = DatabaseContentFactory
691: .getInstance(mDatasource);
692:
693: assertFalse(manager.hasContentData("/textcontent"));
694:
695: Content content = new Content(MimeType.APPLICATION_XHTML,
696: "<p>some text</p>").fragment(true);
697: manager.storeContent("/textcontent", content, null);
698:
699: DatabaseTextStore store = DatabaseTextStoreFactory
700: .getInstance(mDatasource);
701: DatabaseContentInfo info = manager
702: .getContentInfo("/textcontent");
703: assertNotNull(info);
704: manager.useContentData("/textcontent", new ContentDataUser() {
705: public Object useContentData(Object contentData)
706: throws InnerClassException {
707: assertNotNull(contentData);
708: return null;
709: }
710: });
711: assertTrue(manager.hasContentData("/textcontent"));
712: assertTrue(store.hasContentData(info.getContentId()));
713: assertTrue(manager.deleteContent("/textcontent"));
714:
715: assertNull(manager.getContentInfo("/textcontent"));
716: manager.useContentData("/textcontent", new ContentDataUser() {
717: public Object useContentData(Object contentData)
718: throws InnerClassException {
719: assertNull(contentData);
720: return null;
721: }
722: });
723: assertFalse(manager.hasContentData("/textcontent"));
724: assertFalse(store.hasContentData(info.getContentId()));
725: assertFalse(manager.deleteContent("/textcontent"));
726: }
727:
728: public void testDeleteContentRaw() throws Exception {
729: DatabaseContent manager = DatabaseContentFactory
730: .getInstance(mDatasource);
731:
732: int size = 1024 * 1024 * 4; // 4Mb
733: final byte[] raw = new byte[size];
734: for (int i = 0; i < size; i++) {
735: raw[i] = (byte) (i % 255);
736: }
737:
738: assertFalse(manager.hasContentData("/rawcontent"));
739:
740: Content content = new Content(MimeType.RAW,
741: new ByteArrayInputStream(raw));
742: manager.storeContent("/rawcontent", content, null);
743:
744: DatabaseRawStore store = DatabaseRawStoreFactory
745: .getInstance(mDatasource);
746: DatabaseContentInfo info = manager
747: .getContentInfo("/rawcontent");
748: assertNotNull(info);
749: manager.useContentData("/rawcontent", new ContentDataUser() {
750: public Object useContentData(Object contentData)
751: throws InnerClassException {
752: assertNotNull(contentData);
753: return null;
754: }
755: });
756: assertTrue(manager.hasContentData("/rawcontent"));
757: assertTrue(store.hasContentData(info.getContentId()));
758: assertTrue(manager.deleteContent("/rawcontent"));
759:
760: assertNull(manager.getContentInfo("/rawcontent"));
761: manager.useContentData("/rawcontent", new ContentDataUser() {
762: public Object useContentData(Object contentData)
763: throws InnerClassException {
764: assertNull(contentData);
765: return null;
766: }
767: });
768: assertFalse(manager.hasContentData("/rawcontent"));
769: assertFalse(store.hasContentData(info.getContentId()));
770: assertFalse(manager.deleteContent("/rawcontent"));
771: }
772:
773: public void testDeleteContentIllegalArguments() throws Exception {
774: ContentManager manager = DatabaseContentFactory
775: .getInstance(mDatasource);
776:
777: try {
778: manager.deleteContent(null);
779: fail();
780: } catch (IllegalArgumentException e) {
781: assertTrue(true);
782: }
783:
784: try {
785: manager.deleteContent("");
786: fail();
787: } catch (IllegalArgumentException e) {
788: assertTrue(true);
789: }
790:
791: try {
792: manager.deleteContent("notabsolute");
793: fail();
794: } catch (IllegalArgumentException e) {
795: assertTrue(true);
796: }
797: }
798:
799: public void testGetContentInfo() {
800: DatabaseContent manager = DatabaseContentFactory
801: .getInstance(mDatasource);
802:
803: assertNull(manager.getContentInfo("/textcontent"));
804:
805: Content content_text1 = new Content(MimeType.APPLICATION_XHTML,
806: "<p>some text</p>").fragment(true).attribute("attr1",
807: "value1").attribute("attr2", "value2");
808: manager.storeContent("/textcontent", content_text1, null);
809:
810: DatabaseContentInfo info1a = manager
811: .getContentInfo("/textcontent");
812: assertNotNull(info1a);
813: assertTrue(info1a.getContentId() >= 0);
814: assertNotNull(info1a.getCreated());
815: assertTrue(info1a.getCreated().getTime() <= System
816: .currentTimeMillis());
817: assertEquals("/textcontent", info1a.getPath());
818: assertEquals(MimeType.APPLICATION_XHTML.toString(), info1a
819: .getMimeType());
820: assertEquals(0, info1a.getVersion());
821: assertNotNull(info1a.getAttributes());
822: assertEquals(2, info1a.getAttributes().size());
823: assertEquals("value1", info1a.getAttributes().get("attr1"));
824: assertEquals("value2", info1a.getAttributes().get("attr2"));
825:
826: DatabaseContentInfo info1b = manager
827: .getContentInfo("/textcontent/mytext.html");
828: assertNull(info1b);
829:
830: Content content_text2 = new Content(MimeType.APPLICATION_XHTML,
831: "<p>some other text</p>").fragment(true);
832: manager.storeContent("/textcontent", content_text2, null);
833:
834: DatabaseContentInfo info2a = manager
835: .getContentInfo("/textcontent");
836: assertNotNull(info2a);
837: assertEquals(info1a.getContentId() + 1, info2a.getContentId());
838: assertNotNull(info2a.getCreated());
839: assertTrue(info2a.getCreated().getTime() <= System
840: .currentTimeMillis());
841: assertEquals("/textcontent", info2a.getPath());
842: assertEquals(MimeType.APPLICATION_XHTML.toString(), info2a
843: .getMimeType());
844: assertEquals(1, info2a.getVersion());
845: assertNull(info2a.getAttributes());
846:
847: ContentInfo info2b = manager
848: .getContentInfo("/textcontent/mytext.html");
849: assertNull(info2b);
850: }
851:
852: public void testGetContentInfoName() {
853: DatabaseContent manager = DatabaseContentFactory
854: .getInstance(mDatasource);
855:
856: assertNull(manager.getContentInfo("/textcontent"));
857:
858: Content content_text1 = new Content(MimeType.APPLICATION_XHTML,
859: "<p>some text</p>").fragment(true).attribute("attr1",
860: "value1").attribute("attr2", "value2").name(
861: "mytext.html");
862: manager.storeContent("/textcontent", content_text1, null);
863:
864: DatabaseContentInfo info1a = manager
865: .getContentInfo("/textcontent");
866: assertNotNull(info1a);
867: assertTrue(info1a.getContentId() >= 0);
868: assertNotNull(info1a.getCreated());
869: assertTrue(info1a.getCreated().getTime() <= System
870: .currentTimeMillis());
871: assertEquals("/textcontent", info1a.getPath());
872: assertEquals(MimeType.APPLICATION_XHTML.toString(), info1a
873: .getMimeType());
874: assertEquals(0, info1a.getVersion());
875: assertNotNull(info1a.getAttributes());
876: assertEquals(2, info1a.getAttributes().size());
877: assertEquals("value1", info1a.getAttributes().get("attr1"));
878: assertEquals("value2", info1a.getAttributes().get("attr2"));
879:
880: DatabaseContentInfo info1b = manager
881: .getContentInfo("/textcontent/mytext.html");
882: assertNotNull(info1b);
883: assertTrue(info1b.getContentId() >= 0);
884: assertNotNull(info1b.getCreated());
885: assertTrue(info1b.getCreated().getTime() <= System
886: .currentTimeMillis());
887: assertEquals("/textcontent", info1b.getPath());
888: assertEquals(MimeType.APPLICATION_XHTML.toString(), info1b
889: .getMimeType());
890: assertEquals(0, info1b.getVersion());
891: assertNotNull(info1b.getAttributes());
892: assertEquals(2, info1b.getAttributes().size());
893: assertEquals("value1", info1b.getAttributes().get("attr1"));
894: assertEquals("value2", info1b.getAttributes().get("attr2"));
895:
896: DatabaseContentInfo info1c = manager
897: .getContentInfo("/textcontent/unknown.html");
898: assertNull(info1c);
899:
900: DatabaseContentInfo info1d = manager
901: .getContentInfo("/unknowncontent/mytext.html");
902: assertNull(info1d);
903:
904: Content content_text2 = new Content(MimeType.APPLICATION_XHTML,
905: "<p>some other text</p>").fragment(true);
906: manager.storeContent("/textcontent", content_text2, null);
907: DatabaseContentInfo info2 = manager
908: .getContentInfo("/textcontent");
909: assertNotNull(info2);
910: assertEquals(info1a.getContentId() + 1, info2.getContentId());
911: assertNotNull(info2.getCreated());
912: assertTrue(info2.getCreated().getTime() <= System
913: .currentTimeMillis());
914: assertEquals("/textcontent", info2.getPath());
915: assertEquals(MimeType.APPLICATION_XHTML.toString(), info2
916: .getMimeType());
917: assertEquals(1, info2.getVersion());
918: assertNull(info2.getAttributes());
919:
920: DatabaseContentInfo info2b = manager
921: .getContentInfo("/textcontent/mytext.html");
922: assertNotNull(info2b);
923:
924: DatabaseContentInfo info2c = manager
925: .getContentInfo("/textcontent/unknown.html");
926: assertNull(info2c);
927:
928: DatabaseContentInfo info2d = manager
929: .getContentInfo("/unknowncontent/mytext.html");
930: assertNull(info2d);
931: }
932:
933: public void testGetContentInfoIllegalArguments() throws Exception {
934: ContentManager manager = DatabaseContentFactory
935: .getInstance(mDatasource);
936:
937: try {
938: manager.getContentInfo(null);
939: fail();
940: } catch (IllegalArgumentException e) {
941: assertTrue(true);
942: }
943:
944: try {
945: manager.getContentInfo("");
946: fail();
947: } catch (IllegalArgumentException e) {
948: assertTrue(true);
949: }
950:
951: try {
952: manager.getContentInfo("notabsolute");
953: fail();
954: } catch (IllegalArgumentException e) {
955: assertTrue(true);
956: }
957: }
958:
959: public void testServeContentIllegalArgument() throws Exception {
960: ContentManager manager = DatabaseContentFactory
961: .getInstance(mDatasource);
962:
963: try {
964: manager.serveContentData(null, "/apath");
965: fail();
966: } catch (IllegalArgumentException e) {
967: assertTrue(true);
968: }
969: }
970:
971: public void testGetContentForHtmlInvalidPath() throws Exception {
972: ContentManager manager = DatabaseContentFactory
973: .getInstance(mDatasource);
974:
975: assertEquals("", manager.getContentForHtml(null, null, null));
976: assertEquals("", manager.getContentForHtml("", null, null));
977: assertEquals("", manager.getContentForHtml("notabsolute", null,
978: null));
979: }
980:
981: public void testGetContentForHtmlUnknownPath() throws Exception {
982: ContentManager manager = DatabaseContentFactory
983: .getInstance(mDatasource);
984:
985: assertEquals("", manager.getContentForHtml("/unknown", null,
986: null));
987: }
988: }
|