Permalink
Browse files

AVRO-1207. Java: Add tests for Maven plugins. Contributed by Sharmark…

…e Aden.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1411406 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 22d55dd commit 88736f8d389c17584508a105d608bca2b3281084 @cutting cutting committed Nov 19, 2012
View
@@ -29,6 +29,8 @@ Trunk (not yet released)
AVRO-1183. Java: Provide a better error message when the schema
for a Pair cannot be inferred. (cutting)
+ AVRO-1207. Java: Add tests for Maven plugins. (Sharmarke Aden via cutting)
+
BUG FIXES
AVRO-1171. Java: Don't call configure() twice on mappers & reducers.
@@ -37,6 +37,10 @@
<maven>${maven.version}</maven>
</prerequisites>
+ <properties>
+ <pluginTestingVersion>1.3</pluginTestingVersion>
+ </properties>
+
<build>
<plugins>
<plugin>
@@ -56,6 +60,7 @@
</build>
<dependencies>
+ <!-- maven -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
@@ -76,6 +81,25 @@
<artifactId>avro-compiler</artifactId>
<version>${project.version}</version>
</dependency>
+ <!-- test-->
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>${maven.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.plugin-testing</groupId>
+ <artifactId>maven-plugin-testing-tools</artifactId>
+ <version>${pluginTestingVersion}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.plugin-testing</groupId>
+ <artifactId>maven-plugin-testing-harness</artifactId>
+ <version>${pluginTestingVersion}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
@@ -63,15 +63,22 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
try {
@SuppressWarnings("rawtypes")
List runtimeClasspathElements = project.getRuntimeClasspathElements();
+ Idl parser;
+
+ // If runtimeClasspathElements is not empty values add its values to Idl path.
+ if(runtimeClasspathElements == null || runtimeClasspathElements.isEmpty()) {
+ parser = new Idl(new File(sourceDirectory, filename));
+ } else {
URL[] runtimeUrls = new URL[runtimeClasspathElements.size()];
for (int i = 0; i < runtimeClasspathElements.size(); i++) {
String element = (String) runtimeClasspathElements.get(i);
runtimeUrls[i] = new File(element).toURI().toURL();
}
URLClassLoader projPathLoader = new URLClassLoader
(runtimeUrls, Thread.currentThread().getContextClassLoader());
+ parser = new Idl(new File(sourceDirectory, filename), projPathLoader);
+ }
- Idl parser = new Idl(new File(sourceDirectory, filename), projPathLoader);
Protocol p = parser.CompilationUnit();
String json = p.toString(true);
Protocol protocol = Protocol.parse(json);
@@ -0,0 +1,14 @@
+@namespace("test")
+protocol IdlTest {
+
+ enum IdlPrivacy {
+ Public, Private
+ }
+
+ record IdlUser {
+ union { null, string } id;
+ union { null, long } createdOn;
+ union { null, IdlPrivacy } privacy;
+ }
+
+}
@@ -0,0 +1,34 @@
+{
+ "protocol" : "ProtocolTest",
+ "namespace" : "test",
+ "types" : [
+ {
+ "type" : "enum",
+ "name" : "ProtocolPrivacy",
+ "symbols" : [ "Public", "Private"]
+ },
+ {
+ "type": "record",
+ "namespace": "test",
+ "name": "ProtocolUser",
+ "doc": "User Test Bean",
+ "fields": [
+ {
+ "name": "id",
+ "type": ["null", "string"],
+ "default": null
+ },
+ {
+ "name": "createdOn",
+ "type": ["null", "long"],
+ "default": null
+ },
+ {
+ "name": "privacy",
+ "type": ["null", "ProtocolPrivacy"],
+ "default": null
+ }
+ ]
+ }
+ ]
+}
@@ -0,0 +1,38 @@
+{
+ "type": "record",
+ "namespace": "test",
+ "name": "SchemaUser",
+ "doc": "User Test Bean",
+ "fields": [
+ {
+ "name": "id",
+ "type": ["null", "string"],
+ "default": null
+ },
+ {
+ "name": "createdOn",
+ "type": ["null", "long"],
+ "default": null
+ },
+ {
+ "name": "privacy",
+ "type": ["null", {
+ "type": "enum",
+ "name": "SchemaPrivacy",
+ "namespace": "test",
+ "symbols" : ["Public","Private"]
+ }],
+ "default": null
+ },
+ {
+ "name": "privacyImported",
+ "type": ["null", "test.PrivacyImport"],
+ "default": null
+ },
+ {
+ "name": "privacyDirectImport",
+ "type": ["null", "test.PrivacyDirectImport"],
+ "default": null
+ }
+ ]
+}
@@ -0,0 +1,7 @@
+{
+ "type": "enum",
+ "namespace": "test",
+ "name": "PrivacyDirectImport",
+ "doc": "Privacy Test Enum",
+ "symbols" : ["Public","Private"]
+}
@@ -0,0 +1,7 @@
+{
+ "type": "enum",
+ "namespace": "test",
+ "name": "PrivacyImport",
+ "doc": "Privacy Test Enum",
+ "symbols" : ["Public","Private"]
+}
@@ -0,0 +1,60 @@
+/**
+ * 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
+ *
+ * http://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.
+ */
+package org.apache.avro.mojo;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.maven.plugin.testing.AbstractMojoTestCase;
+
+/**
+ * Base class for all Arvo mojo test classes.
+ *
+ * @author saden
+ */
+public class AbstractAvroMojoTest extends AbstractMojoTestCase {
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Assert the existence files in the given given directory.
+ *
+ * @param directory the directory being checked
+ * @param files the files whose existence is being checked.
+ */
+ protected void assertFilesExist(File directory, String... files) {
+ assertNotNull(directory);
+ assertTrue(directory.exists());
+ assertNotNull(files);
+ assertTrue(files.length > 0);
+
+ List<String> dirList = Arrays.asList(directory.list());
+
+ for (String file : files) {
+ assertTrue("File " + file + " does not exist.", dirList.contains(file));
+ }
+ }
+}
@@ -0,0 +1,44 @@
+/**
+ * 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
+ *
+ * http://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.
+ */
+package org.apache.avro.mojo;
+
+import java.io.File;
+
+/**
+ * Test the IDL Protocol Mojo.
+ *
+ * @author saden
+ */
+public class TestIDLProtocolMojo extends AbstractAvroMojoTest {
+
+ protected File testPom = new File(getBasedir(),
+ "src/test/resources/unit/idl/pom.xml");
+
+ public void testIdlProtocolMojo() throws Exception {
+ IDLProtocolMojo mojo = (IDLProtocolMojo) lookupMojo("idl-protocol", testPom);
+
+ assertNotNull(mojo);
+ mojo.execute();
+
+ File outputDir = new File(getBasedir(), "target/test-harness/idl/test");
+ String[] generatedFiles = new String[]{"IdlPrivacy.java",
+ "IdlTest.java", "IdlUser.java"};
+
+ assertFilesExist(outputDir, generatedFiles);
+ }
+}
@@ -0,0 +1,44 @@
+/**
+ * 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
+ *
+ * http://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.
+ */
+package org.apache.avro.mojo;
+
+import java.io.File;
+
+/**
+ * Test the Protocol Mojo.
+ *
+ * @author saden
+ */
+public class TestProtocolMojo extends AbstractAvroMojoTest {
+
+ protected File testPom = new File(getBasedir(),
+ "src/test/resources/unit/protocol/pom.xml");
+
+ public void testProtocolMojo() throws Exception {
+ ProtocolMojo mojo = (ProtocolMojo) lookupMojo("protocol", testPom);
+
+ assertNotNull(mojo);
+ mojo.execute();
+
+ File outputDir = new File(getBasedir(), "target/test-harness/protocol/test");
+ String[] generatedFiles = new String[]{"ProtocolPrivacy.java",
+ "ProtocolTest.java", "ProtocolUser.java"};
+
+ assertFilesExist(outputDir, generatedFiles);
+ }
+}
@@ -0,0 +1,44 @@
+/**
+ * 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
+ *
+ * http://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.
+ */
+package org.apache.avro.mojo;
+
+import java.io.File;
+
+/**
+ * Test the Schema Mojo.
+ *
+ * @author saden
+ */
+public class TestSchemaMojo extends AbstractAvroMojoTest {
+
+ protected File testPom = new File(getBasedir(),
+ "src/test/resources/unit/schema/pom.xml");
+
+ public void testSchemaMojo() throws Exception {
+ SchemaMojo mojo = (SchemaMojo) lookupMojo("schema", testPom);
+
+ assertNotNull(mojo);
+ mojo.execute();
+
+ File outputDir = new File(getBasedir(), "target/test-harness/schema/test");
+ String[] generatedFiles = new String[]{"PrivacyDirectImport.java",
+ "PrivacyImport.java", "SchemaPrivacy.java", "SchemaUser.java"};
+
+ assertFilesExist(outputDir, generatedFiles);
+ }
+}
Oops, something went wrong.

0 comments on commit 88736f8

Please sign in to comment.