Skip to content

Commit

Permalink
Add check for compilation in test case
Browse files Browse the repository at this point in the history
  • Loading branch information
tonykwok1992 committed Mar 14, 2024
1 parent 3b7bae9 commit a1914e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
25 changes: 25 additions & 0 deletions codegen/src/test/java/org/web3j/codegen/GeneraterTestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.web3j.codegen;

import javax.tools.*;
import java.io.IOException;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class GeneraterTestUtils {

public static void verifyGeneratedCode(String sourceFile) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnostics, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjectsFromStrings(
Collections.singletonList(sourceFile));
JavaCompiler.CompilationTask task =
compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);
assertTrue(task.call(), "Generated contract contains compile time error");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.tools.*;

Expand Down Expand Up @@ -204,7 +205,6 @@ public void testEventParametersNoNamedCompareJavaFile() throws Exception {

@Test
public void testDeployMethodGenerated() throws Exception {
compareJavaFile("MetaCoin", false);
compareJavaFile("MetaCoin", true);
}

Expand All @@ -227,6 +227,8 @@ private void compareJavaFile(String inputFileName, boolean useBin) throws Except
assertEquals(
new String(Files.readAllBytes(fileExpected.toPath())).replaceAll("(\r\n|\n)", ""),
new String(Files.readAllBytes(fileActual.toPath())).replaceAll("(\r\n|\n)", ""));

verifyGeneratedCode(fileActual.getAbsolutePath());
}

private void testCodeGenerationJvmTypes(String contractName, String inputFileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@
package org.web3j.codegen;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -39,21 +32,6 @@ public class TruffleJsonFunctionWrapperGeneratorTest extends TempFileProvider {

private String contractBaseDir;

private static void verifyGeneratedCode(String sourceFile) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnostics, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjectsFromStrings(
Collections.singletonList(sourceFile));
JavaCompiler.CompilationTask task =
compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);
assertTrue(task.call(), "Generated contract contains compile time error");
}
}

@BeforeEach
public void setUp() throws Exception {
super.setUp();
Expand Down Expand Up @@ -103,7 +81,7 @@ private void testCodeGeneration(
tempDirPath)
.toArray(new String[0]));

verifyGeneratedCode(
GeneraterTestUtils.verifyGeneratedCode(
tempDirPath
+ File.separator
+ packageName.replace('.', File.separatorChar)
Expand Down

0 comments on commit a1914e7

Please sign in to comment.