Skip to content

Commit 5f6299c

Browse files
committed
Test code clean up
1 parent 3e56661 commit 5f6299c

File tree

2 files changed

+78
-67
lines changed

2 files changed

+78
-67
lines changed

src/test/java/org/utplsql/maven/plugin/UtPLSQLMojoIT.java

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.apache.maven.model.Model;
88
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
99
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
10-
import org.junit.Assert;
1110
import org.junit.BeforeClass;
1211
import org.junit.Test;
1312

@@ -19,6 +18,10 @@
1918
import java.util.stream.Collectors;
2019
import java.util.stream.Stream;
2120

21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.fail;
24+
2225
public class UtPLSQLMojoIT {
2326

2427
private static String pluginVersion = null;
@@ -44,16 +47,13 @@ public void testSimpleProject() {
4447
final String PROJECT_NAME = "simple-project";
4548
File testProject = ResourceExtractor.simpleExtractResources(getClass(), "/" + PROJECT_NAME);
4649

47-
Verifier verifier;
48-
verifier = createVerifier(testProject);
49-
50+
Verifier verifier = createVerifier(testProject);
5051
verifier.executeGoal("test");
5152

52-
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml",
53-
"utplsql/coverage-sonar-reporter.xml");
53+
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml", "utplsql/coverage-sonar-reporter.xml");
5454
} catch (Exception e) {
5555
e.printStackTrace();
56-
Assert.fail("Unexpected Exception running the test of Definition " + e.getMessage());
56+
fail("Unexpected Exception running the test of Definition " + e.getMessage());
5757
}
5858
}
5959

@@ -66,11 +66,10 @@ public void testRegexProject() {
6666
Verifier verifier = createVerifier(testProject);
6767
verifier.executeGoal("test");
6868

69-
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml",
70-
"utplsql/coverage-sonar-reporter.xml");
69+
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml", "utplsql/coverage-sonar-reporter.xml");
7170
} catch (Exception e) {
7271
e.printStackTrace();
73-
Assert.fail("Unexpected Exception running the test : " + e.getMessage());
72+
fail("Unexpected Exception running the test : " + e.getMessage());
7473
}
7574
}
7675

@@ -83,11 +82,10 @@ public void testTypeMappingProject() {
8382
Verifier verifier = createVerifier(testProject);
8483
verifier.executeGoal("test");
8584

86-
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml",
87-
"utplsql/coverage-sonar-reporter.xml");
85+
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml", "utplsql/coverage-sonar-reporter.xml");
8886
} catch (Exception e) {
8987
e.printStackTrace();
90-
Assert.fail("Unexpected Exception running the test : " + e.getMessage());
88+
fail("Unexpected Exception running the test : " + e.getMessage());
9189
}
9290
}
9391

@@ -100,11 +98,10 @@ public void testOwnerParameterProject() {
10098
Verifier verifier = createVerifier(testProject);
10199
verifier.executeGoal("test");
102100

103-
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml",
104-
"utplsql/coverage-sonar-reporter.xml");
101+
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml", "utplsql/coverage-sonar-reporter.xml");
105102
} catch (Exception e) {
106103
e.printStackTrace();
107-
Assert.fail("Unexpected Exception running the test : " + e.getMessage());
104+
fail("Unexpected Exception running the test : " + e.getMessage());
108105
}
109106
}
110107

@@ -118,7 +115,7 @@ public void testMinimalistProject() {
118115
verifier.executeGoal("test");
119116
} catch (Exception e) {
120117
e.printStackTrace();
121-
Assert.fail("Unexpected Exception running the test : " + e.getMessage());
118+
fail("Unexpected Exception running the test : " + e.getMessage());
122119
}
123120
}
124121

@@ -134,38 +131,42 @@ public void testTagsProject() {
134131
checkReportsGenerated(PROJECT_NAME, "utplsql/sonar-test-reporter.xml");
135132
} catch (Exception e) {
136133
e.printStackTrace();
137-
Assert.fail("Unexpected Exception running the test of Definition " + e.getMessage());
134+
fail("Unexpected Exception running the test of Definition " + e.getMessage());
138135
}
139136
}
140137

138+
/**
139+
* Duration is set to 1 before comparing contents as it is always different.
140+
* Path separator is set to "/" to ensure windows / linux / mac compatibility.
141+
* \r and \n are removed to provide simpler comparison.
142+
*
143+
* @param projectName Project name
144+
* @param files Files to compare
145+
*/
141146
private void checkReportsGenerated(String projectName, String... files) {
142147
for (String filename : files) {
143148
File outputFile = new File("target/test-classes/" + projectName + "/target", filename);
144149
File expectedOutputFile = new File("target/test-classes/" + projectName + "/expected-output", filename);
145150

146-
Assert.assertTrue("The reporter for " + filename + " was not generated", outputFile.exists());
151+
assertTrue("The reporter for " + filename + " was not generated", outputFile.exists());
152+
147153
try {
148-
// Duration is set to 1 before comparing contents as it is always different.
149-
// Path separator is set to "/" to ensure windows / linux / mac compatibility.
150-
Stream<String> stream = Files
151-
.lines(Paths.get("target", "test-classes", projectName, "target", filename));
152-
153-
String outputContent = stream
154-
.filter(line -> !line.contains("<?xml"))
155-
.map(line -> line.replaceAll("(duration=\"[0-9.]*\")", "duration=\"1\""))
156-
.map(line -> line.replaceAll("\\\\", "/"))
157-
.map(line -> line.replaceAll("\r", "").replaceAll("\n", ""))
158-
.collect(Collectors.joining("\n"));
159-
160-
stream.close();
161-
Assert.assertEquals("The files differ!",
162-
FileUtils.readFileToString(expectedOutputFile, "utf-8")
163-
.replace("\r", "")
164-
.replace("\n", ""),
165-
outputContent.replace("\n", ""));
154+
try (Stream<String> stream = Files.lines(Paths.get("target", "test-classes", projectName, "target", filename))) {
155+
String outputContent = stream
156+
.filter(line -> !line.contains("<?xml"))
157+
.map(line -> line.replaceAll("(duration=\"[0-9.]*\")", "duration=\"1\""))
158+
.map(line -> line.replaceAll("\\\\", "/"))
159+
.map(line -> line.replaceAll("\r", "").replaceAll("\n", ""))
160+
.collect(Collectors.joining("\n"));
161+
assertEquals("The files differ!",
162+
FileUtils.readFileToString(expectedOutputFile, "utf-8")
163+
.replace("\r", "")
164+
.replace("\n", ""),
165+
outputContent.replace("\n", ""));
166+
}
166167
} catch (IOException e) {
167168
e.printStackTrace();
168-
Assert.fail("Unexpected Exception running the test : " + e.getMessage());
169+
fail("Unexpected Exception running the test : " + e.getMessage());
169170
}
170171
}
171172
}

src/test/java/org/utplsql/maven/plugin/UtPLSQLMojoTest.java

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.apache.commons.lang3.tuple.Pair;
44
import org.apache.maven.plugin.MojoExecutionException;
55
import org.apache.maven.plugin.testing.MojoRule;
6-
import org.junit.Assert;
76
import org.junit.Before;
87
import org.junit.Rule;
98
import org.junit.Test;
@@ -29,6 +28,8 @@
2928

3029
import static org.junit.Assert.assertEquals;
3130
import static org.junit.Assert.assertFalse;
31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertThrows;
3233
import static org.junit.Assert.assertTrue;
3334
import static org.mockito.ArgumentMatchers.anyString;
3435
import static org.mockito.Mockito.mock;
@@ -64,7 +65,7 @@ public void setUp() throws Exception {
6465
}
6566

6667
/**
67-
* testInvalidSourcesDirectory.
68+
* Invalid Sources Directory
6869
* <p>
6970
* Given : a pom.xml with invalid sources' directory
7071
* When : pom is read and buildSourcesOptions is run
@@ -74,15 +75,15 @@ public void setUp() throws Exception {
7475
public void testInvalidSourcesDirectory() throws Exception {
7576
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
7677
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/invalidTestsSourcesDirectories/"), "test");
77-
Assert.assertNotNull(utplsqlMojo);
78+
assertNotNull(utplsqlMojo);
7879

79-
MojoExecutionException exception = Assert.assertThrows(MojoExecutionException.class, () -> Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions"));
80+
MojoExecutionException exception = assertThrows(MojoExecutionException.class, () -> Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions"));
8081

81-
Assert.assertEquals("Invalid <SOURCES> in your pom.xml", exception.getMessage());
82+
assertEquals("Invalid <SOURCES> in your pom.xml", exception.getMessage());
8283
}
8384

8485
/**
85-
* testInvalidTestsDirectory.
86+
* Invalid Tests Directory
8687
* <p>
8788
* Given : a pom.xml with invalid tests' directory
8889
* When : pom is read and buildTestsOptions is run
@@ -92,15 +93,15 @@ public void testInvalidSourcesDirectory() throws Exception {
9293
public void testInvalidTestsDirectory() throws Exception {
9394
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
9495
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/invalidTestsSourcesDirectories/"), "test");
95-
Assert.assertNotNull(utplsqlMojo);
96+
assertNotNull(utplsqlMojo);
9697

97-
MojoExecutionException exception = Assert.assertThrows(MojoExecutionException.class, () -> Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions"));
98+
MojoExecutionException exception = assertThrows(MojoExecutionException.class, () -> Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions"));
9899

99-
Assert.assertEquals("Invalid <TESTS> in your pom.xml: Invalid <directory> bar in resource. Check your pom.xml", exception.getMessage());
100+
assertEquals("Invalid <TESTS> in your pom.xml: Invalid <directory> bar in resource. Check your pom.xml", exception.getMessage());
100101
}
101102

102103
/**
103-
* testSourcesTestsParameters.
104+
* Sources Tests Parameters
104105
* <p>
105106
* Given : a pom.xml with sources and tests with a lot of parameters
106107
* When : pom is read and buildSourcesOptions / buildTestsOptions are run
@@ -110,7 +111,7 @@ public void testInvalidTestsDirectory() throws Exception {
110111
public void testSourcesTestsParameters() throws Exception {
111112
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
112113
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/testSourcesTestsParams/"), "test");
113-
Assert.assertNotNull(utplsqlMojo);
114+
assertNotNull(utplsqlMojo);
114115

115116
// TODO : move to another test about reporters
116117
List<String> reporters = Whitebox.getInternalState(utplsqlMojo, "reporters");
@@ -122,9 +123,9 @@ public void testSourcesTestsParameters() throws Exception {
122123
assertEquals("srcs/foo.sql", sources.getFilePaths().get(0));
123124
assertEquals("code_owner", sources.getObjectOwner());
124125
assertEquals(".*/\\w+/(\\w+)/(\\w+)\\.\\w{3}", sources.getRegexPattern());
125-
assertEquals(new Integer(9), sources.getNameSubExpression());
126-
assertEquals(new Integer(1), sources.getTypeSubExpression());
127-
assertEquals(new Integer(4), sources.getOwnerSubExpression());
126+
assertEquals(Integer.valueOf(9), sources.getNameSubExpression());
127+
assertEquals(Integer.valueOf(1), sources.getTypeSubExpression());
128+
assertEquals(Integer.valueOf(4), sources.getOwnerSubExpression());
128129
assertEquals(1, sources.getTypeMappings().size());
129130
assertEquals("bar", sources.getTypeMappings().get(0).getKey());
130131
assertEquals("foo", sources.getTypeMappings().get(0).getValue());
@@ -136,16 +137,16 @@ public void testSourcesTestsParameters() throws Exception {
136137
assertTrue(tests.getFilePaths().contains("te/st/spec.spc"));
137138
assertEquals("tests_owner", tests.getObjectOwner());
138139
assertEquals(".*/\\w+/(\\w+)/(\\w+)\\.\\w{3}", tests.getRegexPattern());
139-
assertEquals(new Integer(54), tests.getNameSubExpression());
140-
assertEquals(new Integer(21), tests.getTypeSubExpression());
141-
assertEquals(new Integer(24), tests.getOwnerSubExpression());
140+
assertEquals(Integer.valueOf(54), tests.getNameSubExpression());
141+
assertEquals(Integer.valueOf(21), tests.getTypeSubExpression());
142+
assertEquals(Integer.valueOf(24), tests.getOwnerSubExpression());
142143
assertEquals(1, tests.getTypeMappings().size());
143144
assertEquals("def", tests.getTypeMappings().get(0).getKey());
144145
assertEquals("abc", tests.getTypeMappings().get(0).getValue());
145146
}
146147

147148
/**
148-
* testSourcesAndTestsParameterDoesNotExist.
149+
* Sources and Tests Parameter does not exist
149150
* <p>
150151
* Given : a pom.xml with no sources / tests tags and default directory does not exist.
151152
* When : pom is read and buildSourcesOptions / buildTestsOptions are run
@@ -155,7 +156,7 @@ public void testSourcesTestsParameters() throws Exception {
155156
public void testSourcesAndTestsParameterDoesNotExist() throws Exception {
156157
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(
157158
new File("src/test/resources/unit-tests/testNoSourcesTestsParams/directoryDoesNotExist/"), "test");
158-
Assert.assertNotNull(utplsqlMojo);
159+
assertNotNull(utplsqlMojo);
159160

160161
// check sources
161162
FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions");
@@ -167,7 +168,7 @@ public void testSourcesAndTestsParameterDoesNotExist() throws Exception {
167168
}
168169

169170
/**
170-
* testSourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists.
171+
* Sources and Tests Parameter does not exist but Default Directory exists
171172
* <p>
172173
* Given : a pom.xml with no sources / tests tags but default directory exists.
173174
* When : pom is read and buildSourcesOptions / buildTestsOptions are run
@@ -177,7 +178,7 @@ public void testSourcesAndTestsParameterDoesNotExist() throws Exception {
177178
public void testSourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists() throws Exception {
178179
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
179180
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/testNoSourcesTestsParams/directoryExists/"), "test");
180-
Assert.assertNotNull(utplsqlMojo);
181+
assertNotNull(utplsqlMojo);
181182

182183
// check sources
183184
FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions");
@@ -193,7 +194,7 @@ public void testSourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists()
193194
}
194195

195196
/**
196-
* testSourcesAndTestsParameterHaveNotDirectoryTag.
197+
* Sources and Tests Parameter have not Directory Tag
197198
* <p>
198199
* Given : a pom.xml with source and test tag not containing a directory tag.
199200
* When : pom is read and buildSourcesOptions / buildTestsOptions are run
@@ -203,7 +204,7 @@ public void testSourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists()
203204
public void testSourcesAndTestsParameterHaveNotDirectoryTag() throws Exception {
204205
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
205206
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/partialSourceAndTestTag/missingDirectory/"), "test");
206-
Assert.assertNotNull(utplsqlMojo);
207+
assertNotNull(utplsqlMojo);
207208

208209
// check sources
209210
FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions");
@@ -220,7 +221,7 @@ public void testSourcesAndTestsParameterHaveNotDirectoryTag() throws Exception {
220221
}
221222

222223
/**
223-
* testSourcesAndTestsParameterHaveNotDirectoryTag.
224+
* Sources and Tests Parameter have not Directory Tag
224225
* <p>
225226
* Given : a pom.xml with source and test tag not containing a directory tag.
226227
* When : pom is read and buildSourcesOptions / buildTestsOptions are run
@@ -230,7 +231,7 @@ public void testSourcesAndTestsParameterHaveNotDirectoryTag() throws Exception {
230231
public void testSourcesAndTestsParameterHaveNotIncludesTag() throws Exception {
231232
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
232233
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/partialSourceAndTestTag/missingIncludes/"), "test");
233-
Assert.assertNotNull(utplsqlMojo);
234+
assertNotNull(utplsqlMojo);
234235

235236
// check sources
236237
FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions");
@@ -245,11 +246,14 @@ public void testSourcesAndTestsParameterHaveNotIncludesTag() throws Exception {
245246
assertTrue(tests.getFilePaths().contains("src/test/bar/f2.pkg"));
246247
}
247248

249+
/**
250+
* Default Console Behaviour
251+
*/
248252
@Test
249253
public void testDefaultConsoleBehaviour() throws Exception {
250254
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
251255
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/defaultConsoleOutputBehaviour/"), "test");
252-
Assert.assertNotNull(utplsqlMojo);
256+
assertNotNull(utplsqlMojo);
253257

254258
List<Reporter> reporterList = new ArrayList<>();
255259
when(mockReporterFactory.createReporter(anyString())).thenAnswer(invocation -> {
@@ -290,11 +294,14 @@ public void testDefaultConsoleBehaviour() throws Exception {
290294
assertTrue(reporterParameter3.isFileOutput());
291295
}
292296

297+
/**
298+
* Add Default Reporter
299+
*/
293300
@Test
294301
public void testAddDefaultReporter() throws Exception {
295302
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
296303
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/defaultConsoleOutputBehaviour/"), "test");
297-
Assert.assertNotNull(utplsqlMojo);
304+
assertNotNull(utplsqlMojo);
298305

299306
List<Reporter> reporterList = new ArrayList<>();
300307
when(mockReporterFactory.createReporter(anyString())).thenAnswer(invocation -> {
@@ -314,11 +321,14 @@ public void testAddDefaultReporter() throws Exception {
314321
verify(reporterList.get(0)).init(mockConnection);
315322
}
316323

324+
/**
325+
* Skip utPLSQL Tests
326+
*/
317327
@Test
318328
public void testSkipUtplsqlTests() throws Exception {
319329
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule
320330
.lookupConfiguredMojo(new File("src/test/resources/unit-tests/skipUtplsqlTests/"), "test");
321-
Assert.assertNotNull(utplsqlMojo);
331+
assertNotNull(utplsqlMojo);
322332

323333
final ByteArrayOutputStream console = new ByteArrayOutputStream();
324334
System.setOut(new PrintStream(console));
@@ -327,6 +337,6 @@ public void testSkipUtplsqlTests() throws Exception {
327337

328338
String standardOutput = console.toString();
329339

330-
Assert.assertTrue(standardOutput.contains("utPLSQLTests are skipped."));
340+
assertTrue(standardOutput.contains("utPLSQLTests are skipped."));
331341
}
332342
}

0 commit comments

Comments
 (0)