Package com.philip.journal.home.dao

Source Code of com.philip.journal.home.dao.BranchDAOImplTest

/**
* @Created Oct 5, 2010 11:01:12 AM
* @author cry30
*/
package com.philip.journal.home.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.philip.journal.core.Constant;
import com.philip.journal.core.dao.BaseJournalTestDAO;
import com.philip.journal.core.exception.JournalException;
import com.philip.journal.home.bean.Branch;

/**
* Requires UserDAOImplTest.
*/
public class BranchDAOImplTest extends BaseJournalTestDAO {

    /** IOC Wired test DAO instance. */
    @Autowired
    private transient BranchDAO branchDAO;

    /** Branch entity attribute name. */
    static final String ATTR_NAME = "name";

    //Test Artifacts.
    /** Test Artifact. New Branch Name. */
    static final String TEST_NEW_BRANCHNAME = "NewBranchName" + System.currentTimeMillis();

    /**
     * Test method for {@link com.philip.journal.home.dao.BranchDAOImpl#readAllByParent(long)}.
     *
     * Case 1: Empty list.
     */
    @Test
    public void testReadAllByParentLong()
    {
        Assert.assertNotNull("Case 1: Empty list failed.", branchDAO.readAllByParent(-1));
    }

    /** Case 2: Not existing ID. */
    @Test
    public void testReadAllByParentLong2()
    {
        Assert.assertEquals("Case 2: Not existing ID failed.", 0, branchDAO.readAllByParent(-1).size());
    }

    /** Case 3: Read Root branches. */
    @Test
    public void testReadAllByParentLong3()
    {
        Assert.assertEquals("Read Root branches. failed.", ROOT_BRANCH_COUNT,
                branchDAO.readAllByParent(Constant.ROOT_ID).size());
    }

    /** Case 4: Read from normal branches (non-root). */
    @Test
    public void testReadAllByParentLong4()
    {
        Assert.assertEquals("Normal branch read test failed.", CNT_BRANCH_LEAVES,
                branchDAO.readAllByParent(ID_LEAFED_BRANCH).size());
    }

    /**
     * Test method for {@link com.philip.journal.home.dao.BranchDAOImpl#save(com.philip.journal.home.bean.Branch)}.
     *
     * Case 1: Null argument.
     */
    @Test(expected = JournalException.class)
    public void testSaveBranch1()
    {
        branchDAO.save(null);
    }

    /**
     * Case 2: Not null constraint.
     */
    @Test(expected = JournalException.class)
    public void testSaveBranch2()
    {
        branchDAO.save(new Branch());
    }

    /**
     * Case 3: Non existing branch id (update).
     */
    @Test(expected = JournalException.class)
    public void testSaveBranch3()
    {
        final Branch branch2 = new Branch();
        branch2.setBranchId(ID_NON_EXISTENT);
        branchDAO.save(branch2);
        Assert.fail("Case 2: Non existing branch id (update).");
    }

    /**
     * Case 4.1: Parent not null: Count increase. <br/>
     * Case 4.2: Parent not null: Read inserted. <br/>
     * Case 4.3: Parent not null: Branch ID set.
     */
    @Test
    public void testSaveBranch4()
    {
        final int count3 = super.getRecordCount(super.getBranchTableName());
        final Branch root3 = branchDAO.read(Constant.ROOT_ID);
        final Branch branch3 = new Branch("Test Insert 3", root3);
        try {
            branchDAO.save(branch3);
            Assert.assertEquals("Case 4.1: Parent not null: Count increase.", count3 + 1,
                    super.getRecordCount(super.getBranchTableName()));
            Assert.assertNotNull("Case 4.2: Parent not null: Read inserted.", branchDAO.read(branch3.getBranchId()));
            Assert.assertNotSame("Case 4.3: Parent not null: Branch ID set.", 0, branch3.getBranchId());
        } catch (final JournalException e) {
            getCommon().failUnexpectedException(e);
        }
    }

    /**
     * Case 5.1: Insert with null parent, count increase. <br/>
     * Case 5.2: Insert with null parent, read inserted. <br/>
     * Case 5.3: Insert with null parent, Branch ID generated.
     */
    @Test
    public void testSaveBranch5()
    {
        final int count4 = super.getRecordCount(super.getBranchTableName());
        final Branch branch4 = new Branch("Test Insert 4", null);
        try {
            branchDAO.save(branch4);
            Assert.assertEquals("Case 5.1: Insert with null parent, count increase.", count4 + 1,
                    super.getRecordCount(super.getBranchTableName()));
            Assert.assertNotNull("Case 5.2: Insert with null parent, read inserted.",
                    branchDAO.read(branch4.getBranchId()));
            Assert.assertNotSame("Case 5.3: Insert with null parent, Branch ID generated.", 0, branch4.getBranchId());
        } catch (final JournalException e) {
            getCommon().failUnexpectedException(e);
        }
    }

    /**
     * Case 6.1: Update, Branch name check. <br/>
     * Case 6.2: Update, increase record count. <br/>
     * Case 6.3: Update, Branch equality. <br/>
     */
    @Test
    public void testSaveBranch6()
    {
        final int count5 = super.getRecordCount(super.getBranchTableName());
        final Branch branch5 = branchDAO.read(ID_1ST_TST_BRANCH);
        final String branchName5 = branch5.getName();
        Assert.assertFalse("Case 6.1: Update, Branch equality.", branchName5.equals(TEST_NEW_BRANCHNAME));
        branch5.setName(TEST_NEW_BRANCHNAME);
        try {
            branchDAO.save(branch5);
            Assert.assertEquals("Case 6.2: Update, increase record count.", count5,
                    super.getRecordCount(super.getBranchTableName()));
            final Branch saved5 = branchDAO.read(ID_1ST_TST_BRANCH);
            Assert.assertEquals("Case 6.3: Update, Branch equality.", TEST_NEW_BRANCHNAME, saved5.getName());
        } catch (final JournalException e) {
            getCommon().failUnexpectedException(e);
        }
    }

    /**
     * Test method for {@link com.philip.journal.home.dao.BranchDAOImpl#readByProperty(java.util.Map)}.
     */
    //public void testReadByProperty() {}//To simple to break.

    /**
     * Test method for {@link com.philip.journal.home.dao.BranchDAOImpl#deleteAll(java.util.List)}. Requires
     * {@link com.philip.journal.home.dao.BranchDAOImpl#delete(com.philip.journal.home.bean.Branch)}
     *
     * Case 1: Null argument.
     */
    @Test(expected = JournalException.class)
    public void testDeleteAll1()
    {
        branchDAO.deleteAll(null);
        Assert.fail("Case 1: Null argument.");
    }

    /** Case 2: Null List element. */
    @Test(expected = JournalException.class)
    public void testDeleteAll2()
    {
        final List<Branch> list1 = new ArrayList<Branch>();
        list1.add(null);
        branchDAO.deleteAll(list1);
        Assert.fail("Case 2: Null List element.");
    }

    /**
     * Case 3: Foreign key constraint.
     *
     * @exception JournalException expected exception
     */
    @Test(expected = JournalException.class)
    public void testDeleteAll3()
    {
        //final int origCount = super.getRecordCount(super.branchTableName);
        final Branch branch2 = branchDAO.read(40002);
        final List<Branch> list2 = new ArrayList<Branch>();
        list2.add(branch2);
        branchDAO.deleteAll(list2);
        Assert.fail("Case 3: Foreignkey constrained.");
        //VERIFY: Assert.assertEquals(test[2], origCount, super.getRecordCount(super.branchTableName));
    }

    /**
     * Case 4.1: Expected count reduction. <br/>
     * Case 4.2: Expected null read on deleted record.
     */
    @Test
    public void testDeleteAll4()
    {
        final int count = super.getRecordCount(super.getBranchTableName());
        final List<Branch> list3n4 = new ArrayList<Branch>();
        list3n4.add(branchDAO.read(ID_NOLEAF_BRANCH));
        try {
            branchDAO.deleteAll(list3n4);
            Assert.assertEquals("Case 4.1: Expected count reduction.", count - 1,
                    super.getRecordCount(super.getBranchTableName()));
            Assert.assertNull("Case 4.2: Expected null read on deleted record.", super.readBranch(ID_NOLEAF_BRANCH));
        } catch (final JournalException e) {
            getCommon().failUnexpectedException(e);
        }
    }

    /**
     * Case 5: Delete already deleted object.
     */
    @Test(expected = JournalException.class)
    public void testDeleteAll5()
    {
        final List<Branch> list3n4 = new ArrayList<Branch>();
        list3n4.add(branchDAO.read(ID_NOLEAF_BRANCH));
        branchDAO.deleteAll(list3n4);
        branchDAO.deleteAll(list3n4);
        Assert.fail("Case 5: Delete already deleted object.");
    }

    /**
     * Test method for
     * {@link com.philip.journal.home.dao.BranchDAOImpl#readAllByProperty(java.lang.String, java.lang.Object)} .
     *
     * Case 1: Null property.
     */
    @Test(expected = JournalException.class)
    public void testReadAllByProperty1()
    {
        branchDAO.readAllByProperty(null, null);
        Assert.fail("Case 1: Null property.");
    }

    /** Case 2: Invalid property. */
    @Test(expected = JournalException.class)
    public void testReadAllByProperty2()
    {
        branchDAO.readAllByProperty("1invalid", null);
        Assert.fail("Case 2: Invalid property.");
    }

    /** Case 3: Invalid property value type. */
    @Test(expected = IllegalArgumentException.class)
    public void testReadAllByProperty3()
    {
        branchDAO.readAllByProperty(ATTR_NAME, new Date());
        Assert.fail("Case 3: Invalid property value type.");
    }

    /**
     * Case 4.1: Property-value match not found, non-null. <br/>
     * Case 4.2: Property-value match not found, zero sized.
     */
    @Test
    public void testReadAllByProperty4()
    {
        final List<Branch> list3 = branchDAO.readAllByProperty(ATTR_NAME, "@!#!%@");
        Assert.assertNotNull("Case 4.1: Property-value match not found, non-null.", list3);
        Assert.assertEquals("Case 4.2: Property-value match not found, zero sized.", 0, list3.size());

    }

    /** Case 5: Expected property value matched. */
    @Test
    public void testReadAllByProperty5()
    {
        final List<Branch> list4 = branchDAO.readAllByProperty(ATTR_NAME, "Test Branch 1");
        Assert.assertEquals("Case 5: Expected property value matched.", 1, list4.size());
    }

    /** Case 6: Property value partially matched. */
    @Test
    public void testReadAllByProperty6()
    {
        final List<Branch> list5 = branchDAO.readAllByProperty(ATTR_NAME, "Test Branch");
        Assert.assertEquals("Case 6: Property value partially matched.", 0, list5.size());
    }
    /**
     * Test method for {@link com.philip.journal.home.dao.BranchDAOImpl#readAll()}.
     *
     * Case 1: Not null List returned.
     */
    @Test
    public void testReadAll1()
    {
        Assert.assertNotNull("Case 1: Not null List returned.", branchDAO.readAll());
    }

    /** Case 2: Expected. */
    @Test
    public void testReadAll2()
    {
        final int count = super.getRecordCount(getBranchTableName());
        Assert.assertEquals("Case 2: Expected.", count, branchDAO.readAll().size());
    }

    /**
     * Test method for {@link com.philip.journal.home.dao.BranchDAOImpl#read(long)}.
     *
     * Case 1: Branch ID non existent.
     */
    @Test
    public void testReadLong1()
    {
        Assert.assertNull("Case 1: Branch ID non existent.", branchDAO.read(ID_NON_EXISTENT));
    }

    /** Case 2: Expected. */
    @Test
    public void testReadLong2()
    {
        Assert.assertNotNull("Read Existent Branch test failed.", branchDAO.read(ID_1ST_TST_BRANCH));
    }

    /**
     * Test method for {@link com.philip.journal.home.dao.BranchDAOImpl#readByName(java.lang.String, long)}.
     *
     * Case 1: Parent ID non existent.
     */
    @Test
    public void testReadByName1()
    {
        Assert.assertNull("Case 1: Parent ID non existent.", branchDAO.readByName("Test Branch 1", ID_NON_EXISTENT));
    }

    /** Case 2: Null Branch name. */
    @Test
    public void testReadByName2()
    {
        Assert.assertNull("Case 2: Null Branch name.", branchDAO.readByName(null, 0));
    }

    /** Case 3: Expected. */
    @Test
    public void testReadByName3()
    {
        Assert.assertNotNull("Case 2: Expected.", branchDAO.readByName("Test Branch 1", Constant.ROOT_ID));
    }

    @Override
    protected Object getTargetDAOImpl()
    {
        return branchDAO;
    }
}
TOP

Related Classes of com.philip.journal.home.dao.BranchDAOImplTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.