Search in sources :

Example 1 with EnabledForTestGroups

use of org.springframework.core.testfixture.EnabledForTestGroups in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method transactionWithTimeout.

@ParameterizedTest(name = "transaction with {0} second timeout")
@ValueSource(ints = { 1, 10 })
@EnabledForTestGroups(LONG_RUNNING)
public void transactionWithTimeout(int timeout) throws Exception {
    PreparedStatement ps = mock(PreparedStatement.class);
    given(con.getAutoCommit()).willReturn(true);
    given(con.prepareStatement("some SQL statement")).willReturn(ps);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setTimeout(timeout);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                }
                try {
                    Connection con = DataSourceUtils.getConnection(ds);
                    PreparedStatement ps = con.prepareStatement("some SQL statement");
                    DataSourceUtils.applyTransactionTimeout(ps, ds);
                } catch (SQLException ex) {
                    throw new DataAccessResourceFailureException("", ex);
                }
            }
        });
        if (timeout <= 1) {
            fail("Should have thrown TransactionTimedOutException");
        }
    } catch (TransactionTimedOutException ex) {
        if (timeout <= 1) {
        // expected
        } else {
            throw ex;
        }
    }
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    if (timeout > 1) {
        verify(ps).setQueryTimeout(timeout - 1);
        verify(con).commit();
    } else {
        verify(con).rollback();
    }
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : TransactionTimedOutException(org.springframework.transaction.TransactionTimedOutException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForTestGroups(org.springframework.core.testfixture.EnabledForTestGroups)

Example 2 with EnabledForTestGroups

use of org.springframework.core.testfixture.EnabledForTestGroups in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method transactionWithTimeout.

@ParameterizedTest(name = "transaction with {0} second timeout")
@ValueSource(ints = { 1, 10 })
@EnabledForTestGroups(LONG_RUNNING)
public void transactionWithTimeout(int timeout) throws Exception {
    PreparedStatement ps = mock(PreparedStatement.class);
    given(con.getAutoCommit()).willReturn(true);
    given(con.prepareStatement("some SQL statement")).willReturn(ps);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setTimeout(timeout);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                }
                try {
                    Connection con = DataSourceUtils.getConnection(ds);
                    PreparedStatement ps = con.prepareStatement("some SQL statement");
                    DataSourceUtils.applyTransactionTimeout(ps, ds);
                } catch (SQLException ex) {
                    throw new DataAccessResourceFailureException("", ex);
                }
            }
        });
        if (timeout <= 1) {
            fail("Should have thrown TransactionTimedOutException");
        }
    } catch (TransactionTimedOutException ex) {
        if (timeout <= 1) {
        // expected
        } else {
            throw ex;
        }
    }
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    if (timeout > 1) {
        verify(ps).setQueryTimeout(timeout - 1);
        verify(con).commit();
    } else {
        verify(con).rollback();
    }
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : TransactionTimedOutException(org.springframework.transaction.TransactionTimedOutException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForTestGroups(org.springframework.core.testfixture.EnabledForTestGroups)

Example 3 with EnabledForTestGroups

use of org.springframework.core.testfixture.EnabledForTestGroups in project spring-framework by spring-projects.

the class QuartzSupportTests method schedulerAccessorBean.

@Test
@EnabledForTestGroups(LONG_RUNNING)
void schedulerAccessorBean() throws Exception {
    try (ClassPathXmlApplicationContext ctx = context("schedulerAccessorBean.xml")) {
        QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
        QuartzTestBean importService = (QuartzTestBean) ctx.getBean("importService");
        Thread.sleep(400);
        assertThat(exportService.getImportCount()).as("doImport called exportService").isEqualTo(0);
        assertThat(exportService.getExportCount()).as("doExport not called on exportService").isEqualTo(2);
        assertThat(importService.getImportCount()).as("doImport not called on importService").isEqualTo(2);
        assertThat(importService.getExportCount()).as("doExport called on importService").isEqualTo(0);
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.jupiter.api.Test) EnabledForTestGroups(org.springframework.core.testfixture.EnabledForTestGroups)

Example 4 with EnabledForTestGroups

use of org.springframework.core.testfixture.EnabledForTestGroups in project spring-framework by spring-projects.

the class QuartzSupportTests method schedulerWithSpringBeanJobFactoryAndQuartzJobBean.

@Test
@EnabledForTestGroups(LONG_RUNNING)
void schedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Exception {
    DummyJobBean.param = 0;
    DummyJobBean.count = 0;
    JobDetailImpl jobDetail = new JobDetailImpl();
    jobDetail.setDurability(true);
    jobDetail.setJobClass(DummyJobBean.class);
    jobDetail.setName("myJob");
    jobDetail.getJobDataMap().put("param", "10");
    SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
    trigger.setName("myTrigger");
    trigger.setJobDetail(jobDetail);
    trigger.setStartDelay(1);
    trigger.setRepeatInterval(500);
    trigger.setRepeatCount(1);
    trigger.afterPropertiesSet();
    SchedulerFactoryBean bean = new SchedulerFactoryBean();
    bean.setJobFactory(new SpringBeanJobFactory());
    bean.setTriggers(trigger.getObject());
    bean.setJobDetails(jobDetail);
    bean.afterPropertiesSet();
    bean.start();
    Thread.sleep(500);
    assertThat(DummyJobBean.param).isEqualTo(10);
    assertThat(DummyJobBean.count > 0).isTrue();
    bean.destroy();
}
Also used : JobDetailImpl(org.quartz.impl.JobDetailImpl) Test(org.junit.jupiter.api.Test) EnabledForTestGroups(org.springframework.core.testfixture.EnabledForTestGroups)

Example 5 with EnabledForTestGroups

use of org.springframework.core.testfixture.EnabledForTestGroups in project spring-framework by spring-projects.

the class QuartzSupportTests method schedulerWithQuartzJobBean.

@Test
@EnabledForTestGroups(LONG_RUNNING)
void schedulerWithQuartzJobBean() throws Exception {
    DummyJob.param = 0;
    DummyJob.count = 0;
    JobDetailImpl jobDetail = new JobDetailImpl();
    jobDetail.setDurability(true);
    jobDetail.setJobClass(DummyJobBean.class);
    jobDetail.setName("myJob");
    jobDetail.getJobDataMap().put("param", "10");
    SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
    trigger.setName("myTrigger");
    trigger.setJobDetail(jobDetail);
    trigger.setStartDelay(1);
    trigger.setRepeatInterval(500);
    trigger.setRepeatCount(1);
    trigger.afterPropertiesSet();
    SchedulerFactoryBean bean = new SchedulerFactoryBean();
    bean.setTriggers(trigger.getObject());
    bean.setJobDetails(jobDetail);
    bean.afterPropertiesSet();
    bean.start();
    Thread.sleep(500);
    assertThat(DummyJobBean.param).isEqualTo(10);
    assertThat(DummyJobBean.count > 0).isTrue();
    bean.destroy();
}
Also used : JobDetailImpl(org.quartz.impl.JobDetailImpl) Test(org.junit.jupiter.api.Test) EnabledForTestGroups(org.springframework.core.testfixture.EnabledForTestGroups)

Aggregations

EnabledForTestGroups (org.springframework.core.testfixture.EnabledForTestGroups)25 Test (org.junit.jupiter.api.Test)23 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 JobDetailImpl (org.quartz.impl.JobDetailImpl)5 NoOpRunnable (org.springframework.core.task.NoOpRunnable)5 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 InOrder (org.mockito.InOrder)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 TransactionTimedOutException (org.springframework.transaction.TransactionTimedOutException)2 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 HttpWebConnection (com.gargoylesoftware.htmlunit.HttpWebConnection)1