Skip to content

Commit b7e53cf

Browse files
authored
Rename tests , add javadoc to test and replace Junit with AssertJ(#234)
test names from `testThingDoesThing`() to `it_handles_failures_gracefully`()
1 parent c6e1cbf commit b7e53cf

File tree

11 files changed

+573
-371
lines changed

11 files changed

+573
-371
lines changed

plugins/codemodder-plugin-maven/build.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ dependencies {
1515
val javaSemverVersion = "0.9.0"
1616
val slf4jSimpleVersion = "2.0.0"
1717
val hamcrestVersion = "1.3"
18-
val junitVersion = "4.13.2"
1918
val kotlinTestVersion = "1.7.10"
2019
val slf4jApiVersion = "2.0.0"
2120
val juniversalchardetVersion = "2.4.0"
@@ -44,7 +43,6 @@ dependencies {
4443
testImplementation("fun.mike:diff-match-patch:$diffMatchPatchVersion")
4544
testImplementation("org.slf4j:slf4j-simple:$slf4jSimpleVersion")
4645
testImplementation("org.hamcrest:hamcrest-all:$hamcrestVersion")
47-
testImplementation("junit:junit:$junitVersion")
4846
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinTestVersion")
4947
compileOnly("org.slf4j:slf4j-api:$slf4jApiVersion")
5048
}

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/WrongDependencyTypeException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.codemodder.plugins.maven.operator;
22

3-
/** An exception that indicates a wrong dependency type was encountered. */
3+
/**
4+
* An exception that indicates the packaging type is incorrect for parent POMs or the main POM file.
5+
*/
46
class WrongDependencyTypeException extends RuntimeException {
57

68
/**

plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/AbstractTestBase.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.codemodder.plugins.maven.operator;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import fun.mike.dmp.DiffMatchPatch;
46
import fun.mike.dmp.Patch;
57
import java.io.IOException;
@@ -14,7 +16,6 @@
1416
import javax.xml.stream.XMLStreamException;
1517
import org.dom4j.Document;
1618
import org.dom4j.io.SAXReader;
17-
import org.junit.Assert;
1819
import org.slf4j.Logger;
1920
import org.slf4j.LoggerFactory;
2021
import org.xmlunit.builder.DiffBuilder;
@@ -38,15 +39,17 @@ Path getResourceAsPath(String name) throws URISyntaxException {
3839
return resourceUrl;
3940
}
4041

41-
protected ProjectModel gwt(String name, ProjectModelFactory pmf) throws Exception {
42-
return gwt(name, pmf.build(), OperationType.MODIFY);
42+
protected ProjectModel performAndAssertModifyPomOperation(String name, ProjectModelFactory pmf)
43+
throws Exception {
44+
return performAndAssertPomOperation(name, pmf.build(), OperationType.MODIFY);
4345
}
4446

45-
protected ProjectModel performInsert(String name, ProjectModelFactory pmf) throws Exception {
46-
return gwt(name, pmf.build(), OperationType.INSERT);
47+
protected ProjectModel performAndAssertInsertPomOperation(String name, ProjectModelFactory pmf)
48+
throws Exception {
49+
return performAndAssertPomOperation(name, pmf.build(), OperationType.INSERT);
4750
}
4851

49-
protected ProjectModel gwt(
52+
protected ProjectModel performAndAssertPomOperation(
5053
String testName, ProjectModel context, final OperationType operationType) throws Exception {
5154

5255
String resultFile = "pom-" + testName + "-result.xml";
@@ -55,10 +58,9 @@ protected ProjectModel gwt(
5558
if (resource != null) {
5659
Document outcome = new SAXReader().read(resource);
5760
performPomOperation(operationType, context);
58-
59-
Assert.assertFalse(
60-
"Expected and outcome have differences",
61-
getXmlDifferences(context.getPomFile().getResultPom(), outcome).hasDifferences());
61+
// "Expected and outcome have differences"
62+
assertThat(getXmlDifferences(context.getPomFile().getResultPom(), outcome).hasDifferences())
63+
.isFalse();
6264
} else {
6365
Path resultFilePath =
6466
Paths.get(

plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/POMOperatorDependencyQueryTest.java

Lines changed: 95 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.codemodder.plugins.maven.operator;
22

3-
import static org.junit.Assert.*;
3+
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.io.File;
66
import java.io.IOException;
@@ -11,7 +11,6 @@
1111
import java.util.ArrayList;
1212
import java.util.Collection;
1313
import java.util.List;
14-
import java.util.stream.Collectors;
1514
import javax.xml.stream.XMLStreamException;
1615
import org.dom4j.DocumentException;
1716
import org.junit.jupiter.api.Test;
@@ -21,8 +20,12 @@
2120
final class POMOperatorDependencyQueryTest {
2221
private static final Logger LOGGER = LoggerFactory.getLogger(POMOperatorTest.class);
2322

23+
/**
24+
* Tests whether queryDependency uses the safe query type and retrieves dependencies successfully.
25+
* Ensures that dependencies are retrieved when a safe query type is used.
26+
*/
2427
@Test
25-
void testBasicQuery()
28+
void queryDependency_uses_safe_query_type_successfully()
2629
throws DocumentException, IOException, URISyntaxException, XMLStreamException {
2730
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
2831
context.withSafeQueryType();
@@ -31,61 +34,80 @@ void testBasicQuery()
3134

3235
LOGGER.debug("Dependencies found: {}", dependencies);
3336

34-
assertTrue("Dependencies are not empty", dependencies != null && !dependencies.isEmpty());
37+
// "Dependencies are not empty"
38+
assertThat(dependencies != null && !dependencies.isEmpty()).isTrue();
3539
}
3640

41+
/**
42+
* Tests queryDependency for a broken POM, expecting no dependencies. Verifies that the query for
43+
* a broken POM returns an empty list of dependencies.
44+
*/
3745
@Test
38-
void testFailedSafeQuery()
46+
void queryDependency_for_broken_pom_returns_no_dependencies()
3947
throws DocumentException, IOException, URISyntaxException, XMLStreamException {
4048
ProjectModelFactory context =
4149
ProjectModelFactory.load(getClass().getResource("pom-broken.xml"));
4250
context.withSafeQueryType();
4351

4452
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
4553

46-
assertTrue("Dependencies are empty", dependencies.isEmpty());
54+
// "Dependencies are empty"
55+
assertThat(dependencies).isEmpty();
4756
}
4857

58+
/**
59+
* Tests whether queryDependency uses available dependency query commands successfully. Verifies
60+
* that the query uses available dependency query commands and returns non-empty dependencies.
61+
*/
4962
@Test
50-
void testAllQueryTypes()
51-
throws DocumentException, IOException, URISyntaxException, XMLStreamException {
63+
void queryDependency_uses_available_dependency_query_commands_successfully()
64+
throws DocumentException,
65+
IOException,
66+
URISyntaxException,
67+
XMLStreamException,
68+
ClassNotFoundException,
69+
InstantiationException,
70+
IllegalAccessException {
5271
String[] pomFiles = {"pom-1.xml", "pom-3.xml"};
5372
for (String pomFile : pomFiles) {
5473
for (Pair<QueryType, String> chain : CommandChain.AVAILABLE_DEPENDENCY_QUERY_COMMANDS) {
5574
String commandClassName = "io.codemodder.plugins.maven.operator." + chain.getSecond();
5675

5776
List<Command> commandListOverride = new ArrayList<>();
58-
try {
59-
Command command = (Command) Class.forName(commandClassName).newInstance();
60-
commandListOverride.add(command);
61-
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
62-
e.printStackTrace();
63-
}
77+
78+
Command command = (Command) Class.forName(commandClassName).newInstance();
79+
commandListOverride.add(command);
6480

6581
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource(pomFile));
6682
context.withSafeQueryType();
6783

6884
Collection<Dependency> dependencies =
6985
POMOperator.queryDependency(context.build(), commandListOverride);
7086

71-
assertTrue("Dependencies are not empty", !dependencies.isEmpty());
87+
// "Dependencies are not empty"
88+
assertThat(!dependencies.isEmpty()).isTrue();
7289
}
7390
}
7491
}
7592

93+
/**
94+
* Tests whether queryDependency uses a temporary directory successfully. Verifies that the query
95+
* uses a temporary directory and returns non-empty dependencies.
96+
*/
7697
@Test
77-
void testTemporaryDirectory()
98+
void queryDependency_uses_temporary_directory_successfully()
7899
throws IOException, DocumentException, URISyntaxException, XMLStreamException {
79100

80101
File tempDirectory = new File("/tmp/mvn-repo-" + System.currentTimeMillis() + ".dir");
81102

82-
assertFalse("Temp Directory does not exist initially", tempDirectory.exists());
83-
assertEquals(
84-
"There must be no files",
85-
tempDirectory.list() != null
86-
? (int) Files.list(tempDirectory.toPath()).filter(Files::isDirectory).count()
87-
: 0,
88-
0);
103+
// "Temp Directory does not exist initially"
104+
assertThat(tempDirectory).doesNotExist();
105+
// "There must be no files"
106+
assertThat(
107+
tempDirectory.list() != null
108+
? (int) Files.list(tempDirectory.toPath()).filter(Files::isDirectory).count()
109+
: 0)
110+
.isZero();
89111

90112
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
91113
context.withSafeQueryType();
@@ -95,14 +117,22 @@ void testTemporaryDirectory()
95117

96118
LOGGER.debug("Dependencies found: " + dependencies);
97119

98-
assertTrue("Dependencies are not empty", dependencies != null && !dependencies.isEmpty());
120+
// "Dependencies are not empty"
121+
assertThat(dependencies != null && !dependencies.isEmpty()).isTrue();
99122

100-
assertTrue("Temp Directory ends up existing", tempDirectory.exists());
101-
assertTrue("Temp Directory is a directory", tempDirectory.isDirectory());
123+
// "Temp Directory ends up existing"
124+
assertThat(tempDirectory).exists();
125+
// "Temp Directory is a directory"
126+
assertThat(tempDirectory).isDirectory();
102127
}
103128

129+
/**
130+
* Tests whether queryDependency uses a temporary directory and offline mode successfully.
131+
* Verifies that the query uses a temporary directory and offline mode, returning non-empty
132+
* dependencies.
133+
*/
104134
@Test
105-
void testTemporaryDirectoryAndFullyOffline()
135+
void queryDependency_uses_temporary_directory_and_offline_mode_successfully()
106136
throws IOException, DocumentException, URISyntaxException, XMLStreamException {
107137

108138
Path tempDirectory = Files.createTempDirectory("mvn-repo");
@@ -115,11 +145,16 @@ void testTemporaryDirectoryAndFullyOffline()
115145

116146
LOGGER.debug("Dependencies found: " + dependencies);
117147

118-
assertTrue("Dependencies are not empty", !dependencies.isEmpty());
148+
// "Dependencies are not empty"
149+
assertThat(!dependencies.isEmpty()).isTrue();
119150
}
120151

152+
/**
153+
* Tests whether queryDependency handles a synthetic dependency. Verifies the handling of
154+
* synthetic dependencies in the query.
155+
*/
121156
@Test
122-
void testOnSyntheticDependency() throws Exception {
157+
void queryDependency_handles_synthetic_dependency() throws Exception {
123158
Path tempDirectory = Files.createTempDirectory("mvn-repo");
124159

125160
Path tempPom = tempDirectory.resolve("pom.xml");
@@ -159,19 +194,19 @@ void testOnSyntheticDependency() throws Exception {
159194

160195
LOGGER.debug("Dependencies found: " + dependencies);
161196

162-
assertTrue("Dependencies are not empty", !dependencies.isEmpty());
197+
// "Dependencies are not empty"
198+
assertThat(!dependencies.isEmpty()).isTrue();
163199

164-
assertTrue(
165-
"Random name matches",
166-
dependencies.stream()
167-
.collect(Collectors.toList())
168-
.get(0)
169-
.getArtifactId()
170-
.equals(randomName));
200+
// "Random name matches"
201+
assertThat(dependencies.stream().toList().get(0).getArtifactId()).isEqualTo(randomName);
171202
}
172203

204+
/**
205+
* Tests queryDependency handles a composite synthetic dependency. Verifies the handling of
206+
* composite synthetic dependencies in the query.
207+
*/
173208
@Test
174-
void testOnCompositeSyntheticDependency() throws Exception {
209+
void queryDependency_handles_composite_synthetic_dependency() throws Exception {
175210
Path tempDirectory = Files.createTempDirectory("mvn-repo");
176211

177212
Path tempParentPom = tempDirectory.resolve("pom-parent.xml");
@@ -247,19 +282,21 @@ void testOnCompositeSyntheticDependency() throws Exception {
247282

248283
LOGGER.debug("Dependencies found: " + dependencies);
249284

250-
assertTrue("Dependencies are not empty", !dependencies.isEmpty());
285+
// "Dependencies are not empty"
286+
assertThat(!dependencies.isEmpty()).isTrue();
251287

252-
assertTrue(
253-
"Random name matches",
254-
dependencies.stream()
255-
.collect(Collectors.toList())
256-
.get(0)
257-
.getArtifactId()
258-
.equals(randomName));
288+
// "Random name matches"
289+
assertThat(dependencies.stream().toList().get(0).getArtifactId()).isEqualTo(randomName);
259290
}
260291

292+
/**
293+
* Tests queryDependency handles composite incomplete synthetic dependency but with a parser.
294+
* Verifies the handling of composite incomplete synthetic dependencies with a parser in the
295+
* query.
296+
*/
261297
@Test
262-
void testOnCompositeSyntheticDependencyIncompleteButWithParser() throws Exception {
298+
void queryDependency_handles_composite_incomplete_synthetic_dependency_but_with_parser()
299+
throws Exception {
263300
Path tempDirectory = Files.createTempDirectory("mvn-repo");
264301
Path tempPom = tempDirectory.resolve("pom.xml");
265302
String randomName = "random-artifact-" + System.currentTimeMillis();
@@ -316,7 +353,8 @@ void testOnCompositeSyntheticDependencyIncompleteButWithParser() throws Exceptio
316353

317354
LOGGER.debug("Dependencies found: {}", dependencies);
318355

319-
assertTrue("Dependencies are empty", !dependencies.isEmpty());
356+
// "Dependencies are empty"
357+
assertThat(!dependencies.isEmpty()).isTrue();
320358

321359
Dependency foundDependency =
322360
dependencies.stream()
@@ -327,7 +365,8 @@ void testOnCompositeSyntheticDependencyIncompleteButWithParser() throws Exceptio
327365
.findAny()
328366
.orElse(null);
329367

330-
assertTrue("There's a dependency with managed-version", foundDependency != null);
368+
// "There's a dependency with managed-version",
369+
assertThat(foundDependency).isNotNull();
331370
}
332371

333372
private List<Command> getCommandListFor(String... names) {
@@ -348,8 +387,12 @@ private List<Command> getCommandListFor(String... names) {
348387
return commandList;
349388
}
350389

390+
/**
391+
* Tests queryDependency handles offline mode. Verifies the behavior of dependency retrieval in
392+
* offline mode.
393+
*/
351394
@Test
352-
void testOfflineQueryResolution() throws Exception {
395+
void queryDependency_handles_offline_mode() throws Exception {
353396
Path tempDirectory = Files.createTempDirectory("mvn-repo");
354397
Path pomFilePath =
355398
Paths.get(getClass().getResource("nested/child/pom/pom-3-child.xml").toURI());
@@ -362,6 +405,7 @@ void testOfflineQueryResolution() throws Exception {
362405

363406
LOGGER.debug("Dependencies found: {}", dependencies);
364407

365-
assertTrue("Dependencies are empty", dependencies.isEmpty());
408+
// "Dependencies are empty"
409+
assertThat(dependencies).isEmpty();
366410
}
367411
}

0 commit comments

Comments
 (0)