package com.vst.service;
import java.util.List;
import java.util.ArrayList;
import com.vst.service.BaseManagerTestCase;
import com.vst.dao.DefectCategoryDao;
import com.vst.model.DefectCategory;
import com.vst.service.impl.DefectCategoryManagerImpl;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
public class DefectCategoryManagerTest extends BaseManagerTestCase {
private final String defectCategoryId = "1";
private DefectCategoryManagerImpl defectCategoryManager = new DefectCategoryManagerImpl();
private Mock defectCategoryDao = null;
protected void setUp() throws Exception {
super.setUp();
defectCategoryDao = new Mock(DefectCategoryDao.class);
defectCategoryManager.setDefectCategoryDao((DefectCategoryDao) defectCategoryDao.proxy());
}
protected void tearDown() throws Exception {
super.tearDown();
defectCategoryManager = null;
}
public void testGetDefectCategorys() throws Exception {
List results = new ArrayList();
DefectCategory defectCategory = new DefectCategory();
results.add(defectCategory);
// set expected behavior on dao
defectCategoryDao.expects(once()).method("getDefectCategorys")
.will(returnValue(results));
List defectCategorys = defectCategoryManager.getDefectCategorys(null);
assertTrue(defectCategorys.size() == 1);
defectCategoryDao.verify();
}
public void testGetDefectCategory() throws Exception {
// set expected behavior on dao
defectCategoryDao.expects(once()).method("getDefectCategory")
.will(returnValue(new DefectCategory()));
DefectCategory defectCategory = defectCategoryManager.getDefectCategory(defectCategoryId);
assertTrue(defectCategory != null);
defectCategoryDao.verify();
}
public void testSaveDefectCategory() throws Exception {
DefectCategory defectCategory = new DefectCategory();
// set expected behavior on dao
defectCategoryDao.expects(once()).method("saveDefectCategory")
.with(same(defectCategory)).isVoid();
defectCategoryManager.saveDefectCategory(defectCategory);
defectCategoryDao.verify();
}
public void testAddAndRemoveDefectCategory() throws Exception {
DefectCategory defectCategory = new DefectCategory();
// set required fields
// set expected behavior on dao
defectCategoryDao.expects(once()).method("saveDefectCategory")
.with(same(defectCategory)).isVoid();
defectCategoryManager.saveDefectCategory(defectCategory);
defectCategoryDao.verify();
// reset expectations
defectCategoryDao.reset();
defectCategoryDao.expects(once()).method("removeDefectCategory").with(eq(new Long(defectCategoryId)));
defectCategoryManager.removeDefectCategory(defectCategoryId);
defectCategoryDao.verify();
// reset expectations
defectCategoryDao.reset();
// remove
Exception ex = new ObjectRetrievalFailureException(DefectCategory.class, defectCategory.getDefectCategoryId());
defectCategoryDao.expects(once()).method("removeDefectCategory").isVoid();
defectCategoryDao.expects(once()).method("getDefectCategory").will(throwException(ex));
defectCategoryManager.removeDefectCategory(defectCategoryId);
try {
defectCategoryManager.getDefectCategory(defectCategoryId);
fail("DefectCategory with identifier '" + defectCategoryId + "' found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
}
defectCategoryDao.verify();
}
}