use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class AgentDaoTest method shouldCacheCookieForAgent.
@Test
public void shouldCacheCookieForAgent() throws Exception {
AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
agentDao.associateCookie(agentIdentifier, "cookie");
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Agent agent = (Agent) session.createQuery("from Agent where uuid = 'uuid'").uniqueResult();
agent.update("updated_cookie", agentIdentifier.getHostName(), agentIdentifier.getIpAddress());
session.update(agent);
return null;
}
});
Agent agent = getAgentByUuid(agentIdentifier);
assertThat(agent.getCookie(), is("updated_cookie"));
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
goCache.clear();
assertThat(agentDao.cookieFor(agentIdentifier), is("updated_cookie"));
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class AgentDaoTest method shouldNotClearCacheIfTransactionFails.
@Test
public void shouldNotClearCacheIfTransactionFails() throws Exception {
HibernateTemplate originalTemplate = agentDao.getHibernateTemplate();
AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
agentDao.associateCookie(agentIdentifier, "cookie");
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Agent agent = (Agent) session.createQuery("from Agent where uuid = 'uuid'").uniqueResult();
agent.update("updated_cookie", agentIdentifier.getHostName(), agentIdentifier.getIpAddress());
session.update(agent);
return null;
}
});
Agent agent = getAgentByUuid(agentIdentifier);
assertThat(agent.getCookie(), is("updated_cookie"));
agentDao.setHibernateTemplate(mockHibernateTemplate);
doThrow(new RuntimeException("holy smoke")).when(mockHibernateTemplate).saveOrUpdate(any(Agent.class));
try {
agentDao.associateCookie(agentIdentifier, "cookie");
fail("should have propagated saveOrUpdate exception");
} catch (Exception e) {
assertThat(e.getMessage(), is("holy smoke"));
}
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
agentDao.setHibernateTemplate(originalTemplate);
}
Aggregations