Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3889: [Java][Build] Maven IDL Generation Modification Check #2561

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec

URLClassLoader projPathLoader = new URLClassLoader(runtimeUrls.toArray(new URL[0]),
Thread.currentThread().getContextClassLoader());
try (Idl parser = new Idl(new File(sourceDirectory, filename), projPathLoader)) {
File sourceFile = new File(sourceDirectory, filename);
try (Idl parser = new Idl(sourceFile, projPathLoader)) {

Protocol p = parser.CompilationUnit();
for (String warning : parser.getWarningsAfterParsing()) {
Expand All @@ -104,7 +105,7 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
compiler.addCustomConversion(projPathLoader.loadClass(customConversion));
}
compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"));
compiler.compileToDestination(null, outputDirectory);
compiler.compileToDestination(sourceFile, outputDirectory);
}
} catch (ParseException | ClassNotFoundException | DependencyResolutionRequiredException e) {
throw new IOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
*/
package org.apache.avro.mojo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import org.codehaus.plexus.util.FileUtils;
import org.junit.Test;

Expand All @@ -37,14 +41,17 @@ public class TestIDLProtocolMojo extends AbstractAvroMojoTest {

@Test
public void testIdlProtocolMojo() throws Exception {
// Clear output directory to ensure files are recompiled.
final File outputDir = new File(getBasedir(), "target/test-harness/idl/test/");
FileUtils.deleteDirectory(outputDir);

final IDLProtocolMojo mojo = (IDLProtocolMojo) lookupMojo("idl-protocol", testPom);
final TestLog log = new TestLog();
mojo.setLog(log);

assertNotNull(mojo);
mojo.execute();

final File outputDir = new File(getBasedir(), "target/test-harness/idl/test/");
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java",
"IdlUserWrapper.java", "IdlClasspathImportTest.java"));
assertFilesExist(outputDir, generatedFiles);
Expand All @@ -60,14 +67,17 @@ public void testIdlProtocolMojo() throws Exception {

@Test
public void testSetCompilerVelocityAdditionalTools() throws Exception {
// Clear output directory to ensure files are recompiled.
final File outputDir = new File(getBasedir(), "target/test-harness/idl-inject/test");
FileUtils.deleteDirectory(outputDir);

final IDLProtocolMojo mojo = (IDLProtocolMojo) lookupMojo("idl-protocol", injectingVelocityToolsTestPom);
final TestLog log = new TestLog();
mojo.setLog(log);

assertNotNull(mojo);
mojo.execute();

final File outputDir = new File(getBasedir(), "target/test-harness/idl-inject/test");
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java",
"IdlUserWrapper.java", "IdlClasspathImportTest.java"));

Expand All @@ -79,4 +89,37 @@ public void testSetCompilerVelocityAdditionalTools() throws Exception {
// The previous test already verifies the warnings.
assertFalse(log.getLogEntries().isEmpty());
}

@Test
public void testIdlProtocolMojoDoesntReplaceUpToDateFiles() throws Exception {
// Ensure that the IDL files have already been compiled once.
final IDLProtocolMojo mojo = (IDLProtocolMojo) lookupMojo("idl-protocol", testPom);
final TestLog log = new TestLog();
mojo.setLog(log);

assertNotNull(mojo);
mojo.execute();

// Remove one file to ensure it is recreated and the others are not.
final Path outputDirPath = Paths.get(getBasedir(), "target/test-harness/idl/test/");
final File outputDir = outputDirPath.toFile();

final Path idlPrivacyFilePath = outputDirPath.resolve("IdlPrivacy.java");
final FileTime idpPrivacyModificationTime = Files.getLastModifiedTime(idlPrivacyFilePath);
Files.delete(idlPrivacyFilePath);

final Path idlUserFilePath = outputDirPath.resolve("IdlUser.java");
final FileTime idlUserModificationTime = Files.getLastModifiedTime(idlUserFilePath);

mojo.execute();

// Asserting contents is done in previous tests so just assert existence.
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java",
"IdlUserWrapper.java", "IdlClasspathImportTest.java"));
assertFilesExist(outputDir, generatedFiles);

assertTrue(idlPrivacyFilePath.toFile().exists());
assertEquals(Files.getLastModifiedTime(idlUserFilePath), idlUserModificationTime);
assertTrue(Files.getLastModifiedTime(idlPrivacyFilePath).compareTo(idpPrivacyModificationTime) > 0);
}
}