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: TestDatabaseTextStore.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.dam.contentstores;
009:
010: import com.uwyn.rife.cmf.dam.contentstores.exceptions.*;
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.dam.ContentDataUser;
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.config.RifeConfig;
021: import com.uwyn.rife.database.Datasource;
022: import com.uwyn.rife.database.DbPreparedStatement;
023: import com.uwyn.rife.database.DbPreparedStatementHandler;
024: import com.uwyn.rife.database.queries.Insert;
025: import com.uwyn.rife.database.queries.Select;
026: import com.uwyn.rife.tools.InnerClassException;
027: import junit.framework.TestCase;
028:
029: public class TestDatabaseTextStore extends TestCase {
030: private Datasource mDatasource = null;
031:
032: public TestDatabaseTextStore(Datasource datasource,
033: String datasourceName, String name) {
034: super (name);
035:
036: mDatasource = datasource;
037: }
038:
039: public void setUp() throws Exception {
040: DatabaseContentFactory.getInstance(mDatasource).install();
041: }
042:
043: public void tearDown() throws Exception {
044: try {
045: DatabaseContentFactory.getInstance(mDatasource).remove();
046: } catch (Throwable e) {
047: // discart errors
048: }
049: }
050:
051: public void testInstallError() throws Exception {
052: DatabaseTextStore store = DatabaseTextStoreFactory
053: .getInstance(mDatasource);
054: try {
055: store.install();
056: fail();
057: } catch (InstallContentStoreErrorException e) {
058: assertNotNull(e.getCause());
059: }
060: }
061:
062: public void testRemoveError() throws Exception {
063: DatabaseContentFactory.getInstance(mDatasource).remove();
064: DatabaseTextStore store = DatabaseTextStoreFactory
065: .getInstance(mDatasource);
066: try {
067: store.remove();
068: fail();
069: } catch (RemoveContentStoreErrorException e) {
070: assertNotNull(e.getCause());
071: }
072: }
073:
074: public void testGetSupportedMimeTypes() {
075: DatabaseTextStore store = DatabaseTextStoreFactory
076: .getInstance(mDatasource);
077: assertTrue(store.getSupportedMimeTypes().size() > 0);
078: }
079:
080: public void testGetContentType() {
081: DatabaseTextStore store = DatabaseTextStoreFactory
082: .getInstance(mDatasource);
083: ContentInfo info = new ContentInfo();
084: info.setMimeType(MimeType.APPLICATION_XHTML.toString());
085: assertNotNull(store.getContentType(info));
086: info.setMimeType(MimeType.IMAGE_GIF.toString());
087: assertNull(store.getContentType(info));
088: }
089:
090: public void testGetFormatter() {
091: DatabaseTextStore store = DatabaseTextStoreFactory
092: .getInstance(mDatasource);
093: assertNotNull(store.getFormatter(MimeType.APPLICATION_XHTML,
094: false));
095: assertNotNull(store.getFormatter(MimeType.TEXT_PLAIN, false));
096: assertNull(store.getFormatter(MimeType.IMAGE_GIF, true));
097: }
098:
099: public void testStoreContentDataXhtml() throws Exception {
100: final int[] id = new int[] { 1 };
101: final DatabaseContent manager = DatabaseContentFactory
102: .getInstance(mDatasource);
103: Insert insert = null;
104: if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
105: .getAliasedDriver())) {
106: insert = new Insert(mDatasource).into(
107: RifeConfig.Cmf.getTableContentInfo())
108: .fieldsParametersExcluded(
109: DatabaseContentInfo.class,
110: new String[] { "contentId" })
111: .fieldParameter("version").fieldParameter(
112: "repositoryId");
113: } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
114: .equals(mDatasource.getAliasedDriver())) {
115: insert = new Insert(mDatasource).into(
116: RifeConfig.Cmf.getTableContentInfo())
117: .fieldsParametersExcluded(
118: DatabaseContentInfo.class,
119: new String[] { "path" }).fieldParameter(
120: "\"path\"", "path").fieldParameter(
121: "version").fieldParameter("repositoryId");
122: } else {
123: insert = new Insert(mDatasource).into(
124: RifeConfig.Cmf.getTableContentInfo())
125: .fieldsParameters(DatabaseContentInfo.class)
126: .fieldParameter("version").fieldParameter(
127: "repositoryId");
128: }
129: manager.executeUpdate(insert, new DbPreparedStatementHandler() {
130: public void setParameters(DbPreparedStatement statement) {
131: DatabaseContentInfo content_info = new DatabaseContentInfo();
132: if (!"org.apache.derby.jdbc.EmbeddedDriver"
133: .equals(mDatasource.getAliasedDriver())) {
134: content_info.setContentId(id[0]);
135: }
136: content_info.setFragment(false);
137: content_info.setPath("/testpath");
138: content_info.setMimeType(MimeType.APPLICATION_XHTML
139: .toString());
140: statement
141: .setInt("version", 1)
142: .setInt(
143: "repositoryId",
144: manager
145: .executeGetFirstInt(new Select(
146: mDatasource)
147: .from(
148: RifeConfig.Cmf
149: .getTableContentRepository())
150: .field("repositoryId")
151: .where(
152: "name",
153: "=",
154: ContentRepository.DEFAULT)))
155: .setBean(content_info);
156: }
157: });
158:
159: final String data = "<i>cool beans</i><p>hôt <a href=\"http://uwyn.com\">chili</a></p>";
160:
161: DatabaseTextStore store = DatabaseTextStoreFactory
162: .getInstance(mDatasource);
163: Content content = new Content(MimeType.APPLICATION_XHTML, data)
164: .fragment(true);
165: assertTrue(store.storeContentData(id[0], content, null));
166:
167: store.useContentData(id[0], new ContentDataUser() {
168: public Object useContentData(Object contentData)
169: throws InnerClassException {
170: String data_encoded = "<i>cool beans</i><p>hôt <a href=\"http://uwyn.com\">chili</a></p>";
171: assertEquals(data_encoded, contentData);
172: return null;
173: }
174: });
175: }
176:
177: public void testStoreContentDataXhtmlLarge() throws Exception {
178: final int[] id = new int[] { 1 };
179: final DatabaseContent manager = DatabaseContentFactory
180: .getInstance(mDatasource);
181: Insert insert = null;
182: if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
183: .getAliasedDriver())) {
184: insert = new Insert(mDatasource).into(
185: RifeConfig.Cmf.getTableContentInfo())
186: .fieldsParametersExcluded(
187: DatabaseContentInfo.class,
188: new String[] { "contentId" })
189: .fieldParameter("version").fieldParameter(
190: "repositoryId");
191: } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
192: .equals(mDatasource.getAliasedDriver())) {
193: insert = new Insert(mDatasource).into(
194: RifeConfig.Cmf.getTableContentInfo())
195: .fieldsParametersExcluded(
196: DatabaseContentInfo.class,
197: new String[] { "path" }).fieldParameter(
198: "\"path\"", "path").fieldParameter(
199: "version").fieldParameter("repositoryId");
200: } else {
201: insert = new Insert(mDatasource).into(
202: RifeConfig.Cmf.getTableContentInfo())
203: .fieldsParameters(DatabaseContentInfo.class)
204: .fieldParameter("version").fieldParameter(
205: "repositoryId");
206: }
207: manager.executeUpdate(insert, new DbPreparedStatementHandler() {
208: public void setParameters(DbPreparedStatement statement) {
209: DatabaseContentInfo content_info = new DatabaseContentInfo();
210: if (!"org.apache.derby.jdbc.EmbeddedDriver"
211: .equals(mDatasource.getAliasedDriver())) {
212: content_info.setContentId(id[0]);
213: }
214: content_info.setFragment(false);
215: content_info.setPath("/testpath");
216: content_info.setMimeType(MimeType.APPLICATION_XHTML
217: .toString());
218: statement
219: .setInt("version", 1)
220: .setInt(
221: "repositoryId",
222: manager
223: .executeGetFirstInt(new Select(
224: mDatasource)
225: .from(
226: RifeConfig.Cmf
227: .getTableContentRepository())
228: .field("repositoryId")
229: .where(
230: "name",
231: "=",
232: ContentRepository.DEFAULT)))
233: .setBean(content_info);
234: }
235: });
236:
237: String data_part = "<i>cool beans</i><p>hôt <a href=\"http://uwyn.com\">chili</a></p>";
238: final StringBuffer data = new StringBuffer();
239: for (int i = 0; i < 14000; i++) {
240: data.append(data_part);
241: }
242:
243: DatabaseTextStore store = DatabaseTextStoreFactory
244: .getInstance(mDatasource);
245: Content content = new Content(MimeType.APPLICATION_XHTML, data
246: .toString()).fragment(true);
247: assertTrue(store.storeContentData(id[0], content, null));
248:
249: store.useContentData(id[0], new ContentDataUser() {
250: public Object useContentData(Object contentData)
251: throws InnerClassException {
252: String data_encoded_part = "<i>cool beans</i><p>hôt <a href=\"http://uwyn.com\">chili</a></p>";
253: StringBuffer data_encoded = new StringBuffer();
254: for (int i = 0; i < 14000; i++) {
255: data_encoded.append(data_encoded_part);
256: }
257: assertEquals(data_encoded.toString(), contentData);
258: return null;
259: }
260: });
261: }
262:
263: public void testStoreContentDataTextPlain() throws Exception {
264: final int[] id = new int[] { 1 };
265: final DatabaseContent manager = DatabaseContentFactory
266: .getInstance(mDatasource);
267: Insert insert = null;
268: if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
269: .getAliasedDriver())) {
270: insert = new Insert(mDatasource).into(
271: RifeConfig.Cmf.getTableContentInfo())
272: .fieldsParametersExcluded(
273: DatabaseContentInfo.class,
274: new String[] { "contentId" })
275: .fieldParameter("version").fieldParameter(
276: "repositoryId");
277: } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
278: .equals(mDatasource.getAliasedDriver())) {
279: insert = new Insert(mDatasource).into(
280: RifeConfig.Cmf.getTableContentInfo())
281: .fieldsParametersExcluded(
282: DatabaseContentInfo.class,
283: new String[] { "path" }).fieldParameter(
284: "\"path\"", "path").fieldParameter(
285: "version").fieldParameter("repositoryId");
286: } else {
287: insert = new Insert(mDatasource).into(
288: RifeConfig.Cmf.getTableContentInfo())
289: .fieldsParameters(DatabaseContentInfo.class)
290: .fieldParameter("version").fieldParameter(
291: "repositoryId");
292: }
293: manager.executeUpdate(insert, new DbPreparedStatementHandler() {
294: public void setParameters(DbPreparedStatement statement) {
295: DatabaseContentInfo content_info = new DatabaseContentInfo();
296: if (!"org.apache.derby.jdbc.EmbeddedDriver"
297: .equals(mDatasource.getAliasedDriver())) {
298: content_info.setContentId(id[0]);
299: }
300: content_info.setFragment(false);
301: content_info.setPath("/testpath");
302: content_info
303: .setMimeType(MimeType.TEXT_PLAIN.toString());
304: statement
305: .setInt("version", 1)
306: .setInt(
307: "repositoryId",
308: manager
309: .executeGetFirstInt(new Select(
310: mDatasource)
311: .from(
312: RifeConfig.Cmf
313: .getTableContentRepository())
314: .field("repositoryId")
315: .where(
316: "name",
317: "=",
318: ContentRepository.DEFAULT)))
319: .setBean(content_info);
320: }
321: });
322:
323: final String data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse lacinia, neque eget euismod scelerisque, arcu est accumsan lectus, id accumsan elit nunc eget elit.";
324:
325: DatabaseTextStore store = DatabaseTextStoreFactory
326: .getInstance(mDatasource);
327: Content content = new Content(MimeType.TEXT_PLAIN, data)
328: .fragment(true);
329: assertTrue(store.storeContentData(id[0], content, null));
330:
331: store.useContentData(id[0], new ContentDataUser() {
332: public Object useContentData(Object contentData)
333: throws InnerClassException {
334: assertTrue(data.equals(contentData));
335: return null;
336: }
337: });
338: }
339:
340: public void testStoreContentDataTextPlainLarge() throws Exception {
341: final int[] id = new int[] { 1 };
342: final DatabaseContent manager = DatabaseContentFactory
343: .getInstance(mDatasource);
344: Insert insert = null;
345: if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
346: .getAliasedDriver())) {
347: insert = new Insert(mDatasource).into(
348: RifeConfig.Cmf.getTableContentInfo())
349: .fieldsParametersExcluded(
350: DatabaseContentInfo.class,
351: new String[] { "contentId" })
352: .fieldParameter("version").fieldParameter(
353: "repositoryId");
354: } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
355: .equals(mDatasource.getAliasedDriver())) {
356: insert = new Insert(mDatasource).into(
357: RifeConfig.Cmf.getTableContentInfo())
358: .fieldsParametersExcluded(
359: DatabaseContentInfo.class,
360: new String[] { "path" }).fieldParameter(
361: "\"path\"", "path").fieldParameter(
362: "version").fieldParameter("repositoryId");
363: } else {
364: insert = new Insert(mDatasource).into(
365: RifeConfig.Cmf.getTableContentInfo())
366: .fieldsParameters(DatabaseContentInfo.class)
367: .fieldParameter("version").fieldParameter(
368: "repositoryId");
369: }
370: manager.executeUpdate(insert, new DbPreparedStatementHandler() {
371: public void setParameters(DbPreparedStatement statement) {
372: DatabaseContentInfo content_info = new DatabaseContentInfo();
373: if (!"org.apache.derby.jdbc.EmbeddedDriver"
374: .equals(mDatasource.getAliasedDriver())) {
375: content_info.setContentId(id[0]);
376: }
377: content_info.setFragment(false);
378: content_info.setPath("/testpath");
379: content_info
380: .setMimeType(MimeType.TEXT_PLAIN.toString());
381: statement
382: .setInt("version", 1)
383: .setInt(
384: "repositoryId",
385: manager
386: .executeGetFirstInt(new Select(
387: mDatasource)
388: .from(
389: RifeConfig.Cmf
390: .getTableContentRepository())
391: .field("repositoryId")
392: .where(
393: "name",
394: "=",
395: ContentRepository.DEFAULT)))
396: .setBean(content_info);
397: }
398: });
399:
400: String data_part = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse lacinia, neque eget euismod scelerisque, arcu est accumsan lectus, id accumsan elit nunc eget elit.";
401: final StringBuffer data = new StringBuffer();
402: for (int i = 0; i < 5000; i++) {
403: data.append(data_part);
404: }
405:
406: DatabaseTextStore store = DatabaseTextStoreFactory
407: .getInstance(mDatasource);
408: Content content = new Content(MimeType.TEXT_PLAIN, data
409: .toString()).fragment(true);
410: assertTrue(store.storeContentData(id[0], content, null));
411:
412: store.useContentData(id[0], new ContentDataUser() {
413: public Object useContentData(Object contentData)
414: throws InnerClassException {
415: assertTrue(data.toString().equals(contentData));
416: return null;
417: }
418: });
419: }
420:
421: public void testStoreContentDataContentEmpty() throws Exception {
422: final int[] id = new int[] { 1 };
423: final DatabaseContent manager = DatabaseContentFactory
424: .getInstance(mDatasource);
425: Insert insert = null;
426: if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
427: .getAliasedDriver())) {
428: insert = new Insert(mDatasource).into(
429: RifeConfig.Cmf.getTableContentInfo())
430: .fieldsParametersExcluded(
431: DatabaseContentInfo.class,
432: new String[] { "contentId" })
433: .fieldParameter("version").fieldParameter(
434: "repositoryId");
435: } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
436: .equals(mDatasource.getAliasedDriver())) {
437: insert = new Insert(mDatasource).into(
438: RifeConfig.Cmf.getTableContentInfo())
439: .fieldsParametersExcluded(
440: DatabaseContentInfo.class,
441: new String[] { "path" }).fieldParameter(
442: "\"path\"", "path").fieldParameter(
443: "version").fieldParameter("repositoryId");
444: } else {
445: insert = new Insert(mDatasource).into(
446: RifeConfig.Cmf.getTableContentInfo())
447: .fieldsParameters(DatabaseContentInfo.class)
448: .fieldParameter("version").fieldParameter(
449: "repositoryId");
450: }
451: manager.executeUpdate(insert, new DbPreparedStatementHandler() {
452: public void setParameters(DbPreparedStatement statement) {
453: DatabaseContentInfo content_info = new DatabaseContentInfo();
454: if (!"org.apache.derby.jdbc.EmbeddedDriver"
455: .equals(mDatasource.getAliasedDriver())) {
456: content_info.setContentId(id[0]);
457: }
458: content_info.setFragment(false);
459: content_info.setPath("/testpath");
460: content_info.setMimeType(MimeType.APPLICATION_XHTML
461: .toString());
462: statement
463: .setInt("version", 1)
464: .setInt(
465: "repositoryId",
466: manager
467: .executeGetFirstInt(new Select(
468: mDatasource)
469: .from(
470: RifeConfig.Cmf
471: .getTableContentRepository())
472: .field("repositoryId")
473: .where(
474: "name",
475: "=",
476: ContentRepository.DEFAULT)))
477: .setBean(content_info);
478: }
479: });
480:
481: DatabaseTextStore store = DatabaseTextStoreFactory
482: .getInstance(mDatasource);
483: Content content = new Content(MimeType.APPLICATION_XHTML, null);
484: assertTrue(store.storeContentData(id[0], content, null));
485: store.useContentData(id[0], new ContentDataUser() {
486: public Object useContentData(Object contentData)
487: throws InnerClassException {
488: assertNull(contentData);
489: return null;
490: }
491: });
492: }
493:
494: public void testStoreContentDataContentNull() throws Exception {
495: final int[] id = new int[] { 1 };
496: final DatabaseContent manager = DatabaseContentFactory
497: .getInstance(mDatasource);
498: Insert insert = null;
499: if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
500: .getAliasedDriver())) {
501: insert = new Insert(mDatasource).into(
502: RifeConfig.Cmf.getTableContentInfo())
503: .fieldsParametersExcluded(
504: DatabaseContentInfo.class,
505: new String[] { "contentId" })
506: .fieldParameter("version").fieldParameter(
507: "repositoryId");
508: } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
509: .equals(mDatasource.getAliasedDriver())) {
510: insert = new Insert(mDatasource).into(
511: RifeConfig.Cmf.getTableContentInfo())
512: .fieldsParametersExcluded(
513: DatabaseContentInfo.class,
514: new String[] { "path" }).fieldParameter(
515: "\"path\"", "path").fieldParameter(
516: "version").fieldParameter("repositoryId");
517: } else {
518: insert = new Insert(mDatasource).into(
519: RifeConfig.Cmf.getTableContentInfo())
520: .fieldsParameters(DatabaseContentInfo.class)
521: .fieldParameter("version").fieldParameter(
522: "repositoryId");
523: }
524: manager.executeUpdate(insert, new DbPreparedStatementHandler() {
525: public void setParameters(DbPreparedStatement statement) {
526: DatabaseContentInfo content_info = new DatabaseContentInfo();
527: if (!"org.apache.derby.jdbc.EmbeddedDriver"
528: .equals(mDatasource.getAliasedDriver())) {
529: content_info.setContentId(id[0]);
530: }
531: content_info.setFragment(false);
532: content_info.setPath("/testpath");
533: content_info.setMimeType(MimeType.APPLICATION_XHTML
534: .toString());
535: statement
536: .setInt("version", 1)
537: .setInt(
538: "repositoryId",
539: manager
540: .executeGetFirstInt(new Select(
541: mDatasource)
542: .from(
543: RifeConfig.Cmf
544: .getTableContentRepository())
545: .field("repositoryId")
546: .where(
547: "name",
548: "=",
549: ContentRepository.DEFAULT)))
550: .setBean(content_info);
551: }
552: });
553:
554: DatabaseTextStore store = DatabaseTextStoreFactory
555: .getInstance(mDatasource);
556: assertTrue(store.storeContentData(id[0], null, null));
557: store.useContentData(id[0], new ContentDataUser() {
558: public Object useContentData(Object contentData)
559: throws InnerClassException {
560: assertNull(contentData);
561: return null;
562: }
563: });
564: }
565:
566: public void testStoreContentDataMimeTypeWithoutFormatter()
567: throws Exception {
568: final int[] id = new int[] { 1 };
569: final DatabaseContent manager = DatabaseContentFactory
570: .getInstance(mDatasource);
571: Insert insert = null;
572: if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
573: .getAliasedDriver())) {
574: insert = new Insert(mDatasource).into(
575: RifeConfig.Cmf.getTableContentInfo())
576: .fieldsParametersExcluded(
577: DatabaseContentInfo.class,
578: new String[] { "contentId" })
579: .fieldParameter("version").fieldParameter(
580: "repositoryId");
581: } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
582: .equals(mDatasource.getAliasedDriver())) {
583: insert = new Insert(mDatasource).into(
584: RifeConfig.Cmf.getTableContentInfo())
585: .fieldsParametersExcluded(
586: DatabaseContentInfo.class,
587: new String[] { "path" }).fieldParameter(
588: "\"path\"", "path").fieldParameter(
589: "version").fieldParameter("repositoryId");
590: } else {
591: insert = new Insert(mDatasource).into(
592: RifeConfig.Cmf.getTableContentInfo())
593: .fieldsParameters(DatabaseContentInfo.class)
594: .fieldParameter("version").fieldParameter(
595: "repositoryId");
596: }
597: manager.executeUpdate(insert, new DbPreparedStatementHandler() {
598: public void setParameters(DbPreparedStatement statement) {
599: DatabaseContentInfo content_info = new DatabaseContentInfo();
600: if (!"org.apache.derby.jdbc.EmbeddedDriver"
601: .equals(mDatasource.getAliasedDriver())) {
602: content_info.setContentId(id[0]);
603: }
604: content_info.setFragment(false);
605: content_info.setPath("/testpath");
606: content_info.setMimeType(MimeType.IMAGE_PNG.toString());
607: statement
608: .setInt("version", 1)
609: .setInt(
610: "repositoryId",
611: manager
612: .executeGetFirstInt(new Select(
613: mDatasource)
614: .from(
615: RifeConfig.Cmf
616: .getTableContentRepository())
617: .field("repositoryId")
618: .where(
619: "name",
620: "=",
621: ContentRepository.DEFAULT)))
622: .setBean(content_info);
623: }
624: });
625:
626: final String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
627:
628: DatabaseTextStore store = DatabaseTextStoreFactory
629: .getInstance(mDatasource);
630: Content content = new Content(MimeType.IMAGE_PNG, data)
631: .fragment(true);
632: assertTrue(store.storeContentData(id[0], content, null));
633:
634: store.useContentData(id[0], new ContentDataUser() {
635: public Object useContentData(Object contentData)
636: throws InnerClassException {
637: assertEquals(data, contentData);
638: return null;
639: }
640: });
641: }
642:
643: public void testStoreContentDataIllegalArgument() throws Exception {
644: DatabaseTextStore store = DatabaseTextStoreFactory
645: .getInstance(mDatasource);
646:
647: try {
648: store.storeContentData(-1, null, null);
649: fail();
650: } catch (IllegalArgumentException e) {
651: assertTrue(true);
652: }
653:
654: try {
655: store.storeContentData(1, new Content(
656: MimeType.APPLICATION_XHTML, new Object()), null);
657: fail();
658: } catch (IllegalArgumentException e) {
659: assertTrue(true);
660: }
661: }
662:
663: public void testStoreContentDataError() throws Exception {
664: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
665: DatabaseTextStore store = DatabaseTextStoreFactory
666: .getInstance(mDatasource);
667: Content content = new Content(MimeType.APPLICATION_XHTML, data)
668: .fragment(true);
669: try {
670: store.storeContentData(2, content, null);
671: assertTrue("com.mysql.jdbc.Driver".equals(mDatasource
672: .getAliasedDriver()));
673: } catch (StoreContentDataErrorException e) {
674: assertEquals(2, e.getId());
675: }
676: }
677:
678: public void testUseContentData() throws Exception {
679: DatabaseContent manager = DatabaseContentFactory
680: .getInstance(mDatasource);
681:
682: final String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
683:
684: Content content = new Content(MimeType.APPLICATION_XHTML, data)
685: .fragment(true);
686: assertTrue(manager.storeContent("/testxhtml", content, null));
687: DatabaseContentInfo content_info = manager
688: .getContentInfo("/testxhtml");
689:
690: DatabaseTextStore store = DatabaseTextStoreFactory
691: .getInstance(mDatasource);
692:
693: store.useContentData(content_info.getContentId(),
694: new ContentDataUser() {
695: public Object useContentData(Object contentData)
696: throws InnerClassException {
697: assertEquals(data, contentData);
698: return null;
699: }
700: });
701: }
702:
703: public void testUseContentDataUnknown() throws Exception {
704: DatabaseTextStore store = DatabaseTextStoreFactory
705: .getInstance(mDatasource);
706:
707: store.useContentData(232, new ContentDataUser() {
708: public Object useContentData(Object contentData)
709: throws InnerClassException {
710: assertNull(contentData);
711: return null;
712: }
713: });
714: }
715:
716: public void testUseContentDataIllegalArgument() throws Exception {
717: DatabaseTextStore store = DatabaseTextStoreFactory
718: .getInstance(mDatasource);
719:
720: try {
721: store.useContentData(-1, new ContentDataUser() {
722: public Object useContentData(Object contentData)
723: throws InnerClassException {
724: fail();
725: return null;
726: }
727: });
728: } catch (IllegalArgumentException e) {
729: assertTrue(true);
730: }
731:
732: try {
733: store.useContentData(23, null);
734: fail();
735: } catch (IllegalArgumentException e) {
736: assertTrue(true);
737: }
738: }
739:
740: public void testUseContentDataError() throws Exception {
741: DatabaseContentFactory.getInstance(mDatasource).remove();
742: DatabaseTextStore store = DatabaseTextStoreFactory
743: .getInstance(mDatasource);
744: try {
745: store.useContentData(2, new ContentDataUser() {
746: public Object useContentData(Object contentData)
747: throws InnerClassException {
748: fail();
749: return null;
750: }
751: });
752: } catch (UseContentDataErrorException e) {
753: assertEquals(2, e.getId());
754: }
755: }
756:
757: public void testHasContentData() throws Exception {
758: DatabaseContent manager = DatabaseContentFactory
759: .getInstance(mDatasource);
760:
761: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
762: Content content = new Content(MimeType.APPLICATION_XHTML, data)
763: .fragment(true);
764: assertTrue(manager.storeContent("/testxhtml", content, null));
765: DatabaseContentInfo content_info = manager
766: .getContentInfo("/testxhtml");
767:
768: DatabaseTextStore store = DatabaseTextStoreFactory
769: .getInstance(mDatasource);
770: assertTrue(store.hasContentData(content_info.getContentId()));
771: }
772:
773: public void testHasContentDataContentEmpty() throws Exception {
774: DatabaseContent manager = DatabaseContentFactory
775: .getInstance(mDatasource);
776:
777: Content content = new Content(MimeType.APPLICATION_XHTML, null);
778: assertTrue(manager.storeContent("/testxhtml", content, null));
779: DatabaseContentInfo content_info = manager
780: .getContentInfo("/testxhtml");
781:
782: DatabaseTextStore store = DatabaseTextStoreFactory
783: .getInstance(mDatasource);
784: assertFalse(store.hasContentData(content_info.getContentId()));
785: }
786:
787: public void testHasContentDataUnknown() throws Exception {
788: DatabaseTextStore store = DatabaseTextStoreFactory
789: .getInstance(mDatasource);
790: assertFalse(store.hasContentData(3));
791: }
792:
793: public void testHasContentDataIllegalArgument() throws Exception {
794: DatabaseTextStore store = DatabaseTextStoreFactory
795: .getInstance(mDatasource);
796: try {
797: store.hasContentData(-1);
798: fail();
799: } catch (IllegalArgumentException e) {
800: assertTrue(true);
801: }
802: }
803:
804: public void testHasContentDataError() throws Exception {
805: DatabaseContentFactory.getInstance(mDatasource).remove();
806: DatabaseTextStore store = DatabaseTextStoreFactory
807: .getInstance(mDatasource);
808: try {
809: store.hasContentData(2);
810: fail();
811: } catch (HasContentDataErrorException e) {
812: assertEquals(2, e.getId());
813: }
814: }
815:
816: public void testDeleteContentData() throws Exception {
817: DatabaseContent manager = DatabaseContentFactory
818: .getInstance(mDatasource);
819:
820: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
821: Content content = new Content(MimeType.APPLICATION_XHTML, data)
822: .fragment(true);
823: assertTrue(manager.storeContent("/testxhtml", content, null));
824: DatabaseContentInfo content_info = manager
825: .getContentInfo("/testxhtml");
826:
827: DatabaseTextStore store = DatabaseTextStoreFactory
828: .getInstance(mDatasource);
829: assertTrue(store.hasContentData(content_info.getContentId()));
830: assertTrue(store.deleteContentData(content_info.getContentId()));
831: assertFalse(store.hasContentData(content_info.getContentId()));
832: assertFalse(store
833: .deleteContentData(content_info.getContentId()));
834: }
835:
836: public void testDeleteContentDataUnknown() throws Exception {
837: DatabaseTextStore store = DatabaseTextStoreFactory
838: .getInstance(mDatasource);
839: assertFalse(store.deleteContentData(3));
840: }
841:
842: public void testDeleteContentDataIllegalArgument() throws Exception {
843: DatabaseTextStore store = DatabaseTextStoreFactory
844: .getInstance(mDatasource);
845: try {
846: store.deleteContentData(-1);
847: fail();
848: } catch (IllegalArgumentException e) {
849: assertTrue(true);
850: }
851: }
852:
853: public void testDeleteContentDataError() throws Exception {
854: DatabaseContentFactory.getInstance(mDatasource).remove();
855: DatabaseTextStore store = DatabaseTextStoreFactory
856: .getInstance(mDatasource);
857: try {
858: store.deleteContentData(2);
859: fail();
860: } catch (DeleteContentDataErrorException e) {
861: assertEquals(2, e.getId());
862: }
863: }
864:
865: public void testRetrieveSize() throws Exception {
866: DatabaseContent manager = DatabaseContentFactory
867: .getInstance(mDatasource);
868:
869: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
870: Content content = new Content(MimeType.APPLICATION_XHTML, data)
871: .fragment(true);
872: assertTrue(manager.storeContent("/testxhtml", content, null));
873: DatabaseContentInfo content_info = manager
874: .getContentInfo("/testxhtml");
875:
876: DatabaseTextStore store = DatabaseTextStoreFactory
877: .getInstance(mDatasource);
878:
879: assertEquals(store.getSize(content_info.getContentId()), data
880: .length());
881: }
882:
883: public void testRetrieveSizeIllegalArgument() throws Exception {
884: DatabaseTextStore store = DatabaseTextStoreFactory
885: .getInstance(mDatasource);
886:
887: try {
888: store.getSize(-1);
889: fail();
890: } catch (IllegalArgumentException e) {
891: assertTrue(true);
892: }
893: }
894:
895: public void testRetrieveSizeError() throws Exception {
896: DatabaseContentFactory.getInstance(mDatasource).remove();
897: DatabaseTextStore store = DatabaseTextStoreFactory
898: .getInstance(mDatasource);
899: try {
900: store.getSize(2);
901: fail();
902: } catch (RetrieveSizeErrorException e) {
903: assertEquals(2, e.getId());
904: }
905: }
906: }
|