This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/branch-1.11 by this push:
new 817765b3e AVRO-3795: [Java] Raise exception for nonexistent imports in
maven-plugin (#2334)
817765b3e is described below
commit 817765b3edff84a3a5fb19ea605800fce2d1e42d
Author: MichaĆ Jagielski <[email protected]>
AuthorDate: Fri Jul 14 13:49:49 2023 +0200
AVRO-3795: [Java] Raise exception for nonexistent imports in maven-plugin
(#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
---
.../org/apache/avro/mojo/AbstractAvroMojo.java | 10 ++++
.../java/org/apache/avro/mojo/TestSchemaMojo.java | 25 ++++++++
.../resources/unit/schema/pom-nonexistent-file.xml | 69 +++++++++++++++++++++
.../unit/schema/pom-nonexistent-second-file.xml | 70 ++++++++++++++++++++++
4 files changed, 174 insertions(+)
diff --git
a/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
b/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
index 968d8d0bd..5120aa621 100644
---
a/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
+++
b/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
@@ -211,6 +211,7 @@ public abstract class AbstractAvroMojo extends AbstractMojo
{
}
if (hasImports) {
+ checkImportPaths();
for (String importedFile : imports) {
File file = new File(importedFile);
if (file.isDirectory()) {
@@ -241,6 +242,15 @@ public abstract class AbstractAvroMojo extends
AbstractMojo {
}
}
+ 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();
diff --git
a/lang/java/maven-plugin/src/test/java/org/apache/avro/mojo/TestSchemaMojo.java
b/lang/java/maven-plugin/src/test/java/org/apache/avro/mojo/TestSchemaMojo.java
index f6bdc7fd0..682bf6d8f 100644
---
a/lang/java/maven-plugin/src/test/java/org/apache/avro/mojo/TestSchemaMojo.java
+++
b/lang/java/maven-plugin/src/test/java/org/apache/avro/mojo/TestSchemaMojo.java
@@ -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;
@@ -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 {
@@ -67,4 +72,24 @@ public class TestSchemaMojo extends AbstractAvroMojoTest {
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) {
+ }
+ }
}
diff --git
a/lang/java/maven-plugin/src/test/resources/unit/schema/pom-nonexistent-file.xml
b/lang/java/maven-plugin/src/test/resources/unit/schema/pom-nonexistent-file.xml
new file mode 100644
index 000000000..49965752d
--- /dev/null
+++
b/lang/java/maven-plugin/src/test/resources/unit/schema/pom-nonexistent-file.xml
@@ -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>
diff --git
a/lang/java/maven-plugin/src/test/resources/unit/schema/pom-nonexistent-second-file.xml
b/lang/java/maven-plugin/src/test/resources/unit/schema/pom-nonexistent-second-file.xml
new file mode 100644
index 000000000..f5b7134cd
--- /dev/null
+++
b/lang/java/maven-plugin/src/test/resources/unit/schema/pom-nonexistent-second-file.xml
@@ -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>