Search in sources :

Example 1 with HibernateTemplate

use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.

the class PipelineRepositoryTest method stubPipelineInstancesInDb.

private void stubPipelineInstancesInDb(Object[]... rows) {
    pipelineRepository.setHibernateTemplate(new HibernateTemplate() {

        @Override
        public <T> T execute(HibernateCallback<T> action) throws DataAccessException {
            try {
                return action.doInHibernate(session);
            } catch (SQLException e) {
                throw new RuntimeException();
            }
        }
    });
    when(session.createSQLQuery(anyString())).thenReturn(sqlQuery);
    when(sqlQuery.list()).thenReturn(Arrays.asList(rows));
}
Also used : SQLException(java.sql.SQLException) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) DataAccessException(org.springframework.dao.DataAccessException)

Example 2 with HibernateTemplate

use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method findOrCreateFrom_shouldEnsureOnlyOneThreadCanCreateAtATime.

@Test
public void findOrCreateFrom_shouldEnsureOnlyOneThreadCanCreateAtATime() throws Exception {
    final Material svn = MaterialsMother.svnMaterial("url", null, "username", "password", false, null);
    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo = new MaterialRepository(repo.getSessionFactory(), goCache, 200, transactionSynchronizationManager, materialConfigConverter, materialExpansionService, databaseStrategy) {

        @Override
        public MaterialInstance findMaterialInstance(Material material) {
            MaterialInstance result = super.findMaterialInstance(material);
            // force multiple threads to try to create the material
            TestUtils.sleepQuietly(20);
            return result;
        }

        @Override
        public void saveOrUpdate(MaterialInstance material) {
            material.setId(10);
            super.saveOrUpdate(material);
        }
    };
    repo.setHibernateTemplate(mockTemplate);
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                repo.findOrCreateFrom(svn);
            }
        }, "thread-" + i);
        threads.add(thread);
        thread.start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    verify(mockTemplate, times(1)).saveOrUpdate(Mockito.<MaterialInstance>any());
}
Also used : HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) ArrayList(java.util.ArrayList) PackageMaterial(com.thoughtworks.go.config.materials.PackageMaterial) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) PluggableSCMMaterial(com.thoughtworks.go.config.materials.PluggableSCMMaterial) HgMaterial(com.thoughtworks.go.config.materials.mercurial.HgMaterial) ScmMaterial(com.thoughtworks.go.config.materials.ScmMaterial) P4Material(com.thoughtworks.go.config.materials.perforce.P4Material) DependencyMaterial(com.thoughtworks.go.config.materials.dependency.DependencyMaterial) 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 3 with HibernateTemplate

use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method findMaterialInstance_shouldCacheMaterialInstance.

@Test
public void findMaterialInstance_shouldCacheMaterialInstance() throws Exception {
    Material svn1 = MaterialsMother.svnMaterial("url", null, "username", "password", false, null);
    repo.saveOrUpdate(svn1.createMaterialInstance());
    MaterialInstance instance = repo.findMaterialInstance(svn1);
    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);
    MaterialInstance cachedInstance = repo.findMaterialInstance(svn1);
    assertSame(instance, cachedInstance);
    verifyNoMoreInteractions(mockTemplate);
}
Also used : HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) PackageMaterial(com.thoughtworks.go.config.materials.PackageMaterial) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) PluggableSCMMaterial(com.thoughtworks.go.config.materials.PluggableSCMMaterial) HgMaterial(com.thoughtworks.go.config.materials.mercurial.HgMaterial) ScmMaterial(com.thoughtworks.go.config.materials.ScmMaterial) P4Material(com.thoughtworks.go.config.materials.perforce.P4Material) DependencyMaterial(com.thoughtworks.go.config.materials.dependency.DependencyMaterial) 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 4 with HibernateTemplate

use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method findModificationsSince_shouldCacheResults.

@Test
public void findModificationsSince_shouldCacheResults() {
    SvnMaterial material = MaterialsMother.svnMaterial();
    MaterialRevision zero = saveOneScmModification(material, "user1", "file1");
    MaterialRevision first = saveOneScmModification(material, "user1", "file1");
    MaterialRevision second = saveOneScmModification(material, "user2", "file2");
    MaterialRevision third = saveOneScmModification(material, "user2", "file2");
    repo.findModificationsSince(material, first);
    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);
    List<Modification> modifications = repo.findModificationsSince(material, first);
    assertThat(modifications.size(), is(2));
    assertEquals(third.getLatestModification(), modifications.get(0));
    assertEquals(second.getLatestModification(), modifications.get(1));
    verifyNoMoreInteractions(mockTemplate);
}
Also used : SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) Test(org.junit.Test)

Example 5 with HibernateTemplate

use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method findModificationsFor_shouldCacheModifications.

@Test
public void findModificationsFor_shouldCacheModifications() {
    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);
    ArrayList modifications = new ArrayList();
    when(mockTemplate.find("FROM Modification WHERE materialId = ? AND id BETWEEN ? AND ? ORDER BY id DESC", new Object[] { 10L, -1L, -1L })).thenReturn(modifications);
    MaterialInstance materialInstance = material().createMaterialInstance();
    materialInstance.setId(10);
    when(mockTemplate.findByCriteria(any(DetachedCriteria.class))).thenReturn(asList(materialInstance));
    PipelineMaterialRevision pmr = pipelineMaterialRevision();
    List<Modification> actual;
    actual = repo.findModificationsFor(pmr);
    actual = repo.findModificationsFor(pmr);
    assertSame(modifications, actual);
    verify(mockTemplate, times(1)).find("FROM Modification WHERE materialId = ? AND id BETWEEN ? AND ? ORDER BY id DESC", new Object[] { 10L, -1L, -1L });
}
Also used : HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) ArrayList(java.util.ArrayList) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) 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)

Aggregations

HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate)11 Test (org.junit.Test)9 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 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 ArrayList (java.util.ArrayList)2 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)2 GoCache (com.thoughtworks.go.server.cache.GoCache)1 StubGoCache (com.thoughtworks.go.server.service.StubGoCache)1 SQLException (java.sql.SQLException)1 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)1 H2Dialect (org.hibernate.dialect.H2Dialect)1