Skip to content

Commit 2c9bc81

Browse files
committed
Refactor tests to fully use JUnit 5 capabilities
1 parent 2e9cc41 commit 2c9bc81

File tree

8 files changed

+100
-187
lines changed

8 files changed

+100
-187
lines changed

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@
4747
<version>${junit.jupiter.version}</version>
4848
<scope>test</scope>
4949
</dependency>
50-
<dependency>
51-
<groupId>org.junit.jupiter</groupId>
52-
<artifactId>junit-jupiter-migrationsupport</artifactId>
53-
<version>${junit.jupiter.version}</version>
54-
<scope>test</scope>
55-
</dependency>
5650
</dependencies>
5751

5852
<repositories>
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
1-
package org.utplsql.api.rules;
1+
package org.utplsql.api;
22

3-
import org.junit.rules.ExternalResource;
4-
import org.utplsql.api.EnvironmentVariableUtil;
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
55

66
import java.sql.Connection;
77
import java.sql.DriverManager;
88
import java.sql.SQLException;
99
import java.util.ArrayList;
1010
import java.util.List;
1111

12-
/**
13-
* Created by Vinicius on 13/04/2017.
14-
*/
15-
public class DatabaseRule extends ExternalResource {
12+
public abstract class AbstractDatabaseTest {
1613

1714
private static String sUrl;
1815
private static String sUser;
1916
private static String sPass;
20-
2117
static {
2218
sUrl = EnvironmentVariableUtil.getEnvValue("DB_URL", "192.168.99.100:1521:XE");
2319
sUser = EnvironmentVariableUtil.getEnvValue("DB_USER", "app");
2420
sPass = EnvironmentVariableUtil.getEnvValue("DB_PASS", "app");
2521
}
2622

23+
private Connection conn;
24+
private List<Connection> connectionList = new ArrayList<>();
2725

28-
29-
private List<Connection> connectionList;
30-
31-
public DatabaseRule() {
32-
connectionList = new ArrayList<>();
26+
@BeforeEach
27+
public void setupConnection() throws SQLException {
28+
conn = newConnection();
3329
}
3430

35-
public String getUser() {
36-
return sUser;
31+
protected Connection getConnection() {
32+
return conn;
3733
}
3834

39-
public synchronized Connection newConnection() throws SQLException {
35+
protected synchronized Connection newConnection() throws SQLException {
4036
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + sUrl, sUser, sPass);
4137
connectionList.add(conn);
4238
return conn;
4339
}
4440

45-
@Override
46-
protected void after() {
41+
public static String getUser() {
42+
return sUser;
43+
}
44+
45+
@AfterEach
46+
public void teardownConnection() {
4747
for (Connection conn : connectionList)
4848
try { conn.close(); } catch (SQLException ignored) {}
4949
}
50-
5150
}
Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,28 @@
11
package org.utplsql.api;
22

3-
import org.junit.Rule;
43
import org.junit.jupiter.api.Test;
54
import org.utplsql.api.compatibility.CompatibilityProxy;
6-
import org.utplsql.api.rules.DatabaseRule;
75

8-
import java.sql.Connection;
96
import java.sql.SQLException;
107

118
import static org.junit.jupiter.api.Assertions.assertEquals;
12-
import static org.junit.jupiter.api.Assertions.fail;
139

14-
public class CompatibilityIT {
10+
public class CompatibilityIT extends AbstractDatabaseTest {
1511

16-
@Rule
17-
public final DatabaseRule db = new DatabaseRule();
1812

1913
@Test
20-
public void compatibleVersion() {
21-
try {
22-
Connection conn = db.newConnection();
23-
CompatibilityProxy proxy = new CompatibilityProxy(conn);
24-
proxy.failOnNotCompatible();
25-
assertEquals(true, proxy.isCompatible());
26-
} catch (SQLException e) {
27-
fail(e);
28-
}
14+
public void compatibleVersion() throws SQLException {
15+
CompatibilityProxy proxy = new CompatibilityProxy(getConnection());
16+
proxy.failOnNotCompatible();
17+
assertEquals(true, proxy.isCompatible());
2918
}
3019

3120
@Test
32-
public void skipCompatibilityCheck()
33-
{
34-
try {
35-
Connection conn = db.newConnection();
36-
CompatibilityProxy proxy = new CompatibilityProxy(conn, true);
37-
proxy.failOnNotCompatible();
38-
assertEquals(true, proxy.isCompatible());
39-
assertEquals(CompatibilityProxy.UTPLSQL_API_VERSION, proxy.getDatabaseVersion().toString());
40-
} catch (SQLException e) {
41-
fail(e);
42-
}
21+
public void skipCompatibilityCheck() throws SQLException {
22+
CompatibilityProxy proxy = new CompatibilityProxy(getConnection(), true);
23+
proxy.failOnNotCompatible();
24+
assertEquals(true, proxy.isCompatible());
25+
assertEquals(CompatibilityProxy.UTPLSQL_API_VERSION, proxy.getDatabaseVersion().toString());
26+
4327
}
4428
}
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
package org.utplsql.api;
22

3-
import org.junit.Rule;
43
import org.junit.jupiter.api.Test;
5-
import org.utplsql.api.rules.DatabaseRule;
64

75
import java.sql.SQLException;
86

97
import static org.junit.jupiter.api.Assertions.assertEquals;
10-
import static org.junit.jupiter.api.Assertions.fail;
118

12-
public class DBHelperIT {
13-
14-
@Rule
15-
public final DatabaseRule db = new DatabaseRule();
9+
public class DBHelperIT extends AbstractDatabaseTest {
1610

1711
@Test
18-
public void getFrameworkVersion()
19-
{
20-
try {
21-
Version v = DBHelper.getDatabaseFrameworkVersion(db.newConnection());
22-
assertEquals(true, v.isValid());
23-
} catch (SQLException e)
24-
{
25-
fail(e);
26-
}
12+
public void getFrameworkVersion() throws SQLException {
13+
Version v = DBHelper.getDatabaseFrameworkVersion(getConnection());
14+
assertEquals(true, v.isValid());
2715
}
2816

2917
}

src/test/java/org/utplsql/api/FileMapperIT.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.utplsql.api;
22

33
import org.junit.jupiter.api.Test;
4-
import org.utplsql.api.rules.DatabaseRule;
5-
import org.junit.Rule;
64

75
import java.sql.SQLException;
86
import java.util.ArrayList;
@@ -11,10 +9,7 @@
119
import static org.junit.jupiter.api.Assertions.assertEquals;
1210
import static org.junit.jupiter.api.Assertions.fail;
1311

14-
public class FileMapperIT {
15-
16-
@Rule
17-
public final DatabaseRule db = new DatabaseRule();
12+
public class FileMapperIT extends AbstractDatabaseTest {
1813

1914
@Test
2015
public void testFileMapper() throws SQLException {
@@ -34,7 +29,7 @@ public void testFileMapper() throws SQLException {
3429
mapperOptions.setTypeSubExpression(2);
3530
mapperOptions.setNameSubExpression(3);
3631

37-
List<FileMapping> fileMappings = FileMapper.buildFileMappingList(db.newConnection(), mapperOptions);
32+
List<FileMapping> fileMappings = FileMapper.buildFileMappingList(getConnection(), mapperOptions);
3833

3934
if (fileMappings.size() != 2)
4035
fail("Wrong mapping list size.");

src/test/java/org/utplsql/api/OutputBufferIT.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
import org.junit.jupiter.api.Test;
44
import org.utplsql.api.reporter.DocumentationReporter;
55
import org.utplsql.api.reporter.Reporter;
6-
import org.utplsql.api.rules.DatabaseRule;
7-
import org.junit.Rule;
86

97
import java.io.File;
108
import java.io.FileOutputStream;
119
import java.io.PrintStream;
12-
import java.sql.Connection;
1310
import java.sql.SQLException;
1411
import java.util.ArrayList;
1512
import java.util.List;
@@ -19,34 +16,32 @@
1916
import static org.junit.jupiter.api.Assertions.fail;
2017

2118
/**
22-
* Created by Vinicius on 13/04/2017.
19+
* Integration-test for OutputBuffers
20+
*
21+
* @author viniciusam
22+
* @author pesse
2323
*/
24-
public class OutputBufferIT {
25-
26-
@Rule
27-
public final DatabaseRule db = new DatabaseRule();
24+
public class OutputBufferIT extends AbstractDatabaseTest {
2825

2926
public Reporter createReporter() throws SQLException {
30-
Connection conn = db.newConnection();
31-
Reporter reporter = new DocumentationReporter().init(conn);
27+
Reporter reporter = new DocumentationReporter().init(newConnection());
3228
System.out.println("Reporter ID: " + reporter.getReporterId());
3329
return reporter;
3430
}
3531

3632
@Test
37-
public void printAvailableLines() {
33+
public void printAvailableLines() throws SQLException {
3834
ExecutorService executorService = Executors.newFixedThreadPool(2);
3935

4036
try {
4137
final Reporter reporter = createReporter();
4238

4339
Future<Object> task1 = executorService.submit(() -> {
4440
try {
45-
Connection conn = db.newConnection();
4641
new TestRunner()
47-
.addPath(db.getUser())
42+
.addPath(getUser())
4843
.addReporter(reporter)
49-
.run(conn);
44+
.run(getConnection());
5045

5146
return Boolean.TRUE;
5247
} catch (SQLException e) {
@@ -58,15 +53,14 @@ public void printAvailableLines() {
5853
FileOutputStream fileOutStream = null;
5954
File outFile = new File("output.txt");
6055
try {
61-
Connection conn = db.newConnection();
6256
fileOutStream = new FileOutputStream(outFile);
6357

6458
List<PrintStream> printStreams = new ArrayList<>();
6559
printStreams.add(System.out);
6660
printStreams.add(new PrintStream(fileOutStream));
6761

6862
new OutputBuffer(reporter)
69-
.printAvailable(conn, printStreams);
63+
.printAvailable(newConnection(), printStreams);
7064

7165
return Boolean.TRUE;
7266
} catch (SQLException e) {
@@ -90,30 +84,23 @@ public void printAvailableLines() {
9084

9185
if (res2 instanceof Exception)
9286
fail((Exception) res2);
93-
} catch (SQLException e) {
94-
fail(e.getMessage());
9587
} catch (InterruptedException | ExecutionException e) {
9688
e.printStackTrace();
9789
}
9890
}
9991

10092
@Test
101-
public void fetchAllLines() {
102-
try {
103-
final Reporter reporter = createReporter();
104-
Connection conn = db.newConnection();
105-
new TestRunner()
106-
.addPath(db.getUser())
107-
.addReporter(reporter)
108-
.run(conn);
109-
110-
List<String> outputLines = new OutputBuffer(reporter)
111-
.fetchAll(conn);
112-
113-
assertTrue(outputLines.size() > 0);
114-
} catch (SQLException e) {
115-
fail(e);
116-
}
93+
public void fetchAllLines() throws SQLException {
94+
final Reporter reporter = createReporter();
95+
new TestRunner()
96+
.addPath(getUser())
97+
.addReporter(reporter)
98+
.run(getConnection());
99+
100+
List<String> outputLines = new OutputBuffer(reporter)
101+
.fetchAll(getConnection());
102+
103+
assertTrue(outputLines.size() > 0);
117104
}
118105

119106
}

0 commit comments

Comments
 (0)