package com.vst.service;
import java.util.List;
import java.util.ArrayList;
import com.vst.service.BaseManagerTestCase;
import com.vst.dao.QuestionTypeDao;
import com.vst.model.QuestionType;
import com.vst.service.impl.QuestionTypeManagerImpl;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
public class QuestionTypeManagerTest extends BaseManagerTestCase {
private final String questionTypeId = "1";
private QuestionTypeManagerImpl questionTypeManager = new QuestionTypeManagerImpl();
private Mock questionTypeDao = null;
protected void setUp() throws Exception {
super.setUp();
questionTypeDao = new Mock(QuestionTypeDao.class);
questionTypeManager.setQuestionTypeDao((QuestionTypeDao) questionTypeDao.proxy());
}
protected void tearDown() throws Exception {
super.tearDown();
questionTypeManager = null;
}
public void testGetQuestionTypes() throws Exception {
List results = new ArrayList();
QuestionType questionType = new QuestionType();
results.add(questionType);
// set expected behavior on dao
questionTypeDao.expects(once()).method("getQuestionTypes")
.will(returnValue(results));
List questionTypes = questionTypeManager.getQuestionTypes(null);
assertTrue(questionTypes.size() == 1);
questionTypeDao.verify();
}
public void testGetQuestionType() throws Exception {
// set expected behavior on dao
questionTypeDao.expects(once()).method("getQuestionType")
.will(returnValue(new QuestionType()));
QuestionType questionType = questionTypeManager.getQuestionType(questionTypeId);
assertTrue(questionType != null);
questionTypeDao.verify();
}
public void testSaveQuestionType() throws Exception {
QuestionType questionType = new QuestionType();
// set expected behavior on dao
questionTypeDao.expects(once()).method("saveQuestionType")
.with(same(questionType)).isVoid();
questionTypeManager.saveQuestionType(questionType);
questionTypeDao.verify();
}
public void testAddAndRemoveQuestionType() throws Exception {
QuestionType questionType = new QuestionType();
// set required fields
// set expected behavior on dao
questionTypeDao.expects(once()).method("saveQuestionType")
.with(same(questionType)).isVoid();
questionTypeManager.saveQuestionType(questionType);
questionTypeDao.verify();
// reset expectations
questionTypeDao.reset();
questionTypeDao.expects(once()).method("removeQuestionType").with(eq(new Long(questionTypeId)));
questionTypeManager.removeQuestionType(questionTypeId);
questionTypeDao.verify();
// reset expectations
questionTypeDao.reset();
// remove
Exception ex = new ObjectRetrievalFailureException(QuestionType.class, questionType.getQuestionTypeId());
questionTypeDao.expects(once()).method("removeQuestionType").isVoid();
questionTypeDao.expects(once()).method("getQuestionType").will(throwException(ex));
questionTypeManager.removeQuestionType(questionTypeId);
try {
questionTypeManager.getQuestionType(questionTypeId);
fail("QuestionType with identifier '" + questionTypeId + "' found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
}
questionTypeDao.verify();
}
}