package com.vst.service;
import java.util.List;
import java.util.ArrayList;
import com.vst.service.BaseManagerTestCase;
import com.vst.dao.ObjectTypeDao;
import com.vst.model.ObjectType;
import com.vst.service.impl.ObjectTypeManagerImpl;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
public class ObjectTypeManagerTest extends BaseManagerTestCase {
private final String objectTypeId = "1";
private ObjectTypeManagerImpl objectTypeManager = new ObjectTypeManagerImpl();
private Mock objectTypeDao = null;
protected void setUp() throws Exception {
super.setUp();
objectTypeDao = new Mock(ObjectTypeDao.class);
objectTypeManager.setObjectTypeDao((ObjectTypeDao) objectTypeDao.proxy());
}
protected void tearDown() throws Exception {
super.tearDown();
objectTypeManager = null;
}
public void testGetObjectTypes() throws Exception {
List results = new ArrayList();
ObjectType objectType = new ObjectType();
results.add(objectType);
// set expected behavior on dao
objectTypeDao.expects(once()).method("getObjectTypes")
.will(returnValue(results));
List objectTypes = objectTypeManager.getObjectTypes(null);
assertTrue(objectTypes.size() == 1);
objectTypeDao.verify();
}
public void testGetObjectType() throws Exception {
// set expected behavior on dao
objectTypeDao.expects(once()).method("getObjectType")
.will(returnValue(new ObjectType()));
ObjectType objectType = objectTypeManager.getObjectType(objectTypeId);
assertTrue(objectType != null);
objectTypeDao.verify();
}
public void testSaveObjectType() throws Exception {
ObjectType objectType = new ObjectType();
// set expected behavior on dao
objectTypeDao.expects(once()).method("saveObjectType")
.with(same(objectType)).isVoid();
objectTypeManager.saveObjectType(objectType);
objectTypeDao.verify();
}
public void testAddAndRemoveObjectType() throws Exception {
ObjectType objectType = new ObjectType();
// set required fields
// set expected behavior on dao
objectTypeDao.expects(once()).method("saveObjectType")
.with(same(objectType)).isVoid();
objectTypeManager.saveObjectType(objectType);
objectTypeDao.verify();
// reset expectations
objectTypeDao.reset();
objectTypeDao.expects(once()).method("removeObjectType").with(eq(new Long(objectTypeId)));
objectTypeManager.removeObjectType(objectTypeId);
objectTypeDao.verify();
// reset expectations
objectTypeDao.reset();
// remove
Exception ex = new ObjectRetrievalFailureException(ObjectType.class, objectType.getObjectTypeId());
objectTypeDao.expects(once()).method("removeObjectType").isVoid();
objectTypeDao.expects(once()).method("getObjectType").will(throwException(ex));
objectTypeManager.removeObjectType(objectTypeId);
try {
objectTypeManager.getObjectType(objectTypeId);
fail("ObjectType with identifier '" + objectTypeId + "' found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
}
objectTypeDao.verify();
}
}