Search in sources :

Example 11 with HibernateTemplate

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);
}
Also used : Agent(com.thoughtworks.go.server.domain.Agent) HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) AgentIdentifier(com.thoughtworks.go.remote.AgentIdentifier) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 12 with HibernateTemplate

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);
}
Also used : HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) Test(org.junit.Test)

Example 13 with HibernateTemplate

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());
}
Also used : SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) PackageMaterialInstance(com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance) PluggableSCMMaterialInstance(com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance) SvnMaterialInstance(com.thoughtworks.go.domain.materials.svn.SvnMaterialInstance) Test(org.junit.Test)

Example 14 with HibernateTemplate

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));
}
Also used : SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Test(org.junit.Test)

Aggregations

HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate)13 Test (org.junit.Test)10 SvnMaterial (com.thoughtworks.go.config.materials.svn.SvnMaterial)6 PackageMaterialInstance (com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance)5 PluggableSCMMaterialInstance (com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance)5 SvnMaterialInstance (com.thoughtworks.go.domain.materials.svn.SvnMaterialInstance)5 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)4 PackageMaterial (com.thoughtworks.go.config.materials.PackageMaterial)3 PluggableSCMMaterial (com.thoughtworks.go.config.materials.PluggableSCMMaterial)3 ScmMaterial (com.thoughtworks.go.config.materials.ScmMaterial)3 DependencyMaterial (com.thoughtworks.go.config.materials.dependency.DependencyMaterial)3 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)3 HgMaterial (com.thoughtworks.go.config.materials.mercurial.HgMaterial)3 P4Material (com.thoughtworks.go.config.materials.perforce.P4Material)3 SQLException (java.sql.SQLException)3 HibernateException (org.hibernate.HibernateException)2 Session (org.hibernate.Session)2 ReadWriteUpdateLock (com.googlecode.concurentlocks.ReadWriteUpdateLock)1 ReentrantReadWriteUpdateLock (com.googlecode.concurentlocks.ReentrantReadWriteUpdateLock)1 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)1