Skip to content

Commit

Permalink
AVRO-2712: allow extends custom class
Browse files Browse the repository at this point in the history
  • Loading branch information
clesaec authored and dkulp committed Jul 31, 2023
1 parent ed2dbd4 commit 72a0a6c
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ void addLogicalTypeConversions(SpecificData specificData) {
private String suffix = ".java";
private List<Object> additionalVelocityTools = Collections.emptyList();

private String recordSpecificClass = "org.apache.avro.specific.SpecificRecordBase";

private String errorSpecificClass = "org.apache.avro.specific.SpecificExceptionBase";

/*
* Used in the record.vm template.
*/
Expand Down Expand Up @@ -1322,4 +1326,20 @@ public static void main(String[] args) throws Exception {
public void setOutputCharacterEncoding(String outputCharacterEncoding) {
this.outputCharacterEncoding = outputCharacterEncoding;
}

public String getSchemaParentClass(boolean isError) {
if (isError) {
return this.errorSpecificClass;
} else {
return this.recordSpecificClass;
}
}

public void setRecordSpecificClass(final String recordSpecificClass) {
this.recordSpecificClass = recordSpecificClass;
}

public void setErrorSpecificClass(final String errorSpecificClass) {
this.errorSpecificClass = errorSpecificClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.apache.avro.message.SchemaStore;
@$annotation
#end
@org.apache.avro.specific.AvroGenerated
public class ${this.mangleTypeIdentifier($schema.getName())}#if ($schema.isError()) extends org.apache.avro.specific.SpecificExceptionBase#else extends org.apache.avro.specific.SpecificRecordBase#end implements org.apache.avro.specific.SpecificRecord {
public class ${this.mangleTypeIdentifier($schema.getName())} extends ${this.getSchemaParentClass($schema.isError())} implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = ${this.fingerprint64($schema)}L;

#set ($schemaString = $this.javaSplit($schema.toString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public class SchemaMojo extends AbstractAvroMojo {
*/
private String[] testIncludes = new String[] { "**/*.avsc" };

/**
* generated record schema classes will extend this class.
*/
private String recordSpecificClass = "org.apache.avro.specific.SpecificRecordBase";

/**
* generated error schema classes will extend this class.
*/
private String errorSpecificClass = "org.apache.avro.specific.SpecificExceptionBase";

@Override
protected void doCompile(String filename, File sourceDirectory, File outputDirectory) throws IOException {
File src = new File(sourceDirectory, filename);
Expand Down Expand Up @@ -95,6 +105,8 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
}
compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"));
compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools());
compiler.setRecordSpecificClass(this.recordSpecificClass);
compiler.setErrorSpecificClass(this.errorSpecificClass);
compiler.compileToDestination(src, outputDirectory);
}

Expand Down
18 changes: 18 additions & 0 deletions lang/java/maven-plugin/src/test/avro/extends/Custom.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "record",
"namespace": "test",
"name": "SchemaCustom",
"doc": "Custom Test Bean",
"fields": [
{
"name": "id",
"type": ["null", "string"],
"default": null
},
{
"name": "createdOn",
"type": ["null", "long"],
"default": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/
package org.apache.avro.custom;

import org.apache.avro.specific.SpecificRecordBase;

public abstract class CustomRecordBase extends SpecificRecordBase {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.FileUtils;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Test the Schema Mojo.
Expand All @@ -39,6 +43,8 @@ public class TestSchemaMojo extends AbstractAvroMojoTest {
private File testNonexistentSecondFilePom = new File(getBasedir(),
"src/test/resources/unit/schema/pom-nonexistent-second-file.xml");

private File testExtendsFilePom = new File(getBasedir(), "src/test/resources/unit/schema/pom-customExtends.xml");

@Test
public void testSchemaMojo() throws Exception {
final SchemaMojo mojo = (SchemaMojo) lookupMojo("schema", testPom);
Expand All @@ -47,8 +53,8 @@ public void testSchemaMojo() throws Exception {
mojo.execute();

final File outputDir = new File(getBasedir(), "target/test-harness/schema/test");
final Set<String> generatedFiles = new HashSet<>(
Arrays.asList("PrivacyDirectImport.java", "PrivacyImport.java", "SchemaPrivacy.java", "SchemaUser.java"));
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("PrivacyDirectImport.java", "PrivacyImport.java",
"SchemaPrivacy.java", "SchemaUser.java", "SchemaCustom.java", "SchemaCustom.java"));

assertFilesExist(outputDir, generatedFiles);

Expand All @@ -64,8 +70,8 @@ public void testSetCompilerVelocityAdditionalTools() throws Exception {
mojo.execute();

final File outputDir = new File(getBasedir(), "target/test-harness/schema-inject/test");
final Set<String> generatedFiles = new HashSet<>(
Arrays.asList("PrivacyDirectImport.java", "PrivacyImport.java", "SchemaPrivacy.java", "SchemaUser.java"));
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("PrivacyDirectImport.java", "PrivacyImport.java",
"SchemaPrivacy.java", "SchemaUser.java", "SchemaCustom.java"));

assertFilesExist(outputDir, generatedFiles);

Expand All @@ -92,4 +98,21 @@ public void testThrowsErrorForNonexistentSecondFile() throws Exception {
} catch (MojoExecutionException ignored) {
}
}

@Test
public void testExtends() throws Exception {
final SchemaMojo mojo = (SchemaMojo) lookupMojo("schema", testExtendsFilePom);
assertNotNull(mojo);

mojo.execute();
final File outputDir = new File(getBasedir(), "target/extends/schema/test");
File outputFile = new File(outputDir, "SchemaCustom.java");
assertTrue(outputFile.exists());
List<String> extendsLines = Files.readAllLines(outputFile.toPath()).stream()
.filter((String line) -> line.contains("class SchemaCustom extends ")).collect(Collectors.toList());
assertEquals(1, extendsLines.size());
String extendLine = extendsLines.get(0);
assertTrue(extendLine.contains(" org.apache.avro.custom.CustomRecordBase "));
assertFalse(extendLine.contains("org.apache.avro.specific.SpecificRecordBase"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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>

<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/extends</sourceDirectory>
<outputDirectory>${basedir}/target/extends/schema</outputDirectory>
<recordSpecificClass>org.apache.avro.custom.CustomRecordBase</recordSpecificClass>
<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 72a0a6c

Please sign in to comment.