use of org.springframework.orm.hibernate3.HibernateTemplate 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);
}
use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.
the class MaterialRepositoryIntegrationTest method findPipelineMaterialRevisions_shouldCacheResults.
@Test
public void findPipelineMaterialRevisions_shouldCacheResults() {
HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
repo.setHibernateTemplate(mockTemplate);
repo.findPipelineMaterialRevisions(2);
repo.findPipelineMaterialRevisions(2);
verify(mockTemplate, times(1)).find("FROM PipelineMaterialRevision WHERE pipelineId = ? ORDER BY id", 2L);
}
use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.
the class MaterialRepositoryIntegrationTest method findLatestModifications_shouldCacheResults.
@Test
public void findLatestModifications_shouldCacheResults() {
SvnMaterial material = MaterialsMother.svnMaterial();
MaterialInstance materialInstance = material.createMaterialInstance();
repo.saveOrUpdate(materialInstance);
Modification mod = ModificationsMother.oneModifiedFile("file3");
mod.setId(8);
HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
repo.setHibernateTemplate(mockTemplate);
when(mockTemplate.execute(any())).thenReturn(mod);
repo.findLatestModification(materialInstance);
Modification modification = repo.findLatestModification(materialInstance);
assertSame(mod, modification);
verify(mockTemplate, times(1)).execute(any());
}
use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.
the class MaterialRepositoryIntegrationTest method findLatestModifications_shouldQueryIfNotEnoughElementsInCache.
@Test
public void findLatestModifications_shouldQueryIfNotEnoughElementsInCache() {
SvnMaterial material = MaterialsMother.svnMaterial();
MaterialRevision mod = saveOneScmModification(material, "user2", "file3");
goCache.remove(repo.latestMaterialModificationsKey(repo.findMaterialInstance(material)));
HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
repo.setHibernateTemplate(mockTemplate);
when(mockTemplate.execute(any(HibernateCallback.class))).thenReturn(mod.getModification(0));
Modification modification = repo.findLatestModification(repo.findMaterialInstance(material));
assertThat(modification, is(mod.getLatestModification()));
verify(mockTemplate).execute(any(HibernateCallback.class));
}
Aggregations