0001: /*
0002: * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
0003: *
0004: * Project: OpenChronicle
0005: *
0006: * $Id: BlogControllerTest.java,v 1.6 2007/03/05 07:28:12 bastafidli Exp $
0007: *
0008: * This program is free software; you can redistribute it and/or modify
0009: * it under the terms of the GNU General Public License as published by
0010: * the Free Software Foundation; version 2 of the License.
0011: *
0012: * This program is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015: * GNU General Public License for more details.
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * along with this program; if not, write to the Free Software
0019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0020: */
0021:
0022: package org.opensubsystems.blog.logic;
0023:
0024: import java.sql.PreparedStatement;
0025: import java.util.ArrayList;
0026: import java.util.Collections;
0027: import java.util.Iterator;
0028: import java.util.List;
0029: import java.util.Map;
0030:
0031: import junit.extensions.TestSetup;
0032: import junit.framework.Test;
0033: import junit.framework.TestSuite;
0034:
0035: import org.opensubsystems.blog.data.Blog;
0036: import org.opensubsystems.blog.data.Entry;
0037: import org.opensubsystems.blog.persist.BlogFactory;
0038: import org.opensubsystems.blog.persist.EntryFactory;
0039: import org.opensubsystems.blog.persist.db.BlogDatabaseSchema;
0040: import org.opensubsystems.blog.persist.db.BlogListDatabaseTestUtils;
0041: import org.opensubsystems.blog.persist.db.EntryListDatabaseTestUtils;
0042: import org.opensubsystems.core.data.DataObject;
0043: import org.opensubsystems.core.error.OSSException;
0044: import org.opensubsystems.core.logic.ControllerManager;
0045: import org.opensubsystems.core.persist.DataFactoryManager;
0046: import org.opensubsystems.core.persist.db.Database;
0047: import org.opensubsystems.core.persist.db.DatabaseImpl;
0048: import org.opensubsystems.core.persist.db.DatabaseSchemaManager;
0049: import org.opensubsystems.core.persist.db.DatabaseTestSetup;
0050: import org.opensubsystems.core.persist.db.DatabaseTestSuite;
0051: import org.opensubsystems.core.util.CreationTimestampIdComparator;
0052: import org.opensubsystems.core.util.DatabaseUtils;
0053: import org.opensubsystems.patterns.listdata.data.ListDefinition;
0054: import org.opensubsystems.patterns.listdata.data.ListOptions;
0055: import org.opensubsystems.patterns.listdata.logic.ListControllerTest;
0056: import org.opensubsystems.patterns.listdata.persist.db.ListDatabaseTestUtils;
0057:
0058: /**
0059: * Test for BlogController interface implementation class.
0060: *
0061: * @version $Id: BlogControllerTest.java,v 1.6 2007/03/05 07:28:12 bastafidli Exp $
0062: * @author Julian Legeny
0063: * @code.reviewer TODO: Review this code
0064: * @code.reviewed
0065: */
0066: public final class BlogControllerTest {
0067: // Constructors /////////////////////////////////////////////////////////////
0068:
0069: /**
0070: * Private constructor since this class cannot be instantiated
0071: */
0072: private BlogControllerTest() {
0073: // Do nothing
0074: }
0075:
0076: // Public methods ///////////////////////////////////////////////////////////
0077:
0078: /**
0079: * Create the suite for this test since this is the only way how to create
0080: * test setup which can initialize and shutdown the database for us
0081: *
0082: * @return Test - suite of tests to run for this database
0083: */
0084: public static Test suite() {
0085: TestSuite suite = new DatabaseTestSuite("BlogControllerTest");
0086: suite.addTestSuite(BlogControllerTestInternal.class);
0087: TestSetup wrapper = new DatabaseTestSetup(suite);
0088:
0089: return wrapper;
0090: }
0091:
0092: /**
0093: * Internal class which can be included in other test suites directly without
0094: * including the above suite. This allows us to group multiple tests
0095: * together and the execute the DatabaseTestSetup only once
0096: */
0097: public static class BlogControllerTestInternal extends
0098: ListControllerTest {
0099: // Cached values ////////////////////////////////////////////////////////////
0100:
0101: /**
0102: * Factory to manage blogs.
0103: */
0104: protected BlogFactory m_blogFactory;
0105:
0106: /**
0107: * Factory to manage entries.
0108: */
0109: protected EntryFactory m_entryFactory;
0110:
0111: /**
0112: * Controller used to manipulate blogs.
0113: */
0114: protected BlogController m_blogControl;
0115:
0116: /**
0117: * Utility class used to generate entries for controller test.
0118: */
0119: protected ListDatabaseTestUtils m_entryListTestUtils;
0120:
0121: // Constructors /////////////////////////////////////////////////////////////
0122:
0123: /**
0124: * Static initializer.
0125: */
0126: static {
0127: Database dbDatabase;
0128: try {
0129: dbDatabase = DatabaseImpl.getInstance();
0130: dbDatabase.add(DatabaseSchemaManager
0131: .getInstance(BlogDatabaseSchema.class));
0132: } catch (OSSException bfeExc) {
0133: throw new RuntimeException("Unexpected exception.",
0134: bfeExc);
0135: }
0136: }
0137:
0138: /**
0139: * Constructor
0140: *
0141: * @param strTestName - name of the test
0142: * @throws Exception - an error has occured
0143: */
0144: public BlogControllerTestInternal(String strTestName)
0145: throws Exception {
0146: super (strTestName, new BlogListDatabaseTestUtils());
0147:
0148: m_blogFactory = (BlogFactory) DataFactoryManager
0149: .getInstance(BlogFactory.class);
0150: m_entryFactory = (EntryFactory) DataFactoryManager
0151: .getInstance(EntryFactory.class);
0152: m_blogControl = (BlogController) ControllerManager
0153: .getInstance(BlogController.class);
0154: m_entryListTestUtils = new EntryListDatabaseTestUtils();
0155: }
0156:
0157: // Tests ////////////////////////////////////////////////////////////////////
0158:
0159: /**
0160: * Test of get Blog by ID
0161: * @throws Exception - an error has occured during test
0162: */
0163: public void testGetBlogByID() throws Exception {
0164: PreparedStatement statement = null;
0165:
0166: Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0167: "testfolder1", "testcaption1", "testcomment1",
0168: null, null);
0169:
0170: Blog data2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0171: "testfolder2", "testcaption2", "testcomment2",
0172: null, null);
0173: Blog testdata = null;
0174: try {
0175: m_transaction.begin();
0176: try {
0177: data1 = (Blog) m_blogFactory.create(data1);
0178: m_transaction.commit();
0179: } catch (Throwable thr) {
0180: m_transaction.rollback();
0181: throw new Exception(thr);
0182: }
0183:
0184: m_transaction.begin();
0185: try {
0186: data2 = (Blog) m_blogFactory.create(data2);
0187: m_transaction.commit();
0188: } catch (Throwable thr) {
0189: m_transaction.rollback();
0190: throw new Exception(thr);
0191: }
0192:
0193: // try to get blog 2
0194: testdata = (Blog) m_blogControl.get(data2.getId());
0195:
0196: assertNotNull("Blog 2 should not be null", testdata);
0197: assertTrue("Blog 2 is not the same", data2
0198: .isSame(testdata));
0199:
0200: // try to get blog 1
0201: testdata = (Blog) m_blogControl.get(data1.getId());
0202:
0203: assertNotNull("Blog 1 should not be null", testdata);
0204: assertTrue("Blog 1 is not the same", data1
0205: .isSame(testdata));
0206: } finally {
0207: m_transaction.begin();
0208: try {
0209: if ((data1 != null)
0210: && (data1.getId() != DataObject.NEW_ID)) {
0211: // delete blog 1
0212: m_blogFactory.delete(data1.getId(),
0213: DataObject.NEW_ID);
0214: }
0215: if ((data2 != null)
0216: && (data2.getId() != DataObject.NEW_ID)) {
0217: // delete blog 2
0218: m_blogFactory.delete(data2.getId(),
0219: DataObject.NEW_ID);
0220: }
0221: m_transaction.commit();
0222: } catch (Throwable thr) {
0223: m_transaction.rollback();
0224: throw new Exception(thr);
0225: } finally {
0226: DatabaseUtils.closeStatement(statement);
0227: }
0228: }
0229: }
0230:
0231: /**
0232: * Test of getting Blog by unique Folder name
0233: * @throws Exception - an error has occured during test
0234: */
0235: public void testGetBlogByFolder() throws Exception {
0236: PreparedStatement statement = null;
0237:
0238: Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0239: "testfolder1", "testcaption1", "testcomment1",
0240: null, null);
0241:
0242: Blog data2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0243: "testfolder2", "testcaption2", "testcomment2",
0244: null, null);
0245: Blog testdata;
0246:
0247: try {
0248: m_transaction.begin();
0249: try {
0250: data1 = (Blog) m_blogFactory.create(data1);
0251: m_transaction.commit();
0252: } catch (Throwable thr) {
0253: m_transaction.rollback();
0254: throw new Exception(thr);
0255: }
0256:
0257: m_transaction.begin();
0258: try {
0259: data2 = (Blog) m_blogFactory.create(data2);
0260: m_transaction.commit();
0261: } catch (Throwable thr) {
0262: m_transaction.rollback();
0263: throw new Exception(thr);
0264: }
0265:
0266: // try to get blog 2
0267: testdata = (Blog) m_blogControl.get(data2.getFolder());
0268:
0269: assertNotNull("Blog 2 should not be null", testdata);
0270: assertTrue("Blog 2 is not the same", data2
0271: .isSame(testdata));
0272:
0273: // try to get blog 1
0274: testdata = (Blog) m_blogControl.get(data1.getFolder());
0275:
0276: assertNotNull("Blog 1 should not be null", testdata);
0277: assertTrue("Blog 1 is not the same", data1
0278: .isSame(testdata));
0279: } finally {
0280: m_transaction.begin();
0281: try {
0282: if ((data1 != null)
0283: && (data1.getId() != DataObject.NEW_ID)) {
0284: // delete blog 1
0285: m_blogFactory.delete(data1.getId(),
0286: DataObject.NEW_ID);
0287: }
0288: if ((data2 != null)
0289: && (data2.getId() != DataObject.NEW_ID)) {
0290: // delete blog 2
0291: m_blogFactory.delete(data2.getId(),
0292: DataObject.NEW_ID);
0293: }
0294: m_transaction.commit();
0295: } catch (Throwable thr) {
0296: m_transaction.rollback();
0297: throw new Exception(thr);
0298: } finally {
0299: DatabaseUtils.closeStatement(statement);
0300: }
0301: }
0302: }
0303:
0304: /**
0305: * Test for getting page with blogs
0306: * @throws Exception - an error has occured during test
0307: */
0308: public void testGetPage() throws Exception {
0309: Blog data;
0310: int iCount = 123; // test this with real big amount of data
0311:
0312: PreparedStatement statement = null;
0313: try {
0314: int iIndex;
0315: List lstSourceData = new ArrayList();
0316:
0317: m_transaction.begin();
0318: try {
0319: for (iIndex = 0; iIndex < iCount; iIndex++) {
0320: data = new Blog(DataObject.NEW_ID,
0321: DataObject.NEW_ID, "testfolder_"
0322: + iIndex, "testcaption"
0323: + iIndex, "testcomment"
0324: + iIndex, null, null);
0325: data = (Blog) m_blogFactory.create(data);
0326: lstSourceData.add(data);
0327: }
0328: m_transaction.commit();
0329: } catch (Throwable thr) {
0330: m_transaction.rollback();
0331: throw new Exception(thr);
0332: }
0333:
0334: Collections.sort(lstSourceData, BlogCaptionComparator
0335: .getInstance());
0336:
0337: int iIterations = iCount
0338: / ListDefinition.DEFAULT_PAGE_SIZE;
0339:
0340: if ((iCount % ListDefinition.DEFAULT_PAGE_SIZE) != 0) {
0341: iIterations++;
0342: }
0343:
0344: Object[] arReturnData;
0345: List lstTestData;
0346:
0347: for (iIndex = 1; iIndex <= iIterations; iIndex++) {
0348: arReturnData = m_blogControl.getPage(iIndex);
0349: lstTestData = (List) arReturnData[0];
0350: assertNotNull("Loaded blog list is null",
0351: lstTestData);
0352: if (iIndex == iIterations) {
0353: assertEquals(
0354: "Number of loaded blogs is incorrect",
0355: iCount
0356: % ListDefinition.DEFAULT_PAGE_SIZE,
0357: lstTestData.size());
0358: assertEquals(
0359: "The loaded blogs are incorrect",
0360: lstSourceData
0361: .subList(
0362: (iIndex - 1)
0363: * ListDefinition.DEFAULT_PAGE_SIZE,
0364: lstSourceData.size()),
0365: lstTestData);
0366: } else {
0367: assertEquals(
0368: "Number of loaded blogs is incorrect",
0369: ListDefinition.DEFAULT_PAGE_SIZE,
0370: lstTestData.size());
0371: assertEquals(
0372: "The loaded blogs are incorrect",
0373: lstSourceData
0374: .subList(
0375: (iIndex - 1)
0376: * ListDefinition.DEFAULT_PAGE_SIZE,
0377: iIndex
0378: * ListDefinition.DEFAULT_PAGE_SIZE),
0379: lstTestData);
0380: }
0381:
0382: }
0383: } finally {
0384: m_transaction.begin();
0385: try {
0386: statement = m_connection
0387: .prepareStatement("delete from BF_BLOG where FOLDER "
0388: + "like 'testfolder_%'");
0389: statement.executeUpdate();
0390: m_transaction.commit();
0391: } catch (Throwable thr) {
0392: m_transaction.rollback();
0393: throw new Exception(thr);
0394: } finally {
0395: DatabaseUtils.closeStatement(statement);
0396: }
0397: }
0398: }
0399:
0400: /**
0401: * Test for getting of the entries for specified blog
0402: * @throws Exception - an error has occured during test
0403: */
0404: public void testGetEntries() throws Exception {
0405: // This test is based on the test in ListControllerTest.testGetExactPage
0406: // since all what this method should do is to invoke that method and
0407: // return modified returned values.
0408:
0409: Object[] data = null;
0410:
0411: try {
0412: Object parent;
0413: int iActualPage = 2;
0414:
0415: // We test for different page than 1 since 1st page is always loaded
0416: data = m_entryListTestUtils
0417: .insertTestData(m_transaction);
0418: parent = data[0];
0419:
0420: Object[] listOpResults = m_blogControl.getEntries(
0421: ((Blog) parent).getId(), iActualPage);
0422: ListOptions testOptions = (ListOptions) listOpResults[1];
0423: List lstTestData = (List) listOpResults[0];
0424: ListOptions options = testOptions;
0425:
0426: //m_entryListTestUtils.printDebug(lstTestData);
0427: //m_entryListTestUtils.printDebug(testOptions);
0428:
0429: String[] arrExpectedData = getExpectedResultListColumnOneSortedByCreationDate(data);
0430:
0431: assertNotNull("Selected data object list is null",
0432: lstTestData);
0433: assertEquals("ActualListSize is incorrectly set",
0434: arrExpectedData.length, testOptions
0435: .getActualListSize());
0436: if ((arrExpectedData.length
0437: - testOptions.getBeginPosition() + 1) < options
0438: .getPageSize()) {
0439: assertEquals(
0440: "Number of selected items is incorrect",
0441: (arrExpectedData.length
0442: - testOptions.getBeginPosition() + 1),
0443: lstTestData.size());
0444: } else {
0445: assertEquals(
0446: "Number of selected items is incorrect",
0447: options.getPageSize(), lstTestData.size());
0448: }
0449:
0450: if ((iActualPage - 1) * options.getPageSize() < arrExpectedData.length) {
0451: assertEquals(
0452: "BeginPosition is incorrectly set",
0453: ((iActualPage - 1) * options.getPageSize()) + 1,
0454: testOptions.getBeginPosition());
0455: } else {
0456: assertEquals("BeginPosition is incorrectly set",
0457: ((arrExpectedData.length / options
0458: .getPageSize()) * options
0459: .getPageSize()) + 1, testOptions
0460: .getBeginPosition());
0461: }
0462:
0463: assertTrue(
0464: "EndPosition is incorrectly set",
0465: ((testOptions.getBeginPosition()
0466: + options.getPageSize() - 1) == testOptions
0467: .getEndPosition() || (testOptions
0468: .getEndPosition() == arrExpectedData.length)));
0469:
0470: if ((iActualPage - 1) * options.getPageSize() < arrExpectedData.length) {
0471: assertEquals("ActualPage is incorrectly set",
0472: iActualPage, testOptions.getActualPage());
0473: } else {
0474: assertEquals(
0475: "ActualPage is incorrectly set",
0476: (((testOptions.getBeginPosition()
0477: + options.getPageSize() - 2) / options
0478: .getPageSize()) + 1), testOptions
0479: .getActualPage());
0480: }
0481:
0482: int helpCounter;
0483: int assertCounter;
0484: for (assertCounter = (testOptions.getBeginPosition() - 1), helpCounter = 0; assertCounter < testOptions
0485: .getEndPosition(); assertCounter++, helpCounter++) {
0486: assertEquals(
0487: "The retrieved element #" + helpCounter
0488: + " is not correct",
0489: arrExpectedData[assertCounter],
0490: m_entryListTestUtils
0491: .getColumnOneValue((DataObject) lstTestData
0492: .get(helpCounter)));
0493: }
0494: } catch (Throwable thr) {
0495: throw new Exception(thr);
0496: } finally {
0497: m_entryListTestUtils
0498: .deleteTestData(m_transaction, data);
0499: }
0500: }
0501:
0502: /**
0503: * Test for getting of blog and its entries knowing just the folder
0504: * @throws Exception - an error has occured during test
0505: */
0506: public void testGetWithEntries() throws Exception {
0507: PreparedStatement statement = null;
0508:
0509: Blog blog1 = null;
0510: Blog blog2 = null;
0511:
0512: Entry data11 = null;
0513: Entry data12 = null;
0514: Entry data13 = null;
0515:
0516: Entry data21 = null;
0517: Entry data22 = null;
0518:
0519: Object[] arrObject = null;
0520: Blog testBlog = null;
0521: List lstEntries = null;
0522:
0523: try {
0524: blog1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0525: "testfolder1", "testcaption1", "testcomment1",
0526: null, null);
0527:
0528: m_transaction.begin();
0529: try {
0530: // create blog 1
0531: blog1 = (Blog) m_blogFactory.create(blog1);
0532: m_transaction.commit();
0533: } catch (Throwable thr) {
0534: m_transaction.rollback();
0535: throw new Exception(thr);
0536: }
0537:
0538: data11 = new Entry(DataObject.NEW_ID,
0539: DataObject.NEW_ID, blog1.getId(),
0540: "test_entry_caption11", "test_entry_comment11",
0541: "test_image_url11", "test_target_url11", null,
0542: null);
0543: m_transaction.begin();
0544: try {
0545: data11 = (Entry) m_entryFactory.create(data11);
0546: m_transaction.commit();
0547: } catch (Throwable thr) {
0548: m_transaction.rollback();
0549: throw new Exception(thr);
0550: }
0551: Thread.sleep(1000);
0552:
0553: data12 = new Entry(DataObject.NEW_ID,
0554: DataObject.NEW_ID, blog1.getId(),
0555: "test_entry_caption12", "test_entry_comment12",
0556: "test_image_url12", "test_target_url12", null,
0557: null);
0558: m_transaction.begin();
0559: try {
0560: data12 = (Entry) m_entryFactory.create(data12);
0561: m_transaction.commit();
0562: } catch (Throwable thr) {
0563: m_transaction.rollback();
0564: throw new Exception(thr);
0565: }
0566: Thread.sleep(1000);
0567:
0568: data13 = new Entry(DataObject.NEW_ID,
0569: DataObject.NEW_ID, blog1.getId(),
0570: "test_entry_caption13", "test_entry_comment13",
0571: "test_image_url13", "test_target_url13", null,
0572: null);
0573: m_transaction.begin();
0574: try {
0575: data13 = (Entry) m_entryFactory.create(data13);
0576: m_transaction.commit();
0577: } catch (Throwable thr) {
0578: m_transaction.rollback();
0579: throw new Exception(thr);
0580: }
0581:
0582: blog2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0583: "testfolder2", "testcaption2", "testcomment2",
0584: null, null);
0585:
0586: m_transaction.begin();
0587: try {
0588: // create blog 2
0589: blog2 = (Blog) m_blogFactory.create(blog2);
0590: m_transaction.commit();
0591: } catch (Throwable thr) {
0592: m_transaction.rollback();
0593: throw new Exception(thr);
0594: }
0595:
0596: data21 = new Entry(DataObject.NEW_ID,
0597: DataObject.NEW_ID, blog2.getId(),
0598: "test_entry_caption21", "test_entry_comment21",
0599: "test_image_url21", "test_target_url21", null,
0600: null);
0601: m_transaction.begin();
0602: try {
0603: data21 = (Entry) m_entryFactory.create(data21);
0604: m_transaction.commit();
0605: } catch (Throwable thr) {
0606: m_transaction.rollback();
0607: throw new Exception(thr);
0608: }
0609: Thread.sleep(1000);
0610:
0611: data22 = new Entry(DataObject.NEW_ID,
0612: DataObject.NEW_ID, blog2.getId(),
0613: "test_entry_caption22", "test_entry_comment22",
0614: "test_image_url22", "test_target_url22", null,
0615: null);
0616: m_transaction.begin();
0617: try {
0618: data22 = (Entry) m_entryFactory.create(data22);
0619: m_transaction.commit();
0620: } catch (Throwable thr) {
0621: m_transaction.rollback();
0622: throw new Exception(thr);
0623: }
0624:
0625: // get blog 1 with its entries
0626: arrObject = m_blogControl.getWithEntries(blog1
0627: .getFolder(), 1);
0628: testBlog = (Blog) arrObject[0];
0629: lstEntries = (List) arrObject[1];
0630: assertNotNull("Blog 1 should not be null", testBlog);
0631: assertTrue("Blog 1 is not same", blog1.isSame(testBlog));
0632:
0633: assertNotNull(
0634: "List of entries for blog 1 should not be null",
0635: lstEntries);
0636: assertEquals(
0637: "Size of the list of entries for blog 1 is incorrect",
0638: 3, lstEntries.size());
0639: assertTrue("Entry 11 is not the same", data11
0640: .isSame((Entry) lstEntries.get(2)));
0641: assertTrue("Entry 12 is not the same", data12
0642: .isSame((Entry) lstEntries.get(1)));
0643: assertTrue("Entry 13 is not the same", data13
0644: .isSame((Entry) lstEntries.get(0)));
0645:
0646: // get blog 2 with its entries
0647: arrObject = m_blogControl.getWithEntries(blog2
0648: .getFolder(), 1);
0649: testBlog = (Blog) arrObject[0];
0650: lstEntries = (List) arrObject[1];
0651: assertNotNull("Blog 2 should not be null", testBlog);
0652: assertTrue("Blog 2 is not same", blog2.isSame(testBlog));
0653:
0654: assertNotNull(
0655: "List of entries for blog 2 should not be null",
0656: lstEntries);
0657: assertEquals(
0658: "Size of the list of entries for blog 1 is incorrect",
0659: 2, lstEntries.size());
0660: assertTrue("Entry 21 is not the same", data21
0661: .isSame((Entry) lstEntries.get(1)));
0662: assertTrue("Entry 22 is not the same", data22
0663: .isSame((Entry) lstEntries.get(0)));
0664: } finally {
0665: m_transaction.begin();
0666: try {
0667: if ((data11 != null)
0668: && (data11.getId() != DataObject.NEW_ID)) {
0669: // delete entry 11
0670: m_entryFactory.delete(data11.getId(),
0671: DataObject.NEW_ID);
0672: }
0673: if ((data12 != null)
0674: && (data12.getId() != DataObject.NEW_ID)) {
0675: // delete entry 12
0676: m_entryFactory.delete(data12.getId(),
0677: DataObject.NEW_ID);
0678: }
0679: if ((data13 != null)
0680: && (data13.getId() != DataObject.NEW_ID)) {
0681: // delete entry 13
0682: m_entryFactory.delete(data13.getId(),
0683: DataObject.NEW_ID);
0684: }
0685: if ((data21 != null)
0686: && (data21.getId() != DataObject.NEW_ID)) {
0687: // delete entry 21
0688: m_entryFactory.delete(data21.getId(),
0689: DataObject.NEW_ID);
0690: }
0691: if ((data22 != null)
0692: && (data22.getId() != DataObject.NEW_ID)) {
0693: // delete entry 22
0694: m_entryFactory.delete(data22.getId(),
0695: DataObject.NEW_ID);
0696: }
0697: if ((blog1 != null)
0698: && (blog1.getId() != DataObject.NEW_ID)) {
0699: // delete blog 1
0700: m_blogFactory.delete(blog1.getId(),
0701: DataObject.NEW_ID);
0702: }
0703: if ((blog2 != null)
0704: && (blog2.getId() != DataObject.NEW_ID)) {
0705: // delete blog 2
0706: m_blogFactory.delete(blog2.getId(),
0707: DataObject.NEW_ID);
0708: }
0709: m_transaction.commit();
0710: } catch (Throwable thr) {
0711: m_transaction.rollback();
0712: throw new Exception(thr);
0713: } finally {
0714: DatabaseUtils.closeStatement(statement);
0715: }
0716: }
0717: }
0718:
0719: /**
0720: * Test for getting of blog and its entry knowing just the folder and the id of the entry
0721: * @throws Exception - an error has occured during test
0722: */
0723: public void testGetWithEntry() throws Exception {
0724: PreparedStatement statement = null;
0725:
0726: Blog blog = null;
0727:
0728: Entry data1 = null;
0729: Entry data2 = null;
0730: Entry data3 = null;
0731:
0732: Object[] arrObject = null;
0733: Blog testBlog = null;
0734: Entry testEntry = null;
0735:
0736: try {
0737: blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0738: "testfolder1", "testcaption1", "testcomment1",
0739: null, null);
0740:
0741: m_transaction.begin();
0742: try {
0743: // create blog
0744: blog = (Blog) m_blogFactory.create(blog);
0745: m_transaction.commit();
0746: } catch (Throwable thr) {
0747: m_transaction.rollback();
0748: throw new Exception(thr);
0749: }
0750:
0751: data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
0752: blog.getId(), "test_entry_caption11",
0753: "test_entry_comment11", "test_image_url11",
0754: "test_target_url11", null, null);
0755: m_transaction.begin();
0756: try {
0757: data1 = (Entry) m_entryFactory.create(data1);
0758: m_transaction.commit();
0759: } catch (Throwable thr) {
0760: m_transaction.rollback();
0761: throw new Exception(thr);
0762: }
0763: Thread.sleep(200);
0764:
0765: data2 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
0766: blog.getId(), "test_entry_caption12",
0767: "test_entry_comment12", "test_image_url12",
0768: "test_target_url12", null, null);
0769: m_transaction.begin();
0770: try {
0771: data2 = (Entry) m_entryFactory.create(data2);
0772: m_transaction.commit();
0773: } catch (Throwable thr) {
0774: m_transaction.rollback();
0775: throw new Exception(thr);
0776: }
0777: Thread.sleep(200);
0778:
0779: data3 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
0780: blog.getId(), "test_entry_caption13",
0781: "test_entry_comment13", "test_image_url13",
0782: "test_target_url13", null, null);
0783: m_transaction.begin();
0784: try {
0785: data3 = (Entry) m_entryFactory.create(data3);
0786: m_transaction.commit();
0787: } catch (Throwable thr) {
0788: m_transaction.rollback();
0789: throw new Exception(thr);
0790: }
0791:
0792: // get blog with belonging entry 2
0793: arrObject = m_blogControl.getWithEntry(
0794: blog.getFolder(), data2.getId());
0795: testBlog = (Blog) arrObject[0];
0796: testEntry = (Entry) arrObject[1];
0797: assertNotNull("Blog should not be null", testBlog);
0798: assertTrue("Blog is not same", blog.isSame(testBlog));
0799:
0800: assertNotNull("Entry should not be null", testEntry);
0801: assertTrue("Entry is not same", data2.isSame(testEntry));
0802:
0803: // get blog with belonging entry 1
0804: arrObject = m_blogControl.getWithEntry(
0805: blog.getFolder(), data1.getId());
0806: testBlog = (Blog) arrObject[0];
0807: testEntry = (Entry) arrObject[1];
0808: assertNotNull("Blog should not be null", testBlog);
0809: assertTrue("Blog is not same", blog.isSame(testBlog));
0810:
0811: assertNotNull("Entry should not be null", testEntry);
0812: assertTrue("Entry is not same", data1.isSame(testEntry));
0813:
0814: // get blog with belonging entry 3
0815: arrObject = m_blogControl.getWithEntry(
0816: blog.getFolder(), data3.getId());
0817: testBlog = (Blog) arrObject[0];
0818: testEntry = (Entry) arrObject[1];
0819: assertNotNull("Blog should not be null", testBlog);
0820: assertTrue("Blog is not same", blog.isSame(testBlog));
0821:
0822: assertNotNull("Entry should not be null", testEntry);
0823: assertTrue("Entry is not same", data3.isSame(testEntry));
0824: } finally {
0825: m_transaction.begin();
0826: try {
0827: if ((data1 != null)
0828: && (data1.getId() != DataObject.NEW_ID)) {
0829: // delete entry 1
0830: m_entryFactory.delete(data1.getId(),
0831: DataObject.NEW_ID);
0832: }
0833: if ((data2 != null)
0834: && (data2.getId() != DataObject.NEW_ID)) {
0835: // delete entry 2
0836: m_entryFactory.delete(data2.getId(),
0837: DataObject.NEW_ID);
0838: }
0839: if ((data3 != null)
0840: && (data3.getId() != DataObject.NEW_ID)) {
0841: // delete entry 3
0842: m_entryFactory.delete(data3.getId(),
0843: DataObject.NEW_ID);
0844: }
0845: if ((blog != null)
0846: && (blog.getId() != DataObject.NEW_ID)) {
0847: // delete blog
0848: m_blogFactory.delete(blog.getId(),
0849: DataObject.NEW_ID);
0850: }
0851: m_transaction.commit();
0852: } catch (Throwable thr) {
0853: m_transaction.rollback();
0854: throw new Exception(thr);
0855: } finally {
0856: DatabaseUtils.closeStatement(statement);
0857: }
0858: }
0859: }
0860:
0861: /**
0862: * Test for getting of blog and its entry knowing just the blog id and the id of the entry
0863: * @throws Exception - an error has occured during test
0864: */
0865: public void testGetWithEntryById() throws Exception {
0866: PreparedStatement statement = null;
0867:
0868: Blog blog = null;
0869:
0870: Entry data1 = null;
0871: Entry data2 = null;
0872: Entry data3 = null;
0873:
0874: Object[] arrObject = null;
0875: Blog testBlog = null;
0876: Entry testEntry = null;
0877:
0878: try {
0879: blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
0880: "testfolder1", "testcaption1", "testcomment1",
0881: null, null);
0882:
0883: m_transaction.begin();
0884: try {
0885: // create blog
0886: blog = (Blog) m_blogFactory.create(blog);
0887: m_transaction.commit();
0888: } catch (Throwable thr) {
0889: m_transaction.rollback();
0890: throw new Exception(thr);
0891: }
0892:
0893: data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
0894: blog.getId(), "test_entry_caption11",
0895: "test_entry_comment11", "test_image_url11",
0896: "test_target_url11", null, null);
0897: m_transaction.begin();
0898: try {
0899: data1 = (Entry) m_entryFactory.create(data1);
0900: m_transaction.commit();
0901: } catch (Throwable thr) {
0902: m_transaction.rollback();
0903: throw new Exception(thr);
0904: }
0905: Thread.sleep(200);
0906:
0907: data2 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
0908: blog.getId(), "test_entry_caption12",
0909: "test_entry_comment12", "test_image_url12",
0910: "test_target_url12", null, null);
0911: m_transaction.begin();
0912: try {
0913: data2 = (Entry) m_entryFactory.create(data2);
0914: m_transaction.commit();
0915: } catch (Throwable thr) {
0916: m_transaction.rollback();
0917: throw new Exception(thr);
0918: }
0919: Thread.sleep(200);
0920:
0921: data3 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
0922: blog.getId(), "test_entry_caption13",
0923: "test_entry_comment13", "test_image_url13",
0924: "test_target_url13", null, null);
0925: m_transaction.begin();
0926: try {
0927: data3 = (Entry) m_entryFactory.create(data3);
0928: m_transaction.commit();
0929: } catch (Throwable thr) {
0930: m_transaction.rollback();
0931: throw new Exception(thr);
0932: }
0933:
0934: // get blog with belonging entry 2
0935: arrObject = m_blogControl.getWithEntry(blog.getId(),
0936: data2.getId());
0937: testBlog = (Blog) arrObject[0];
0938: testEntry = (Entry) arrObject[1];
0939: assertNotNull("Blog should not be null", testBlog);
0940: assertTrue("Blog is not same", blog.isSame(testBlog));
0941:
0942: assertNotNull("Entry should not be null", testEntry);
0943: assertTrue("Entry is not same", data2.isSame(testEntry));
0944:
0945: // get blog with belonging entry 1
0946: arrObject = m_blogControl.getWithEntry(blog.getId(),
0947: data1.getId());
0948: testBlog = (Blog) arrObject[0];
0949: testEntry = (Entry) arrObject[1];
0950: assertNotNull("Blog should not be null", testBlog);
0951: assertTrue("Blog is not same", blog.isSame(testBlog));
0952:
0953: assertNotNull("Entry should not be null", testEntry);
0954: assertTrue("Entry is not same", data1.isSame(testEntry));
0955:
0956: // get blog with belonging entry 3
0957: arrObject = m_blogControl.getWithEntry(blog.getId(),
0958: data3.getId());
0959: testBlog = (Blog) arrObject[0];
0960: testEntry = (Entry) arrObject[1];
0961: assertNotNull("Blog should not be null", testBlog);
0962: assertTrue("Blog is not same", blog.isSame(testBlog));
0963:
0964: assertNotNull("Entry should not be null", testEntry);
0965: assertTrue("Entry is not same", data3.isSame(testEntry));
0966: } finally {
0967: m_transaction.begin();
0968: try {
0969: if ((data1 != null)
0970: && (data1.getId() != DataObject.NEW_ID)) {
0971: // delete entry 1
0972: m_entryFactory.delete(data1.getId(),
0973: DataObject.NEW_ID);
0974: }
0975: if ((data2 != null)
0976: && (data2.getId() != DataObject.NEW_ID)) {
0977: // delete entry 2
0978: m_entryFactory.delete(data2.getId(),
0979: DataObject.NEW_ID);
0980: }
0981: if ((data3 != null)
0982: && (data3.getId() != DataObject.NEW_ID)) {
0983: // delete entry 3
0984: m_entryFactory.delete(data3.getId(),
0985: DataObject.NEW_ID);
0986: }
0987: if ((blog != null)
0988: && (blog.getId() != DataObject.NEW_ID)) {
0989: // delete blog
0990: m_blogFactory.delete(blog.getId(),
0991: DataObject.NEW_ID);
0992: }
0993: m_transaction.commit();
0994: } catch (Throwable thr) {
0995: m_transaction.rollback();
0996: throw new Exception(thr);
0997: } finally {
0998: DatabaseUtils.closeStatement(statement);
0999: }
1000: }
1001: }
1002:
1003: /**
1004: * Test create blog.
1005: *
1006: * @throws Exception - an error has occured during test
1007: */
1008: public void testCreateBlog() throws Exception {
1009: Blog data = null;
1010: Blog testData = null;
1011: Blog selectedData = null;
1012:
1013: data = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
1014: "testfolder", "testcaption", "testcomment", null,
1015: null);
1016:
1017: try {
1018: m_transaction.begin();
1019: try {
1020: testData = (Blog) m_blogControl.create(data);
1021: m_transaction.commit();
1022: } catch (Throwable thr) {
1023: m_transaction.rollback();
1024: throw new Exception(thr);
1025: }
1026:
1027: assertNotNull("Blog should not be null", testData);
1028: assertTrue("Id was not generated",
1029: testData.getId() != DataObject.NEW_ID);
1030: assertNotNull("Creation timestamp was not generated",
1031: testData.getCreationTimestamp());
1032: assertNotNull(
1033: "Modification timestamp are not the same",
1034: testData.getModificationTimestamp());
1035: assertTrue("Blog is not the same", data
1036: .isSame(testData));
1037:
1038: // try to test inserted blog data from the DB
1039: // Use factory to perform the verification operation since we relay
1040: // that the factory was tested
1041: selectedData = (Blog) m_blogFactory.get(data.getId(),
1042: DataObject.NEW_ID);
1043: assertNotNull("Blog should be created but was not",
1044: selectedData);
1045: assertTrue("Blog is not the same", testData
1046: .isSame(selectedData));
1047: } finally {
1048: if ((data != null)
1049: && (data.getId() != DataObject.NEW_ID)) {
1050: m_transaction.begin();
1051: // delete inserted records
1052: try {
1053: // delete blog
1054: m_blogFactory.delete(data.getId(),
1055: DataObject.NEW_ID);
1056: m_transaction.commit();
1057: } catch (Throwable thr) {
1058: m_transaction.rollback();
1059: throw new Exception(thr);
1060: }
1061: }
1062: }
1063: }
1064:
1065: /**
1066: * Test create entry.
1067: *
1068: * @throws Exception - an error has occured during test
1069: */
1070: public void testCreateEntry() throws Exception {
1071: Blog blog = null;
1072: Entry data = null;
1073: Entry testData = null;
1074: Entry selectedData = null;
1075:
1076: blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
1077: "testfolder", "testcaption", "testcomment", null,
1078: null);
1079:
1080: try {
1081: m_transaction.begin();
1082: try {
1083: blog = (Blog) m_blogFactory.create(blog);
1084: m_transaction.commit();
1085: } catch (Throwable thr) {
1086: m_transaction.rollback();
1087: throw new Exception(thr);
1088: }
1089:
1090: data = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
1091: blog.getId(), "test_entry_caption",
1092: "test_entry_comment", "test_image_url",
1093: "test_target_url", null, null);
1094: m_transaction.begin();
1095: try {
1096: // create test entry
1097: testData = (Entry) m_blogControl.create(data);
1098: m_transaction.commit();
1099: } catch (Throwable thr) {
1100: m_transaction.rollback();
1101: throw new Exception(thr);
1102: }
1103:
1104: assertNotNull("Entry should not be null", testData);
1105: assertTrue("Id was not generated",
1106: testData.getId() != DataObject.NEW_ID);
1107: assertNotNull("Creation timestamp was not generated",
1108: testData.getCreationTimestamp());
1109: assertNotNull(
1110: "Modification timestamp are not the same",
1111: testData.getModificationTimestamp());
1112: assertTrue("Blog is not the same", data
1113: .isSame(testData));
1114:
1115: // try to test inserted entry data from the DB
1116: // Use factory to perform the verification operation since we relay
1117: // that the factory was tested
1118: selectedData = (Entry) m_entryFactory.get(data.getId(),
1119: DataObject.NEW_ID);
1120: assertNotNull("Entry should be created but was not",
1121: selectedData);
1122: assertTrue("Entry is not the same", testData
1123: .isSame(selectedData));
1124: } finally {
1125: m_transaction.begin();
1126: // delete inserted records
1127: try {
1128: if ((data != null)
1129: && (data.getId() != DataObject.NEW_ID)) {
1130: // delete data
1131: m_entryFactory.delete(data.getId(),
1132: DataObject.NEW_ID);
1133: }
1134: if ((blog != null)
1135: && (blog.getId() != DataObject.NEW_ID)) {
1136: // delete blog
1137: m_blogFactory.delete(blog.getId(),
1138: DataObject.NEW_ID);
1139: }
1140: m_transaction.commit();
1141: } catch (Throwable thr) {
1142: m_transaction.rollback();
1143: throw new Exception(thr);
1144: }
1145: }
1146: }
1147:
1148: /**
1149: * Test save blog
1150: * @throws Exception - an error has occured during test
1151: */
1152: public void testSaveBlog() throws Exception {
1153: Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
1154: "testfolder1", "testcaption1", "testcomment1",
1155: null, null);
1156: Blog data2 = null;
1157: Blog testdata = null;
1158: Blog selectedData = null;
1159:
1160: try {
1161: m_transaction.begin();
1162: try {
1163: data1 = (Blog) m_blogFactory.create(data1);
1164: m_transaction.commit();
1165: } catch (Throwable thr) {
1166: m_transaction.rollback();
1167: throw new Exception(thr);
1168: }
1169:
1170: data2 = new Blog(data1.getId(), DataObject.NEW_ID,
1171: "testfolder2", "testcaption2", "testcomment2",
1172: data1.getCreationTimestamp(), data1
1173: .getModificationTimestamp());
1174: Thread.sleep(1000);
1175:
1176: m_transaction.begin();
1177: try {
1178: testdata = (Blog) m_blogControl.save(data2);
1179: m_transaction.commit();
1180: } catch (Throwable thr) {
1181: m_transaction.rollback();
1182: throw new Exception(thr);
1183: }
1184:
1185: selectedData = (Blog) m_blogFactory.get(testdata
1186: .getId(), DataObject.NEW_ID);
1187:
1188: // Here the save - update is completed
1189: // We need to test
1190: assertTrue("Modification timestamp not changed",
1191: selectedData.getModificationTimestamp()
1192: .getTime() > data1
1193: .getModificationTimestamp().getTime());
1194: assertTrue("Blog is not same", testdata
1195: .isSame(selectedData));
1196: } finally {
1197: m_transaction.begin();
1198: try {
1199: if ((data1 != null)
1200: && (data1.getId() != DataObject.NEW_ID)) {
1201: // delete blog 1
1202: m_blogFactory.delete(data1.getId(),
1203: DataObject.NEW_ID);
1204: }
1205: m_transaction.commit();
1206: } catch (Throwable thr) {
1207: m_transaction.rollback();
1208: throw new Exception(thr);
1209: }
1210: }
1211: }
1212:
1213: /**
1214: * Test save entry
1215: * @throws Exception - an error has occured during test
1216: */
1217: public void testSaveEntry() throws Exception {
1218: Blog blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
1219: "testfolder1", "testcaption1", "testcomment1",
1220: null, null);
1221: Entry data1 = null;
1222: Entry data2 = null;
1223: Entry testdata = null;
1224: Entry selectedData = null;
1225:
1226: try {
1227: m_transaction.begin();
1228: try {
1229: blog = (Blog) m_blogFactory.create(blog);
1230: m_transaction.commit();
1231: } catch (Throwable thr) {
1232: m_transaction.rollback();
1233: throw new Exception(thr);
1234: }
1235:
1236: data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
1237: blog.getId(), "test_entry_caption1",
1238: "test_entry_comment1", "test_image_url1",
1239: "test_target_url1", null, null);
1240:
1241: m_transaction.begin();
1242: try {
1243: data1 = (Entry) m_entryFactory.create(data1);
1244: m_transaction.commit();
1245: } catch (Throwable thr) {
1246: m_transaction.rollback();
1247: throw new Exception(thr);
1248: }
1249:
1250: data2 = new Entry(data1.getId(), DataObject.NEW_ID,
1251: blog.getId(), "test_entry_caption2",
1252: "test_entry_comment2", "test_image_url2",
1253: "test_target_url2", data1
1254: .getCreationTimestamp(), data1
1255: .getModificationTimestamp());
1256: Thread.sleep(1000);
1257:
1258: m_transaction.begin();
1259: try {
1260: testdata = (Entry) m_blogControl.save(data2);
1261: m_transaction.commit();
1262: } catch (Throwable thr) {
1263: m_transaction.rollback();
1264: throw new Exception(thr);
1265: }
1266:
1267: selectedData = (Entry) m_entryFactory.get(testdata
1268: .getId(), DataObject.NEW_ID);
1269:
1270: // Here the save - update is completed
1271: // We need to test
1272: assertTrue("Modification timestamp not changed",
1273: selectedData.getModificationTimestamp()
1274: .getTime() > data1
1275: .getModificationTimestamp().getTime());
1276: assertTrue("Entry is not same", testdata
1277: .isSame(selectedData));
1278: } finally {
1279: m_transaction.begin();
1280: try {
1281: if ((data1 != null)
1282: && (data1.getId() != DataObject.NEW_ID)) {
1283: // delete entry
1284: m_entryFactory.delete(data1.getId(),
1285: DataObject.NEW_ID);
1286: }
1287: if ((blog != null)
1288: && (blog.getId() != DataObject.NEW_ID)) {
1289: // delete blog
1290: m_blogFactory.delete(blog.getId(),
1291: DataObject.NEW_ID);
1292: }
1293: m_transaction.commit();
1294: } catch (Throwable thr) {
1295: m_transaction.rollback();
1296: throw new Exception(thr);
1297: }
1298: }
1299: }
1300:
1301: /**
1302: * Test for deleting blog with assigned entries
1303: * @throws Exception - an error has occured during test
1304: */
1305: public void testDelete() throws Exception {
1306: Blog blog = null;
1307: Entry data1 = null;
1308: Entry data2 = null;
1309: Entry selectedData = null;
1310: Blog selectedBlog = null;
1311:
1312: try {
1313: blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
1314: "testfolder", "testcaption", "testcomment",
1315: null, null);
1316:
1317: m_transaction.begin();
1318: try {
1319: // create blog first
1320: blog = (Blog) m_blogFactory.create(blog);
1321: m_transaction.commit();
1322: } catch (Throwable thr) {
1323: m_transaction.rollback();
1324: throw new Exception(thr);
1325: }
1326:
1327: data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
1328: blog.getId(), "test_entry_caption1",
1329: "test_entry_comment1", "test_image_url1",
1330: "test_target_url1", null, null);
1331: data2 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
1332: blog.getId(), "test_entry_caption2",
1333: "test_entry_comment2", "test_image_url2",
1334: "test_target_url2", null, null);
1335:
1336: m_transaction.begin();
1337: try {
1338: data1 = (Entry) m_entryFactory.create(data1);
1339: m_transaction.commit();
1340: } catch (Throwable thr) {
1341: m_transaction.rollback();
1342: throw new Exception(thr);
1343: }
1344:
1345: m_transaction.begin();
1346: try {
1347: data2 = (Entry) m_entryFactory.create(data2);
1348: m_transaction.commit();
1349: } catch (Throwable thr) {
1350: m_transaction.rollback();
1351: throw new Exception(thr);
1352: }
1353:
1354: m_transaction.begin();
1355: try {
1356: // try to delete entry 2
1357: m_blogControl.delete(blog.getId());
1358: m_transaction.commit();
1359: } catch (Throwable thr) {
1360: m_transaction.rollback();
1361: throw new Exception(thr);
1362: }
1363:
1364: // Try to get deleted data
1365: // Use factory to perform the verification operation since we relay
1366: // that the factory was tested
1367: selectedData = (Entry) m_entryFactory.get(
1368: data2.getId(), DataObject.NEW_ID);
1369: assertNull("Entry 2 should be deleted but was not",
1370: selectedData);
1371: data2 = null;
1372: selectedData = (Entry) m_entryFactory.get(
1373: data1.getId(), DataObject.NEW_ID);
1374: assertNull("Entry 1 should be deleted but was not",
1375: selectedData);
1376: data1 = null;
1377: selectedBlog = (Blog) m_blogFactory.get(blog.getId(),
1378: DataObject.NEW_ID);
1379: assertNull("Blog should be deleted but was not",
1380: selectedBlog);
1381: blog = null;
1382: } finally {
1383: m_transaction.begin();
1384: try {
1385: if ((data1 != null)
1386: && (data1.getId() != DataObject.NEW_ID)) {
1387: // delete entry 1
1388: m_entryFactory.delete(data1.getId(),
1389: DataObject.NEW_ID);
1390: }
1391: if ((data2 != null)
1392: && (data2.getId() != DataObject.NEW_ID)) {
1393: // delete entry 2
1394: m_entryFactory.delete(data2.getId(),
1395: DataObject.NEW_ID);
1396: }
1397: if ((blog != null)
1398: && (blog.getId() != DataObject.NEW_ID)) {
1399: // delete blog
1400: m_blogFactory.delete(blog.getId(),
1401: DataObject.NEW_ID);
1402: }
1403: m_transaction.commit();
1404: } catch (Throwable thr) {
1405: m_transaction.rollback();
1406: throw new Exception(thr);
1407: }
1408: }
1409: }
1410:
1411: /**
1412: * Test for deleting particular entry
1413: * @throws Exception - an error has occured during test
1414: */
1415: public void testDeleteEntry() throws Exception {
1416: Blog blog = null;
1417: Entry data1 = null;
1418: Entry data2 = null;
1419: Entry selectedData = null;
1420:
1421: try {
1422: blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
1423: "testfolder", "testcaption", "testcomment",
1424: null, null);
1425:
1426: m_transaction.begin();
1427: try {
1428: // create blog first
1429: blog = (Blog) m_blogFactory.create(blog);
1430: m_transaction.commit();
1431: } catch (Throwable thr) {
1432: m_transaction.rollback();
1433: throw new Exception(thr);
1434: }
1435:
1436: data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
1437: blog.getId(), "test_entry_caption1",
1438: "test_entry_comment1", "test_image_url1",
1439: "test_target_url1", null, null);
1440: data2 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID,
1441: blog.getId(), "test_entry_caption2",
1442: "test_entry_comment2", "test_image_url2",
1443: "test_target_url2", null, null);
1444:
1445: m_transaction.begin();
1446: try {
1447: data1 = (Entry) m_entryFactory.create(data1);
1448: m_transaction.commit();
1449: } catch (Throwable thr) {
1450: m_transaction.rollback();
1451: throw new Exception(thr);
1452: }
1453:
1454: m_transaction.begin();
1455: try {
1456: data2 = (Entry) m_entryFactory.create(data2);
1457: m_transaction.commit();
1458: } catch (Throwable thr) {
1459: m_transaction.rollback();
1460: throw new Exception(thr);
1461: }
1462:
1463: m_transaction.begin();
1464: try {
1465: // try to delete entry 2
1466: m_blogControl.deleteEntry(data2.getId());
1467: m_transaction.commit();
1468: } catch (Throwable thr) {
1469: m_transaction.rollback();
1470: throw new Exception(thr);
1471: }
1472:
1473: // Use factory to perform the verification operation since we relay
1474: // that the factory was tested
1475: selectedData = (Entry) m_entryFactory.get(
1476: data2.getId(), DataObject.NEW_ID);
1477: assertNull("Entry 2 should be deleted but was not",
1478: selectedData);
1479: data2 = null;
1480:
1481: // Try to get not deleted data
1482: selectedData = (Entry) m_entryFactory.get(
1483: data1.getId(), DataObject.NEW_ID);
1484: assertNotNull("Entry 1 has not be deleted but it was",
1485: selectedData);
1486: assertTrue("Entry 1 is not the same", data1
1487: .isSame(selectedData));
1488: } finally {
1489: m_transaction.begin();
1490: try {
1491: if ((data1 != null)
1492: && (data1.getId() != DataObject.NEW_ID)) {
1493: // delete entry 1
1494: m_entryFactory.delete(data1.getId(),
1495: DataObject.NEW_ID);
1496: }
1497: if ((data2 != null)
1498: && (data2.getId() != DataObject.NEW_ID)) {
1499: // delete entry 2
1500: m_entryFactory.delete(data2.getId(),
1501: DataObject.NEW_ID);
1502: }
1503: if ((blog != null)
1504: && (blog.getId() != DataObject.NEW_ID)) {
1505: // delete blog
1506: m_blogFactory.delete(blog.getId(),
1507: DataObject.NEW_ID);
1508: }
1509: m_transaction.commit();
1510: } catch (Throwable thr) {
1511: m_transaction.rollback();
1512: throw new Exception(thr);
1513: }
1514: }
1515: }
1516:
1517: // Helper methods ///////////////////////////////////////////////////////////
1518:
1519: /**
1520: * Get expected results for columns 1 if the list is sorted using creation
1521: * date of the data objects.
1522: *
1523: * @param data - the value returned from ListDatabaseTestUtils.insertTestData
1524: * method
1525: * @return String[] - array of returned column one values sorted by creation
1526: * date
1527: */
1528: protected String[] getExpectedResultListColumnOneSortedByCreationDate(
1529: Object[] data) {
1530: Object parent = data[0];
1531: Map mapData = (Map) data[1];
1532: List listdata;
1533:
1534: if (parent == null) {
1535: parent = ListDatabaseTestUtils.NO_PARENT;
1536: }
1537: // Create a copy so that we can reorder it as we need it
1538: listdata = new ArrayList((List) mapData.get(parent));
1539: Collections.sort(listdata, Collections
1540: .reverseOrder(CreationTimestampIdComparator
1541: .getInstance()));
1542:
1543: String[] arrReturn = new String[listdata.size()];
1544: Iterator items;
1545: int iIndex;
1546: DataObject item;
1547: for (items = listdata.iterator(), iIndex = 0; items
1548: .hasNext(); iIndex++) {
1549: item = (DataObject) items.next();
1550: arrReturn[iIndex] = m_entryListTestUtils
1551: .getColumnOneValue(item);
1552: }
1553:
1554: return arrReturn;
1555: }
1556: }
1557: }
|