Skip to content

Commit

Permalink
AVRO-3795: [Java] Raise exception for nonexistent imports in maven-pl…
Browse files Browse the repository at this point in the history
…ugin (#2334)

* AVRO-3795: [Java] Testcase for nonexistent files in maven-plugin

* AVRO-3795: [Java] Raise exception for nonexistent imports in maven-plugin

* AVRO-3795: [Java] Fix testcases for nonexistent files in maven-plugin

* AVRO-3795: [Java] Check imports for maven-plugin in separate method
  • Loading branch information
dervan committed Jul 14, 2023
1 parent 7e178b9 commit b7023cf
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public void execute() throws MojoExecutionException {
}

if (hasImports) {
checkImportPaths();
for (String importedFile : imports) {
File file = new File(importedFile);
if (file.isDirectory()) {
Expand Down Expand Up @@ -241,6 +242,15 @@ public void execute() throws MojoExecutionException {
}
}

private void checkImportPaths() throws MojoExecutionException {
for (String importedFile : imports) {
File file = new File(importedFile);
if (!file.exists()) {
throw new MojoExecutionException("Path " + file.getAbsolutePath() + " does not exist");
}
}
}

private String[] getIncludedFiles(String absPath, String[] excludes, String[] includes) {
final FileSetManager fileSetManager = new FileSetManager();
final FileSet fs = new FileSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.avro.mojo;

import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.FileUtils;
import org.junit.Test;

Expand All @@ -33,6 +34,10 @@ public class TestSchemaMojo extends AbstractAvroMojoTest {
private File testPom = new File(getBasedir(), "src/test/resources/unit/schema/pom.xml");
private File injectingVelocityToolsTestPom = new File(getBasedir(),
"src/test/resources/unit/schema/pom-injecting-velocity-tools.xml");
private File testNonexistentFilePom = new File(getBasedir(),
"src/test/resources/unit/schema/pom-nonexistent-file.xml");
private File testNonexistentSecondFilePom = new File(getBasedir(),
"src/test/resources/unit/schema/pom-nonexistent-second-file.xml");

@Test
public void testSchemaMojo() throws Exception {
Expand Down Expand Up @@ -67,4 +72,24 @@ public void testSetCompilerVelocityAdditionalTools() throws Exception {
final String schemaUserContent = FileUtils.fileRead(new File(outputDir, "SchemaUser.java"));
assertTrue("Got " + schemaUserContent + " instead", schemaUserContent.contains("It works!"));
}

@Test
public void testThrowsErrorForNonexistentFile() throws Exception {
try {
final SchemaMojo mojo = (SchemaMojo) lookupMojo("schema", testNonexistentFilePom);
mojo.execute();
fail("MojoExecutionException not thrown!");
} catch (MojoExecutionException ignored) {
}
}

@Test
public void testThrowsErrorForNonexistentSecondFile() throws Exception {
try {
final SchemaMojo mojo = (SchemaMojo) lookupMojo("schema", testNonexistentSecondFilePom);
mojo.execute();
fail("MojoExecutionException not thrown!");
} catch (MojoExecutionException ignored) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>avro-parent</artifactId>
<groupId>org.apache.avro</groupId>
<version>1.12.0-SNAPSHOT</version>
<relativePath>../../../../../../../../../pom.xml</relativePath>
</parent>

<artifactId>avro-maven-plugin-test</artifactId>
<packaging>jar</packaging>

<name>testproject</name>

<build>
<plugins>
<plugin>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schema</id>
<goals>
<goal>schema</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>${basedir}/src/test/avro</sourceDirectory>
<outputDirectory>${basedir}/target/test-harness/schema</outputDirectory>
<imports>
<import>${basedir}/src/test/avro/nonexistent-dir</import>
</imports>
<project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub"/>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>${parent.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>avro-parent</artifactId>
<groupId>org.apache.avro</groupId>
<version>1.12.0-SNAPSHOT</version>
<relativePath>../../../../../../../../../pom.xml</relativePath>
</parent>

<artifactId>avro-maven-plugin-test</artifactId>
<packaging>jar</packaging>

<name>testproject</name>

<build>
<plugins>
<plugin>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schema</id>
<goals>
<goal>schema</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>${basedir}/src/test/avro</sourceDirectory>
<outputDirectory>${basedir}/target/test-harness/schema</outputDirectory>
<imports>
<import>${basedir}/src/test/avro/imports</import>
<import>${basedir}/src/test/avro/nonexistent-dir</import>
</imports>
<project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub"/>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>${parent.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</project>

0 comments on commit b7023cf

Please sign in to comment.