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 setUp() throws Exception {
    containerControl = MockControl.createStrictControl(ObjectContainer.class);
    container = (ObjectContainer) containerControl.getMock();

    templateControl = MockClassControl.createStrictControl(Db4oTemplate.class);
    template = (Db4oTemplate) templateControl.getMock();
  }
  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 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 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 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();
  }
  /** Check that a transaction is created and committed. */
  public void testTransactionShouldSucceedWithNotNew() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();

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

    MockControl statusControl = MockControl.createControl(TransactionStatus.class);
    TransactionStatus status = (TransactionStatus) statusControl.getMock();
    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);
    // verification!?
    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();
  }
  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 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 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();
  }
  protected void setUp() throws Exception {
    super.setUp();

    controlInitialQueue_ = MockControl.createControl(MessageQueueAdapter.class);
    mockInitialQueue_ = (MessageQueueAdapter) controlInitialQueue_.getMock();
    objectUnderTest_ = new RWLockEventQueueDecorator(mockInitialQueue_);

    controlReplacementQueue_ = MockControl.createControl(MessageQueueAdapter.class);
    mockReplacementQueue_ = (MessageQueueAdapter) controlReplacementQueue_.getMock();
  }
示例#14
0
  protected void setUp() throws Exception {
    super.setUp();

    controlScheduledFuture_ = MockControl.createControl(ScheduledFuture.class);
    mockScheduledFuture_ = (ScheduledFuture) controlScheduledFuture_.getMock();
    controlTaskProcessor_ = MockControl.createControl(TaskProcessor.class);
    mockTaskProcessor_ = (TaskProcessor) controlTaskProcessor_.getMock();
    controlMessageSupplier_ = MockControl.createControl(MessageSupplier.class);
    mockMessageSupplier_ = (MessageSupplier) controlMessageSupplier_.getMock();
    objectUnderTest_ = new PullMessagesUtility(mockTaskProcessor_, mockMessageSupplier_);
  }
  protected void setUp() throws Exception {
    super.setUp();
    control = MockControl.createControl(ICityBroadcastDAO.class);
    mock = (ICityBroadcastDAO) control.getMock();

    control2 = MockControl.createControl(IUserDAO.class);
    mock2 = (IUserDAO) control2.getMock();
    // mock2.findBySetby(1);
    // control2.setReturnValue(new ArrayList<Strongholds>());

  }
  protected void setUp() throws Exception {
    super.setUp();

    contextControl = MockControl.createControl(Context.class);
    contextMock = (Context) contextControl.getMock();

    contextControl2 = MockControl.createControl(Context.class);
    contextMock2 = (Context) contextControl2.getMock();

    tested = new DefaultDirObjectFactory();
  }
  /**
   * 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();
  }
  protected void setUp() throws Exception {
    super.setUp();

    ctrlStatement = MockControl.createControl(Statement.class);
    mockStatement = (Statement) ctrlStatement.getMock();
    ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
    mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
    ctrlResultSet = MockControl.createControl(ResultSet.class);
    mockResultSet = (ResultSet) ctrlResultSet.getMock();
    ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
    mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
  }
  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();
  }
  @Before
  public void setUp() throws Exception {
    super.setUp();

    controlConsumer_ = MockControl.createNiceControl(IProxyPushSupplier.class);
    mockConsumer_ = (IProxyPushSupplier) controlConsumer_.getMock();

    controlPushOperation_ = MockControl.createNiceControl(PushOperation.class);
    mockPushOperation_ = (PushOperation) controlPushOperation_.getMock();

    setUpTest();

    objectUnderTest_ = newRetryStrategy();
  }
  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 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 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());
  }
  /**
   * 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();
  }
  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();
  }
  /** 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();
  }
  /** 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 testLocalSessionFactoryBeanWithEntityInterceptor() throws Exception {
   LocalSessionFactoryBean sfb =
       new LocalSessionFactoryBean() {
         protected Configuration newConfiguration() {
           return new Configuration() {
             public Configuration setInterceptor(Interceptor interceptor) {
               throw new IllegalArgumentException(interceptor.toString());
             }
           };
         }
       };
   sfb.setMappingResources(new String[0]);
   sfb.setDataSource(new DriverManagerDataSource());
   MockControl interceptorControl = MockControl.createControl(Interceptor.class);
   Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock();
   interceptorControl.replay();
   sfb.setEntityInterceptor(entityInterceptor);
   try {
     sfb.afterPropertiesSet();
     fail("Should have thrown IllegalArgumentException");
   } catch (IllegalArgumentException ex) {
     // expected
     assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString()));
   }
 }
示例#29
0
 @Test
 public void defaultName() {
   control = MockControl.createControl(IMethods.class);
   String expected = "EasyMock for " + IMethods.class.toString();
   String actual = control.getMock().toString();
   assertEquals(expected, actual);
 }
  /** Check that two transactions are created and committed. */
  public void testTwoTransactionsShouldSucceed() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();

    MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource();
    tas1.register(getNameMethod, txatt);
    MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource();
    tas2.register(setNameMethod, txatt);

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

    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, new TransactionAttributeSource[] {tas1, tas2});

    checkTransactionStatus(false);
    itb.getName();
    checkTransactionStatus(false);
    itb.setName("myName");
    checkTransactionStatus(false);

    ptmControl.verify();
  }