public void testConnectionFactory102WithTopic() throws JMSException {
    MockControl cfControl = MockControl.createControl(TopicConnectionFactory.class);
    TopicConnectionFactory cf = (TopicConnectionFactory) cfControl.getMock();
    MockControl conControl = MockControl.createControl(TopicConnection.class);
    TopicConnection con = (TopicConnection) conControl.getMock();

    cf.createTopicConnection();
    cfControl.setReturnValue(con, 1);
    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    cfControl.replay();
    conControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory102(cf, true);
    TopicConnection con1 = scf.createTopicConnection();
    con1.start();
    con1.close(); // should be ignored
    TopicConnection con2 = scf.createTopicConnection();
    con2.start();
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    conControl.verify();
  }
  public void testQueryForObjectWithIntegerAndNull() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt(1);
    ctrlResultSet.setReturnValue(0);
    mockResultSet.wasNull();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(null);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    assertNull(template.queryForObject(sql, Integer.class));
  }
  public void testWithTopicConnection() throws JMSException {
    MockControl conControl = MockControl.createControl(TopicConnection.class);
    Connection con = (TopicConnection) conControl.getMock();

    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    conControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory(con);
    TopicConnection con1 = scf.createTopicConnection();
    con1.start();
    con1.stop(); // should be ignored
    con1.close(); // should be ignored
    TopicConnection con2 = scf.createTopicConnection();
    con2.start();
    con2.stop(); // should be ignored
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    conControl.verify();
  }
  public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage()
      throws JMSException {
    MockControl cfControl = MockControl.createControl(TopicConnectionFactory.class);
    TopicConnectionFactory cf = (TopicConnectionFactory) cfControl.getMock();
    MockControl conControl = MockControl.createControl(TopicConnection.class);
    TopicConnection con = (TopicConnection) conControl.getMock();
    MockControl txSessionControl = MockControl.createControl(TopicSession.class);
    TopicSession txSession = (TopicSession) txSessionControl.getMock();
    MockControl nonTxSessionControl = MockControl.createControl(TopicSession.class);
    TopicSession nonTxSession = (TopicSession) nonTxSessionControl.getMock();

    cf.createTopicConnection();
    cfControl.setReturnValue(con, 1);
    con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
    conControl.setReturnValue(txSession, 1);
    txSession.getTransacted();
    txSessionControl.setReturnValue(true, 2);
    txSession.close();
    txSessionControl.setVoidCallable(1);
    con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
    conControl.setReturnValue(nonTxSession, 1);
    nonTxSession.close();
    nonTxSessionControl.setVoidCallable(1);
    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    cfControl.replay();
    conControl.replay();
    txSessionControl.replay();
    nonTxSessionControl.replay();

    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createTopicConnection();
    Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session1.getTransacted();
    session1.close(); // should lead to rollback
    session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session1.close(); // should be ignored
    con1.start();
    con1.close(); // should be ignored
    TopicConnection con2 = scf.createTopicConnection();
    Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close(); // should be ignored
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.getTransacted();
    session2.close(); // should be ignored
    con2.start();
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    conControl.verify();
    txSessionControl.verify();
    nonTxSessionControl.verify();
  }
  public void testInterceptorWithThreadBoundAndFilters() {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    session.isOpen();
    sessionControl.setReturnValue(true, 1);
    session.enableFilter("myFilter");
    sessionControl.setReturnValue(null, 1);
    session.enableFilter("yourFilter");
    sessionControl.setReturnValue(null, 1);
    session.disableFilter("myFilter");
    sessionControl.setVoidCallable(1);
    session.disableFilter("yourFilter");
    sessionControl.setVoidCallable(1);
    sfControl.replay();
    sessionControl.replay();

    TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    interceptor.setFilterNames(new String[] {"myFilter", "yourFilter"});
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    } finally {
      TransactionSynchronizationManager.unbindResource(sf);
    }

    sfControl.verify();
    sessionControl.verify();
  }
  public void testInterceptorWithThreadBoundAndFlushAlways() {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    session.isOpen();
    sessionControl.setReturnValue(true, 1);
    session.getFlushMode();
    sessionControl.setReturnValue(FlushMode.AUTO, 1);
    session.setFlushMode(FlushMode.ALWAYS);
    sessionControl.setVoidCallable(1);
    session.setFlushMode(FlushMode.AUTO);
    sessionControl.setVoidCallable(1);
    sfControl.replay();
    sessionControl.replay();

    TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    interceptor.setFlushMode(HibernateInterceptor.FLUSH_ALWAYS);
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    } finally {
      TransactionSynchronizationManager.unbindResource(sf);
    }

    sfControl.verify();
    sessionControl.verify();
  }
  public void testQueryForLong() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getLong(1);
    ctrlResultSet.setReturnValue(87);
    mockResultSet.wasNull();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(null);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    long l = template.queryForLong(sql);
    assertEquals("Return of a long", 87, l);
  }
  public void testWithConnectionFactoryAndClientId() throws JMSException {
    MockControl cfControl = MockControl.createControl(ConnectionFactory.class);
    ConnectionFactory cf = (ConnectionFactory) cfControl.getMock();
    MockControl conControl = MockControl.createControl(Connection.class);
    Connection con = (Connection) conControl.getMock();

    cf.createConnection();
    cfControl.setReturnValue(con, 1);
    con.setClientID("myId");
    conControl.setVoidCallable(1);
    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    cfControl.replay();
    conControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setClientId("myId");
    Connection con1 = scf.createConnection();
    con1.start();
    con1.close(); // should be ignored
    Connection con2 = scf.createConnection();
    con2.start();
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    conControl.verify();
  }
  public void testQueryForListWithArgsAndEmptyResult() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";

    ctrlResultSet = MockControl.createControl(ResultSet.class);
    mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    mockPreparedStatement.getWarnings();
    ctrlPreparedStatement.setReturnValue(null);
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    List li = template.queryForList(sql, new Object[] {new Integer(3)});
    assertEquals("All rows returned", 0, li.size());
  }
  public void testValid() throws Exception {
    String sql = "SELECT NAME AS NAME, PROPERTY AS PROPERTY, VALUE AS VALUE FROM T";

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    ctrlResultSet.expectAndReturn(mockResultSet.next(), true, 2);
    ctrlResultSet.expectAndReturn(mockResultSet.next(), false);

    // first row
    ctrlResultSet.expectAndReturn(mockResultSet.getString(1), "one");
    ctrlResultSet.expectAndReturn(mockResultSet.getString(2), "(class)");
    ctrlResultSet.expectAndReturn(mockResultSet.getString(3), "org.springframework.beans.TestBean");

    // second row
    ctrlResultSet.expectAndReturn(mockResultSet.getString(1), "one");
    ctrlResultSet.expectAndReturn(mockResultSet.getString(2), "age");
    ctrlResultSet.expectAndReturn(mockResultSet.getString(3), "53");

    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(Statement.class);
    Statement mockStatement = (Statement) ctrlStatement.getMock();
    ctrlStatement.expectAndReturn(mockStatement.executeQuery(sql), mockResultSet);
    if (debugEnabled) {
      ctrlStatement.expectAndReturn(mockStatement.getWarnings(), null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlResultSet.replay();
    ctrlStatement.replay();
    replay();

    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    JdbcBeanDefinitionReader reader = new JdbcBeanDefinitionReader(bf);
    reader.setDataSource(mockDataSource);
    reader.loadBeanDefinitions(sql);
    assertEquals("Incorrect number of bean definitions", 1, bf.getBeanDefinitionCount());
    TestBean tb = (TestBean) bf.getBean("one");
    assertEquals("Age in TestBean was wrong.", 53, tb.getAge());

    ctrlResultSet.verify();
    ctrlStatement.verify();
  }
  /** Check that a transaction is created and committed. */
  public void testTransactionShouldSucceed() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();

    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(getNameMethod, txatt);

    TransactionStatus status = transactionStatusForNewTransaction();
    MockControl ptmControl = MockControl.createControl(PlatformTransactionManager.class);
    PlatformTransactionManager ptm = (PlatformTransactionManager) ptmControl.getMock();
    // expect a transaction
    ptm.getTransaction(txatt);
    ptmControl.setReturnValue(status, 1);
    ptm.commit(status);
    ptmControl.setVoidCallable(1);
    ptmControl.replay();

    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);

    checkTransactionStatus(false);
    itb.getName();
    checkTransactionStatus(false);

    ptmControl.verify();
  }
  public void testInterceptorWithThreadBoundEmptyHolder() {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    SessionHolder holder = new SessionHolder("key", session);
    holder.removeSession("key");
    TransactionSynchronizationManager.bindResource(sf, holder);
    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    }

    sfControl.verify();
    sessionControl.verify();
  }
  /** Test that TransactionStatus.setRollbackOnly works. */
  public void testProgrammaticRollback() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();

    Method m = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);

    TransactionStatus status = transactionStatusForNewTransaction();
    MockControl ptmControl = MockControl.createControl(PlatformTransactionManager.class);
    PlatformTransactionManager ptm = (PlatformTransactionManager) ptmControl.getMock();

    ptm.getTransaction(txatt);
    ptmControl.setReturnValue(status, 1);
    ptm.commit(status);
    ptmControl.setVoidCallable(1);
    ptmControl.replay();

    final String name = "jenny";
    TestBean tb =
        new TestBean() {
          public String getName() {
            TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus();
            txStatus.setRollbackOnly();
            return name;
          }
        };

    ITestBean itb = (ITestBean) advised(tb, ptm, tas);

    // verification!?
    assertTrue(name.equals(itb.getName()));

    ptmControl.verify();
  }
  public void testInterceptorWithEntityInterceptor() throws HibernateException {
    MockControl interceptorControl = MockControl.createControl(org.hibernate.Interceptor.class);
    org.hibernate.Interceptor entityInterceptor =
        (org.hibernate.Interceptor) interceptorControl.getMock();
    interceptorControl.replay();
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession(entityInterceptor);
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    interceptor.setEntityInterceptor(entityInterceptor);
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    }

    interceptorControl.verify();
    sfControl.verify();
    sessionControl.verify();
  }
  public void testInterceptorWithNewSessionAndFlushNever() throws HibernateException {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf);
    session.setFlushMode(FlushMode.MANUAL);
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setFlushModeName("FLUSH_NEVER");
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    }

    sfControl.verify();
    sessionControl.verify();
  }
  public void testQueryForObjectThrowsIncorrectResultSizeForMoreThanOneRow() throws Exception {
    String sql = "select pass from t_account where first_name='Alef'";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getString(1);
    ctrlResultSet.setReturnValue("pass");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);
    mockResultSet.getString(1);
    ctrlResultSet.setReturnValue("pass");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(null);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    try {
      template.queryForObject(sql, String.class);
      fail("Should have thrown IncorrectResultSizeDataAccessException");
    } catch (IncorrectResultSizeDataAccessException ex) {
      // expected
    }
  }
  public void testSqlMapClientTemplate() throws SQLException {
    MockControl dsControl = MockControl.createControl(DataSource.class);
    DataSource ds = (DataSource) dsControl.getMock();
    MockControl conControl = MockControl.createControl(Connection.class);
    Connection con = (Connection) conControl.getMock();
    ds.getConnection();
    dsControl.setReturnValue(con, 1);
    con.close();
    conControl.setVoidCallable(1);
    dsControl.replay();
    conControl.replay();

    MockControl sessionControl = MockControl.createControl(SqlMapSession.class);
    final SqlMapSession session = (SqlMapSession) sessionControl.getMock();
    MockControl clientControl = MockControl.createControl(SqlMapClient.class);
    SqlMapClient client = (SqlMapClient) clientControl.getMock();
    client.openSession();
    clientControl.setReturnValue(session, 1);
    session.getCurrentConnection();
    sessionControl.setReturnValue(null, 1);
    session.setUserConnection(con);
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setVoidCallable(1);
    sessionControl.replay();
    clientControl.replay();

    SqlMapClientTemplate template = new SqlMapClientTemplate();
    template.setDataSource(ds);
    template.setSqlMapClient(client);
    template.afterPropertiesSet();
    Object result =
        template.execute(
            new SqlMapClientCallback() {
              public Object doInSqlMapClient(SqlMapExecutor executor) {
                assertTrue(executor == session);
                return "done";
              }
            });
    assertEquals("done", result);
    dsControl.verify();
    conControl.verify();
    sessionControl.verify();
    clientControl.verify();
  }
  public void testOpenPersistenceManagerInViewInterceptor() throws Exception {
    MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
    PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
    MockControl pmControl = MockControl.createControl(PersistenceManager.class);
    PersistenceManager pm = (PersistenceManager) pmControl.getMock();

    OpenPersistenceManagerInViewInterceptor interceptor =
        new OpenPersistenceManagerInViewInterceptor();
    interceptor.setPersistenceManagerFactory(pmf);

    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);

    pmf.getPersistenceManager();
    pmfControl.setReturnValue(pm, 1);
    pmfControl.replay();
    pmControl.replay();
    interceptor.preHandle(new ServletWebRequest(request));
    assertTrue(TransactionSynchronizationManager.hasResource(pmf));

    // check that further invocations simply participate
    interceptor.preHandle(new ServletWebRequest(request));

    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);

    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);

    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);

    pmfControl.verify();
    pmControl.verify();

    pmfControl.reset();
    pmControl.reset();
    pmfControl.replay();
    pmControl.replay();
    interceptor.postHandle(new ServletWebRequest(request), null);
    assertTrue(TransactionSynchronizationManager.hasResource(pmf));
    pmfControl.verify();
    pmControl.verify();

    pmfControl.reset();
    pmControl.reset();
    pm.close();
    pmControl.setVoidCallable(1);
    pmfControl.replay();
    pmControl.replay();
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    assertFalse(TransactionSynchronizationManager.hasResource(pmf));
    pmfControl.verify();
    pmControl.verify();
  }
  private void doTestQueryForListWithArgs(String sql) throws Exception {
    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1, 2);
    mockResultSetMetaData.getColumnLabel(1);
    ctrlResultSetMetaData.setReturnValue("age", 2);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData, 2);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getObject(1);
    ctrlResultSet.setReturnValue(new Integer(11));
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getObject(1);
    ctrlResultSet.setReturnValue(new Integer(12));
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    mockPreparedStatement.getWarnings();
    ctrlPreparedStatement.setReturnValue(null);
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    List li = template.queryForList(sql, new Object[] {new Integer(3)});
    assertEquals("All rows returned", 2, li.size());
    assertEquals("First row is Integer", 11, ((Integer) ((Map) li.get(0)).get("age")).intValue());
    assertEquals("Second row is Integer", 12, ((Integer) ((Map) li.get(1)).get("age")).intValue());
  }
  public void testQueryForList() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1, 2);
    mockResultSetMetaData.getColumnLabel(1);
    ctrlResultSetMetaData.setReturnValue("age", 2);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData, 2);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getObject(1);
    ctrlResultSet.setReturnValue(new Integer(11));
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getObject(1);
    ctrlResultSet.setReturnValue(new Integer(12));
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(null);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    List li = template.queryForList(sql);
    assertEquals("All rows returned", 2, li.size());
    assertEquals("First row is Integer", 11, ((Integer) ((Map) li.get(0)).get("age")).intValue());
    assertEquals("Second row is Integer", 12, ((Integer) ((Map) li.get(1)).get("age")).intValue());
  }
  /**
   * Check that the given exception thrown by the target can produce the desired behavior with the
   * appropriate transaction attribute.
   *
   * @param ex exception to be thrown by the target
   * @param shouldRollback whether this should cause a transaction rollback
   */
  protected void doTestRollbackOnException(
      final Exception ex, final boolean shouldRollback, boolean rollbackException)
      throws Exception {

    TransactionAttribute txatt =
        new DefaultTransactionAttribute() {
          public boolean rollbackOn(Throwable t) {
            assertTrue(t == ex);
            return shouldRollback;
          }
        };

    Method m = exceptionalMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);

    MockControl statusControl = MockControl.createControl(TransactionStatus.class);
    TransactionStatus status = (TransactionStatus) statusControl.getMock();
    MockControl ptmControl = MockControl.createControl(PlatformTransactionManager.class);
    PlatformTransactionManager ptm = (PlatformTransactionManager) ptmControl.getMock();
    // Gets additional call(s) from TransactionControl

    ptm.getTransaction(txatt);
    ptmControl.setReturnValue(status, 1);

    if (shouldRollback) {
      ptm.rollback(status);
    } else {
      ptm.commit(status);
    }
    TransactionSystemException tex = new TransactionSystemException("system exception");
    if (rollbackException) {
      ptmControl.setThrowable(tex, 1);
    } else {
      ptmControl.setVoidCallable(1);
    }
    ptmControl.replay();

    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);

    try {
      itb.exceptional(ex);
      fail("Should have thrown exception");
    } catch (Throwable t) {
      if (rollbackException) {
        assertEquals("Caught wrong exception", tex, t);
      } else {
        assertEquals("Caught wrong exception", ex, t);
      }
    }

    ptmControl.verify();
  }
  public void testQueryForObjectOnSqlMapSession() throws SQLException {
    MockControl dsControl = MockControl.createControl(DataSource.class);
    DataSource ds = (DataSource) dsControl.getMock();
    MockControl conControl = MockControl.createControl(Connection.class);
    Connection con = (Connection) conControl.getMock();
    MockControl clientControl = MockControl.createControl(SqlMapClient.class);
    SqlMapClient client = (SqlMapClient) clientControl.getMock();
    MockControl sessionControl = MockControl.createControl(SqlMapSession.class);
    SqlMapSession session = (SqlMapSession) sessionControl.getMock();

    ds.getConnection();
    dsControl.setReturnValue(con, 1);
    con.close();
    conControl.setVoidCallable(1);
    client.getDataSource();
    clientControl.setReturnValue(ds, 2);
    client.openSession();
    clientControl.setReturnValue(session, 1);
    session.getCurrentConnection();
    sessionControl.setReturnValue(null, 1);
    session.setUserConnection(con);
    sessionControl.setVoidCallable(1);
    session.queryForObject("myStatement", "myParameter");
    sessionControl.setReturnValue("myResult", 1);
    session.close();
    sessionControl.setVoidCallable(1);

    dsControl.replay();
    conControl.replay();
    clientControl.replay();
    sessionControl.replay();

    SqlMapClientTemplate template = new SqlMapClientTemplate();
    template.setSqlMapClient(client);
    template.afterPropertiesSet();
    assertEquals("myResult", template.queryForObject("myStatement", "myParameter"));

    dsControl.verify();
    clientControl.verify();
  }
  public void testInterceptorWithEntityInterceptorBeanName() throws HibernateException {
    MockControl interceptorControl = MockControl.createControl(org.hibernate.Interceptor.class);
    org.hibernate.Interceptor entityInterceptor =
        (org.hibernate.Interceptor) interceptorControl.getMock();
    interceptorControl.replay();
    MockControl interceptor2Control = MockControl.createControl(org.hibernate.Interceptor.class);
    org.hibernate.Interceptor entityInterceptor2 =
        (org.hibernate.Interceptor) interceptor2Control.getMock();
    interceptor2Control.replay();

    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession(entityInterceptor);
    sfControl.setReturnValue(session, 1);
    sf.openSession(entityInterceptor2);
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 2);
    session.flush();
    sessionControl.setVoidCallable(2);
    session.close();
    sessionControl.setReturnValue(null, 2);
    sfControl.replay();
    sessionControl.replay();

    MockControl beanFactoryControl = MockControl.createControl(BeanFactory.class);
    BeanFactory beanFactory = (BeanFactory) beanFactoryControl.getMock();
    beanFactory.getBean("entityInterceptor", org.hibernate.Interceptor.class);
    beanFactoryControl.setReturnValue(entityInterceptor, 1);
    beanFactory.getBean("entityInterceptor", org.hibernate.Interceptor.class);
    beanFactoryControl.setReturnValue(entityInterceptor2, 1);
    beanFactoryControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    interceptor.setEntityInterceptorBeanName("entityInterceptor");
    interceptor.setBeanFactory(beanFactory);
    for (int i = 0; i < 2; i++) {
      try {
        interceptor.invoke(new TestInvocation(sf));
      } catch (Throwable t) {
        fail("Should not have thrown Throwable: " + t.getMessage());
      }
    }

    interceptorControl.verify();
    interceptor2Control.verify();
    sfControl.verify();
    sessionControl.verify();
  }
  public void testQueryForObjectWithArgsAndRowMapper() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";

    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt(1);
    ctrlResultSet.setReturnValue(22);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    mockPreparedStatement.getWarnings();
    ctrlPreparedStatement.setReturnValue(null);
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    Object o =
        template.queryForObject(
            sql,
            new Object[] {new Integer(3)},
            new RowMapper() {
              public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new Integer(rs.getInt(1));
              }
            });
    assertTrue("Correct result type", o instanceof Integer);
  }
  public void testQueryForListWithArgsAndIntegerElementAndSingleRowAndColumn() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt(1);
    ctrlResultSet.setReturnValue(11);
    mockResultSet.wasNull();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    mockPreparedStatement.getWarnings();
    ctrlPreparedStatement.setReturnValue(null);
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    List li = template.queryForList(sql, new Object[] {new Integer(3)}, Integer.class);
    assertEquals("All rows returned", 1, li.size());
    assertEquals("First row is Integer", 11, ((Integer) li.get(0)).intValue());
  }
  public void testWithConnectionFactoryAndExceptionListener() throws JMSException {
    MockControl cfControl = MockControl.createControl(ConnectionFactory.class);
    ConnectionFactory cf = (ConnectionFactory) cfControl.getMock();
    MockControl conControl = MockControl.createControl(Connection.class);
    Connection con = (Connection) conControl.getMock();

    ExceptionListener listener = new ChainedExceptionListener();
    cf.createConnection();
    cfControl.setReturnValue(con, 1);
    con.setExceptionListener(listener);
    conControl.setVoidCallable(1);
    con.getExceptionListener();
    conControl.setReturnValue(listener, 1);
    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    cfControl.replay();
    conControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setExceptionListener(listener);
    Connection con1 = scf.createConnection();
    assertEquals(listener, con1.getExceptionListener());
    con1.start();
    con1.stop(); // should be ignored
    con1.close(); // should be ignored
    Connection con2 = scf.createConnection();
    con2.start();
    con2.stop(); // should be ignored
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    conControl.verify();
  }
  public void testQueryForObjectWithArgsAndInteger() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt(1);
    ctrlResultSet.setReturnValue(22);
    mockResultSet.wasNull();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    mockPreparedStatement.getWarnings();
    ctrlPreparedStatement.setReturnValue(null);
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    Object o = template.queryForObject(sql, new Object[] {new Integer(3)}, Integer.class);
    assertTrue("Correct result type", o instanceof Integer);
  }
  public void testQueryForMapWithArgsAndSingleRowAndColumn() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);
    mockResultSetMetaData.getColumnLabel(1);
    ctrlResultSetMetaData.setReturnValue("age", 1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getObject(1);
    ctrlResultSet.setReturnValue(new Integer(11));
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    mockPreparedStatement.getWarnings();
    ctrlPreparedStatement.setReturnValue(null);
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    Map map = template.queryForMap(sql, new Object[] {new Integer(3)});
    assertEquals("Row is Integer", 11, ((Integer) map.get("age")).intValue());
  }
  public void testQueryForIntWithArgs() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt(1);
    ctrlResultSet.setReturnValue(22);
    mockResultSet.wasNull();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    mockPreparedStatement.getWarnings();
    ctrlPreparedStatement.setReturnValue(null);
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    int i = template.queryForInt(sql, new Object[] {new Integer(3)});
    assertEquals("Return of an int", 22, i);
  }
  public void testQueryForListWithIntegerElement() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt(1);
    ctrlResultSet.setReturnValue(11);
    mockResultSet.wasNull();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(null);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    List li = template.queryForList(sql, Integer.class);
    assertEquals("All rows returned", 1, li.size());
    assertEquals("Element is Integer", 11, ((Integer) li.get(0)).intValue());
  }