Skip to content

Introduce EJB-based transactional tests in the TCF #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,9 @@ project("spring-test") {
testCompile(project(":spring-context-support"))
testCompile(project(":spring-oxm"))
testCompile(project(":spring-webmvc-tiles3"))
testCompile("javax.ejb:ejb-api:3.0")
testCompile("org.hibernate:hibernate-core:${hibernate3Version}")
testCompile("org.hibernate:hibernate-entitymanager:${hibernate3Version}")
testCompile "org.slf4j:slf4j-jcl:${slf4jVersion}"
testCompile("org.hsqldb:hsqldb:${hsqldbVersion}")
testCompile("org.hibernate:hibernate-validator:4.3.0.Final")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.testng.transaction.ejb;

import javax.ejb.EJB;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.transaction.ejb.dao.TestEntityDao;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.*;

/**
* Abstract base class for all TestNG-based tests involving EJB transaction
* support in the TestContext framework.
*
* @author Sam Brannen
* @author Xavier Detant
* @since 4.0.1
*/
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public abstract class AbstractEjbTxDaoTestNGTests extends AbstractTransactionalTestNGSpringContextTests {

protected static final String TEST_NAME = "test-name";

@EJB
protected TestEntityDao dao;

@PersistenceContext
protected EntityManager em;


@Test
public void test1InitialState() {
int count = dao.getCount(TEST_NAME);
assertEquals("New TestEntity should have count=0.", 0, count);
}

@Test(dependsOnMethods = "test1InitialState")
public void test2IncrementCount1() {
int count = dao.incrementCount(TEST_NAME);
assertEquals("Expected count=1 after first increment.", 1, count);
}

/**
* The default implementation of this method assumes that the transaction
* for {@link #test2IncrementCount1()} was committed. Therefore, it is
* expected that the previous increment has been persisted in the database.
*/
@Test(dependsOnMethods = "test2IncrementCount1")
public void test3IncrementCount2() {
int count = dao.getCount(TEST_NAME);
assertEquals("Expected count=1 after test2IncrementCount1().", 1, count);

count = dao.incrementCount(TEST_NAME);
assertEquals("Expected count=2 now.", 2, count);
}

@AfterMethod(alwaysRun = true)
public void synchronizePersistenceContext() {
em.flush();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.testng.transaction.ejb;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.transaction.ejb.dao.RequiredEjbTxTestEntityDao;
import org.testng.annotations.Test;

/**
* Concrete subclass of {@link AbstractEjbTxDaoTestNGTests} which uses the
* {@link RequiredEjbTxTestEntityDao} and sets the default rollback semantics
* for the {@link TransactionalTestExecutionListener} to {@code false} (i.e.,
* <em>commit</em>).
*
* @author Sam Brannen
* @since 4.0.1
*/
@Test(suiteName = "Commit for REQUIRED")
@ContextConfiguration("/org/springframework/test/context/transaction/ejb/required-tx-config.xml")
@TransactionConfiguration(defaultRollback = false)
public class CommitForRequiredEjbTxDaoTestNGTests extends AbstractEjbTxDaoTestNGTests {

/* test methods in superclass */

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.testng.transaction.ejb;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.transaction.ejb.dao.RequiresNewEjbTxTestEntityDao;
import org.testng.annotations.Test;

/**
* Concrete subclass of {@link AbstractEjbTxDaoTestNGTests} which uses the
* {@link RequiresNewEjbTxTestEntityDao} and sets the default rollback semantics
* for the {@link TransactionalTestExecutionListener} to {@code false} (i.e.,
* <em>commit</em>).
*
* @author Sam Brannen
* @since 4.0.1
*/
@Test(suiteName = "Commit for REQUIRES_NEW")
@ContextConfiguration("/org/springframework/test/context/transaction/ejb/requires-new-tx-config.xml")
@TransactionConfiguration(defaultRollback = false)
public class CommitForRequiresNewEjbTxDaoTestNGTests extends AbstractEjbTxDaoTestNGTests {

/* test methods in superclass */

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.testng.transaction.ejb;

import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.*;

/**
* Extension of {@link CommitForRequiredEjbTxDaoTestNGTests} which sets the default
* rollback semantics for the {@link TransactionalTestExecutionListener} to
* {@code true}. The transaction managed by the TestContext framework will be
* rolled back after each test method. Consequently, any work performed in
* transactional methods that participate in the test-managed transaction will
* be rolled back automatically.
*
* @author Sam Brannen
* @since 4.0.1
*/
@Test(suiteName = "Rollback for REQUIRED")
@TransactionConfiguration(defaultRollback = true)
public class RollbackForRequiredEjbTxDaoTestNGTests extends CommitForRequiredEjbTxDaoTestNGTests {

/**
* Redeclared to ensure test method execution order. Simply delegates to super.
*/
@Test
@Override
public void test1InitialState() {
super.test1InitialState();
}

/**
* Redeclared to ensure test method execution order. Simply delegates to super.
*/
@Test(dependsOnMethods = "test1InitialState")
@Override
public void test2IncrementCount1() {
super.test2IncrementCount1();
}

/**
* Overrides parent implementation in order to change expectations to align with
* behavior associated with "required" transactions on repositories/DAOs and
* default rollback semantics for transactions managed by the TestContext
* framework.
*/
@Test(dependsOnMethods = "test2IncrementCount1")
@Override
public void test3IncrementCount2() {
int count = dao.getCount(TEST_NAME);
// Expecting count=0 after test2IncrementCount1() since REQUIRED transactions
// participate in the existing transaction (if present), which in this case is the
// transaction managed by the TestContext framework which will be rolled back
// after each test method.
assertEquals("Expected count=0 after test2IncrementCount1().", 0, count);

count = dao.incrementCount(TEST_NAME);
assertEquals("Expected count=1 now.", 1, count);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.testng.transaction.ejb;

import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.testng.annotations.Test;

/**
* Extension of {@link CommitForRequiresNewEjbTxDaoTestNGTests} which sets the default
* rollback semantics for the {@link TransactionalTestExecutionListener} to
* {@code true}. The transaction managed by the TestContext framework will be
* rolled back after each test method. Consequently, any work performed in
* transactional methods that participate in the test-managed transaction will
* be rolled back automatically. On the other hand, any work performed in
* transactional methods that do <strong>not</strong> participate in the
* test-managed transaction will not be affected by the rollback of the
* test-managed transaction. For example, such work may in fact be committed
* outside the scope of the test-managed transaction.
*
* @author Sam Brannen
* @since 4.0.1
*/
@Test(suiteName = "Rollback for REQUIRES_NEW")
@TransactionConfiguration(defaultRollback = true)
public class RollbackForRequiresNewEjbTxDaoTestNGTests extends CommitForRequiresNewEjbTxDaoTestNGTests {

/* test methods in superclass */

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="EJB-TX-Package" verbose="1">
<test name="Package">
<packages>
<package name="org.springframework.test.context.testng.transaction.ejb" />
</packages>
</test>
</suite>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="EJB-TX-Separate" verbose="1">
<test name="RollbackForRequiredEjbTxDaoTestNGTests">
<classes>
<class name="org.springframework.test.context.testng.transaction.ejb.RollbackForRequiredEjbTxDaoTestNGTests" />
</classes>
</test>
<test name="CommitForRequiredEjbTxDaoTestNGTests">
<classes>
<class name="org.springframework.test.context.testng.transaction.ejb.CommitForRequiredEjbTxDaoTestNGTests" />
</classes>
</test>
<test name="CommitForRequiresNewEjbTxDaoTestNGTests">
<classes>
<class name="org.springframework.test.context.testng.transaction.ejb.CommitForRequiresNewEjbTxDaoTestNGTests" />
</classes>
</test>
<test name="RollbackForRequiresNewEjbTxDaoTestNGTests">
<classes>
<class name="org.springframework.test.context.testng.transaction.ejb.RollbackForRequiresNewEjbTxDaoTestNGTests" />
</classes>
</test>
</suite>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="EJB-TX-Together" verbose="1">
<test name="Together">
<classes>
<class name="org.springframework.test.context.testng.transaction.ejb.RollbackForRequiredEjbTxDaoTestNGTests" />
<class name="org.springframework.test.context.testng.transaction.ejb.CommitForRequiredEjbTxDaoTestNGTests" />
<class name="org.springframework.test.context.testng.transaction.ejb.CommitForRequiresNewEjbTxDaoTestNGTests" />
<class name="org.springframework.test.context.testng.transaction.ejb.RollbackForRequiresNewEjbTxDaoTestNGTests" />
</classes>
</test>
</suite>
Loading