use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class ScheduleTestUtil method modForRev.
private Modification modForRev(final String revision) {
return (Modification) materialRepository.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery("from Modification where revision = ?");
q.setParameter(0, revision);
List list = q.list();
if (list.isEmpty()) {
throw new RuntimeException("you are trying to load revision " + revision + " which doesn't exist");
}
return list.get(0);
}
});
}
use of org.springframework.orm.hibernate3.HibernateCallback 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.HibernateCallback in project gocd by gocd.
the class UserSqlMapDaoTest method shouldDoADoubleCheckOfCacheBeforeLoadingFromTheDB.
@Test
public void shouldDoADoubleCheckOfCacheBeforeLoadingFromTheDB() throws Exception {
GoCache cache = mock(GoCache.class);
UserSqlMapDao userSqlMapDaoSpy = spy(new UserSqlMapDao(sessionFactory, transactionTemplate, cache, transactionSynchronizationManager));
doReturn(mockHibernateTemplate).when(userSqlMapDaoSpy).hibernateTemplate();
doReturn(10).when(mockHibernateTemplate).execute(Matchers.<HibernateCallback<Object>>any());
Integer firstEnabledUserCount = userSqlMapDaoSpy.enabledUserCount();
assertThat(firstEnabledUserCount, is(10));
verify(mockHibernateTemplate, times(1)).execute(any(HibernateCallback.class));
verify(cache, times(2)).get(UserSqlMapDao.ENABLED_USER_COUNT_CACHE_KEY);
verify(cache, times(1)).put(UserSqlMapDao.ENABLED_USER_COUNT_CACHE_KEY, 10);
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class MaterialRepositoryWithH2IntegrationTest method materialFingerprintShouldUseTheHashAlgoritmInMigration47.
@Test
@RunIf(value = DatabaseChecker.class, arguments = { DatabaseChecker.H2 })
public void materialFingerprintShouldUseTheHashAlgoritmInMigration47() throws Exception {
final HgMaterial material = new HgMaterial("url", null);
byte[] fingerprint = (byte[]) repo.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String pattern = format("'type=%s%surl=%s'", material.getType(), AbstractMaterial.FINGERPRINT_DELIMITER, material.getUrl());
SQLQuery query = session.createSQLQuery(format("CALL HASH('SHA256', STRINGTOUTF8(%s), 1)", pattern));
return query.uniqueResult();
}
});
assertThat(Hex.encodeHexString(fingerprint), is(material.getFingerprint()));
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class MaterialRepository method modificationFor.
public List<Modification> modificationFor(final StageIdentifier stageIdentifier) {
if (stageIdentifier == null) {
return null;
}
String key = cacheKeyForModificationsForStageLocator(stageIdentifier);
List<Modification> modifications = (List<Modification>) goCache.get(key);
if (modifications == null) {
synchronized (key) {
modifications = (List<Modification>) goCache.get(key);
if (modifications == null) {
modifications = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery("FROM Modification WHERE revision = :revision ORDER BY id DESC");
q.setParameter("revision", stageIdentifier.getStageLocator());
return q.list();
}
});
if (!modifications.isEmpty()) {
goCache.put(key, modifications);
}
}
}
}
return modifications;
}
Aggregations