/*****************************************************************************
* Company OPEN CASCADE
* Application SIMAN
* File $Id$
* Creation date 12 Oct 2012
* @author $Author$
* @version $Revision$
* Generated by Siman Generator on $date
*****************************************************************************/
package test.splat.dao;
import java.util.Date;
import org.splat.dal.bo.kernel.User;
import org.splat.dal.bo.som.${EntityClass};
import org.splat.dal.dao.som.${EntityClass}DAO;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MissedPropertyException;
import org.splat.kernel.MultiplyDefinedException;
import org.splat.log.AppLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.testng.Assert;
import org.testng.annotations.Test;
import test.splat.common.BaseTest;
/**
* Test class for ${EntityClass}DAO.
*
* @author Roman Kozlov (RKV)
*
*/
public class Test${EntityClass}DAO extends BaseTest {
/**
* Logger for the class.
*/
private static final AppLogger LOG = AppLogger
.getLogger(Test${EntityClass}DAO.class);
/**
* The tested ${EntityClass}DAO. Later injected by Spring.
*/
@Autowired
@Qualifier("${DAOBean}")
private transient ${EntityClass}DAO _${DAOBean};
/**
* Test creation of an object.
* Description :
* Create an object.
* Action :
* 1. call DAO's create method for a good transient object.
* 2. call DAO's create method for an object with non-zero id.
* Test data :
* no input parameters
* no input parameters
*
* Outcome results:
*
*
* - Object is created in the database successfully
*
* - Exception is thrown
*
*
*
*
* @throws InvalidPropertyException
* if an invalid property is used when creating objects
* @throws MultiplyDefinedException
* when trying to create an object with already existing id
* @throws MissedPropertyException
* if a mandatory property is not defined for an object to be created
*
*/
@Test
public void testCreate() throws InvalidPropertyException,
MissedPropertyException, MultiplyDefinedException {
LOG.debug(">>>>> BEGIN testCreate()");
${EntityClass} anObject = get${EntityClass}();
// Call DAO's create method for a good transient object.
Long id = _${DAOBean}.create(anObject);
Assert.assertNotNull(id,
"Create method returns null instead of a new id.");
Assert.assertTrue(id > 0, "The new id is not a positive number.");
${EntityClass} anObjectFound = getHibernateTemplate().get(
${EntityClass}.class, id);
compareObjects(anObjectFound, anObject);
// Call DAO's create method for an object with non-zero id.
// TODO: Fill the object's properties
${EntityClass} aBadObj = new ${EntityClass}(
(new ${EntityClass}.Properties()));
aBadObj.setIndex(anObjectFound.getIndex());
try {
_${DAOBean}.create(aBadObj);
getHibernateTemplate().flush();
Assert.fail("Creation with existing id must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
LOG.debug(">>>>> END testCreate()");
}
/**
* Test of getting an object.
* Description :
* Create an object and try to get it from the database.
* Action :
* 1. call DAO's read method for an existing id.
* 2. call DAO's read method for a not existing id.
* Test data :
* no input parameters
* no input parameters
*
* Outcome results:
*
*
* - Object is found in the database successfully
*
* - Exception is thrown
*
*
*
*
* @throws InvalidPropertyException
* if an invalid property is used when creating objects
* @throws MultiplyDefinedException
* when trying to create an object with already existing id
* @throws MissedPropertyException
* if a mandatory property is not defined for an object to be created
*
*/
@Test
public void testGet() throws InvalidPropertyException,
MissedPropertyException, MultiplyDefinedException {
LOG.debug(">>>>> BEGIN testGet()");
${EntityClass} anObject = get${EntityClass}();
// Call DAO's create method for a good transient object.
Long id = _${DAOBean}.create(anObject);
Assert.assertNotNull(id,
"Create method returns null instead of a new id.");
Assert.assertTrue(id > 0, "The new id is not a positive number.");
// Call DAO's get method for an existing id.
${EntityClass} anObjectFound = _${DAOBean}.get(id);
compareObjects(anObjectFound, anObject);
// Call DAO's get method for a not existing id.
try {
anObjectFound = _${DAOBean}.get(-1L);
getHibernateTemplate().flush();
Assert
.fail("Getting an object with not existing id must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
try {
anObjectFound = _${DAOBean}.get(0L);
getHibernateTemplate().flush();
Assert
.fail("Getting an object with not existing id must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
try {
anObjectFound = _${DAOBean}.get(id + 1);
getHibernateTemplate().flush();
Assert
.fail("Getting an object with not existing id must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
LOG.debug(">>>>> END testGet()");
}
/**
* Test of updating an object.
* Description :
* Create an object and try to update it with another data.
* Action :
* 1. call DAO's update method for an existing id.
* 2. call DAO's update method for a not existing id.
* 3. call DAO's update method for wrong data.
* Test data :
* no input parameters
* no input parameters
* no input parameters
*
* Outcome results:
*
*
* - Object is update in the database successfully
*
* - Exception is thrown
*
* - Exception is thrown
*
*
*
*
* @throws InvalidPropertyException
* if an invalid property is used when creating objects
* @throws MultiplyDefinedException
* when trying to create an object with already existing id
* @throws MissedPropertyException
* if a mandatory property is not defined for an object to be created
*
*/
@Test
public void testUpdate() throws InvalidPropertyException,
MissedPropertyException, MultiplyDefinedException {
LOG.debug(">>>>> BEGIN testUpdate()");
${EntityClass} anObject = get${EntityClass}();
// Call DAO's create method for a good transient object.
Long id = _${DAOBean}.create(anObject);
Assert.assertNotNull(id,
"Create method returns null instead of a new id.");
Assert.assertTrue(id > 0, "The new id is not a positive number.");
// Call DAO's update method for an existing id.
// TODO: Modify some object's properties to update it later
/* Assert
.assertTrue(anObject.getProgressState() != ProgressState.APPROVED,
"The initial state of the object should not be APPROVED.");
anObject.setProgressState(ProgressState.APPROVED);
anObject.setTitle(anObject.getTitle() + " updated");*/
_${DAOBean}.update(anObject);
// Check that the object has been updated.
${EntityClass} anObjectFound = _${DAOBean}.get(id);
compareObjects(anObjectFound, anObject);
// Call DAO's create method for an object with non-zero id.
// TODO: Fill the object's properties
${EntityClass} aBadObj = new ${EntityClass}(
(new ${EntityClass}.Properties()));
// aBadObj.setIndex(anObjectFound.getIndex());
try {
_${DAOBean}.update(aBadObj);
getHibernateTemplate().flush();
Assert.fail("Update with not existing id must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
// Call update with bad data (null title).
aBadObj.setIndex(anObjectFound.getIndex());
aBadObj.setTitle(null);
try {
_${DAOBean}.update(aBadObj);
getHibernateTemplate().flush();
Assert.fail("Update with null title must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
// Call update with bad data (null state).
aBadObj.setTitle(anObjectFound.getTitle());
aBadObj.setProgressState(null);
try {
_${DAOBean}.update(aBadObj);
getHibernateTemplate().flush();
Assert.fail("Update with null state must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
LOG.debug(">>>>> END testUpdate()");
}
/**
* Test of deleting an object.
* Description :
* Create an object and try to delete it.
* Action :
* 1. call DAO's delete method for an existing id.
* 2. call DAO's delete method for a not existing id.
* Test data :
* no input parameters
* no input parameters
*
* Outcome results:
*
*
* - Object is found in the database successfully
*
* - Exception is thrown
*
*
*
*
* @throws InvalidPropertyException
* if an invalid property is used when creating objects
* @throws MultiplyDefinedException
* when trying to create an object with already existing id
* @throws MissedPropertyException
* if a mandatory property is not defined for an object to be created
*
*/
@Test
public void testDelete() throws InvalidPropertyException,
MissedPropertyException, MultiplyDefinedException {
LOG.debug(">>>>> BEGIN testDelete()");
${EntityClass} anObject = get${EntityClass}();
// Call DAO's create method for a good transient object.
Long id = _${DAOBean}.create(anObject);
Assert.assertNotNull(id,
"Create method returns null instead of a new id.");
Assert.assertTrue(id > 0, "The new id is not a positive number.");
// Call DAO's delete method for an existing id.
_${DAOBean}.delete(anObject);
// Check that the object has been deleted.
${EntityClass} anObjectFound = _${DAOBean}.get(id);
Assert.assertNull(anObjectFound, "Deleted object must not be found.");
// Call DAO's delete method for a not existing id.
try {
_${DAOBean}.delete(anObject);
getHibernateTemplate().flush();
Assert.fail("Delete with with not existing id must be failed.");
} catch (Exception e) {
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
LOG.debug(">>>>> END testDelete()");
}
/**
* Create a transient ${EntityClass} for tests.
*
* @return a transient ${EntityClass}
* @throws InvalidPropertyException
* if an invalid property is used when creating objects
* @throws MultiplyDefinedException
* when trying to create an object with already existing id
* @throws MissedPropertyException
* if a mandatory property is not defined for an object to be created
*/
private ${EntityClass} get${EntityClass}()
throws InvalidPropertyException, MissedPropertyException,
MultiplyDefinedException {
// Create a test knowledge type
HibernateTemplate ht = getHibernateTemplate();
${EntityClass}.Properties kprops = new ${EntityClass}.Properties();
// Prepare a transient object
return new ${EntityClass}(kprops);
}
/**
* Check that given objects are equal.
*
* @param anActual
* the object to check
* @param anExpected
* the expected object
*/
private void compareObjects(${EntityClass} anActual,
${EntityClass} anExpected) {
Assert.assertNotNull(anActual,
"Created object is not found in the database.");
Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
"object author is not saved.");
Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
"object date is not saved.");
Assert
.assertEquals(anActual.getOwnerScenario(), anExpected
.getOwnerScenario(),
"object scenario is not saved.");
Assert.assertEquals(anActual.getProgressState(), anExpected
.getProgressState(), "object state is not saved.");
Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
"object title is not saved.");
Assert.assertEquals(anActual.getType(), anExpected.getType(),
"object type is not saved.");
Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
"object value is not saved.");
Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
"object index is not saved.");
}
}