package com.vst.service;
import java.util.List;
import java.util.ArrayList;
import com.vst.service.BaseManagerTestCase;
import com.vst.dao.ConstructionDefectDao;
import com.vst.model.ConstructionDefect;
import com.vst.service.impl.ConstructionDefectManagerImpl;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
public class ConstructionDefectManagerTest extends BaseManagerTestCase {
private final String constructionDefectId = "1";
private ConstructionDefectManagerImpl constructionDefectManager = new ConstructionDefectManagerImpl();
private Mock constructionDefectDao = null;
protected void setUp() throws Exception {
super.setUp();
constructionDefectDao = new Mock(ConstructionDefectDao.class);
constructionDefectManager.setConstructionDefectDao((ConstructionDefectDao) constructionDefectDao.proxy());
}
protected void tearDown() throws Exception {
super.tearDown();
constructionDefectManager = null;
}
public void testGetConstructionDefects() throws Exception {
List results = new ArrayList();
ConstructionDefect constructionDefect = new ConstructionDefect();
results.add(constructionDefect);
// set expected behavior on dao
constructionDefectDao.expects(once()).method("getConstructionDefects")
.will(returnValue(results));
List constructionDefects = constructionDefectManager.getConstructionDefects(null);
assertTrue(constructionDefects.size() == 1);
constructionDefectDao.verify();
}
public void testGetConstructionDefect() throws Exception {
// set expected behavior on dao
constructionDefectDao.expects(once()).method("getConstructionDefect")
.will(returnValue(new ConstructionDefect()));
ConstructionDefect constructionDefect = constructionDefectManager.getConstructionDefect(constructionDefectId);
assertTrue(constructionDefect != null);
constructionDefectDao.verify();
}
public void testSaveConstructionDefect() throws Exception {
ConstructionDefect constructionDefect = new ConstructionDefect();
// set expected behavior on dao
constructionDefectDao.expects(once()).method("saveConstructionDefect")
.with(same(constructionDefect)).isVoid();
constructionDefectManager.saveConstructionDefect(constructionDefect);
constructionDefectDao.verify();
}
public void testAddAndRemoveConstructionDefect() throws Exception {
ConstructionDefect constructionDefect = new ConstructionDefect();
// set required fields
// set expected behavior on dao
constructionDefectDao.expects(once()).method("saveConstructionDefect")
.with(same(constructionDefect)).isVoid();
constructionDefectManager.saveConstructionDefect(constructionDefect);
constructionDefectDao.verify();
// reset expectations
constructionDefectDao.reset();
constructionDefectDao.expects(once()).method("removeConstructionDefect").with(eq(new Long(constructionDefectId)));
constructionDefectManager.removeConstructionDefect(constructionDefectId);
constructionDefectDao.verify();
// reset expectations
constructionDefectDao.reset();
// remove
Exception ex = new ObjectRetrievalFailureException(ConstructionDefect.class, constructionDefect.getConstructionDefectId());
constructionDefectDao.expects(once()).method("removeConstructionDefect").isVoid();
constructionDefectDao.expects(once()).method("getConstructionDefect").will(throwException(ex));
constructionDefectManager.removeConstructionDefect(constructionDefectId);
try {
constructionDefectManager.getConstructionDefect(constructionDefectId);
fail("ConstructionDefect with identifier '" + constructionDefectId + "' found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
}
constructionDefectDao.verify();
}
}