0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: TestContentQueryManager.java 3848 2007-07-12 08:55:48Z gbevin $
0007: */
0008: package com.uwyn.rife.cmf.dam;
0009:
0010: import java.io.ByteArrayInputStream;
0011: import java.io.InputStream;
0012: import java.net.URL;
0013: import java.util.Arrays;
0014: import java.util.Iterator;
0015: import java.util.List;
0016:
0017: import junit.framework.TestCase;
0018:
0019: import com.uwyn.rife.cmf.MimeType;
0020: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContent;
0021: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentFactory;
0022: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentInfo;
0023: import com.uwyn.rife.cmf.dam.exceptions.*;
0024: import com.uwyn.rife.config.RifeConfig;
0025: import com.uwyn.rife.database.Datasource;
0026: import com.uwyn.rife.database.DbQueryManager;
0027: import com.uwyn.rife.database.queries.Select;
0028: import com.uwyn.rife.database.querymanagers.generic.GenericQueryManager;
0029: import com.uwyn.rife.database.querymanagers.generic.GenericQueryManagerFactory;
0030: import com.uwyn.rife.resources.ResourceFinderClasspath;
0031: import com.uwyn.rife.tools.FileUtils;
0032: import com.uwyn.rife.tools.InnerClassException;
0033: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
0034:
0035: public class TestContentQueryManager extends TestCase {
0036: private Datasource mDatasource = new Datasource();
0037:
0038: public TestContentQueryManager(Datasource datasource,
0039: String datasourceName, String name) {
0040: super (name);
0041:
0042: mDatasource = datasource;
0043: }
0044:
0045: public void setUp() throws Exception {
0046: DatabaseContentFactory.getInstance(mDatasource).install();
0047: }
0048:
0049: public void tearDown() throws Exception {
0050: DatabaseContentFactory.getInstance(mDatasource).remove();
0051: }
0052:
0053: public void testInstantiation() {
0054: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0055: mDatasource, ContentImage.class);
0056: assertNotNull(manager);
0057: assertNotNull(manager.getContentManager());
0058: assertTrue(manager.getContentManager() instanceof ContentManager);
0059: }
0060:
0061: public void testBuildCmfPathBean() throws Exception {
0062: ContentImage content = new ContentImage()
0063: .name("the content name");
0064: content.setId(3);
0065:
0066: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0067: mDatasource, ContentImage.class);
0068: assertEquals("/contentimage/3/image", manager.buildCmfPath(
0069: content, "image"));
0070: }
0071:
0072: public void testBuildCmfPathId() throws Exception {
0073: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0074: mDatasource, ContentImage.class);
0075: assertEquals("/contentimage/4/image", manager.buildCmfPath(4,
0076: "image"));
0077: }
0078:
0079: public void testBuildCmfPathBeanRepository() throws Exception {
0080: ContentImageRepository content = new ContentImageRepository()
0081: .name("the content name");
0082: content.setId(3);
0083:
0084: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0085: mDatasource, ContentImageRepository.class);
0086: assertEquals("testrep:/contentimagerepository/3/image", manager
0087: .buildCmfPath(content, "image"));
0088: }
0089:
0090: public void testBuildCmfPathIdRepository() throws Exception {
0091: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0092: mDatasource, ContentImageRepository.class);
0093: assertEquals("testrep:/contentimagerepository/4/image", manager
0094: .buildCmfPath(4, "image"));
0095: }
0096:
0097: public void testBuildServeContentPathBean() throws Exception {
0098: ContentImage content = new ContentImage()
0099: .name("the content name");
0100: content.setId(3);
0101:
0102: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0103: mDatasource, ContentImage.class);
0104: assertEquals("/contentimage/3/image", manager
0105: .buildServeContentPath(content, "image"));
0106: }
0107:
0108: public void testBuildServeContentPathId() throws Exception {
0109: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0110: mDatasource, ContentImage.class);
0111: assertEquals("/contentimage/4/image", manager
0112: .buildServeContentPath(4, "image"));
0113: }
0114:
0115: public void testBuildServeContentPathBeanRepository()
0116: throws Exception {
0117: ContentImageRepository content = new ContentImageRepository()
0118: .name("the content name");
0119: content.setId(3);
0120:
0121: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0122: mDatasource, ContentImageRepository.class);
0123: assertEquals("/contentimagerepository/3/image", manager
0124: .buildServeContentPath(content, "image"));
0125: }
0126:
0127: public void testBuildServeContentPathIdRepository()
0128: throws Exception {
0129: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0130: mDatasource, ContentImageRepository.class);
0131: assertEquals("/contentimagerepository/4/image", manager
0132: .buildServeContentPath(4, "image"));
0133: }
0134:
0135: public void testSaveContent() throws Exception {
0136: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0137: mDatasource, ContentImage.class);
0138: manager.install();
0139: try {
0140: URL image_resource_gif = ResourceFinderClasspath
0141: .getInstance().getResource("uwyn.gif");
0142: byte[] data_image_gif = FileUtils
0143: .readBytes(image_resource_gif);
0144: ContentImage content = new ContentImage().name(
0145: "the content name").image(data_image_gif);
0146:
0147: int id = manager.save(content);
0148: assertTrue(id >= 0);
0149:
0150: DatabaseContent content_manager = DatabaseContentFactory
0151: .getInstance(mDatasource);
0152: String path = manager.buildCmfPath(content, "image");
0153:
0154: GenericQueryManager<ContentImage> gqm = GenericQueryManagerFactory
0155: .getInstance(mDatasource, ContentImage.class);
0156: ContentImage restored = gqm.restore(id);
0157: assertEquals(content.getId(), restored.getId());
0158: assertEquals(content.getName(), restored.getName());
0159: assertNull(restored.getImage());
0160:
0161: DatabaseContentInfo info = content_manager
0162: .getContentInfo(path);
0163: assertEquals(id, info.getContentId());
0164: assertEquals(MimeType.IMAGE_PNG, info.getMimeType());
0165: assertEquals("myimage.png", info.getName());
0166:
0167: URL image_resource_png = ResourceFinderClasspath
0168: .getInstance().getResource("uwyn.png");
0169: final byte[] data_image_png = FileUtils
0170: .readBytes(image_resource_png);
0171:
0172: content_manager.useContentData(path, new ContentDataUser() {
0173: public Object useContentData(Object contentData)
0174: throws InnerClassException {
0175: assertTrue(Arrays.equals(data_image_png,
0176: (byte[]) contentData));
0177: return null;
0178: }
0179: });
0180: } finally {
0181: manager.remove();
0182: }
0183: }
0184:
0185: public void testSaveContentOtherTable() throws Exception {
0186: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0187: mDatasource, ContentImage.class, "othercontentimage");
0188: manager.install();
0189: try {
0190: URL image_resource_gif = ResourceFinderClasspath
0191: .getInstance().getResource("uwyn.gif");
0192: byte[] data_image_gif = FileUtils
0193: .readBytes(image_resource_gif);
0194: ContentImage content = new ContentImage().name(
0195: "the content name").image(data_image_gif);
0196:
0197: int id = manager.save(content);
0198: assertTrue(id >= 0);
0199:
0200: DatabaseContent content_manager = DatabaseContentFactory
0201: .getInstance(mDatasource);
0202: String path = manager.buildCmfPath(content, "image");
0203: assertTrue(path.startsWith("/contentimage/"));
0204:
0205: GenericQueryManager<ContentImage> gqm = GenericQueryManagerFactory
0206: .getInstance(mDatasource, ContentImage.class,
0207: "othercontentimage");
0208: ContentImage restored = gqm.restore(id);
0209: assertEquals(content.getId(), restored.getId());
0210: assertEquals(content.getName(), restored.getName());
0211: assertNull(restored.getImage());
0212: assertEquals(1, new DbQueryManager(mDatasource)
0213: .executeGetFirstInt(new Select(mDatasource).field(
0214: "count(*)").from("othercontentimage")));
0215:
0216: DatabaseContentInfo info = content_manager
0217: .getContentInfo(path);
0218: assertEquals(id, info.getContentId());
0219: assertEquals(MimeType.IMAGE_PNG, info.getMimeType());
0220: assertEquals("myimage.png", info.getName());
0221:
0222: URL image_resource_png = ResourceFinderClasspath
0223: .getInstance().getResource("uwyn.png");
0224: final byte[] data_image_png = FileUtils
0225: .readBytes(image_resource_png);
0226:
0227: content_manager.useContentData(path, new ContentDataUser() {
0228: public Object useContentData(Object contentData)
0229: throws InnerClassException {
0230: assertTrue(Arrays.equals(data_image_png,
0231: (byte[]) contentData));
0232: return null;
0233: }
0234: });
0235: } finally {
0236: manager.remove();
0237: }
0238: }
0239:
0240: public void testSaveContentRepository() throws Exception {
0241: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0242: mDatasource, ContentImageRepository.class);
0243: manager.install();
0244: manager.getContentManager().createRepository("testrep");
0245: try {
0246: URL image_resource_gif = ResourceFinderClasspath
0247: .getInstance().getResource("uwyn.gif");
0248: byte[] data_image_gif = FileUtils
0249: .readBytes(image_resource_gif);
0250: ContentImageRepository content = new ContentImageRepository()
0251: .name("the content name").image(data_image_gif);
0252:
0253: int id = manager.save(content);
0254: assertTrue(id >= 0);
0255:
0256: checkContentRepository(id, "testrep");
0257:
0258: DatabaseContent content_manager = DatabaseContentFactory
0259: .getInstance(mDatasource);
0260: String path = manager.buildCmfPath(content, "image");
0261: assertTrue(path.startsWith("testrep:"));
0262:
0263: GenericQueryManager<ContentImageRepository> gqm = GenericQueryManagerFactory
0264: .getInstance(mDatasource,
0265: ContentImageRepository.class);
0266: ContentImageRepository restored = gqm.restore(id);
0267: assertEquals(content.getId(), restored.getId());
0268: assertEquals(content.getName(), restored.getName());
0269: assertNull(restored.getImage());
0270:
0271: DatabaseContentInfo info = content_manager
0272: .getContentInfo(path);
0273: assertEquals(id, info.getContentId());
0274: assertEquals(MimeType.IMAGE_PNG, info.getMimeType());
0275: assertEquals("myimage.png", info.getName());
0276:
0277: URL image_resource_png = ResourceFinderClasspath
0278: .getInstance().getResource("uwyn.png");
0279: final byte[] data_image_png = FileUtils
0280: .readBytes(image_resource_png);
0281:
0282: content_manager.useContentData(path, new ContentDataUser() {
0283: public Object useContentData(Object contentData)
0284: throws InnerClassException {
0285: assertTrue(Arrays.equals(data_image_png,
0286: (byte[]) contentData));
0287: return null;
0288: }
0289: });
0290: } finally {
0291: manager.remove();
0292: }
0293: }
0294:
0295: public void testSaveContentRaw() throws Exception {
0296: ContentQueryManager<ContentRaw> manager = new ContentQueryManager<ContentRaw>(
0297: mDatasource, ContentRaw.class);
0298: manager.install();
0299: try {
0300: int size = 1024 * 1024 * 4; // 4Mb
0301: final byte[] raw = new byte[size];
0302: for (int i = 0; i < size; i++) {
0303: raw[i] = (byte) (i % 255);
0304: }
0305:
0306: ContentRaw content = new ContentRaw().name(
0307: "the content name").raw(
0308: new ByteArrayInputStream(raw));
0309:
0310: int id = manager.save(content);
0311: assertTrue(id >= 0);
0312:
0313: DatabaseContent content_manager = DatabaseContentFactory
0314: .getInstance(mDatasource);
0315: String path = manager.buildCmfPath(content, "raw");
0316:
0317: GenericQueryManager<ContentRaw> gqm = GenericQueryManagerFactory
0318: .getInstance(mDatasource, ContentRaw.class);
0319: ContentRaw restored = gqm.restore(id);
0320: assertEquals(content.getId(), restored.getId());
0321: assertEquals(content.getName(), restored.getName());
0322: assertNull(restored.getRaw());
0323:
0324: DatabaseContentInfo info = content_manager
0325: .getContentInfo(path);
0326: assertEquals(id, info.getContentId());
0327: assertEquals(MimeType.RAW, info.getMimeType());
0328:
0329: content_manager.useContentData(path, new ContentDataUser() {
0330: public Object useContentData(Object contentData)
0331: throws InnerClassException {
0332: try {
0333: assertTrue(Arrays.equals(raw, FileUtils
0334: .readBytes((InputStream) contentData)));
0335: } catch (FileUtilsErrorException e) {
0336: throwException(e);
0337: }
0338: return null;
0339: }
0340: });
0341: } finally {
0342: manager.remove();
0343: }
0344: }
0345:
0346: public void testSaveContentUpdate() throws Exception {
0347: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0348: mDatasource, ContentImage.class);
0349: manager.install();
0350: try {
0351: URL image_resource_gif = ResourceFinderClasspath
0352: .getInstance().getResource("uwyn.gif");
0353: byte[] data_image_gif = FileUtils
0354: .readBytes(image_resource_gif);
0355:
0356: ContentImage content = new ContentImage().name(
0357: "the content name").image(data_image_gif);
0358:
0359: int id = manager.save(content);
0360: assertTrue(id >= 0);
0361:
0362: URL rife_resource_tif = ResourceFinderClasspath
0363: .getInstance().getResource("rife-logo_small.tif");
0364: byte[] rife_image_tif = FileUtils
0365: .readBytes(rife_resource_tif);
0366: content.name("updated content name").image(rife_image_tif);
0367:
0368: manager.save(content);
0369:
0370: DatabaseContent content_manager = DatabaseContentFactory
0371: .getInstance(mDatasource);
0372: String path = manager.buildCmfPath(content, "image");
0373:
0374: GenericQueryManager<ContentImage> gqm = GenericQueryManagerFactory
0375: .getInstance(mDatasource, ContentImage.class);
0376: ContentImage restored = gqm.restore(id);
0377: assertEquals(content.getId(), restored.getId());
0378: assertEquals(content.getName(), restored.getName());
0379: assertNull(restored.getImage());
0380:
0381: DatabaseContentInfo info = content_manager
0382: .getContentInfo(path);
0383: assertEquals(id + 1, info.getContentId());
0384: assertEquals(MimeType.IMAGE_PNG, info.getMimeType());
0385:
0386: URL rife_resource_png = ResourceFinderClasspath
0387: .getInstance().getResource("rife-logo_small.png");
0388: final byte[] rife_image_png = FileUtils
0389: .readBytes(rife_resource_png);
0390:
0391: content_manager.useContentData(path, new ContentDataUser() {
0392: public Object useContentData(Object contentData)
0393: throws InnerClassException {
0394: assertTrue(Arrays.equals(rife_image_png,
0395: (byte[]) contentData));
0396: return null;
0397: }
0398: });
0399: } finally {
0400: manager.remove();
0401: }
0402: }
0403:
0404: public void testSaveContentUpdateRepository() throws Exception {
0405: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0406: mDatasource, ContentImageRepository.class);
0407: manager.install();
0408: manager.getContentManager().createRepository("testrep");
0409: try {
0410: URL image_resource_gif = ResourceFinderClasspath
0411: .getInstance().getResource("uwyn.gif");
0412: byte[] data_image_gif = FileUtils
0413: .readBytes(image_resource_gif);
0414:
0415: ContentImageRepository content = new ContentImageRepository()
0416: .name("the content name").image(data_image_gif);
0417:
0418: int id = manager.save(content);
0419: assertTrue(id >= 0);
0420:
0421: checkContentRepository(id, "testrep");
0422:
0423: URL rife_resource_tif = ResourceFinderClasspath
0424: .getInstance().getResource("rife-logo_small.tif");
0425: byte[] rife_image_tif = FileUtils
0426: .readBytes(rife_resource_tif);
0427: content.name("updated content name").image(rife_image_tif);
0428:
0429: manager.save(content);
0430:
0431: DatabaseContent content_manager = DatabaseContentFactory
0432: .getInstance(mDatasource);
0433: String path = manager.buildCmfPath(content, "image");
0434: assertTrue(path.startsWith("testrep:"));
0435:
0436: GenericQueryManager<ContentImageRepository> gqm = GenericQueryManagerFactory
0437: .getInstance(mDatasource,
0438: ContentImageRepository.class);
0439: ContentImageRepository restored = gqm.restore(id);
0440: assertEquals(content.getId(), restored.getId());
0441: assertEquals(content.getName(), restored.getName());
0442: assertNull(restored.getImage());
0443:
0444: DatabaseContentInfo info = content_manager
0445: .getContentInfo(path);
0446: assertEquals(id + 1, info.getContentId());
0447: assertEquals(MimeType.IMAGE_PNG, info.getMimeType());
0448:
0449: checkContentRepository(info.getContentId(), "testrep");
0450:
0451: URL rife_resource_png = ResourceFinderClasspath
0452: .getInstance().getResource("rife-logo_small.png");
0453: final byte[] rife_image_png = FileUtils
0454: .readBytes(rife_resource_png);
0455:
0456: content_manager.useContentData(path, new ContentDataUser() {
0457: public Object useContentData(Object contentData)
0458: throws InnerClassException {
0459: assertTrue(Arrays.equals(rife_image_png,
0460: (byte[]) contentData));
0461: return null;
0462: }
0463: });
0464: } finally {
0465: manager.remove();
0466: }
0467: }
0468:
0469: public void testSavePojo() throws Exception {
0470: ContentQueryManager<RegularPojo> manager = new ContentQueryManager<RegularPojo>(
0471: mDatasource, RegularPojo.class);
0472: manager.install();
0473: try {
0474: RegularPojo content = new RegularPojo()
0475: .name("the regular pojo name");
0476:
0477: int id = manager.save(content);
0478: assertTrue(id >= 0);
0479:
0480: GenericQueryManager<RegularPojo> gqm = GenericQueryManagerFactory
0481: .getInstance(mDatasource, RegularPojo.class);
0482: RegularPojo restored = gqm.restore(id);
0483: assertEquals(content.getId(), restored.getId());
0484: assertEquals(content.getName(), restored.getName());
0485: } finally {
0486: manager.remove();
0487: }
0488: }
0489:
0490: public void testStoreEmptyContent() throws Exception {
0491: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0492: mDatasource, ContentImage.class);
0493: manager.install();
0494: try {
0495: URL image_resource_gif = ResourceFinderClasspath
0496: .getInstance().getResource("uwyn.gif");
0497: byte[] data_image_gif = FileUtils
0498: .readBytes(image_resource_gif);
0499: ContentImage content = new ContentImage().name(
0500: "the content name").image(data_image_gif);
0501:
0502: int id = manager.save(content);
0503: assertTrue(id >= 0);
0504:
0505: DatabaseContent content_manager = DatabaseContentFactory
0506: .getInstance(mDatasource);
0507: String path = manager.buildCmfPath(content, "image");
0508:
0509: assertTrue(content_manager.hasContentData(path));
0510: content_manager.useContentData(path, new ContentDataUser() {
0511: public Object useContentData(Object contentData)
0512: throws InnerClassException {
0513: assertNotNull(contentData);
0514: return null;
0515: }
0516: });
0517:
0518: assertTrue(manager.storeEmptyContent(content, "image"));
0519:
0520: GenericQueryManager<ContentImage> gqm = GenericQueryManagerFactory
0521: .getInstance(mDatasource, ContentImage.class);
0522: ContentImage restored = gqm.restore(id);
0523: assertEquals(content.getId(), restored.getId());
0524: assertEquals(content.getName(), restored.getName());
0525: assertNull(restored.getImage());
0526:
0527: DatabaseContentInfo info = content_manager
0528: .getContentInfo(path);
0529: assertEquals(id + 1, info.getContentId());
0530: assertEquals(MimeType.IMAGE_PNG, info.getMimeType());
0531:
0532: assertFalse(content_manager.hasContentData(path));
0533: content_manager.useContentData(path, new ContentDataUser() {
0534: public Object useContentData(Object contentData)
0535: throws InnerClassException {
0536: assertNull(contentData);
0537: return null;
0538: }
0539: });
0540: } finally {
0541: manager.remove();
0542: }
0543: }
0544:
0545: public void testStoreEmptyContentRepository() throws Exception {
0546: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0547: mDatasource, ContentImageRepository.class);
0548: manager.install();
0549: manager.getContentManager().createRepository("testrep");
0550: try {
0551: URL image_resource_gif = ResourceFinderClasspath
0552: .getInstance().getResource("uwyn.gif");
0553: byte[] data_image_gif = FileUtils
0554: .readBytes(image_resource_gif);
0555: ContentImageRepository content = new ContentImageRepository()
0556: .name("the content name").image(data_image_gif);
0557:
0558: int id = manager.save(content);
0559: assertTrue(id >= 0);
0560:
0561: checkContentRepository(id, "testrep");
0562:
0563: DatabaseContent content_manager = DatabaseContentFactory
0564: .getInstance(mDatasource);
0565: String path = manager.buildCmfPath(content, "image");
0566: assertTrue(path.startsWith("testrep:"));
0567:
0568: assertTrue(content_manager.hasContentData(path));
0569: content_manager.useContentData(path, new ContentDataUser() {
0570: public Object useContentData(Object contentData)
0571: throws InnerClassException {
0572: assertNotNull(contentData);
0573: return null;
0574: }
0575: });
0576:
0577: assertTrue(manager.storeEmptyContent(content, "image"));
0578:
0579: GenericQueryManager<ContentImageRepository> gqm = GenericQueryManagerFactory
0580: .getInstance(mDatasource,
0581: ContentImageRepository.class);
0582: ContentImageRepository restored = gqm.restore(id);
0583: assertEquals(content.getId(), restored.getId());
0584: assertEquals(content.getName(), restored.getName());
0585: assertNull(restored.getImage());
0586:
0587: DatabaseContentInfo info = content_manager
0588: .getContentInfo(path);
0589: assertEquals(id + 1, info.getContentId());
0590: assertEquals(MimeType.IMAGE_PNG, info.getMimeType());
0591:
0592: assertFalse(content_manager.hasContentData(path));
0593: content_manager.useContentData(path, new ContentDataUser() {
0594: public Object useContentData(Object contentData)
0595: throws InnerClassException {
0596: assertNull(contentData);
0597: return null;
0598: }
0599: });
0600: } finally {
0601: manager.remove();
0602: }
0603: }
0604:
0605: private void checkContentRepository(int id, String repository) {
0606: assertEquals(repository, new DbQueryManager(mDatasource)
0607: .executeGetFirstString(new Select(mDatasource).from(
0608: RifeConfig.Cmf.getTableContentInfo() + " i")
0609: .join(
0610: RifeConfig.Cmf
0611: .getTableContentRepository()
0612: + " r").field("r.name").where(
0613: "contentId", "=", id).whereAnd(
0614: "i.repositoryId = r.repositoryId")));
0615: }
0616:
0617: public void testStoreEmptyContentIllegalArguments()
0618: throws Exception {
0619: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0620: mDatasource, ContentImage.class);
0621: manager.install();
0622: try {
0623: try {
0624: manager.storeEmptyContent(null, "image");
0625: fail();
0626: } catch (IllegalArgumentException e) {
0627: assertTrue(true);
0628: }
0629:
0630: try {
0631: manager.storeEmptyContent(new ContentImage(), null);
0632: fail();
0633: } catch (IllegalArgumentException e) {
0634: assertTrue(true);
0635: }
0636:
0637: try {
0638: manager.storeEmptyContent(new ContentImage(), "");
0639: fail();
0640: } catch (IllegalArgumentException e) {
0641: assertTrue(true);
0642: }
0643: } finally {
0644: manager.remove();
0645: }
0646: }
0647:
0648: public void testStoreEmptyContentPojo() throws Exception {
0649: ContentQueryManager<RegularPojo> manager = new ContentQueryManager<RegularPojo>(
0650: mDatasource, RegularPojo.class);
0651: manager.install();
0652: try {
0653: RegularPojo content = new RegularPojo()
0654: .name("the regular pojo name");
0655:
0656: assertFalse(manager.storeEmptyContent(content, "name"));
0657: } finally {
0658: manager.remove();
0659: }
0660: }
0661:
0662: public void testStoreEmptyContentMissingIdentifier()
0663: throws Exception {
0664: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0665: mDatasource, ContentImage.class);
0666: manager.install();
0667: try {
0668: try {
0669: manager.storeEmptyContent(new ContentImage(), "image");
0670: fail();
0671: } catch (MissingIdentifierValueException e) {
0672: assertSame(ContentImage.class, e.getBeanClass());
0673: assertEquals("id", e.getIdentifierName());
0674: }
0675: } finally {
0676: manager.remove();
0677: }
0678: }
0679:
0680: public void testStoreEmptyContentUnknownProperty() throws Exception {
0681: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0682: mDatasource, ContentImage.class);
0683: manager.install();
0684: try {
0685: URL image_resource_gif = ResourceFinderClasspath
0686: .getInstance().getResource("uwyn.gif");
0687: byte[] data_image_gif = FileUtils
0688: .readBytes(image_resource_gif);
0689: ContentImage content = new ContentImage().name(
0690: "the content name").image(data_image_gif);
0691:
0692: manager.save(content);
0693:
0694: try {
0695: manager.storeEmptyContent(content, "imageunknown");
0696: fail();
0697: } catch (UnknownConstrainedPropertyException e) {
0698: assertSame(ContentImage.class, e.getBeanClass());
0699: assertEquals("imageunknown", e.getProperty());
0700: }
0701: } finally {
0702: manager.remove();
0703: }
0704: }
0705:
0706: public void testStoreEmptyContentMimeTypeExpected()
0707: throws Exception {
0708: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0709: mDatasource, ContentImage.class);
0710: manager.install();
0711: try {
0712: URL image_resource_gif = ResourceFinderClasspath
0713: .getInstance().getResource("uwyn.gif");
0714: byte[] data_image_gif = FileUtils
0715: .readBytes(image_resource_gif);
0716: ContentImage content = new ContentImage().name(
0717: "the content name").image(data_image_gif);
0718:
0719: manager.save(content);
0720:
0721: try {
0722: manager.storeEmptyContent(content, "name");
0723: fail();
0724: } catch (ExpectedMimeTypeConstraintException e) {
0725: assertSame(ContentImage.class, e.getBeanClass());
0726: assertEquals("name", e.getProperty());
0727: }
0728: } finally {
0729: manager.remove();
0730: }
0731: }
0732:
0733: public void testDeleteContent() throws Exception {
0734: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0735: mDatasource, ContentImage.class);
0736: manager.install();
0737: try {
0738: URL image_resource_gif = ResourceFinderClasspath
0739: .getInstance().getResource("uwyn.gif");
0740: byte[] data_image_gif = FileUtils
0741: .readBytes(image_resource_gif);
0742: ContentImage content = new ContentImage().name(
0743: "the content name").image(data_image_gif);
0744:
0745: int id = manager.save(content);
0746: assertTrue(id >= 0);
0747:
0748: assertTrue(manager.delete(id));
0749:
0750: ContentManager content_manager = DatabaseContentFactory
0751: .getInstance(mDatasource);
0752: String path = manager.buildCmfPath(content, "image");
0753:
0754: GenericQueryManager<ContentImage> gqm = GenericQueryManagerFactory
0755: .getInstance(mDatasource, ContentImage.class);
0756: assertNull(gqm.restore(id));
0757: assertNull(content_manager.getContentInfo(path));
0758: assertFalse(content_manager.hasContentData(path));
0759: content_manager.useContentData(path, new ContentDataUser() {
0760: public Object useContentData(Object contentData)
0761: throws InnerClassException {
0762: assertNull(contentData);
0763: return null;
0764: }
0765: });
0766: } finally {
0767: manager.remove();
0768: }
0769: }
0770:
0771: public void testDeleteContentRepository() throws Exception {
0772: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
0773: mDatasource, ContentImageRepository.class);
0774: manager.install();
0775: manager.getContentManager().createRepository("testrep");
0776: try {
0777: URL image_resource_gif = ResourceFinderClasspath
0778: .getInstance().getResource("uwyn.gif");
0779: byte[] data_image_gif = FileUtils
0780: .readBytes(image_resource_gif);
0781: ContentImageRepository content = new ContentImageRepository()
0782: .name("the content name").image(data_image_gif);
0783:
0784: int id = manager.save(content);
0785: assertTrue(id >= 0);
0786:
0787: checkContentRepository(id, "testrep");
0788:
0789: assertTrue(manager.delete(id));
0790:
0791: ContentManager content_manager = DatabaseContentFactory
0792: .getInstance(mDatasource);
0793: String path = manager.buildCmfPath(content, "image");
0794: assertTrue(path.startsWith("testrep:"));
0795:
0796: GenericQueryManager<ContentImageRepository> gqm = GenericQueryManagerFactory
0797: .getInstance(mDatasource,
0798: ContentImageRepository.class);
0799: assertNull(gqm.restore(id));
0800: assertNull(content_manager.getContentInfo(path));
0801: assertFalse(content_manager.hasContentData(path));
0802: content_manager.useContentData(path, new ContentDataUser() {
0803: public Object useContentData(Object contentData)
0804: throws InnerClassException {
0805: assertNull(contentData);
0806: return null;
0807: }
0808: });
0809: } finally {
0810: manager.remove();
0811: }
0812: }
0813:
0814: public void testDeleteContentNonCmfProperty() throws Exception {
0815: ContentQueryManager<ContentImageNonCmfProps> manager = new ContentQueryManager<ContentImageNonCmfProps>(
0816: mDatasource, ContentImageNonCmfProps.class);
0817: manager.install();
0818: try {
0819: URL image_resource_gif = ResourceFinderClasspath
0820: .getInstance().getResource("uwyn.gif");
0821: byte[] data_image_gif = FileUtils
0822: .readBytes(image_resource_gif);
0823: ContentImageNonCmfProps content = new ContentImageNonCmfProps()
0824: .name("the content name").image(data_image_gif);
0825:
0826: int id = manager.save(content);
0827: assertTrue(id >= 0);
0828:
0829: assertTrue(manager.delete(id));
0830:
0831: ContentManager content_manager = DatabaseContentFactory
0832: .getInstance(mDatasource);
0833: String path = manager.buildCmfPath(content, "image");
0834:
0835: GenericQueryManager<ContentImageNonCmfProps> gqm = GenericQueryManagerFactory
0836: .getInstance(mDatasource,
0837: ContentImageNonCmfProps.class);
0838: assertNull(gqm.restore(id));
0839: assertNull(content_manager.getContentInfo(path));
0840: assertFalse(content_manager.hasContentData(path));
0841: content_manager.useContentData(path, new ContentDataUser() {
0842: public Object useContentData(Object contentData)
0843: throws InnerClassException {
0844: assertNull(contentData);
0845: return null;
0846: }
0847: });
0848: } finally {
0849: manager.remove();
0850: }
0851: }
0852:
0853: public void testDeletePojo() throws Exception {
0854: ContentQueryManager<RegularPojo> manager = new ContentQueryManager<RegularPojo>(
0855: mDatasource, RegularPojo.class);
0856: manager.install();
0857: try {
0858: RegularPojo content = new RegularPojo()
0859: .name("the regular pojo name");
0860:
0861: int id = manager.save(content);
0862: assertTrue(id >= 0);
0863:
0864: assertTrue(manager.delete(id));
0865:
0866: GenericQueryManager<RegularPojo> gqm = GenericQueryManagerFactory
0867: .getInstance(mDatasource, RegularPojo.class);
0868: assertNull(gqm.restore(id));
0869: } finally {
0870: manager.remove();
0871: }
0872: }
0873:
0874: public void testDeleteOrdinal() throws Exception {
0875: ContentQueryManager<Ordered> manager = new ContentQueryManager<Ordered>(
0876: mDatasource, Ordered.class);
0877: manager.install();
0878: try {
0879: Ordered content1 = new Ordered().name("the content name");
0880:
0881: int id1 = manager.save(content1);
0882: assertTrue(id1 >= 0);
0883:
0884: Ordered content2 = new Ordered()
0885: .name("another content name");
0886:
0887: int id2 = manager.save(content2);
0888: assertTrue(id2 > id1);
0889:
0890: Ordered content3 = new Ordered()
0891: .name("one more content name");
0892:
0893: int id3 = manager.save(content3);
0894: assertTrue(id3 > id2);
0895:
0896: assertTrue(manager.delete(id2));
0897:
0898: GenericQueryManager<Ordered> gqm = GenericQueryManagerFactory
0899: .getInstance(mDatasource, Ordered.class);
0900: content1 = gqm.restore(id1);
0901: assertEquals(0, content1.getPriority());
0902: assertNull(gqm.restore(id2));
0903: content3 = gqm.restore(id3);
0904: assertEquals(1, content3.getPriority());
0905: } finally {
0906: manager.remove();
0907: }
0908: }
0909:
0910: public void testDeleteOrdinalRestricted() throws Exception {
0911: ContentQueryManager<OrderedRestricted> manager = new ContentQueryManager<OrderedRestricted>(
0912: mDatasource, OrderedRestricted.class);
0913: manager.install();
0914: try {
0915: OrderedRestricted content1 = new OrderedRestricted().name(
0916: "the content name").restricted(3);
0917:
0918: int id1 = manager.save(content1);
0919: assertTrue(id1 >= 0);
0920:
0921: OrderedRestricted content2 = new OrderedRestricted().name(
0922: "another content name").restricted(5);
0923:
0924: int id2 = manager.save(content2);
0925: assertTrue(id2 > id1);
0926:
0927: OrderedRestricted content3 = new OrderedRestricted().name(
0928: "some other content name").restricted(3);
0929:
0930: int id3 = manager.save(content3);
0931: assertTrue(id3 > id2);
0932:
0933: OrderedRestricted content4 = new OrderedRestricted().name(
0934: "yet one more content name").restricted(3);
0935:
0936: int id4 = manager.save(content4);
0937: assertTrue(id4 > id3);
0938:
0939: OrderedRestricted content5 = new OrderedRestricted().name(
0940: "the last content name").restricted(5);
0941:
0942: int id5 = manager.save(content5);
0943: assertTrue(id5 > id4);
0944:
0945: assertTrue(manager.delete(id3));
0946:
0947: GenericQueryManager<OrderedRestricted> gqm = GenericQueryManagerFactory
0948: .getInstance(mDatasource, OrderedRestricted.class);
0949: content1 = gqm.restore(id1);
0950: assertEquals(0, content1.getPriority());
0951: content2 = gqm.restore(id2);
0952: assertEquals(0, content2.getPriority());
0953: assertNull(gqm.restore(id3));
0954: content4 = gqm.restore(id4);
0955: assertEquals(1, content4.getPriority());
0956: content5 = gqm.restore(id5);
0957: assertEquals(1, content5.getPriority());
0958: } finally {
0959: manager.remove();
0960: }
0961: }
0962:
0963: public void testDeleteNotPresent() throws Exception {
0964: ContentQueryManager<RegularPojo> manager = new ContentQueryManager<RegularPojo>(
0965: mDatasource, RegularPojo.class);
0966: manager.install();
0967: try {
0968: assertFalse(manager.delete(3));
0969: } finally {
0970: manager.remove();
0971: }
0972: }
0973:
0974: public void testDeleteContentUnknownId() throws Exception {
0975: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
0976: mDatasource, ContentImage.class);
0977: manager.install();
0978: try {
0979: assertFalse(manager.delete(3));
0980: } finally {
0981: manager.remove();
0982: }
0983: }
0984:
0985: public void testSaveOrdinal() throws Exception {
0986: ContentQueryManager<Ordered> manager = new ContentQueryManager<Ordered>(
0987: mDatasource, Ordered.class);
0988: manager.install();
0989: try {
0990: Ordered content1 = new Ordered().name("the content name");
0991:
0992: int id1 = manager.save(content1);
0993: assertTrue(id1 >= 0);
0994:
0995: Ordered content2 = new Ordered()
0996: .name("another content name");
0997:
0998: int id2 = manager.save(content2);
0999: assertTrue(id2 > id1);
1000:
1001: Ordered content3 = new Ordered()
1002: .name("one more content name");
1003:
1004: int id3 = manager.save(content3);
1005: assertTrue(id3 > id2);
1006:
1007: GenericQueryManager<Ordered> gqm = GenericQueryManagerFactory
1008: .getInstance(mDatasource, Ordered.class);
1009: content1 = gqm.restore(id1);
1010: assertEquals(0, content1.getPriority());
1011: content2 = gqm.restore(id2);
1012: assertEquals(1, content2.getPriority());
1013: content3 = gqm.restore(id3);
1014: assertEquals(2, content3.getPriority());
1015: } finally {
1016: manager.remove();
1017: }
1018: }
1019:
1020: public void testSaveOrdinalRestricted() throws Exception {
1021: ContentQueryManager<OrderedRestricted> manager = new ContentQueryManager<OrderedRestricted>(
1022: mDatasource, OrderedRestricted.class);
1023: manager.install();
1024: try {
1025: OrderedRestricted content1 = new OrderedRestricted().name(
1026: "the content name").restricted(3);
1027:
1028: int id1 = manager.save(content1);
1029: assertTrue(id1 >= 0);
1030:
1031: OrderedRestricted content2 = new OrderedRestricted().name(
1032: "another content name").restricted(5);
1033:
1034: int id2 = manager.save(content2);
1035: assertTrue(id2 > id1);
1036:
1037: OrderedRestricted content3 = new OrderedRestricted().name(
1038: "some other content name").restricted(3);
1039:
1040: int id3 = manager.save(content3);
1041: assertTrue(id3 > id2);
1042:
1043: OrderedRestricted content4 = new OrderedRestricted().name(
1044: "yet one more content name").restricted(3);
1045:
1046: int id4 = manager.save(content4);
1047: assertTrue(id4 > id3);
1048:
1049: OrderedRestricted content5 = new OrderedRestricted().name(
1050: "the last content name").restricted(5);
1051:
1052: int id5 = manager.save(content5);
1053: assertTrue(id5 > id4);
1054:
1055: GenericQueryManager<OrderedRestricted> gqm = GenericQueryManagerFactory
1056: .getInstance(mDatasource, OrderedRestricted.class);
1057: content1 = gqm.restore(id1);
1058: assertEquals(0, content1.getPriority());
1059: content2 = gqm.restore(id2);
1060: assertEquals(0, content2.getPriority());
1061: content3 = gqm.restore(id3);
1062: assertEquals(1, content3.getPriority());
1063: content4 = gqm.restore(id4);
1064: assertEquals(2, content4.getPriority());
1065: content5 = gqm.restore(id5);
1066: assertEquals(1, content5.getPriority());
1067: } finally {
1068: manager.remove();
1069: }
1070: }
1071:
1072: public void testMoveOrdinal() throws Exception {
1073: ContentQueryManager<Ordered> manager = new ContentQueryManager<Ordered>(
1074: mDatasource, Ordered.class);
1075: manager.install();
1076: try {
1077: Ordered content1 = new Ordered().name("the content name");
1078: Ordered content2 = new Ordered()
1079: .name("another content name");
1080: Ordered content3 = new Ordered()
1081: .name("one more content name");
1082:
1083: manager.save(content1);
1084: manager.save(content2);
1085: manager.save(content3);
1086:
1087: GenericQueryManager<Ordered> gqm = GenericQueryManagerFactory
1088: .getInstance(mDatasource, Ordered.class);
1089: content1 = gqm.restore(content1.getId());
1090: assertEquals(0, content1.getPriority());
1091: content2 = gqm.restore(content2.getId());
1092: assertEquals(1, content2.getPriority());
1093: content3 = gqm.restore(content3.getId());
1094: assertEquals(2, content3.getPriority());
1095:
1096: assertTrue(manager.move(content1, "priority",
1097: OrdinalManager.DOWN));
1098:
1099: content1 = gqm.restore(content1.getId());
1100: assertEquals(1, content1.getPriority());
1101: content2 = gqm.restore(content2.getId());
1102: assertEquals(0, content2.getPriority());
1103: content3 = gqm.restore(content3.getId());
1104: assertEquals(2, content3.getPriority());
1105:
1106: assertTrue(manager.move(content3, "priority",
1107: OrdinalManager.UP));
1108:
1109: content1 = gqm.restore(content1.getId());
1110: assertEquals(2, content1.getPriority());
1111: content2 = gqm.restore(content2.getId());
1112: assertEquals(0, content2.getPriority());
1113: content3 = gqm.restore(content3.getId());
1114: assertEquals(1, content3.getPriority());
1115:
1116: manager.up(content2, "priority");
1117:
1118: content1 = gqm.restore(content1.getId());
1119: assertEquals(2, content1.getPriority());
1120: content2 = gqm.restore(content2.getId());
1121: assertEquals(0, content2.getPriority());
1122: content3 = gqm.restore(content3.getId());
1123: assertEquals(1, content3.getPriority());
1124:
1125: manager.down(content1, "priority");
1126:
1127: content1 = gqm.restore(content1.getId());
1128: assertEquals(2, content1.getPriority());
1129: content2 = gqm.restore(content2.getId());
1130: assertEquals(0, content2.getPriority());
1131: content3 = gqm.restore(content3.getId());
1132: assertEquals(1, content3.getPriority());
1133: } finally {
1134: manager.remove();
1135: }
1136: }
1137:
1138: public void testMoveIllegalArguments() throws Exception {
1139: ContentQueryManager<Ordered> manager = new ContentQueryManager<Ordered>(
1140: mDatasource, Ordered.class);
1141: manager.install();
1142: try {
1143: try {
1144: manager.move(null, "priority", OrdinalManager.UP);
1145: fail();
1146: } catch (IllegalArgumentException e) {
1147: assertTrue(true);
1148: }
1149:
1150: try {
1151: manager.move(new Ordered(), null, OrdinalManager.UP);
1152: fail();
1153: } catch (IllegalArgumentException e) {
1154: assertTrue(true);
1155: }
1156:
1157: try {
1158: manager.move(new Ordered(), "", OrdinalManager.UP);
1159: fail();
1160: } catch (IllegalArgumentException e) {
1161: assertTrue(true);
1162: }
1163: } finally {
1164: manager.remove();
1165: }
1166: }
1167:
1168: public void testMoveUnknownProperty() throws Exception {
1169: ContentQueryManager<Ordered> manager = new ContentQueryManager<Ordered>(
1170: mDatasource, Ordered.class);
1171: manager.install();
1172: try {
1173: try {
1174: manager.move(new Ordered(), "priorityunknown",
1175: OrdinalManager.UP);
1176: fail();
1177: } catch (UnknownConstrainedPropertyException e) {
1178: assertSame(Ordered.class, e.getBeanClass());
1179: assertEquals("priorityunknown", e.getProperty());
1180: }
1181: } finally {
1182: manager.remove();
1183: }
1184: }
1185:
1186: public void testMoveNotOrdinalConstraint() throws Exception {
1187: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
1188: mDatasource, ContentImage.class);
1189: manager.install();
1190: try {
1191: try {
1192: manager.move(new ContentImage(), "name",
1193: OrdinalManager.UP);
1194: fail();
1195: } catch (ExpectedOrdinalConstraintException e) {
1196: assertSame(ContentImage.class, e.getBeanClass());
1197: assertEquals("name", e.getProperty());
1198: }
1199: } finally {
1200: manager.remove();
1201: }
1202: }
1203:
1204: public void testMoveNotOrdinalInvalidOrdinalType() throws Exception {
1205: ContentQueryManager<OrderedInvalidType> manager = new ContentQueryManager<OrderedInvalidType>(
1206: mDatasource, OrderedInvalidType.class);
1207: manager.install();
1208: try {
1209: try {
1210: manager.move(new OrderedInvalidType(), "priority",
1211: OrdinalManager.UP);
1212: fail();
1213: } catch (InvalidOrdinalTypeException e) {
1214: assertSame(OrderedInvalidType.class, e.getBeanClass());
1215: assertEquals("priority", e.getProperty());
1216: }
1217: } finally {
1218: manager.remove();
1219: }
1220: }
1221:
1222: public void testMoveOrdinalRestricted() throws Exception {
1223: ContentQueryManager<OrderedRestricted> manager = new ContentQueryManager<OrderedRestricted>(
1224: mDatasource, OrderedRestricted.class);
1225: manager.install();
1226: try {
1227: OrderedRestricted content1 = new OrderedRestricted().name(
1228: "the content name").restricted(3);
1229:
1230: OrderedRestricted content2 = new OrderedRestricted().name(
1231: "another content name").restricted(5);
1232:
1233: OrderedRestricted content3 = new OrderedRestricted().name(
1234: "some other content name").restricted(3);
1235:
1236: OrderedRestricted content4 = new OrderedRestricted().name(
1237: "yet one more content name").restricted(3);
1238:
1239: OrderedRestricted content5 = new OrderedRestricted().name(
1240: "the last content name").restricted(5);
1241:
1242: manager.save(content1);
1243: manager.save(content2);
1244: manager.save(content3);
1245: manager.save(content4);
1246: manager.save(content5);
1247:
1248: GenericQueryManager<OrderedRestricted> gqm = GenericQueryManagerFactory
1249: .getInstance(mDatasource, OrderedRestricted.class);
1250: content1 = gqm.restore(content1.getId());
1251: assertEquals(0, content1.getPriority());
1252: content2 = gqm.restore(content2.getId());
1253: assertEquals(0, content2.getPriority());
1254: content3 = gqm.restore(content3.getId());
1255: assertEquals(1, content3.getPriority());
1256: content4 = gqm.restore(content4.getId());
1257: assertEquals(2, content4.getPriority());
1258: content5 = gqm.restore(content5.getId());
1259: assertEquals(1, content5.getPriority());
1260:
1261: assertTrue(manager.move(content1, "priority",
1262: OrdinalManager.DOWN));
1263:
1264: content1 = gqm.restore(content1.getId());
1265: assertEquals(1, content1.getPriority());
1266: content2 = gqm.restore(content2.getId());
1267: assertEquals(0, content2.getPriority());
1268: content3 = gqm.restore(content3.getId());
1269: assertEquals(0, content3.getPriority());
1270: content4 = gqm.restore(content4.getId());
1271: assertEquals(2, content4.getPriority());
1272: content5 = gqm.restore(content5.getId());
1273: assertEquals(1, content5.getPriority());
1274:
1275: assertTrue(manager.move(content4, "priority",
1276: OrdinalManager.UP));
1277:
1278: content1 = gqm.restore(content1.getId());
1279: assertEquals(2, content1.getPriority());
1280: content2 = gqm.restore(content2.getId());
1281: assertEquals(0, content2.getPriority());
1282: content3 = gqm.restore(content3.getId());
1283: assertEquals(0, content3.getPriority());
1284: content4 = gqm.restore(content4.getId());
1285: assertEquals(1, content4.getPriority());
1286: content5 = gqm.restore(content5.getId());
1287: assertEquals(1, content5.getPriority());
1288:
1289: assertTrue(manager.up(content5, "priority"));
1290:
1291: content1 = gqm.restore(content1.getId());
1292: assertEquals(2, content1.getPriority());
1293: content2 = gqm.restore(content2.getId());
1294: assertEquals(1, content2.getPriority());
1295: content3 = gqm.restore(content3.getId());
1296: assertEquals(0, content3.getPriority());
1297: content4 = gqm.restore(content4.getId());
1298: assertEquals(1, content4.getPriority());
1299: content5 = gqm.restore(content5.getId());
1300: assertEquals(0, content5.getPriority());
1301:
1302: assertTrue(manager.down(content5, "priority"));
1303:
1304: content1 = gqm.restore(content1.getId());
1305: assertEquals(2, content1.getPriority());
1306: content2 = gqm.restore(content2.getId());
1307: assertEquals(0, content2.getPriority());
1308: content3 = gqm.restore(content3.getId());
1309: assertEquals(0, content3.getPriority());
1310: content4 = gqm.restore(content4.getId());
1311: assertEquals(1, content4.getPriority());
1312: content5 = gqm.restore(content5.getId());
1313: assertEquals(1, content5.getPriority());
1314:
1315: manager.up(content2, "priority");
1316:
1317: content1 = gqm.restore(content1.getId());
1318: assertEquals(2, content1.getPriority());
1319: content2 = gqm.restore(content2.getId());
1320: assertEquals(0, content2.getPriority());
1321: content3 = gqm.restore(content3.getId());
1322: assertEquals(0, content3.getPriority());
1323: content4 = gqm.restore(content4.getId());
1324: assertEquals(1, content4.getPriority());
1325: content5 = gqm.restore(content5.getId());
1326: assertEquals(1, content5.getPriority());
1327:
1328: manager.down(content1, "priority");
1329:
1330: content1 = gqm.restore(content1.getId());
1331: assertEquals(2, content1.getPriority());
1332: content2 = gqm.restore(content2.getId());
1333: assertEquals(0, content2.getPriority());
1334: content3 = gqm.restore(content3.getId());
1335: assertEquals(0, content3.getPriority());
1336: content4 = gqm.restore(content4.getId());
1337: assertEquals(1, content4.getPriority());
1338: content5 = gqm.restore(content5.getId());
1339: assertEquals(1, content5.getPriority());
1340: } finally {
1341: manager.remove();
1342: }
1343: }
1344:
1345: public void testSaveOrdinalRestrictedInvalidType() throws Exception {
1346: ContentQueryManager<OrdrdRestrInvalidType> manager = new ContentQueryManager<OrdrdRestrInvalidType>(
1347: mDatasource, OrdrdRestrInvalidType.class);
1348: manager.install();
1349: try {
1350: OrdrdRestrInvalidType content = new OrdrdRestrInvalidType()
1351: .name("the content name").restricted("3");
1352:
1353: try {
1354: manager.save(content);
1355: fail();
1356: } catch (InvalidOrdinalRestrictionTypeException e) {
1357: assertSame(OrdrdRestrInvalidType.class, e
1358: .getBeanClass());
1359: assertEquals("priority", e.getProperty());
1360: assertEquals("restricted", e.getRestriction());
1361: }
1362: } finally {
1363: manager.remove();
1364: }
1365: }
1366:
1367: public void testMoveUnknownOrdinal() throws Exception {
1368: ContentQueryManager<OrdrdUnknown> manager = new ContentQueryManager<OrdrdUnknown>(
1369: mDatasource, OrdrdUnknown.class);
1370: manager.install();
1371: try {
1372: try {
1373: manager.move(new OrdrdUnknown(), "unknown",
1374: OrdinalManager.UP);
1375: fail();
1376: } catch (UnknownOrdinalException e) {
1377: assertSame(OrdrdUnknown.class, e.getBeanClass());
1378: assertEquals("unknown", e.getProperty());
1379: }
1380: } finally {
1381: manager.remove();
1382: }
1383: }
1384:
1385: public void testMoveUnknownRestriction() throws Exception {
1386: ContentQueryManager<OrdrdRestrUnknown> manager = new ContentQueryManager<OrdrdRestrUnknown>(
1387: mDatasource, OrdrdRestrUnknown.class);
1388: manager.install();
1389: try {
1390: try {
1391: manager.move(new OrdrdRestrUnknown(), "priority",
1392: OrdinalManager.UP);
1393: fail();
1394: } catch (UnknownOrdinalRestrictionException e) {
1395: assertSame(OrdrdRestrUnknown.class, e.getBeanClass());
1396: assertEquals("priority", e.getProperty());
1397: assertEquals("restrictedunknown", e.getRestriction());
1398: }
1399: } finally {
1400: manager.remove();
1401: }
1402: }
1403:
1404: public void testMoveRestrictionInvalidType() throws Exception {
1405: ContentQueryManager<OrdrdRestrInvalidType> manager = new ContentQueryManager<OrdrdRestrInvalidType>(
1406: mDatasource, OrdrdRestrInvalidType.class);
1407: manager.install();
1408: try {
1409: try {
1410: manager.move(new OrdrdRestrInvalidType()
1411: .restricted("restricted"), "priority",
1412: OrdinalManager.UP);
1413: fail();
1414: } catch (InvalidOrdinalRestrictionTypeException e) {
1415: assertSame(OrdrdRestrInvalidType.class, e
1416: .getBeanClass());
1417: assertEquals("priority", e.getProperty());
1418: assertEquals("restricted", e.getRestriction());
1419: }
1420: } finally {
1421: manager.remove();
1422: }
1423: }
1424:
1425: public void testMoveRestrictionNull() throws Exception {
1426: ContentQueryManager<OrdrdRestrInvalidType> manager = new ContentQueryManager<OrdrdRestrInvalidType>(
1427: mDatasource, OrdrdRestrInvalidType.class);
1428: manager.install();
1429: try {
1430: try {
1431: manager.move(new OrdrdRestrInvalidType(), "priority",
1432: OrdinalManager.UP);
1433: fail();
1434: } catch (OrdinalRestrictionCantBeNullException e) {
1435: assertSame(OrdrdRestrInvalidType.class, e
1436: .getBeanClass());
1437: assertEquals("priority", e.getProperty());
1438: assertEquals("restricted", e.getRestriction());
1439: }
1440: } finally {
1441: manager.remove();
1442: }
1443: }
1444:
1445: public void testSaveOrdinalRestrictedUnknownRestriction()
1446: throws Exception {
1447: ContentQueryManager<OrdrdRestrUnknown> manager = new ContentQueryManager<OrdrdRestrUnknown>(
1448: mDatasource, OrdrdRestrUnknown.class);
1449: manager.install();
1450: try {
1451: OrdrdRestrUnknown content = new OrdrdRestrUnknown().name(
1452: "the content name").restricted(3);
1453:
1454: try {
1455: manager.save(content);
1456: fail();
1457: } catch (UnknownOrdinalRestrictionException e) {
1458: assertSame(OrdrdRestrUnknown.class, e.getBeanClass());
1459: assertEquals("priority", e.getProperty());
1460: assertEquals("restrictedunknown", e.getRestriction());
1461: }
1462: } finally {
1463: manager.remove();
1464: }
1465: }
1466:
1467: public void testHasContent() throws Exception {
1468: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
1469: mDatasource, ContentImage.class);
1470: manager.install();
1471: try {
1472: URL image_resource_gif = ResourceFinderClasspath
1473: .getInstance().getResource("uwyn.gif");
1474: byte[] data_image_gif = FileUtils
1475: .readBytes(image_resource_gif);
1476: ContentImage content = new ContentImage().name(
1477: "the content name").image(data_image_gif);
1478:
1479: int id = manager.save(content);
1480:
1481: assertTrue(manager.hasContent(id, "image"));
1482: assertFalse(manager.hasContent(id, "unknown"));
1483: assertFalse(manager.hasContent(34, "image"));
1484:
1485: assertTrue(manager.hasContent(content, "image"));
1486: assertFalse(manager.hasContent(content, "unknown"));
1487: content.setId(334);
1488: assertFalse(manager.hasContent(content, "image"));
1489: } finally {
1490: manager.remove();
1491: }
1492: }
1493:
1494: public void testHasContentRepository() throws Exception {
1495: ContentQueryManager<ContentImageRepository> manager = new ContentQueryManager<ContentImageRepository>(
1496: mDatasource, ContentImageRepository.class);
1497: manager.install();
1498: manager.getContentManager().createRepository("testrep");
1499: try {
1500: URL image_resource_gif = ResourceFinderClasspath
1501: .getInstance().getResource("uwyn.gif");
1502: byte[] data_image_gif = FileUtils
1503: .readBytes(image_resource_gif);
1504: ContentImageRepository content = new ContentImageRepository()
1505: .name("the content name").image(data_image_gif);
1506:
1507: int id = manager.save(content);
1508:
1509: checkContentRepository(id, "testrep");
1510:
1511: assertTrue(manager.hasContent(id, "image"));
1512: assertFalse(manager.hasContent(id, "unknown"));
1513: assertFalse(manager.hasContent(34, "image"));
1514:
1515: assertTrue(manager.hasContent(content, "image"));
1516: assertFalse(manager.hasContent(content, "unknown"));
1517: content.setId(334);
1518: assertFalse(manager.hasContent(content, "image"));
1519: } finally {
1520: manager.remove();
1521: }
1522: }
1523:
1524: public void testRestoreById() throws Exception {
1525: ContentQueryManager<ContentImage> manager = new ContentQueryManager<ContentImage>(
1526: mDatasource, ContentImage.class);
1527: manager.install();
1528: try {
1529: URL image_resource_gif = ResourceFinderClasspath
1530: .getInstance().getResource("uwyn.gif");
1531: byte[] data_image_gif = FileUtils
1532: .readBytes(image_resource_gif);
1533: ContentImage content = new ContentImage().name(
1534: "the content name").image(data_image_gif);
1535:
1536: int id = manager.save(content);
1537:
1538: ContentImage restored = manager.restore(id);
1539: assertEquals(content.getId(), restored.getId());
1540: assertEquals(content.getName(), restored.getName());
1541: assertNull(restored.getImage());
1542: } finally {
1543: manager.remove();
1544: }
1545: }
1546:
1547: public void testRestoreByIdAutoRetrieved() throws Exception {
1548: ContentQueryManager<ContentImageAutoRetrieved> manager = new ContentQueryManager<ContentImageAutoRetrieved>(
1549: mDatasource, ContentImageAutoRetrieved.class);
1550: manager.install();
1551: try {
1552: URL image_resource_gif = ResourceFinderClasspath
1553: .getInstance().getResource("uwyn.gif");
1554: byte[] data_image_gif = FileUtils
1555: .readBytes(image_resource_gif);
1556: ContentImageAutoRetrieved content = new ContentImageAutoRetrieved()
1557: .name("the content name").image(data_image_gif);
1558:
1559: int id = manager.save(content);
1560:
1561: ContentImageAutoRetrieved restored = manager.restore(id);
1562: assertEquals(content.getId(), restored.getId());
1563: assertEquals(content.getName(), restored.getName());
1564: URL image_resource_png = ResourceFinderClasspath
1565: .getInstance().getResource("uwyn.png");
1566: byte[] data_image_png = FileUtils
1567: .readBytes(image_resource_png);
1568: assertTrue(Arrays.equals(data_image_png, restored
1569: .getImage()));
1570: } finally {
1571: manager.remove();
1572: }
1573: }
1574:
1575: public void testRestoreByIdPojo() throws Exception {
1576: ContentQueryManager<RegularPojo> manager = new ContentQueryManager<RegularPojo>(
1577: mDatasource, RegularPojo.class);
1578: manager.install();
1579: try {
1580: RegularPojo content = new RegularPojo()
1581: .name("the regular pojo name");
1582:
1583: int id = manager.save(content);
1584: assertTrue(id >= 0);
1585:
1586: RegularPojo restored = manager.restore(id);
1587: assertEquals(content.getId(), restored.getId());
1588: assertEquals(content.getName(), restored.getName());
1589: } finally {
1590: manager.remove();
1591: }
1592: }
1593:
1594: public void testRestoreByIdNonCmfProperties() throws Exception {
1595: ContentQueryManager<ContentImageNonCmfProps> manager = new ContentQueryManager<ContentImageNonCmfProps>(
1596: mDatasource, ContentImageNonCmfProps.class);
1597: manager.install();
1598: try {
1599: URL image_resource_gif = ResourceFinderClasspath
1600: .getInstance().getResource("uwyn.gif");
1601: byte[] data_image_gif = FileUtils
1602: .readBytes(image_resource_gif);
1603: ContentImageNonCmfProps content = new ContentImageNonCmfProps()
1604: .name("the content name").image(data_image_gif);
1605:
1606: int id = manager.save(content);
1607:
1608: ContentImageNonCmfProps restored = manager.restore(id);
1609: assertEquals(content.getId(), restored.getId());
1610: assertEquals(content.getName(), restored.getName());
1611: assertNull(restored.getImage());
1612: } finally {
1613: manager.remove();
1614: }
1615: }
1616:
1617: public void testRestoreFirst() throws Exception {
1618: ContentQueryManager<ContentImageAutoRetrieved> manager = new ContentQueryManager<ContentImageAutoRetrieved>(
1619: mDatasource, ContentImageAutoRetrieved.class);
1620: manager.install();
1621: try {
1622: URL image_resource_gif = ResourceFinderClasspath
1623: .getInstance().getResource("uwyn.gif");
1624: byte[] data_image_gif = FileUtils
1625: .readBytes(image_resource_gif);
1626:
1627: ContentImageAutoRetrieved content1 = new ContentImageAutoRetrieved()
1628: .name("the content name").image(data_image_gif);
1629: manager.save(content1);
1630:
1631: URL image_resource_tif = ResourceFinderClasspath
1632: .getInstance().getResource("uwyn.tif");
1633: byte[] data_image_tif = FileUtils
1634: .readBytes(image_resource_tif);
1635:
1636: ContentImageAutoRetrieved content2 = new ContentImageAutoRetrieved()
1637: .name("another content name").image(data_image_tif);
1638: manager.save(content2);
1639:
1640: ContentImageAutoRetrieved restored = manager
1641: .restoreFirst(manager.getRestoreQuery().orderBy(
1642: "id", Select.DESC));
1643: assertEquals(content2.getId(), restored.getId());
1644: assertEquals(content2.getName(), restored.getName());
1645: URL image_resource_png = ResourceFinderClasspath
1646: .getInstance().getResource("uwyn-noalpha.png");
1647: byte[] data_image_png = FileUtils
1648: .readBytes(image_resource_png);
1649: assertTrue(Arrays.equals(data_image_png, restored
1650: .getImage()));
1651: } finally {
1652: manager.remove();
1653: }
1654: }
1655:
1656: public void testRestore() throws Exception {
1657: ContentQueryManager<ContentImageAutoRetrieved> manager = new ContentQueryManager<ContentImageAutoRetrieved>(
1658: mDatasource, ContentImageAutoRetrieved.class);
1659: manager.install();
1660: try {
1661: URL uwyn_resource_gif = ResourceFinderClasspath
1662: .getInstance().getResource("uwyn.gif");
1663: byte[] uwyn_image_gif = FileUtils
1664: .readBytes(uwyn_resource_gif);
1665:
1666: ContentImageAutoRetrieved content1 = new ContentImageAutoRetrieved()
1667: .name("the content name").image(uwyn_image_gif);
1668: manager.save(content1);
1669:
1670: URL rife_resource_tif = ResourceFinderClasspath
1671: .getInstance().getResource("rife-logo_small.tif");
1672: byte[] rife_image_tif = FileUtils
1673: .readBytes(rife_resource_tif);
1674:
1675: ContentImageAutoRetrieved content2 = new ContentImageAutoRetrieved()
1676: .name("another content name").image(rife_image_tif);
1677: manager.save(content2);
1678:
1679: URL uwyn_resource_png = ResourceFinderClasspath
1680: .getInstance().getResource("uwyn.png");
1681: byte[] uwyn_image_png = FileUtils
1682: .readBytes(uwyn_resource_png);
1683:
1684: URL rife_resource_png = ResourceFinderClasspath
1685: .getInstance().getResource("rife-logo_small.png");
1686: byte[] rife_image_png = FileUtils
1687: .readBytes(rife_resource_png);
1688:
1689: List<ContentImageAutoRetrieved> restored_list = manager
1690: .restore();
1691: ContentImageAutoRetrieved restored = null;
1692: Iterator<ContentImageAutoRetrieved> restored_it = restored_list
1693: .iterator();
1694: restored = restored_it.next();
1695: assertEquals(content1.getId(), restored.getId());
1696: assertEquals(content1.getName(), restored.getName());
1697: assertTrue(Arrays.equals(uwyn_image_png, restored
1698: .getImage()));
1699: restored = restored_it.next();
1700: assertEquals(content2.getId(), restored.getId());
1701: assertEquals(content2.getName(), restored.getName());
1702: assertTrue(Arrays.equals(rife_image_png, restored
1703: .getImage()));
1704: assertFalse(restored_it.hasNext());
1705: } finally {
1706: manager.remove();
1707: }
1708: }
1709:
1710: public void testRestoreRepository() throws Exception {
1711: ContentQueryManager<ContentImageAutoRetrRep> manager = new ContentQueryManager<ContentImageAutoRetrRep>(
1712: mDatasource, ContentImageAutoRetrRep.class);
1713: manager.install();
1714: manager.getContentManager().createRepository("testrep");
1715: try {
1716: URL uwyn_resource_gif = ResourceFinderClasspath
1717: .getInstance().getResource("uwyn.gif");
1718: byte[] uwyn_image_gif = FileUtils
1719: .readBytes(uwyn_resource_gif);
1720:
1721: ContentImageAutoRetrRep content1 = new ContentImageAutoRetrRep()
1722: .name("the content name").image(uwyn_image_gif);
1723: int id1 = manager.save(content1);
1724:
1725: checkContentRepository(id1, "testrep");
1726:
1727: URL rife_resource_tif = ResourceFinderClasspath
1728: .getInstance().getResource("rife-logo_small.tif");
1729: byte[] rife_image_tif = FileUtils
1730: .readBytes(rife_resource_tif);
1731:
1732: ContentImageAutoRetrRep content2 = new ContentImageAutoRetrRep()
1733: .name("another content name").image(rife_image_tif);
1734: int id2 = manager.save(content2);
1735:
1736: checkContentRepository(id2, "testrep");
1737:
1738: URL uwyn_resource_png = ResourceFinderClasspath
1739: .getInstance().getResource("uwyn.png");
1740: byte[] uwyn_image_png = FileUtils
1741: .readBytes(uwyn_resource_png);
1742:
1743: URL rife_resource_png = ResourceFinderClasspath
1744: .getInstance().getResource("rife-logo_small.png");
1745: byte[] rife_image_png = FileUtils
1746: .readBytes(rife_resource_png);
1747:
1748: List<ContentImageAutoRetrRep> restored_list = manager
1749: .restore();
1750: ContentImageAutoRetrRep restored = null;
1751: Iterator<ContentImageAutoRetrRep> restored_it = restored_list
1752: .iterator();
1753: restored = restored_it.next();
1754: assertEquals(content1.getId(), restored.getId());
1755: assertEquals(content1.getName(), restored.getName());
1756: assertTrue(Arrays.equals(uwyn_image_png, restored
1757: .getImage()));
1758: restored = restored_it.next();
1759: assertEquals(content2.getId(), restored.getId());
1760: assertEquals(content2.getName(), restored.getName());
1761: assertTrue(Arrays.equals(rife_image_png, restored
1762: .getImage()));
1763: assertFalse(restored_it.hasNext());
1764: } finally {
1765: manager.remove();
1766: }
1767: }
1768:
1769: public void testRestoreQuery() throws Exception {
1770: ContentQueryManager<ContentImageAutoRetrieved> manager = new ContentQueryManager<ContentImageAutoRetrieved>(
1771: mDatasource, ContentImageAutoRetrieved.class);
1772: manager.install();
1773: try {
1774: URL uwyn_resource_gif = ResourceFinderClasspath
1775: .getInstance().getResource("uwyn.gif");
1776: byte[] uwyn_image_gif = FileUtils
1777: .readBytes(uwyn_resource_gif);
1778:
1779: ContentImageAutoRetrieved content1 = new ContentImageAutoRetrieved()
1780: .name("the content name").image(uwyn_image_gif);
1781: manager.save(content1);
1782:
1783: URL rife_resource_tif = ResourceFinderClasspath
1784: .getInstance().getResource("rife-logo_small.tif");
1785: byte[] rife_image_tif = FileUtils
1786: .readBytes(rife_resource_tif);
1787:
1788: ContentImageAutoRetrieved content2 = new ContentImageAutoRetrieved()
1789: .name("another content name").image(rife_image_tif);
1790: manager.save(content2);
1791:
1792: URL uwyn_resource_png = ResourceFinderClasspath
1793: .getInstance().getResource("uwyn.png");
1794: byte[] uwyn_image_png = FileUtils
1795: .readBytes(uwyn_resource_png);
1796:
1797: URL rife_resource_png = ResourceFinderClasspath
1798: .getInstance().getResource("rife-logo_small.png");
1799: byte[] rife_image_png = FileUtils
1800: .readBytes(rife_resource_png);
1801:
1802: List<ContentImageAutoRetrieved> restored_list = manager
1803: .restore(manager.getRestoreQuery().orderBy("id",
1804: Select.DESC));
1805: ContentImageAutoRetrieved restored = null;
1806: Iterator<ContentImageAutoRetrieved> restored_it = restored_list
1807: .iterator();
1808: restored = restored_it.next();
1809: assertEquals(content2.getId(), restored.getId());
1810: assertEquals(content2.getName(), restored.getName());
1811: assertTrue(Arrays.equals(rife_image_png, restored
1812: .getImage()));
1813: restored = restored_it.next();
1814: assertEquals(content1.getId(), restored.getId());
1815: assertEquals(content1.getName(), restored.getName());
1816: assertTrue(Arrays.equals(uwyn_image_png, restored
1817: .getImage()));
1818: assertFalse(restored_it.hasNext());
1819: } finally {
1820: manager.remove();
1821: }
1822: }
1823: }
|