Remove unused test dependencies org.reflections & maven-rewrite (#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
This commit is contained in:
Philzen 2024-06-01 10:42:26 +02:00 committed by GitHub
parent 6a6f39d6f4
commit 66126a8ae4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 224 additions and 86 deletions

View File

@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openapi-generator-cli</artifactId>

View File

@ -8,7 +8,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -7,7 +7,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>openapi-generator-maven-plugin</artifactId>
<name>openapi-generator (maven-plugin)</name>

View File

@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>openapi-generator-online</artifactId>
<packaging>jar</packaging>

View File

@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openapi-generator</artifactId>
@ -349,17 +349,6 @@
<version>3.24.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.java-diff-utils</groupId>
<artifactId>diffutils</artifactId>
@ -392,6 +381,12 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>

View File

@ -1,14 +1,12 @@
package org.openapitools.codegen;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.google.common.collect.ImmutableMap;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
@ -17,30 +15,20 @@ import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.apache.commons.io.IOUtils;
import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.utils.ModelUtils;
import org.openrewrite.maven.internal.RawPom;
import org.testng.Assert;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import com.google.common.collect.ImmutableMap;
import static org.testng.Assert.*;
public class TestUtils {
@ -100,7 +88,7 @@ public class TestUtils {
return openAPI;
}
public static OpenAPI createOpenAPIWithOneSchema(String name, Schema schema) {
public static OpenAPI createOpenAPIWithOneSchema(String name, Schema<?> schema) {
OpenAPI openAPI = createOpenAPI();
openAPI.setComponents(new Components());
openAPI.getComponents().addSchemas(name, schema);
@ -135,47 +123,36 @@ public class TestUtils {
assertFalse(generatedFiles.contains(path.toFile()), "File '" + path.toAbsolutePath() + "' was found in the list of generated files");
}
public static void validatePomXmlFiles(final Map<String, String> fileMap) {
fileMap.forEach( (fileName, fileContents) -> {
if ("pom.xml".equals(fileName)) {
assertValidPomXml(fileContents);
}
});
}
public static void validatePomXmlFiles(final List<File> files) {
files.forEach( f -> {
String fileName = f.getName();
if ("pom.xml".equals(fileName)) {
try {
String fileContents = new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
assertValidPomXml(fileContents);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
}
);
if (files == null
|| files.isEmpty()
|| files.stream().noneMatch(f -> f.getName().equals("pom.xml"))) return;
final XmlMapper mapper = new XmlMapper();
for (File file : files) {
if (!"pom.xml".equals(file.getName())) continue;
try {
JsonNode pomContents = mapper.readTree(file);
assertValidPomXml(pomContents);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
};
}
private static void assertValidPomXml(final String fileContents) {
final InputStream input = new ByteArrayInputStream(fileContents.getBytes(StandardCharsets.UTF_8));
try {
RawPom pom = RawPom.parse(input, null);
assertTrue(pom.getDependencies().getDependencies().size() > 0);
assertNotNull(pom.getName());
assertNotNull(pom.getArtifactId());
assertNotNull(pom.getGroupId());
assertNotNull(pom.getVersion());
} finally {
IOUtils.closeQuietly(input);
}
private static void assertValidPomXml(final JsonNode pom) {
assertFalse(pom.path("dependencies").isEmpty());
assertNotNull(pom.get("name"));
assertNotNull(pom.get("artifactId"));
assertNotNull(pom.get("groupId"));
assertNotNull(pom.get("version"));
}
public static void validateJavaSourceFiles(Map<String, String> fileMap) {
fileMap.forEach( (fileName, fileContents) -> {
if (fileName.endsWith(".java")) {
assertValidJavaSourceCode(fileContents, fileName);
assertValidJavaSourceCode(fileContents);
}
}
);
@ -183,21 +160,20 @@ public class TestUtils {
public static void validateJavaSourceFiles(List<File> files) {
files.forEach( f -> {
String fileName = f.getName();
if (fileName.endsWith(".java")) {
if (f.getName().endsWith(".java")) {
String fileContents = "";
try {
fileContents = new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
fileContents = Files.readString(f.toPath());
} catch (IOException ignored) {
}
assertValidJavaSourceCode(fileContents, fileName);
assertValidJavaSourceCode(fileContents);
}
}
);
}
public static void assertValidJavaSourceCode(String javaSourceCode, String filename) {
public static void assertValidJavaSourceCode(String javaSourceCode) {
ParserConfiguration config = new ParserConfiguration();
config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);
JavaParser parser = new JavaParser(config);
@ -207,7 +183,7 @@ public class TestUtils {
public static void assertFileContains(Path path, String... lines) {
try {
String generatedFile = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
String generatedFile = Files.readString(path);
String file = linearize(generatedFile);
assertNotNull(file);
for (String line : lines)
@ -224,7 +200,7 @@ public class TestUtils {
public static void assertFileNotContains(Path path, String... lines) {
String generatedFile = null;
try {
generatedFile = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
generatedFile = Files.readString(path);
} catch (IOException e) {
fail("Unable to evaluate file " + path);
}
@ -236,7 +212,7 @@ public class TestUtils {
public static void assertFileNotExists(Path path) {
try {
new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
Files.readString(path);
fail("File exists when it should not: " + path);
} catch (IOException e) {
// File exists, pass.
@ -246,7 +222,7 @@ public class TestUtils {
public static void assertFileExists(Path path) {
try {
new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
Files.readString(path);
// File exists, pass.
assertTrue(true);
} catch (IOException e) {

View File

@ -0,0 +1,175 @@
package org.openapitools.codegen;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@SuppressWarnings("NewClassNamingConvention")
public class TestUtilsTest {
// forbiddenapis check will fail if we don't explicitly define localization when using String.format
static final Locale L = null;
public static class validatePomXmlFiles {
static final String POM_SCAFFOLD = "<project>%s<dependencies>%s</dependencies></project>";
static final String POM_PROJECT_INFO = "<name>foo</name><artifactId>foo.bar</artifactId><groupId>foobar</groupId><version>13.12</version>";
@Test void doesNotThrow_ifNotPom_doesNotExist() {
final var vaporFile = newTempDir().resolve("other.xml").toFile();
assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(vaporFile)))
.doesNotThrowAnyException();
}
@Test void throwsRuntimeException_ifPomDoesNotExist() {
final var vaporBom = newTempDir().resolve("pom.xml").toFile();
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(vaporBom)))
.isExactlyInstanceOf(RuntimeException.class);
}
@Test void throwsRuntimeException_ifXmlIsJson() {
Path testFile = newPomXmlFile("{\"not_xml\": 12345}");
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.isExactlyInstanceOf(RuntimeException.class);
}
@Test void throwsRuntimeException_ifXmlIsInvalid() {
final Path testFile = newPomXmlFile("<IAmNotClosed>");
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.isExactlyInstanceOf(RuntimeException.class);
}
@Test void throwsAssertionError_ifNameTagIsMissing() {
final Path testFile = newPomXmlFile(replaceTag("name", "", getMinimalValidPomContent()));
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.isExactlyInstanceOf(AssertionError.class);
}
@Test void doesNotThrow_ifNameIsEmpty() {
final Path testFile = newPomXmlFile(
replaceTag("name", "<name></name>", getMinimalValidPomContent())
);
assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.doesNotThrowAnyException();
}
@Test void throwsAssertionError_ifArtifactIdTagIsMissing() {
final Path testFile = newPomXmlFile(replaceTag("artifactId", "", getMinimalValidPomContent()));
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.isExactlyInstanceOf(AssertionError.class);
}
@Test void doesNotThrow_ifArtifactIdIsEmpty() {
final Path testFile = newPomXmlFile(
replaceTag("artifactId", "<artifactId></artifactId>", getMinimalValidPomContent())
);
assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.doesNotThrowAnyException();
}
@Test void throwsAssertionError_ifGroupIdTagIsMissing() {
final Path testFile = newPomXmlFile(replaceTag("groupId", "", getMinimalValidPomContent()));
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.isExactlyInstanceOf(AssertionError.class);
}
@Test void doesNotThrow_ifGroupIdIsEmpty() {
final Path testFile = newPomXmlFile(
replaceTag("groupId", "<groupId></groupId>", getMinimalValidPomContent())
);
assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.doesNotThrowAnyException();
}
@Test void throwsAssertionError_ifVersionTagIsMissing() {
final Path testFile = newPomXmlFile(replaceTag("version", "", getMinimalValidPomContent()));
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.isExactlyInstanceOf(AssertionError.class);
}
@Test void doesNotThrow_ifVersionIsEmpty() {
final Path testFile = newPomXmlFile(
replaceTag("version", "<version></version>", getMinimalValidPomContent())
);
assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.doesNotThrowAnyException();
}
@Test void throwsAssertionError_ifZeroDependencies() {
final Path testFile = newPomXmlFile(String.format(L, POM_SCAFFOLD, POM_PROJECT_INFO, ""));
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.isExactlyInstanceOf(AssertionError.class);
}
@Test void doesNotThrow_ifAtLeastOneDependency() {
final Path testFile = newPomXmlFile(String.format(L, POM_SCAFFOLD, POM_PROJECT_INFO, "<dependency />"));
assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
.doesNotThrowAnyException();
}
@Test void throwsAssertionError_withTwoValidPoms_whereSecondHasNoName() {
final File testFile1 = newPomXmlFile(getMinimalValidPomContent()).toFile();
final File testFile2 = newPomXmlFile(replaceTag("name", "", getMinimalValidPomContent())).toFile();
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile1, testFile2)))
.isExactlyInstanceOf(AssertionError.class);
}
@Test void doesNotThrow_withTwoValidPoms() {
final File testFile1 = newPomXmlFile(getMinimalValidPomContent()).toFile();
final File testFile2 = newPomXmlFile(getMinimalValidPomContent()).toFile();
assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile1, testFile2)))
.doesNotThrowAnyException();
}
private String replaceTag(String tagToReplace, String replaceWith, String xml) {
return xml.replaceAll("<" + tagToReplace + ">[\\s\\S]*?</" + tagToReplace + ">", replaceWith);
}
private String getMinimalValidPomContent() {
return String.format(L, POM_SCAFFOLD, POM_PROJECT_INFO, "<dependency />");
}
private Path newPomXmlFile(String content) {
try {
return Files.writeString(newTempDir().resolve("pom.xml"), content);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Path newTempDir() {
final Path tempDir;
try {
tempDir = Files.createTempDirectory("test");
} catch (IOException e) {
throw new RuntimeException(e);
}
tempDir.toFile().deleteOnExit();
return tempDir;
}
}
}

View File

@ -1198,12 +1198,6 @@
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
<version>${openrewrite.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
@ -1248,9 +1242,7 @@
<maven-site-plugin.version>4.0.0-M8</maven-site-plugin.version>
<maven-surefire-plugin.version>3.2.5</maven-surefire-plugin.version>
<mockito.version>4.10.0</mockito.version>
<openrewrite.version>8.8.3</openrewrite.version>
<pmd-plugin.version>3.12.0</pmd-plugin.version>
<reflections.version>0.10.2</reflections.version>
<resolver-util-version>1.9.18</resolver-util-version>
<rxgen.version>1.4</rxgen.version>
<scala-maven-plugin.version>4.6.1</scala-maven-plugin.version>