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 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();
  }
  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 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 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 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 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();
  }
  protected void tearDown() throws Exception {
    sfCtrl.verify();
    sessCtrl.verify();
    repoCtrl.verify();

    super.tearDown();
  }
  public void testInterceptorWithFlushFailure() throws Throwable {
    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);
    SQLException sqlEx = new SQLException("argh", "27");
    session.flush();
    ConstraintViolationException jdbcEx = new ConstraintViolationException("", sqlEx, null);
    sessionControl.setThrowable(jdbcEx, 1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
      fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
      // expected
      assertEquals(jdbcEx, ex.getCause());
    }

    sfControl.verify();
    sessionControl.verify();
  }
  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();
  }
 protected void tearDown() throws Exception {
   super.tearDown();
   if (shouldVerify()) {
     ctrlStatement.verify();
     ctrlPreparedStatement.verify();
     ctrlResultSet.verify();
     ctrlResultSetMetaData.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 testNoRetryAllowedDisposesPushOperation() throws Exception {
    mockConsumer_.isRetryAllowed();
    controlConsumer_.setDefaultReturnValue(false);

    controlConsumer_.replay();

    mockPushOperation_.dispose();
    controlPushOperation_.replay();

    objectUnderTest_.retry();

    controlConsumer_.verify();
    controlPushOperation_.verify();
  }
  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();
  }
 public void testGetServletPathWithSemicolon() throws Exception {
   control.expectAndReturn(requestMock.getRequestURI(), "/friend/mycontext/jim;bob");
   control.expectAndReturn(requestMock.getServletPath(), "/mycontext/jim");
   control.replay();
   assertEquals("/mycontext/jim;bob", RequestUtils.getServletPath(requestMock));
   control.verify();
 }
 @Test
 public void zeroOrMoreNoCalls() {
   mock.hasNext();
   control.setReturnValue(false, MockControl.ZERO_OR_MORE);
   control.replay();
   control.verify();
 }
  /**
   * Simulate failure of the underlying transaction infrastructure to commit. Check that the target
   * method was invoked, but that the transaction infrastructure exception was thrown to the client
   */
  public void testCannotCommitTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();

    Method m = setNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    Method m2 = getNameMethod;
    // No attributes for m2

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

    TransactionStatus status = transactionStatusForNewTransaction();
    ptm.getTransaction(txatt);
    ptmControl.setReturnValue(status);
    UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
    ptm.commit(status);
    ptmControl.setThrowable(ex);
    ptmControl.replay();

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

    String name = "new name";
    try {
      itb.setName(name);
      fail("Shouldn't have succeeded");
    } catch (UnexpectedRollbackException thrown) {
      assertTrue(thrown == ex);
    }

    // Should have invoked target and changed name
    assertTrue(itb.getName() == name);
    ptmControl.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();
  }
  /** 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();
  }
  /** Simulate a transaction infrastructure failure. Shouldn't invoke target method. */
  public void testCannotCreateTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();

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

    MockControl ptmControl = MockControl.createControl(PlatformTransactionManager.class);
    PlatformTransactionManager ptm = (PlatformTransactionManager) ptmControl.getMock();
    // Expect a transaction
    ptm.getTransaction(txatt);
    CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
    ptmControl.setThrowable(ex);
    ptmControl.replay();

    TestBean tb =
        new TestBean() {
          public String getName() {
            throw new UnsupportedOperationException(
                "Shouldn't have invoked target method when couldn't create transaction for transactional method");
          }
        };
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);

    try {
      itb.getName();
      fail("Shouldn't have invoked method");
    } catch (CannotCreateTransactionException thrown) {
      assertTrue(thrown == ex);
    }
    ptmControl.verify();
  }
  public void testInterception3() throws Exception {
    contextMap.put(
        ActionContext.PARAMETERS,
        new LinkedHashMap() {
          private static final long serialVersionUID = 0L;

          {
            put("param1", new String[] {"paramValueOne"});
            put("param2", new String[] {"paramValueTwo"});
          }
        });

    actionInvocationControl.replay();

    ParameterRemoverInterceptor interceptor = new ParameterRemoverInterceptor();
    interceptor.setParamNames("param1,param2");
    interceptor.setParamValues("paramValue1,paramValue2");
    interceptor.intercept(actionInvocation);

    Map params = (Map) contextMap.get(ActionContext.PARAMETERS);
    assertEquals(params.size(), 2);
    assertTrue(params.containsKey("param1"));
    assertTrue(params.containsKey("param2"));
    assertEquals(((String[]) params.get("param1"))[0], "paramValueOne");
    assertEquals(((String[]) params.get("param2"))[0], "paramValueTwo");

    actionInvocationControl.verify();
  }
  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 testWithConnectionFactoryAndExceptionListenerAndReconnectOnException()
      throws JMSException {
    MockControl cfControl = MockControl.createControl(ConnectionFactory.class);
    ConnectionFactory cf = (ConnectionFactory) cfControl.getMock();
    TestConnection con = new TestConnection();

    TestExceptionListener listener = new TestExceptionListener();
    cf.createConnection();
    cfControl.setReturnValue(con, 2);
    cfControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setExceptionListener(listener);
    scf.setReconnectOnException(true);
    Connection con1 = scf.createConnection();
    assertSame(listener, con1.getExceptionListener());
    con1.start();
    con.getExceptionListener().onException(new JMSException(""));
    Connection con2 = scf.createConnection();
    con2.start();
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    assertEquals(2, con.getStartCount());
    assertEquals(2, con.getCloseCount());
    assertEquals(1, listener.getCount());
  }
 public void testIsWritable() throws Exception {
   authorizerControl.expectAndReturn(authorizer.hasPermission(0, 0, project, "edit"), true);
   writableTableTag.setPermissions("edit,delete");
   authorizerControl.replay();
   boolean isWritable = writableTableTag.isWritable();
   assertEquals("table should be writable", true, isWritable);
   authorizerControl.verify();
 }
 @Test
 public void zeroOrMoreOneCall() {
   mock.hasNext();
   control.setReturnValue(false, MockControl.ZERO_OR_MORE);
   control.replay();
   assertFalse(mock.hasNext());
   control.verify();
 }
  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();
  }
Example #27
0
 /**
  * Invokes {@link org.easymock.MockControl#verify()}and {@link MockControl#reset()}on all
  * controls created by {@link #newControl(Class)}.
  */
 public void verifyControls() {
   Iterator i = controls.iterator();
   while (i.hasNext()) {
     MockControl c = (MockControl) i.next();
     c.verify();
     c.reset();
   }
   if (parent != null) parent.verifyControls();
 }
Example #28
0
 public void testDivisionByZero() {
   Sample4 sample4 = new Sample4();
   sample4.setEventLogger(logger);
   int a = 3;
   int b = 0;
   logger.logDivisionByZero(a);
   loggerControl.replay();
   sample4.div(a, b);
   loggerControl.verify();
 }
 public void testGetServletPathWithRequestURIAndContextPathSetButNoPatchInfo() throws Exception {
   control.expectAndReturn(requestMock.getServletPath(), null);
   control.expectAndReturn(requestMock.getRequestURI(), "/servlet/mycontext/");
   control.expectAndReturn(requestMock.getContextPath(), "/servlet");
   control.expectAndReturn(requestMock.getContextPath(), "/servlet");
   control.expectAndReturn(requestMock.getPathInfo(), null);
   control.replay();
   assertEquals("/mycontext/", RequestUtils.getServletPath(requestMock));
   control.verify();
 }
 @Test
 public void testSuspendedDoesNothing() throws Exception {
   mockMessageSupplier_.getConnected();
   controlMessageSupplier_.setReturnValue(true);
   mockMessageSupplier_.isSuspended();
   controlMessageSupplier_.setReturnValue(true);
   controlMessageSupplier_.replay();
   objectUnderTest_.runPull();
   controlMessageSupplier_.verify();
 }