package com.vst.service;
import java.util.List;
import java.util.ArrayList;
import com.vst.service.BaseManagerTestCase;
import com.vst.dao.StrengthDao;
import com.vst.model.Strength;
import com.vst.service.impl.StrengthManagerImpl;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
public class StrengthManagerTest extends BaseManagerTestCase {
private final String strengthId = "1";
private StrengthManagerImpl strengthManager = new StrengthManagerImpl();
private Mock strengthDao = null;
protected void setUp() throws Exception {
super.setUp();
strengthDao = new Mock(StrengthDao.class);
strengthManager.setStrengthDao((StrengthDao) strengthDao.proxy());
}
protected void tearDown() throws Exception {
super.tearDown();
strengthManager = null;
}
public void testGetStrengths() throws Exception {
List results = new ArrayList();
Strength strength = new Strength();
results.add(strength);
// set expected behavior on dao
strengthDao.expects(once()).method("getStrengths")
.will(returnValue(results));
List strengths = strengthManager.getStrengths(null);
assertTrue(strengths.size() == 1);
strengthDao.verify();
}
public void testGetStrength() throws Exception {
// set expected behavior on dao
strengthDao.expects(once()).method("getStrength")
.will(returnValue(new Strength()));
Strength strength = strengthManager.getStrength(strengthId);
assertTrue(strength != null);
strengthDao.verify();
}
public void testSaveStrength() throws Exception {
Strength strength = new Strength();
// set expected behavior on dao
strengthDao.expects(once()).method("saveStrength")
.with(same(strength)).isVoid();
strengthManager.saveStrength(strength);
strengthDao.verify();
}
public void testAddAndRemoveStrength() throws Exception {
Strength strength = new Strength();
// set required fields
// set expected behavior on dao
strengthDao.expects(once()).method("saveStrength")
.with(same(strength)).isVoid();
strengthManager.saveStrength(strength);
strengthDao.verify();
// reset expectations
strengthDao.reset();
strengthDao.expects(once()).method("removeStrength").with(eq(new Long(strengthId)));
strengthManager.removeStrength(strengthId);
strengthDao.verify();
// reset expectations
strengthDao.reset();
// remove
Exception ex = new ObjectRetrievalFailureException(Strength.class, strength.getPointId());
strengthDao.expects(once()).method("removeStrength").isVoid();
strengthDao.expects(once()).method("getStrength").will(throwException(ex));
strengthManager.removeStrength(strengthId);
try {
strengthManager.getStrength(strengthId);
fail("Strength with identifier '" + strengthId + "' found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
}
strengthDao.verify();
}
}