OPENJPA-2595 upgrade our test suite to junit-4

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1683154 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Struberg 2015-06-02 18:04:26 +00:00
parent e918d4d0e5
commit 0b26209a2f
14 changed files with 146 additions and 64 deletions

View File

@ -81,7 +81,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jmock</groupId> <groupId>org.jmock</groupId>
<artifactId>jmock-junit3</artifactId> <artifactId>jmock-junit4</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -28,15 +28,23 @@ import javax.sql.DataSource;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration; import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.kernel.StoreContext; import org.apache.openjpa.kernel.StoreContext;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.integration.junit3.MockObjectTestCase; import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Rule;
import org.junit.Test;
public class TestDB2Dictionary extends MockObjectTestCase { import static org.junit.Assert.*;
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
final Statement mockStatement = mock(Statement.class);
final Connection mockConnection = mock(Connection.class); public class TestDB2Dictionary {
final ResultSet mockRS = mock(ResultSet.class); @Rule
final DataSource mockDS = mock(DataSource.class); public JUnitRuleMockery context = new JUnitRuleMockery();
final DatabaseMetaData mockMetaData = mock(DatabaseMetaData.class);
final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final Statement mockStatement = context.mock(Statement.class);
final Connection mockConnection = context.mock(Connection.class);
final ResultSet mockRS = context.mock(ResultSet.class);
final DataSource mockDS = context.mock(DataSource.class);
final DatabaseMetaData mockMetaData = context.mock(DatabaseMetaData.class);
final StoreContext sc = null; final StoreContext sc = null;
final String schema = "abcd"; final String schema = "abcd";
@ -44,11 +52,13 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/* /*
* When DS1 is non null we should get a connection and use it to obtain the schema name. * When DS1 is non null we should get a connection and use it to obtain the schema name.
*/ */
@Test
public void testGetDefaultSchemaNameDS1() throws Exception { public void testGetDefaultSchemaNameDS1() throws Exception {
// Expected method calls on the mock objects above. If any of these are // Expected method calls on the mock objects above. If any of these are
// do not occur, or if any other methods are invoked on the mock objects // do not occur, or if any other methods are invoked on the mock objects
// an exception will be thrown and the test will fail. // an exception will be thrown and the test will fail.
checking(new Expectations() { context.checking(new Expectations()
{
{ {
// Wiring, make sure the appropriate mocks are created. // Wiring, make sure the appropriate mocks are created.
oneOf(mockConfiguration).getDataSource(with(equal(sc))); oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -89,11 +99,13 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/* /*
* When ds1 is null, fallback to ds2 * When ds1 is null, fallback to ds2
*/ */
@Test
public void testGetDefaultSchemaNameDS2() throws Exception { public void testGetDefaultSchemaNameDS2() throws Exception {
// Expected method calls on the mock objects above. If any of these are // Expected method calls on the mock objects above. If any of these are
// do not occur, or if any other methods are invoked on the mock objects // do not occur, or if any other methods are invoked on the mock objects
// an exception will be thrown and the test will fail. // an exception will be thrown and the test will fail.
checking(new Expectations() { context.checking(new Expectations()
{
{ {
// Wiring, make sure the appropriate mocks are created. // Wiring, make sure the appropriate mocks are created.
oneOf(mockConfiguration).getDataSource(with(equal(sc))); oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -137,11 +149,13 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/* /*
* When ds1 is null, fallback to ds2 * When ds1 is null, fallback to ds2
*/ */
@Test
public void testGetDefaultSchemaNameNoDS() throws Exception { public void testGetDefaultSchemaNameNoDS() throws Exception {
// Expected method calls on the mock objects above. If any of these are // Expected method calls on the mock objects above. If any of these are
// do not occur, or if any other methods are invoked on the mock objects // do not occur, or if any other methods are invoked on the mock objects
// an exception will be thrown and the test will fail. // an exception will be thrown and the test will fail.
checking(new Expectations() { context.checking(new Expectations()
{
{ {
// both datasources are null for this test. // both datasources are null for this test.
oneOf(mockConfiguration).getDataSource(with(equal(sc))); oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -163,12 +177,14 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/* /*
* TestWhitespace trim * TestWhitespace trim
*/ */
@Test
public void testGetDefaultSchemaNameTrimmed() throws Exception { public void testGetDefaultSchemaNameTrimmed() throws Exception {
final String schema2 = "abcd "; final String schema2 = "abcd ";
// Expected method calls on the mock objects above. If any of these are // Expected method calls on the mock objects above. If any of these are
// do not occur, or if any other methods are invoked on the mock objects // do not occur, or if any other methods are invoked on the mock objects
// an exception will be thrown and the test will fail. // an exception will be thrown and the test will fail.
checking(new Expectations() { context.checking(new Expectations()
{
{ {
// Wiring, make sure the appropriate mocks are created. // Wiring, make sure the appropriate mocks are created.
oneOf(mockConfiguration).getDataSource(with(equal(sc))); oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -209,8 +225,10 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/* /*
* Verifies that the ConnectedConfiguration method only uses the DBMetaData to determine the correct behavior. * Verifies that the ConnectedConfiguration method only uses the DBMetaData to determine the correct behavior.
*/ */
@Test
public void testConnectedConfigurationOnlyUsesMetaData() throws Exception { public void testConnectedConfigurationOnlyUsesMetaData() throws Exception {
checking(new Expectations() { context.checking(new Expectations()
{
{ {
// No activity on the connection other than getting the metadata. // No activity on the connection other than getting the metadata.
allowing(mockConnection).getMetaData(); allowing(mockConnection).getMetaData();

View File

@ -24,15 +24,24 @@ import org.apache.openjpa.jdbc.identifier.DBIdentifierUtilImpl;
import org.apache.openjpa.jdbc.schema.Table; import org.apache.openjpa.jdbc.schema.Table;
import org.apache.openjpa.util.UserException; import org.apache.openjpa.util.UserException;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.integration.junit3.MockObjectTestCase; import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Rule;
import org.junit.Test;
public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase { import static org.junit.Assert.*;
public class TestDBDictionaryGeneratedSQL {
@Rule
public JUnitRuleMockery context = new JUnitRuleMockery();
@Test
public void testCreateTableLongNameException() { public void testCreateTableLongNameException() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class); final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl(); final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() { context.checking(new Expectations()
{
{ {
allowing(mockConfiguration).getIdentifierUtilInstance(); allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl)); will(returnValue(idImpl));
@ -58,11 +67,13 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
} }
@Test
public void testThrowsExceptionWithSchemaSet() { public void testThrowsExceptionWithSchemaSet() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class); final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl(); final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() { context.checking(new Expectations()
{
{ {
allowing(mockConfiguration).getIdentifierUtilInstance(); allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl)); will(returnValue(idImpl));
@ -88,11 +99,13 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
} }
} }
@Test
public void testSchemaNameIsNotConsidered() { public void testSchemaNameIsNotConsidered() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class); final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl(); final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() { context.checking(new Expectations()
{
{ {
allowing(mockConfiguration).getIdentifierUtilInstance(); allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl)); will(returnValue(idImpl));
@ -115,11 +128,13 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
assertTrue(sqls[0].contains("IAmASchema")); assertTrue(sqls[0].contains("IAmASchema"));
} }
@Test
public void testOverrideProperty() { public void testOverrideProperty() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class); final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl(); final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() { context.checking(new Expectations()
{
{ {
allowing(mockConfiguration).getIdentifierUtilInstance(); allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl)); will(returnValue(idImpl));
@ -146,11 +161,13 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
} }
} }
@Test
public void testOverridePropertyShortName() { public void testOverridePropertyShortName() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class); final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl(); final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() { context.checking(new Expectations()
{
{ {
allowing(mockConfiguration).getIdentifierUtilInstance(); allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl)); will(returnValue(idImpl));

View File

@ -26,9 +26,17 @@ import java.sql.ResultSet;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration; import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfigurationImpl; import org.apache.openjpa.jdbc.kernel.JDBCFetchConfigurationImpl;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.integration.junit3.MockObjectTestCase; import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestMySQLDictionary extends MockObjectTestCase { public class TestMySQLDictionary {
@Rule
public JUnitRuleMockery context = new JUnitRuleMockery();
@Test
public void testDBDictionaryGetBatchFetchSize() throws Exception { public void testDBDictionaryGetBatchFetchSize() throws Exception {
DBDictionary db = new MySQLDictionary(); DBDictionary db = new MySQLDictionary();
assertEquals(Integer.MIN_VALUE, db.getBatchFetchSize(1)); assertEquals(Integer.MIN_VALUE, db.getBatchFetchSize(1));
@ -46,17 +54,19 @@ public class TestMySQLDictionary extends MockObjectTestCase {
* If any of the expectations are not met or any unexpected * If any of the expectations are not met or any unexpected
* method calls are made * method calls are made
*/ */
@Test
public void testPreparedStatementGetFetchBatchSize() throws Exception { public void testPreparedStatementGetFetchBatchSize() throws Exception {
DBDictionary db = new MySQLDictionary(); DBDictionary db = new MySQLDictionary();
SQLBuffer sql = new SQLBuffer(db); SQLBuffer sql = new SQLBuffer(db);
final PreparedStatement mockStatement = mock(PreparedStatement.class); final PreparedStatement mockStatement = context.mock(PreparedStatement.class);
final Connection mockConnection = mock(Connection.class); final Connection mockConnection = context.mock(Connection.class);
// Expected method calls on the mock objects above. If any of these are // Expected method calls on the mock objects above. If any of these are
// do not occur, or if any other methods are invoked on the mock objects // do not occur, or if any other methods are invoked on the mock objects
// an exception will be thrown and the test will fail. // an exception will be thrown and the test will fail.
checking(new Expectations() { context.checking(new Expectations()
{
{ {
oneOf(mockConnection).prepareStatement(with(any(String.class))); oneOf(mockConnection).prepareStatement(with(any(String.class)));
will(returnValue(mockStatement)); will(returnValue(mockStatement));
@ -81,17 +91,18 @@ public class TestMySQLDictionary extends MockObjectTestCase {
* If any of the expectations are not met or any unexpected * If any of the expectations are not met or any unexpected
* method calls are made * method calls are made
*/ */
@Test
public void testPreparedCallGetFetchBatchSize() throws Exception { public void testPreparedCallGetFetchBatchSize() throws Exception {
DBDictionary db = new MySQLDictionary(); DBDictionary db = new MySQLDictionary();
SQLBuffer sql = new SQLBuffer(db); SQLBuffer sql = new SQLBuffer(db);
final CallableStatement mockStatement = mock(CallableStatement.class); final CallableStatement mockStatement = context.mock(CallableStatement.class);
final Connection mockConnection = mock(Connection.class); final Connection mockConnection = context.mock(Connection.class);
// Expected method calls on the mock objects above. If any of these are // Expected method calls on the mock objects above. If any of these are
// do not occur, or if any other methods are invoked on the mock objects // do not occur, or if any other methods are invoked on the mock objects
// an exception will be thrown and the test will fail. // an exception will be thrown and the test will fail.
checking(new Expectations() { context.checking(new Expectations() {
{ {
oneOf(mockConnection).prepareCall(with(any(String.class))); oneOf(mockConnection).prepareCall(with(any(String.class)));
will(returnValue(mockStatement)); will(returnValue(mockStatement));

View File

@ -915,7 +915,7 @@
<excludes> <excludes>
<!-- exclude classes that end with 'Test'; these <!-- exclude classes that end with 'Test'; these
are not test cases per OpenJPA standards --> are not test cases per OpenJPA standards -->
<exclude>org/apache/openjpa/**/*Test.java</exclude> <exclude>org/apache/openjpa/**/*Testq.java</exclude>
<!-- exclude classes that include a $; inner classes <!-- exclude classes that include a $; inner classes
are not test cases per OpenJPA standards --> are not test cases per OpenJPA standards -->

View File

@ -113,6 +113,9 @@ import org.apache.openjpa.persistence.spring.TestLibService;
import org.apache.openjpa.persistence.xml.TestSimpleXmlEntity; import org.apache.openjpa.persistence.xml.TestSimpleXmlEntity;
import org.apache.openjpa.persistence.xml.TestXmlOverrideEntity; import org.apache.openjpa.persistence.xml.TestXmlOverrideEntity;
/**
* TODO: this should be refactored to a @RunWith or similar...
*/
public class DynamicEnhancementSuite extends TestCase { public class DynamicEnhancementSuite extends TestCase {
static { static {
Persistence.createEntityManagerFactory("test", System.getProperties()); Persistence.createEntityManagerFactory("test", System.getProperties());
@ -125,7 +128,7 @@ public class DynamicEnhancementSuite extends TestCase {
// with the dynamic enhaner. // with the dynamic enhaner.
String test = System.getProperty("dynamicTest"); String test = System.getProperty("dynamicTest");
if (test != null) { if (test != null) {
suite.addTestSuite(Class.forName(test)); suite.addTestSuite((Class<? extends TestCase>) Class.forName(test));
} else { } else {
// Subclassing failing tests // Subclassing failing tests

View File

@ -180,3 +180,11 @@ Building and deploying the Site
=============================== ===============================
$ mvn site site:deploy -Pjavadoc-profile,docbook-profile $ mvn site site:deploy -Pjavadoc-profile,docbook-profile
Running unit tests in the Debugger
==================================
TODO: finish!
-Dopenjpa.ConnectionURL=jdbc:derby:target/database/openjpa-derby-database;create=true -Dopenjpa.ConnectionDriverName=org.apache.derby.jdbc.EmbeddedDriver \

View File

@ -12,3 +12,5 @@ Please refer to the following files for more information:
For documentation and project information, please visit our project site: For documentation and project information, please visit our project site:
http://openjpa.apache.org/ http://openjpa.apache.org/

View File

@ -133,6 +133,12 @@
<argLine>${test.jvm.arguments}</argLine> <argLine>${test.jvm.arguments}</argLine>
<excludes> <excludes>
<exclude>**/TestQueryMultiThreaded.java</exclude> <exclude>**/TestQueryMultiThreaded.java</exclude>
<!-- exclude classes that include a $; inner classes
are not test cases per OpenJPA standards -->
<exclude>org/apache/openjpa/**/*$*.class</exclude>
<exclude>org/apache/openjpa/**/*.*.class</exclude>
</excludes> </excludes>
<systemProperties> <systemProperties>
<property> <property>

View File

@ -24,6 +24,8 @@ import java.util.Random;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.RollbackException; import javax.persistence.RollbackException;
import org.junit.Ignore;
/** /**
* Tests that if any of the slices fail then none of the slices are committed. * Tests that if any of the slices fail then none of the slices are committed.
* *
@ -139,6 +141,7 @@ public class TestTransaction extends SliceTestCase {
* @author Pinaki Poddar * @author Pinaki Poddar
* *
*/ */
@Ignore
public static class CarDistributorPolicy implements DistributionPolicy { public static class CarDistributorPolicy implements DistributionPolicy {
public String distribute(Object pc, List<String> slices, Object context) { public String distribute(Object pc, List<String> slices, Object context) {
if (pc instanceof Manufacturer) { if (pc instanceof Manufacturer) {

View File

@ -57,8 +57,6 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope>
<version>3.8.1</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -22,17 +22,18 @@ import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
@Entity @Entity
public class TestEntity { public class SampleEntity
{
@Id @Id
private int xint1; private int xint1;
private String string1; private String string1;
public TestEntity() { public SampleEntity() {
} }
public TestEntity(int int1, String string1) { public SampleEntity(int int1, String string1) {
this.xint1 = int1; this.xint1 = int1;
this.string1 = string1; this.string1 = string1;
} }

View File

@ -23,7 +23,7 @@
<!-- simply all annotated persistent entities will be part of this unit --> <!-- simply all annotated persistent entities will be part of this unit -->
<persistence-unit name="TestUnit"> <persistence-unit name="TestUnit">
<class>org.apache.openjpa.tools.maven.testentity.TestEntity</class> <class>org.apache.openjpa.tools.maven.testentity.SampleEntity</class>
<properties> <properties>
<property name="openjpa.jdbc.DBDictionary" value="hsql" /> <property name="openjpa.jdbc.DBDictionary" value="hsql" />

27
pom.xml
View File

@ -90,6 +90,7 @@
<compile.testclass.target>${java.testclass.version}</compile.testclass.target> <compile.testclass.target>${java.testclass.version}</compile.testclass.target>
<maven.javadoc.version>2.9.1</maven.javadoc.version> <maven.javadoc.version>2.9.1</maven.javadoc.version>
<maven.surefire.version>2.5</maven.surefire.version>
</properties> </properties>
<licenses> <licenses>
@ -373,6 +374,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<configuration> <configuration>
<argLine>${surefire.jvm.args}</argLine> <argLine>${surefire.jvm.args}</argLine>
<includes> <includes>
@ -603,17 +605,24 @@
<dependency> <dependency>
<groupId>org.jmock</groupId> <groupId>org.jmock</groupId>
<artifactId>jmock</artifactId> <artifactId>jmock</artifactId>
<version>2.5.1</version> <version>2.8.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jmock</groupId> <groupId>org.jmock</groupId>
<artifactId>jmock-junit3</artifactId> <artifactId>jmock-junit4</artifactId>
<version>2.5.1</version> <version>2.8.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>3.8.1</version> <version>4.9</version>
<exclusions>
<exclusion>
<!-- as this clashes with the hamcrest version used in jmock -->
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.sourceforge.findbugs</groupId> <groupId>net.sourceforge.findbugs</groupId>
@ -667,12 +676,18 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version> <version>${maven.surefire.version}</version>
<configuration> <configuration>
<argLine>${surefire.jvm.args}</argLine> <argLine>${surefire.jvm.args}</argLine>
<useFile>false</useFile> <useFile>false</useFile>
<trimStackTrace>false</trimStackTrace> <trimStackTrace>false</trimStackTrace>
<useSystemClassLoader>true</useSystemClassLoader> <useSystemClassLoader>true</useSystemClassLoader>
<excludes>
<!-- exclude classes that include a $; inner classes
are not test cases per OpenJPA standards -->
<exclude>org/apache/openjpa/**/*$*.class</exclude>
<exclude>org/apache/openjpa/**/*.*.class</exclude>
</excludes>
<systemProperties> <systemProperties>
<property> <property>
<name>openjpa.Log</name> <name>openjpa.Log</name>
@ -728,7 +743,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId> <artifactId>maven-surefire-report-plugin</artifactId>
<version>2.6</version> <version>${maven.surefire.version}</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>