/**
* @Created Sep 16, 2010 9:04:00 AM
* @author cry30
*/
package com.philip.journal.core.dao.spring.hibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.NonUniqueResultException;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.philip.journal.core.JournalTestCommon;
import com.philip.journal.core.dao.BaseJournalTestDAO;
import com.philip.journal.core.dao.DaoConstant;
import com.philip.journal.core.exception.JournalException;
import com.philip.journal.home.bean.Branch;
import com.philip.journal.home.bean.Entry;
import com.philip.journal.home.dao.BranchDAO;
import com.philip.journal.home.dao.EntryDAO;
/**
* This class requires UserDAO, EntryDAO, BranchDAO. Derived from Entry version. So some members may be duplicated and
* some are removed once confirmed they are errors because they pertain to Entry.
*/
public class BaseDAOSpringHibernateBranchTest extends BaseJournalTestDAO {
/** RTFC. */
private static final int TEST_INT = 667;
/** IOC wired Entry DAO. */
@Autowired
private transient EntryDAO entryDAO;
/** IOC wired Branch DAO. */
@Autowired
private transient BranchDAO branchDAO;
/** Branch Id attribute. */
private static final String ATTR_BRANCH_ID = "branchId";
/** Entry title attribute. */
private static final String ATTR_ENTRY_TITLE = "title";
/** Entry description attribute. */
private static final String ATTR_ENTRY_DESC = "description";
/** Entry name attribute. */
private static final String ATTR_ENTRY_NAME = "name";
/** User username attribute. */
private static final String ATTR_USERNAME = "username";
/** User username attribute. */
private static final String ATTR_BRANCH = "branch";
//TEST ARTIFACTS=======================================================================
/** Test Artifact. Title. */
private static final String TEST_TITLE = "Test Title";
/** Reused constant. what ever. */
private static final String LEAF_ONE = "Leaf 1";
/** IOC wired target test instance. Wired an Entry in the config as of 10/26/2011 */
@Autowired
private transient BaseDAOSpringHibernate<Branch> testBaseSpringHibernateDAOBranchImpl; // NOPMD by r39 on 4/4/11 4:18 PM
@Override
protected String getTestSqlPath()
{
return "/com/philip/journal/core/dao/spring/hibernate/BaseDAOSpringHibernateTest.sql";
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#deleteAll0(java.util.List)}.
*
* Case 1: Null argument.
*/
@Test(expected = JournalException.class)
public void testDeleteAllCase1()
{
testBaseSpringHibernateDAOBranchImpl.deleteAll0(null);
fail("Case 1: Null argument.");
}
/** Case 2: Null in list. */
@Test(expected = JournalException.class)
public void testDeleteAllCase2()
{
final List<Branch> list1 = new ArrayList<Branch>();
list1.add(null);
testBaseSpringHibernateDAOBranchImpl.deleteAll0(list1);
fail("null in List test failed.");
}
/**
* @exception JournalException expected due to deletion of constrained Entity.
*
* Case 3: Foreign key constraint.
*/
@Test(expected = JournalException.class)
public void testDeleteAllCase3()
{
final Branch branch2 = branchDAO.read(40002);
final List<Branch> list2 = new ArrayList<Branch>();
list2.add(branch2);
branchDAO.deleteAll(list2);
fail("Foreign key constraint test failed.");
}
/** Case 4: Expected behavior. */
@Test
public void testDeleteAllCase4()
{
final int count = super.getRecordCount(super.getBranchTableName());
final List<Branch> list3n4 = new ArrayList<Branch>();
list3n4.add(branchDAO.read(ID_NOLEAF_BRANCH));
branchDAO.deleteAll(list3n4);
assertEquals("Count decrease check failed.", count - 1, super.getRecordCount(super.getBranchTableName()));
assertNull("Read deleted record check failed.", super.readBranch(ID_NOLEAF_BRANCH));
}
/** Case 5: Delete already deleted/missing object. */
@Test(expected = JournalException.class)
public void testDeleteAllCase5()
{
final List<Branch> list3n4 = new ArrayList<Branch>();
list3n4.add(branchDAO.read(ID_NOLEAF_BRANCH));
branchDAO.deleteAll(list3n4);
branchDAO.deleteAll(list3n4);
fail("Expected exception thrown check failed.");
}
/**
* Test method for {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#readAll0()}.
*
* Case 1: Read all.
*/
@Test
public void testReadAll1()
{
final List<Branch> list = testBaseSpringHibernateDAOBranchImpl.readAll0();
assertEquals("List size did not match", getRecordCount(super.getBranchTableName()), list.size());
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#readAll0(java.lang.String, java.lang.Object)}
* .
*
* Case 1: Null property name.
*
*/
@Test(expected = JournalException.class)
public void testReadAll0StringObject1()
{
testBaseSpringHibernateDAOBranchImpl.readAll0(null, null);
fail("Expected exception throw check failed.");
}
/** Case 2: Invalid property name. */
@Test(expected = JournalException.class)
public void testReadAll0StringObject2()
{
testBaseSpringHibernateDAOBranchImpl.readAll0("Invalid", null);
fail("Expected exception on invalid property check failed.");
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#delete(java.lang.Object)}.
*
* Case 1: Null argument.
*/
@Test(expected = JournalException.class)
public void testDelete1()
{
testBaseSpringHibernateDAOBranchImpl.delete(null);
}
/**
* Case 2: Non existing Entry.
*
* @exception JournalException Expected thrown exception.
*/
@Test(expected = JournalException.class)
public void testDelete2()
{
testBaseSpringHibernateDAOBranchImpl.delete(new Entry("Test", "Test", null));
fail("Case 2: Non existing Entry.");
}
/**
* Case 3.1: Expected null read after delete. <br/>
* Case 3.2: Expected record count after delete.
*/
@Test
public void testDelete3()
{
for (int i = 0; i < COUNT_TEST_ENTRY; i++) {
final Entry entry = entryDAO.read(ID_1ST_TST_BRANCH + i);
try {
testBaseSpringHibernateDAOBranchImpl.delete(entry);
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
assertNull("Case 3.1: Expected null read after delete: " + (i + 1), super.readEntry(entry.getNodeId()));
assertEquals("Case 3.2: Expected record count after delete: " + (i + 1), COUNT_TEST_ENTRY - 1 - i,
super.getRecordCount(super.getEntryTableName()));
}
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#readObject(java.util.Map)}.
*
* Case 1: Null argument.
*/
@Test(expected = JournalException.class)
public void testReadObjectMapOfStringObject1()
{
testBaseSpringHibernateDAOBranchImpl.readObject(null);
fail("Case 1: Null argument.");
}
/**
* Case 2.1: Multiple result on empty Map criteria. Case 2.2: Correct exception thrown.
*/
@Test
public void testReadObjectMapOfStringObject2()
{
try {
testBaseSpringHibernateDAOBranchImpl.readObject(new HashMap<String, Object>());
fail("Case 2.1: Empty criteria Map.");
} catch (final JournalException iae) {
assertTrue("Case 2.2: Correct exception thrown.", iae.getCause() instanceof NonUniqueResultException);
}
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#readObject(java.lang.String, java.util.Map)}
* .
*
* Case 1: Null named query argument.
*/
@Test(expected = JournalException.class)
public void testReadObjectStringMapOfStringObject1()
{
testBaseSpringHibernateDAOBranchImpl.readObject(null, new HashMap<String, Object>());
fail("Case 1: Null named query argument.");
}
/** Case 2: Non-existing named query. */
@Test(expected = JournalException.class)
public void testReadObjectStringMapOfStringObject2()
{
testBaseSpringHibernateDAOBranchImpl.readObject("Not exist", new HashMap<String, Object>());
fail("Case 2: Non-existing named query. ");
}
/** Case 3: Null map argument. */
@Test(expected = JournalException.class)
public void testReadObjectStringMapOfStringObject3()
{
testBaseSpringHibernateDAOBranchImpl.readObject(DaoConstant.NamedQuery.BYUSERNAME, (Map<String, Object>) null);
fail("Case 3: Null map argument.");
}
/** Case 4: Invalid property in Map parameter. */
@Test(expected = JournalException.class)
public void testReadObjectStringMapOfStringObject4()
{
final Map<String, Object> param4 = new HashMap<String, Object>();
param4.put("invalid", Integer.valueOf(TEST_INT));
testBaseSpringHibernateDAOBranchImpl.readObject(DaoConstant.NamedQuery.BYUSERNAME, param4);
fail("Case 4: Invalid property in Map parameter.");
}
/** Case 5: Null value in Map argument. */
@Test
public void testReadObjectStringMapOfStringObject5()
{
final Map<String, Object> param5 = new HashMap<String, Object>();
param5.put(ATTR_USERNAME, null);
assertNull("Case 5: Null value in Map argument. ",
testBaseSpringHibernateDAOBranchImpl.readObject(DaoConstant.NamedQuery.BYUSERNAME, param5));
}
/** Case 6: Mistyped param. */
@Test(expected = JournalException.class)
public void testReadObjectStringMapOfStringObject6()
{
final Map<String, Object> param6 = new HashMap<String, Object>();
param6.put(ATTR_USERNAME, new HashMap<String, Object>());
testBaseSpringHibernateDAOBranchImpl.readObject(DaoConstant.NamedQuery.BYUSERNAME, param6);
fail("Case 6: Mistyped param.");
}
/** Case 7: Unmatched. */
@Test
public void testReadObjectStringMapOfStringObject7()
{
final Map<String, Object> param7 = new HashMap<String, Object>();
param7.put(ATTR_USERNAME, "not found");
assertNull("Case 7: Unmatched.",
testBaseSpringHibernateDAOBranchImpl.readObject(DaoConstant.NamedQuery.BYUSERNAME, param7));
}
/** Case 8: Matched. */
@Test
public void testReadObjectStringMapOfStringObject8()
{
final Map<String, Object> param8 = new HashMap<String, Object>();
param8.put(ATTR_USERNAME, JournalTestCommon.TEST_USERNAME);
assertNotNull("Case 8: Matched.",
testBaseSpringHibernateDAOBranchImpl.readObject(DaoConstant.NamedQuery.BYUSERNAME, param8));
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#readObject(java.lang.String, java.lang.Object)}
* .
*
* Case 1: Null property argument.
*/
@Test(expected = JournalException.class)
public void testReadObjectStringObject1()
{
testBaseSpringHibernateDAOBranchImpl.readObject((String) null, (Object) null);
fail("Case 1: Null property argument.");
}
/** Case 2: Illegal property. */
@Test(expected = JournalException.class)
public void testReadObjectStringObject2()
{
testBaseSpringHibernateDAOBranchImpl.readObject("wrong", (Object) null);
fail("Case 2: Illegal property.");
}
/** Case 3: Mistyped property value. */
@Test(expected = JournalException.class)
public void testReadObjectStringObject3()
{
testBaseSpringHibernateDAOBranchImpl.readObject(Entry.ID, "Invalid Type");
fail("Case 3: Mistyped property value.");
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#readObjectByParent(java.lang.String, java.lang.String, java.lang.Object, java.lang.String, java.lang.Object)}
* .
*
* Case 1: Null parent bean property.
*/
@Test(expected = JournalException.class)
public void testReadObjectByParent1()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent(null, "", "", "", "");
fail("Case 1: Null parent bean property.");
}
/** Case 2: Null parent property name. */
@Test(expected = JournalException.class)
public void testReadObjectByParent2()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent("", null, "", "", "");
fail("Case 2: Null parent property name.");
}
/** Case 3: Null bean property name. */
@Test(expected = JournalException.class)
public void testReadObjectByParent3()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent(ATTR_BRANCH, ATTR_BRANCH_ID, ID_1ST_TST_BRANCH, null,
"");
fail("Case 3: Null bean property name.");
}
/** Case 4: Illegal parent bean property name. */
@Test(expected = JournalException.class)
public void testReadObjectByParent4()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent("branch666", ATTR_BRANCH_ID, ID_NON_EXISTENT,
ATTR_ENTRY_TITLE, TEST_TITLE);
fail("Case 4: Illegal parent bean property name.");
}
/** Case 5: Illegal parent bean property. */
@Test(expected = JournalException.class)
public void testReadObjectByParent5()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent(ATTR_BRANCH, "branchId666", ID_NON_EXISTENT,
ATTR_ENTRY_TITLE, TEST_TITLE);
fail("Case 5: Illegal parent bean property.");
}
/** Case 6: Illegal bean property. */
@Test(expected = JournalException.class)
public void testReadObjectByParent6()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent(ATTR_BRANCH, ATTR_BRANCH_ID, ID_NON_EXISTENT,
"title666", TEST_TITLE);
fail("Case 6: Illegal bean property.");
}
/** Case 7: Mistyped parent property value. */
@Test(expected = JournalException.class)
public void testReadObjectByParent7()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent(ATTR_BRANCH, ATTR_BRANCH_ID, "Wrong type",
ATTR_ENTRY_TITLE, TEST_TITLE);
fail("Case 7: Mistyped parent property value.");
}
/** Case 8: Mistyped bean property value. */
@Test(expected = JournalException.class)
public void testReadObjectByParent8()
{
testBaseSpringHibernateDAOBranchImpl.readObjectByParent(ATTR_BRANCH, ATTR_BRANCH_ID, ID_NON_EXISTENT,
ATTR_ENTRY_TITLE, Integer.valueOf(TEST_INT));
fail("Case 8: Mistyped bean property value.");
}
/**
* Test method for
* {@link com.philip.journal.core.dao.spring.hibernate.BaseDAOSpringHibernate#save(java.lang.Object)}.
*
* Case 1: Null argument.
*/
@Test(expected = JournalException.class)
public void testSaveCase1()
{
testBaseSpringHibernateDAOBranchImpl.save(null);
}
/** Case 2: Not null constraint. */
@Test(expected = JournalException.class)
public void testSaveCase2()
{
testBaseSpringHibernateDAOBranchImpl.save(new Branch());
}
/** Case 3: Update with non-existent ID. */
@Test(expected = JournalException.class)
public void testSaveCase3()
{
final Branch branch = new Branch();
branch.setName("Test Name");
branch.setBranchId(ID_NON_EXISTENT);
testBaseSpringHibernateDAOBranchImpl.save(branch);
}
/** Case 4: Expected insert. */
@Test
public void testSaveCase4()
{
final Branch branch = new Branch();
final int origCount = super.getRecordCount(super.getBranchTableName());
final String branchName = TEST_TITLE;
branch.setName(branchName);
try {
testBaseSpringHibernateDAOBranchImpl.save(branch);
} catch (final JournalException e) {
fail("Insert test failed.");
}
assertEquals("Record count increase failed.", origCount + 1, super.getRecordCount(super.getBranchTableName()));
final Map<String, Object> saved = super.readBranch(branch.getBranchId());
assertEquals("Read saved object failed.", branchName, saved.get(ATTR_ENTRY_NAME));
}
/** Case 5: Expected update. */
@Test
public final void testSaveCase5()
{
final int origCount = super.getRecordCount(super.getBranchTableName());
final Branch branch = new Branch();
System.out.println(1);
branch.setName(TEST_TITLE);
final long branchId = 40003L;
branch.setBranchId(branchId);
try {
testBaseSpringHibernateDAOBranchImpl.save(branch);
} catch (final JournalException e) {
fail("Save failed with exception.");
}
assertEquals("Failed on record count check.", origCount, super.getRecordCount(super.getBranchTableName()));
final Map<String, Object> saved = super.readBranch(branchId);
assertEquals("Failed on data integrity.", TEST_TITLE, saved.get(ATTR_ENTRY_NAME));
}
@Override
protected final BaseDAOSpringHibernate<Branch> getTargetDAOImpl()
{
return testBaseSpringHibernateDAOBranchImpl;
}
}