Skip to content

Commit a1dafd0

Browse files
Philzenwelshm
authored andcommitted
Remove unused test dependencies org.reflections & maven-rewrite (OpenAPITools#18798)
* Specify exact relative path to parent pom.xml * Remove org.reflections from the test stack * Remove unused parameter * Remove unused method * Fix raw use of parameterized variable warning * Use Files.readString instead of converting raw bytes * Add test coverage for TestUtils.validatePomXmlFiles(List) * Reimplement TestUtils.validatePomXmlFiles(List) with Jackson XML mapper * Remove maven-rewrite dependency
1 parent 435eef2 commit a1dafd0

9 files changed

Lines changed: 224 additions & 86 deletions

File tree

modules/openapi-generator-cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!-- RELEASE_VERSION -->
77
<version>7.7.0-SNAPSHOT</version>
88
<!-- /RELEASE_VERSION -->
9-
<relativePath>../..</relativePath>
9+
<relativePath>../../pom.xml</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>openapi-generator-cli</artifactId>

modules/openapi-generator-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- RELEASE_VERSION -->
99
<version>7.7.0-SNAPSHOT</version>
1010
<!-- /RELEASE_VERSION -->
11-
<relativePath>../..</relativePath>
11+
<relativePath>../../pom.xml</relativePath>
1212
</parent>
1313
<modelVersion>4.0.0</modelVersion>
1414

modules/openapi-generator-gradle-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!-- RELEASE_VERSION -->
77
<version>7.7.0-SNAPSHOT</version>
88
<!-- /RELEASE_VERSION -->
9-
<relativePath>../..</relativePath>
9+
<relativePath>../../pom.xml</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
1212

modules/openapi-generator-maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<!-- RELEASE_VERSION -->
88
<version>7.7.0-SNAPSHOT</version>
99
<!-- /RELEASE_VERSION -->
10-
<relativePath>../..</relativePath>
10+
<relativePath>../../pom.xml</relativePath>
1111
</parent>
1212
<artifactId>openapi-generator-maven-plugin</artifactId>
1313
<name>openapi-generator (maven-plugin)</name>

modules/openapi-generator-online/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!-- RELEASE_VERSION -->
77
<version>7.7.0-SNAPSHOT</version>
88
<!-- /RELEASE_VERSION -->
9-
<relativePath>../..</relativePath>
9+
<relativePath>../../pom.xml</relativePath>
1010
</parent>
1111
<artifactId>openapi-generator-online</artifactId>
1212
<packaging>jar</packaging>

modules/openapi-generator/pom.xml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!-- RELEASE_VERSION -->
77
<version>7.7.0-SNAPSHOT</version>
88
<!-- /RELEASE_VERSION -->
9-
<relativePath>../..</relativePath>
9+
<relativePath>../../pom.xml</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>openapi-generator</artifactId>
@@ -349,17 +349,6 @@
349349
<version>3.24.9</version>
350350
<scope>test</scope>
351351
</dependency>
352-
<dependency>
353-
<groupId>org.openrewrite</groupId>
354-
<artifactId>rewrite-maven</artifactId>
355-
<scope>test</scope>
356-
</dependency>
357-
<dependency>
358-
<groupId>org.reflections</groupId>
359-
<artifactId>reflections</artifactId>
360-
<version>${reflections.version}</version>
361-
<scope>test</scope>
362-
</dependency>
363352
<dependency>
364353
<groupId>com.googlecode.java-diff-utils</groupId>
365354
<artifactId>diffutils</artifactId>
@@ -392,6 +381,12 @@
392381
<artifactId>jackson-databind</artifactId>
393382
<version>${jackson-databind.version}</version>
394383
</dependency>
384+
<dependency>
385+
<groupId>com.fasterxml.jackson.dataformat</groupId>
386+
<artifactId>jackson-dataformat-xml</artifactId>
387+
<version>${jackson.version}</version>
388+
<scope>test</scope>
389+
</dependency>
395390
<dependency>
396391
<groupId>com.fasterxml.jackson.datatype</groupId>
397392
<artifactId>jackson-datatype-joda</artifactId>

modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.openapitools.codegen;
22

3-
import static org.testng.Assert.assertNotNull;
4-
import static org.testng.Assert.fail;
5-
import static org.testng.Assert.assertTrue;
6-
import static org.testng.Assert.assertFalse;
7-
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
85
import com.github.javaparser.JavaParser;
9-
import com.github.javaparser.ParserConfiguration;
106
import com.github.javaparser.ParseResult;
7+
import com.github.javaparser.ParserConfiguration;
118
import com.github.javaparser.ast.CompilationUnit;
9+
import com.google.common.collect.ImmutableMap;
1210
import io.swagger.parser.OpenAPIParser;
1311
import io.swagger.v3.oas.models.Components;
1412
import io.swagger.v3.oas.models.OpenAPI;
@@ -17,30 +15,20 @@
1715
import io.swagger.v3.oas.models.media.Schema;
1816
import io.swagger.v3.oas.models.servers.Server;
1917
import io.swagger.v3.parser.core.models.ParseOptions;
20-
21-
import org.apache.commons.io.IOUtils;
2218
import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
2319
import org.openapitools.codegen.java.assertions.JavaFileAssert;
2420
import org.openapitools.codegen.model.ModelMap;
2521
import org.openapitools.codegen.model.ModelsMap;
2622
import org.openapitools.codegen.utils.ModelUtils;
27-
import org.openrewrite.maven.internal.RawPom;
2823
import org.testng.Assert;
2924

30-
import java.io.ByteArrayInputStream;
3125
import java.io.File;
3226
import java.io.IOException;
33-
import java.io.InputStream;
34-
import java.nio.charset.StandardCharsets;
3527
import java.nio.file.Files;
3628
import java.nio.file.Path;
37-
import java.util.ArrayList;
38-
import java.util.Collections;
39-
import java.util.List;
40-
import java.util.Map;
41-
import java.util.Optional;
29+
import java.util.*;
4230

43-
import com.google.common.collect.ImmutableMap;
31+
import static org.testng.Assert.*;
4432

4533
public class TestUtils {
4634

@@ -100,7 +88,7 @@ public static OpenAPI createOpenAPI() {
10088
return openAPI;
10189
}
10290

103-
public static OpenAPI createOpenAPIWithOneSchema(String name, Schema schema) {
91+
public static OpenAPI createOpenAPIWithOneSchema(String name, Schema<?> schema) {
10492
OpenAPI openAPI = createOpenAPI();
10593
openAPI.setComponents(new Components());
10694
openAPI.getComponents().addSchemas(name, schema);
@@ -135,69 +123,57 @@ public static void ensureDoesNotContainsFile(List<File> generatedFiles, File roo
135123
assertFalse(generatedFiles.contains(path.toFile()), "File '" + path.toAbsolutePath() + "' was found in the list of generated files");
136124
}
137125

138-
public static void validatePomXmlFiles(final Map<String, String> fileMap) {
139-
fileMap.forEach( (fileName, fileContents) -> {
140-
if ("pom.xml".equals(fileName)) {
141-
assertValidPomXml(fileContents);
142-
}
143-
});
144-
}
145-
146126
public static void validatePomXmlFiles(final List<File> files) {
147-
files.forEach( f -> {
148-
String fileName = f.getName();
149-
if ("pom.xml".equals(fileName)) {
150-
try {
151-
String fileContents = new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
152-
assertValidPomXml(fileContents);
153-
} catch (IOException exception) {
154-
throw new RuntimeException(exception);
155-
}
156-
}
157-
}
158-
);
127+
if (files == null
128+
|| files.isEmpty()
129+
|| files.stream().noneMatch(f -> f.getName().equals("pom.xml"))) return;
130+
131+
final XmlMapper mapper = new XmlMapper();
132+
for (File file : files) {
133+
if (!"pom.xml".equals(file.getName())) continue;
134+
135+
try {
136+
JsonNode pomContents = mapper.readTree(file);
137+
assertValidPomXml(pomContents);
138+
} catch (IOException exception) {
139+
throw new RuntimeException(exception);
140+
}
141+
};
159142
}
160143

161-
private static void assertValidPomXml(final String fileContents) {
162-
final InputStream input = new ByteArrayInputStream(fileContents.getBytes(StandardCharsets.UTF_8));
163-
try {
164-
RawPom pom = RawPom.parse(input, null);
165-
assertTrue(pom.getDependencies().getDependencies().size() > 0);
166-
assertNotNull(pom.getName());
167-
assertNotNull(pom.getArtifactId());
168-
assertNotNull(pom.getGroupId());
169-
assertNotNull(pom.getVersion());
170-
} finally {
171-
IOUtils.closeQuietly(input);
172-
}
144+
private static void assertValidPomXml(final JsonNode pom) {
145+
assertFalse(pom.path("dependencies").isEmpty());
146+
assertNotNull(pom.get("name"));
147+
assertNotNull(pom.get("artifactId"));
148+
assertNotNull(pom.get("groupId"));
149+
assertNotNull(pom.get("version"));
173150
}
174151

175152
public static void validateJavaSourceFiles(Map<String, String> fileMap) {
176153
fileMap.forEach( (fileName, fileContents) -> {
177154
if (fileName.endsWith(".java")) {
178-
assertValidJavaSourceCode(fileContents, fileName);
155+
assertValidJavaSourceCode(fileContents);
179156
}
180157
}
181158
);
182159
}
183160

184161
public static void validateJavaSourceFiles(List<File> files) {
185162
files.forEach( f -> {
186-
String fileName = f.getName();
187-
if (fileName.endsWith(".java")) {
163+
if (f.getName().endsWith(".java")) {
188164
String fileContents = "";
189165
try {
190-
fileContents = new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
166+
fileContents = Files.readString(f.toPath());
191167
} catch (IOException ignored) {
192168

193169
}
194-
assertValidJavaSourceCode(fileContents, fileName);
170+
assertValidJavaSourceCode(fileContents);
195171
}
196172
}
197173
);
198174
}
199175

200-
public static void assertValidJavaSourceCode(String javaSourceCode, String filename) {
176+
public static void assertValidJavaSourceCode(String javaSourceCode) {
201177
ParserConfiguration config = new ParserConfiguration();
202178
config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);
203179
JavaParser parser = new JavaParser(config);
@@ -207,7 +183,7 @@ public static void assertValidJavaSourceCode(String javaSourceCode, String filen
207183

208184
public static void assertFileContains(Path path, String... lines) {
209185
try {
210-
String generatedFile = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
186+
String generatedFile = Files.readString(path);
211187
String file = linearize(generatedFile);
212188
assertNotNull(file);
213189
for (String line : lines)
@@ -224,7 +200,7 @@ public static String linearize(String target) {
224200
public static void assertFileNotContains(Path path, String... lines) {
225201
String generatedFile = null;
226202
try {
227-
generatedFile = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
203+
generatedFile = Files.readString(path);
228204
} catch (IOException e) {
229205
fail("Unable to evaluate file " + path);
230206
}
@@ -236,7 +212,7 @@ public static void assertFileNotContains(Path path, String... lines) {
236212

237213
public static void assertFileNotExists(Path path) {
238214
try {
239-
new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
215+
Files.readString(path);
240216
fail("File exists when it should not: " + path);
241217
} catch (IOException e) {
242218
// File exists, pass.
@@ -246,7 +222,7 @@ public static void assertFileNotExists(Path path) {
246222

247223
public static void assertFileExists(Path path) {
248224
try {
249-
new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
225+
Files.readString(path);
250226
// File exists, pass.
251227
assertTrue(true);
252228
} catch (IOException e) {

0 commit comments

Comments
 (0)