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();
  }
  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 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 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 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 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 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();
  }
  /*
   * Make sure that we fallback to default provider
   *
   * Test method for
   * 'org.springmodules.jcr.support.AbstractSessionHolderProviderManager.getSessionProvider(SessionFactory)'
   */
  public void testDifferentSessionProvider() {
    // sanity check

    customProvider =
        new SessionHolderProvider() {

          /**
           * @see org.springmodules.jcr.SessionHolderProvider#acceptsRepository(java.lang.String)
           */
          public boolean acceptsRepository(String repo) {
            return false;
          }

          /**
           * @see org.springmodules.jcr.SessionHolderProvider#createSessionHolder(javax.jcr.Session)
           */
          public SessionHolder createSessionHolder(Session session) {
            return null;
          }
        };
    providers = new ArrayList();
    providers.add(customProvider);

    sfCtrl.replay();
    sessCtrl.replay();
    repoCtrl.replay();

    assertSame(
        GenericSessionHolderProvider.class, providerManager.getSessionProvider(repo).getClass());
  }
 protected void replay() {
   super.replay();
   ctrlStatement.replay();
   ctrlPreparedStatement.replay();
   ctrlResultSet.replay();
   ctrlResultSetMetaData.replay();
 }
  public void testReplaceEmpty() throws Exception {
    mockInitialQueue_.hasPendingMessages();
    controlInitialQueue_.setReturnValue(false);
    controlInitialQueue_.replay();
    controlReplacementQueue_.replay();

    objectUnderTest_.replaceDelegate(mockReplacementQueue_);
  }
  /*
   * Default provider is used even on empty list.
   *
   * Test method for
   * 'org.springmodules.jcr.support.AbstractSessionHolderProviderManager.getSessionProvider(SessionFactory)'
   */
  public void testDefaultSessionProvider() {
    // sanity check
    assertSame(providers, providerManager.getProviders());

    sfCtrl.replay();
    sessCtrl.replay();
    repoCtrl.replay();

    SessionHolderProvider provider = providerManager.getSessionProvider(repo);
    assertSame(GenericSessionHolderProvider.class, provider.getClass());
  }
  /*
   * Make sure that the approapriate provider is selected Test method for
   * 'org.springmodules.jcr.support.AbstractSessionHolderProviderManager.getSessionProvider(SessionFactory)'
   */
  public void testCustomSessionProvider() {
    // sanity check

    providers = new ArrayList();
    providers.add(customProvider);

    sfCtrl.replay();
    sessCtrl.replay();
    repoCtrl.replay();

    assertSame(customProvider, providerManager.getSessionProvider(repo));
  }
 /*
  * Test method for 'org.db4ospring.support.Db4oDaoSupport.checkDaoConfig'
  */
 public void testMissingConfiguration() {
   containerControl.replay();
   templateControl.replay();
   try {
     Db4oDaoSupport dao = new Db4oDaoSupport() {};
     dao.afterPropertiesSet();
     fail(
         "Should have thrown IllegalArgumentException because neither #setObjectContainer nor #setTemplate have been invoked");
   } catch (IllegalArgumentException ex) {
     // it's ok. We expect this exception to be thrown
   }
 }
  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();
  }
  /*
   * Test method for
   * 'org.db4ospring.support.Db4oDaoSupport.setObjectContainer'
   */
  public void testConfigureWithObjectContainer() {
    containerControl.replay();
    templateControl.replay();

    Db4oDaoSupport dao = new Db4oDaoSupport() {};
    dao.setObjectContainer(container);
    // must not throw an exception
    dao.afterPropertiesSet();

    assertNotNull(dao.getDb4oTemplate());
    assertEquals(container, dao.getDb4oTemplate().getObjectContainer());
    assertEquals(container, dao.getObjectContainer());
  }
  /*
   * Test method for 'org.db4ospring.support.Db4oDaoSupport.setDb4oTemplate'
   */
  public void testConfigureWithTemplate() {
    templateControl.expectAndReturn(template.getObjectContainer(), container, 2);

    containerControl.replay();
    templateControl.replay();

    Db4oDaoSupport dao = new Db4oDaoSupport() {};
    dao.setDb4oTemplate(template);
    dao.afterPropertiesSet();

    assertEquals(template, dao.getDb4oTemplate());
    assertEquals(container, dao.getObjectContainer());
    assertEquals(container, dao.getDb4oTemplate().getObjectContainer());
  }
  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 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();
  }
  @Test
  public void testInitMissingParam() {
    MockControl mockRendererComponentFactoryDescriptorControl =
        MockClassControl.createControl(RendererComponentFactoryDescriptor.class);
    RendererComponentFactoryDescriptor mockRendererComponentFactoryDescriptor =
        (RendererComponentFactoryDescriptor)
            mockRendererComponentFactoryDescriptorControl.getMock();
    mockRendererComponentFactoryDescriptor.getPlugin();
    mockRendererComponentFactoryDescriptorControl.setReturnValue(null);
    mockRendererComponentFactoryDescriptor.getParams();
    mockRendererComponentFactoryDescriptorControl.setReturnValue(new HashMap());
    mockRendererComponentFactoryDescriptor.getCompleteKey();
    mockRendererComponentFactoryDescriptorControl.setReturnValue("pluginkey");
    mockRendererComponentFactoryDescriptorControl.replay();

    SimpleRendererComponentFactory simpleRendererComponentFactory =
        new SimpleRendererComponentFactory();
    try {
      simpleRendererComponentFactory.init(mockRendererComponentFactoryDescriptor);
      fail("Expected exception");
    } catch (PluginParseException e) {
      assertTrue(e.getMessage().indexOf("is missing the required") > 0);
      assertTrue(e.getMessage().indexOf("pluginkey") > 0);
    }
  }
 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();
 }
  /** 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 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());
  }
  /**
   * 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();
  }
  /** 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()));
   }
 }
  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();
  }