Search in sources :

Example 31 with HibernateCallback

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

the class AgentDaoTest method shouldCacheCookieForAgent.

@Test
public void shouldCacheCookieForAgent() throws Exception {
    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"));
    assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
    goCache.clear();
    assertThat(agentDao.cookieFor(agentIdentifier), is("updated_cookie"));
}
Also used : Agent(com.thoughtworks.go.server.domain.Agent) HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) AgentIdentifier(com.thoughtworks.go.remote.AgentIdentifier) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Session(org.hibernate.Session) Test(org.junit.Test)

Example 32 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback 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)

Aggregations

HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)31 Session (org.hibernate.Session)19 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10 Query (org.hibernate.Query)9 List (java.util.List)8 SQLException (java.sql.SQLException)7 Criteria (org.hibernate.Criteria)6 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)5 Test (org.junit.Test)4 Agent (com.thoughtworks.go.server.domain.Agent)3 BigInteger (java.math.BigInteger)3 Map (java.util.Map)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 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)2 HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate)2 RunIf (com.googlecode.junit.ext.RunIf)1