Skip to content

Commit b00df52

Browse files
committed
refactor: remove duplicated code in MavenModuleTest
1 parent 0f50103 commit b00df52

File tree

1 file changed

+74
-54
lines changed

1 file changed

+74
-54
lines changed

src/test/java/io/github/algomaster99/maven_module_graph/MavenModuleTest.java

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,81 +14,101 @@
1414

1515

1616
public class MavenModuleTest {
17+
1718
@Test
1819
void legend_engine(@TempDir Path temp) throws XmlPullParserException, IOException {
19-
// arrange
20-
MavenModule module = Utility.createMavenModuleGraph(Path.of("src/test/resources/legend-engine/legend-engine"), null, new HashMap<>());
21-
Path expectedPlainText = Path.of("src/test/resources/legend-engine/output.txt");
22-
Path actualPlainText = temp.resolve("legend-engine.txt");
23-
24-
Path expectedJson = Path.of("src/test/resources/legend-engine/output.json");
25-
Path actualJson = temp.resolve("legend-engine.json");
26-
27-
// act
28-
Utility.printToFile(module, actualPlainText, 2);
29-
Utility.printToJson(module, actualJson, 2);
30-
31-
// assert
32-
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualPlainText)));
33-
assertThat(Files.readString(expectedJson), equalTo(Files.readString(actualJson)));
20+
runTest(
21+
"src/test/resources/legend-engine/legend-engine",
22+
"src/test/resources/legend-engine/output.txt",
23+
"src/test/resources/legend-engine/output.json",
24+
temp.resolve("legend-engine.txt"),
25+
temp.resolve("legend-engine.json"),
26+
2, 2
27+
);
3428
}
3529

3630
@Test
3731
void neo4j_profileModules(@TempDir Path temp) throws XmlPullParserException, IOException {
38-
// arrange
39-
MavenModule module = Utility.createMavenModuleGraph(Path.of("src/test/resources/neo4j/neo4j"), null, new HashMap<>());
40-
Path expectedPlainText = Path.of("src/test/resources/neo4j/output.txt");
41-
Path actualPlainText = temp.resolve("neo4j.txt");
42-
43-
Path expectedJson = Path.of("src/test/resources/neo4j/output.json");
44-
Path actualJson = temp.resolve("neo4j.json");
45-
46-
// act
47-
Utility.printToFile(module, actualPlainText, 2);
48-
Utility.printToJson(module, actualJson, 0);
49-
50-
// assert
51-
assertThat(Files.readAllLines(expectedPlainText).size(), equalTo(125));
52-
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualPlainText)));
53-
assertThat(Files.readString(expectedJson), equalTo(Files.readString(actualJson)));
32+
runTestWithLineCountCheck(
33+
"src/test/resources/neo4j/neo4j",
34+
"src/test/resources/neo4j/output.txt",
35+
"src/test/resources/neo4j/output.json",
36+
temp.resolve("neo4j.txt"),
37+
temp.resolve("neo4j.json"),
38+
2, 0,
39+
125
40+
);
5441
}
5542

5643
@Test
5744
void arthas_mavenProperties(@TempDir Path temp) throws XmlPullParserException, IOException {
58-
// arrange
59-
MavenModule module = Utility.createMavenModuleGraph(Path.of("src/test/resources/arthas/arthas"), null, new HashMap<>());
60-
Path expectedPlainText = Path.of("src/test/resources/arthas/output.txt");
61-
Path actualPlainText = temp.resolve("arthas.txt");
45+
runTest(
46+
"src/test/resources/arthas/arthas",
47+
"src/test/resources/arthas/output.txt",
48+
"src/test/resources/arthas/output.json",
49+
temp.resolve("arthas.txt"),
50+
temp.resolve("arthas.json"),
51+
2, 0
52+
);
53+
}
54+
55+
@Test
56+
void persistence_differentProjectRoot(@TempDir Path temp) throws XmlPullParserException, IOException {
57+
runTest(
58+
"src/test/resources/persistence/persistence/api",
59+
"src/test/resources/persistence/output.txt",
60+
"src/test/resources/persistence/output.json",
61+
temp.resolve("persistence.txt"),
62+
temp.resolve("persistence.json"),
63+
0, 2
64+
);
65+
}
6266

63-
Path expectedJson = Path.of("src/test/resources/arthas/output.json");
64-
Path actualJson = temp.resolve("arthas.json");
67+
private void runTest(
68+
String modulePath,
69+
String expectedTextPath,
70+
String expectedJsonPath,
71+
Path actualTextPath,
72+
Path actualJsonPath,
73+
int textIndent,
74+
int jsonIndent
75+
) throws XmlPullParserException, IOException {
76+
// arrange
77+
MavenModule module = Utility.createMavenModuleGraph(Path.of(modulePath), null, new HashMap<>());
78+
Path expectedPlainText = Path.of(expectedTextPath);
79+
Path expectedJson = Path.of(expectedJsonPath);
6580

6681
// act
67-
Utility.printToFile(module, actualPlainText, 2);
68-
Utility.printToJson(module, actualJson, 0);
82+
Utility.printToFile(module, actualTextPath, textIndent);
83+
Utility.printToJson(module, actualJsonPath, jsonIndent);
6984

7085
// assert
71-
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualPlainText)));
72-
assertThat(Files.readString(expectedJson), equalTo(Files.readString(actualJson)));
86+
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualTextPath)));
87+
assertThat(Files.readString(expectedJson), equalTo(Files.readString(actualJsonPath)));
7388
}
7489

75-
@Test
76-
void persistence_differentProjectRoot(@TempDir Path temp) throws XmlPullParserException, IOException {
90+
private void runTestWithLineCountCheck(
91+
String modulePath,
92+
String expectedTextPath,
93+
String expectedJsonPath,
94+
Path actualTextPath,
95+
Path actualJsonPath,
96+
int textIndent,
97+
int jsonIndent,
98+
int expectedLineCount
99+
) throws XmlPullParserException, IOException {
77100
// arrange
78-
MavenModule module = Utility.createMavenModuleGraph(Path.of("src/test/resources/persistence/persistence/api"), null, new HashMap<>());
79-
Path expectedPlainText = Path.of("src/test/resources/persistence/output.txt");
80-
Path actualPlainText = temp.resolve("arthas.txt");
81-
82-
Path expectedJson = Path.of("src/test/resources/persistence/output.json");
83-
Path actualJson = temp.resolve("arthas.json");
101+
MavenModule module = Utility.createMavenModuleGraph(Path.of(modulePath), null, new HashMap<>());
102+
Path expectedPlainText = Path.of(expectedTextPath);
103+
Path expectedJson = Path.of(expectedJsonPath);
84104

85105
// act
86-
Utility.printToFile(module, actualPlainText, 0);
87-
Utility.printToJson(module, actualJson, 2);
106+
Utility.printToFile(module, actualTextPath, textIndent);
107+
Utility.printToJson(module, actualJsonPath, jsonIndent);
88108

89109
// assert
90-
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualPlainText)));
91-
assertThat(Files.readString(expectedJson), equalTo(Files.readString(actualJson)));
110+
assertThat(Files.readAllLines(expectedPlainText).size(), equalTo(expectedLineCount));
111+
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualTextPath)));
112+
assertThat(Files.readString(expectedJson), equalTo(Files.readString(actualJsonPath)));
92113
}
93-
94114
}

0 commit comments

Comments
 (0)