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));
}
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());
}
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);
}
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);
}
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 });
}
Aggregations