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>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit3</artifactId>
<artifactId>jmock-junit4</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -28,15 +28,23 @@ import javax.sql.DataSource;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.kernel.StoreContext;
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 {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
final Statement mockStatement = mock(Statement.class);
final Connection mockConnection = mock(Connection.class);
final ResultSet mockRS = mock(ResultSet.class);
final DataSource mockDS = mock(DataSource.class);
final DatabaseMetaData mockMetaData = mock(DatabaseMetaData.class);
import static org.junit.Assert.*;
public class TestDB2Dictionary {
@Rule
public JUnitRuleMockery context = new JUnitRuleMockery();
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 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.
*/
@Test
public void testGetDefaultSchemaNameDS1() throws Exception {
// 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
// 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.
oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -89,11 +99,13 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/*
* When ds1 is null, fallback to ds2
*/
@Test
public void testGetDefaultSchemaNameDS2() throws Exception {
// 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
// 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.
oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -137,11 +149,13 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/*
* When ds1 is null, fallback to ds2
*/
@Test
public void testGetDefaultSchemaNameNoDS() throws Exception {
// 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
// an exception will be thrown and the test will fail.
checking(new Expectations() {
context.checking(new Expectations()
{
{
// both datasources are null for this test.
oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -163,12 +177,14 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/*
* TestWhitespace trim
*/
@Test
public void testGetDefaultSchemaNameTrimmed() throws Exception {
final String schema2 = "abcd ";
// 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
// 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.
oneOf(mockConfiguration).getDataSource(with(equal(sc)));
@ -209,15 +225,17 @@ public class TestDB2Dictionary extends MockObjectTestCase {
/*
* Verifies that the ConnectedConfiguration method only uses the DBMetaData to determine the correct behavior.
*/
@Test
public void testConnectedConfigurationOnlyUsesMetaData() throws Exception {
checking(new Expectations() {
context.checking(new Expectations()
{
{
// No activity on the connection other than getting the metadata.
allowing(mockConnection).getMetaData();
will(returnValue(mockMetaData));
// anything on the configuration or DBMetaData is fair game.
allowing(mockMetaData);
allowing(mockMetaData);
allowing(mockConfiguration);
}
});

View File

@ -24,18 +24,27 @@ import org.apache.openjpa.jdbc.identifier.DBIdentifierUtilImpl;
import org.apache.openjpa.jdbc.schema.Table;
import org.apache.openjpa.util.UserException;
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() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() {
context.checking(new Expectations()
{
{
allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl));
will(returnValue(idImpl));
allowing(mockConfiguration);
}
@ -56,16 +65,18 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
assertTrue(ue.getMessage().contains("Table name \"NameIsTooLong\""));
}
}
@Test
public void testThrowsExceptionWithSchemaSet() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() {
context.checking(new Expectations()
{
{
allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl));
will(returnValue(idImpl));
allowing(mockConfiguration);
}
@ -87,15 +98,17 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
assertTrue(ue.getMessage().contains("Table name \"IAmASchema.NameIsTooLong\""));
}
}
@Test
public void testSchemaNameIsNotConsidered() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() {
context.checking(new Expectations()
{
{
allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl));
will(returnValue(idImpl));
allowing(mockConfiguration);
}
@ -114,15 +127,17 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
assertTrue(sqls[0].contains("NameIsRight"));
assertTrue(sqls[0].contains("IAmASchema"));
}
@Test
public void testOverrideProperty() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() {
context.checking(new Expectations()
{
{
allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl));
will(returnValue(idImpl));
allowing(mockConfiguration);
}
@ -145,15 +160,17 @@ public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
assertTrue(ue.getMessage().contains("Table name \"IAmASchema.NameIsTooLong\""));
}
}
@Test
public void testOverridePropertyShortName() {
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
final JDBCConfiguration mockConfiguration = context.mock(JDBCConfiguration.class);
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
checking(new Expectations() {
context.checking(new Expectations()
{
{
allowing(mockConfiguration).getIdentifierUtilInstance();
will(returnValue(idImpl));
will(returnValue(idImpl));
allowing(mockConfiguration);
}

View File

@ -26,9 +26,17 @@ import java.sql.ResultSet;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfigurationImpl;
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 {
DBDictionary db = new MySQLDictionary();
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
* method calls are made
*/
@Test
public void testPreparedStatementGetFetchBatchSize() throws Exception {
DBDictionary db = new MySQLDictionary();
SQLBuffer sql = new SQLBuffer(db);
final PreparedStatement mockStatement = mock(PreparedStatement.class);
final Connection mockConnection = mock(Connection.class);
final PreparedStatement mockStatement = context.mock(PreparedStatement.class);
final Connection mockConnection = context.mock(Connection.class);
// 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
// an exception will be thrown and the test will fail.
checking(new Expectations() {
context.checking(new Expectations()
{
{
oneOf(mockConnection).prepareStatement(with(any(String.class)));
will(returnValue(mockStatement));
@ -81,17 +91,18 @@ public class TestMySQLDictionary extends MockObjectTestCase {
* If any of the expectations are not met or any unexpected
* method calls are made
*/
@Test
public void testPreparedCallGetFetchBatchSize() throws Exception {
DBDictionary db = new MySQLDictionary();
SQLBuffer sql = new SQLBuffer(db);
final CallableStatement mockStatement = mock(CallableStatement.class);
final Connection mockConnection = mock(Connection.class);
final CallableStatement mockStatement = context.mock(CallableStatement.class);
final Connection mockConnection = context.mock(Connection.class);
// 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
// an exception will be thrown and the test will fail.
checking(new Expectations() {
context.checking(new Expectations() {
{
oneOf(mockConnection).prepareCall(with(any(String.class)));
will(returnValue(mockStatement));

View File

@ -915,7 +915,7 @@
<excludes>
<!-- exclude classes that end with 'Test'; these
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
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.TestXmlOverrideEntity;
/**
* TODO: this should be refactored to a @RunWith or similar...
*/
public class DynamicEnhancementSuite extends TestCase {
static {
Persistence.createEntityManagerFactory("test", System.getProperties());
@ -125,7 +128,7 @@ public class DynamicEnhancementSuite extends TestCase {
// with the dynamic enhaner.
String test = System.getProperty("dynamicTest");
if (test != null) {
suite.addTestSuite(Class.forName(test));
suite.addTestSuite((Class<? extends TestCase>) Class.forName(test));
} else {
// Subclassing failing tests

View File

@ -180,3 +180,11 @@ Building and deploying the Site
===============================
$ 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:
http://openjpa.apache.org/

View File

@ -133,6 +133,12 @@
<argLine>${test.jvm.arguments}</argLine>
<excludes>
<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>
<systemProperties>
<property>

View File

@ -24,6 +24,8 @@ import java.util.Random;
import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import org.junit.Ignore;
/**
* 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
*
*/
@Ignore
public static class CarDistributorPolicy implements DistributionPolicy {
public String distribute(Object pc, List<String> slices, Object context) {
if (pc instanceof Manufacturer) {

View File

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

View File

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

View File

@ -23,7 +23,7 @@
<!-- simply all annotated persistent entities will be part of this unit -->
<persistence-unit name="TestUnit">
<class>org.apache.openjpa.tools.maven.testentity.TestEntity</class>
<class>org.apache.openjpa.tools.maven.testentity.SampleEntity</class>
<properties>
<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>
<maven.javadoc.version>2.9.1</maven.javadoc.version>
<maven.surefire.version>2.5</maven.surefire.version>
</properties>
<licenses>
@ -373,6 +374,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<configuration>
<argLine>${surefire.jvm.args}</argLine>
<includes>
@ -603,17 +605,24 @@
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
<version>2.5.1</version>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit3</artifactId>
<version>2.5.1</version>
<artifactId>jmock-junit4</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<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>
<groupId>net.sourceforge.findbugs</groupId>
@ -667,12 +676,18 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<version>${maven.surefire.version}</version>
<configuration>
<argLine>${surefire.jvm.args}</argLine>
<useFile>false</useFile>
<trimStackTrace>false</trimStackTrace>
<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>
<property>
<name>openjpa.Log</name>
@ -728,7 +743,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.6</version>
<version>${maven.surefire.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>