Search in sources :

Example 1 with HibernateCallback

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);
        }
    });
}
Also used : Modification(com.thoughtworks.go.domain.materials.Modification) Query(org.hibernate.Query) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Session(org.hibernate.Session)

Example 2 with HibernateCallback

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));
}
Also used : SQLException(java.sql.SQLException) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) DataAccessException(org.springframework.dao.DataAccessException)

Example 3 with HibernateCallback

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);
}
Also used : GoCache(com.thoughtworks.go.server.cache.GoCache) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Test(org.junit.Test)

Example 4 with HibernateCallback

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()));
}
Also used : HgMaterial(com.thoughtworks.go.config.materials.mercurial.HgMaterial) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Matchers.containsString(org.hamcrest.Matchers.containsString) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) RunIf(com.googlecode.junit.ext.RunIf) Test(org.junit.Test)

Example 5 with HibernateCallback

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;
}
Also used : CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback)

Aggregations

HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)27 Session (org.hibernate.Session)15 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10 SQLException (java.sql.SQLException)7 Query (org.hibernate.Query)6 List (java.util.List)5 Criteria (org.hibernate.Criteria)5 Test (org.junit.Test)4 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)4 AgentCookie (com.thoughtworks.go.server.domain.AgentCookie)3 BigInteger (java.math.BigInteger)3 HibernateException (org.hibernate.HibernateException)3 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)2 ArrayList (java.util.ArrayList)2 SQLQuery (org.hibernate.SQLQuery)2 LongType (org.hibernate.type.LongType)2 RunIf (com.googlecode.junit.ext.RunIf)1 AbstractMaterial (com.thoughtworks.go.config.materials.AbstractMaterial)1 DependencyMaterial (com.thoughtworks.go.config.materials.dependency.DependencyMaterial)1 HgMaterial (com.thoughtworks.go.config.materials.mercurial.HgMaterial)1