Skip to content

Commit

Permalink
Fix Integration Tests (hyperledger-web3j#1294)
Browse files Browse the repository at this point in the history
* Initial Commit.

* Initial IT Commit

* CoreIT test fixes

* 80% of Scenarios pass.

* Fixed more tests.

* All tests are passing
Clean up

* Applied feedback

* Wrappers are now generated with every build of codegen

* Removed primitive contract. There is not use for it

* Fixed failing test.
Wrappers will be generated only if the sol file is provided.
  • Loading branch information
AlexandrouR authored and rach-id committed Apr 12, 2021
1 parent 4515daa commit a183a7c
Show file tree
Hide file tree
Showing 66 changed files with 678 additions and 3,586 deletions.
22 changes: 21 additions & 1 deletion codegen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@ dependencies {
"com.squareup:kotlinpoet:$kotlinPoetVersion",
"com.squareup:javapoet:$javaPoetVersion",
"info.picocli:picocli:$picocliVersion"
testCompile project(path: ':core', configuration: 'testArtifacts')
testCompile project(':core').sourceSets.test.output
implementation("org.junit.platform:junit-platform-launcher:$junitPlatformLauncherVersion")
implementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
implementation("org.junit.vintage:junit-vintage-engine:$junitVersion")
}


file("src/test/resources/solidity").listFiles().each { File file ->
def contractFile = file.listFiles().find({
it.getName().endsWith(".sol")
})
if (contractFile != null) {
def contractName = contractFile.name.substring(0, contractFile.name.length() - 4)
def generateTask = tasks.register("generate${contractName}Wrapper", JavaExec) {
classpath = sourceSets.test.runtimeClasspath
main = 'org.web3j.codegen.SolidityFunctionWrapperGenerator'
args 'generate',
'--abiFile', "${contractFile.parent}/build/${contractName}.abi",
'--binFile', "${contractFile.parent}/build/${contractName}.bin",
'--outputDir', "build/resources/test/java",
'--package', 'org.com.test.contract'
}
test.dependsOn generateTask
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import javax.tools.ToolProvider;

/** Class loader with compilation capabilities. */
class CompilerClassLoader extends ClassLoader {
public class CompilerClassLoader extends ClassLoader {

private final File outputDir;
private final URL[] urls;
Expand All @@ -39,7 +39,7 @@ class CompilerClassLoader extends ClassLoader {
* @param outputDir Directory where classes will be compiled.
* @param urls Classpath URLs to compile the Java sources.
*/
CompilerClassLoader(final File outputDir, final URL... urls) {
public CompilerClassLoader(final File outputDir, final URL... urls) {
super(CompilerClassLoader.class.getClassLoader());
this.outputDir = outputDir;
this.urls = urls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -71,8 +67,8 @@ public void testGreeterGeneration() throws Exception {

@Test
public void testHumanStandardTokenGeneration() throws Exception {
testCodeGenerationJvmTypes("contracts", "HumanStandardToken");
testCodeGenerationSolidityTypes("contracts", "HumanStandardToken");
testCodeGenerationJvmTypes("humanstandardtoken", "HumanStandardToken");
testCodeGenerationSolidityTypes("humanstandardtoken", "HumanStandardToken");
}

@Test
Expand Down Expand Up @@ -107,14 +103,14 @@ public void testMisc() throws Exception {

@Test
public void testContractsNoBin() throws Exception {
testCodeGeneration("contracts", "HumanStandardToken", JAVA_TYPES_ARG, false);
testCodeGeneration("contracts", "HumanStandardToken", SOLIDITY_TYPES_ARG, false);
testCodeGeneration("humanstandardtoken", "HumanStandardToken", JAVA_TYPES_ARG, false);
testCodeGeneration("humanstandardtoken", "HumanStandardToken", SOLIDITY_TYPES_ARG, false);
}

@Test
public void testComplexStorage0425() throws Exception {
testCodeGenerationJvmTypes("complexstorage0.4.25", "ComplexStorage");
testCodeGenerationSolidityTypes("complexstorage0.4.25", "ComplexStorage");
testCodeGenerationJvmTypes("complexstoragenew", "ComplexStorageNew");
testCodeGenerationSolidityTypes("complexstoragenew", "ComplexStorageNew");
}

@Test
Expand All @@ -141,13 +137,13 @@ public void testDuplicateField() throws Exception {
public void testGenerationCommandPrefixes() throws Exception {
testCodeGeneration(
Arrays.asList(COMMAND_SOLIDITY, COMMAND_GENERATE),
"contracts",
"humanstandardtoken",
"HumanStandardToken",
JAVA_TYPES_ARG,
true);
testCodeGeneration(
Arrays.asList(COMMAND_GENERATE),
"contracts",
"humanstandardtoken",
"HumanStandardToken",
SOLIDITY_TYPES_ARG,
true);
Expand Down
14 changes: 6 additions & 8 deletions codegen/src/test/java/org/web3j/codegen/unit/gen/java/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.io.TempDir;

import org.web3j.codegen.unit.gen.ClassProvider;
import org.web3j.codegen.unit.gen.CompilerClassLoader;
import org.web3j.codegen.unit.gen.MethodFilter;

public class Setup {
Expand All @@ -37,6 +37,7 @@ public class Setup {

@BeforeAll
public static void setUp() throws IOException, ClassNotFoundException {

String urlAsString =
Objects.requireNonNull(
Setup.class
Expand All @@ -54,13 +55,10 @@ public static void setUp() throws IOException, ClassNotFoundException {
"contracts",
"GreeterTest.java");
classAsFile = new File(urlAsString);
File greeter = new File(urlAsString.substring(0, urlAsString.indexOf("org/")));
greeterContractClass =
new ClassProvider(greeter)
.getClasses().stream()
.filter(className -> className.getSimpleName().equals("Greeter"))
.findAny()
.orElse(null);
CompilerClassLoader compilerClassLoader =
new CompilerClassLoader(temp, Setup.class.getClassLoader().getResource("java/"));

greeterContractClass = compilerClassLoader.loadClass("org.com.test.contract.Greeter");

filteredMethods = MethodFilter.extractValidMethods(greeterContractClass);
new JavaClassGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.io.TempDir;

import org.web3j.codegen.unit.gen.ClassProvider;
import org.web3j.codegen.unit.gen.CompilerClassLoader;
import org.web3j.codegen.unit.gen.MethodFilter;

public class Setup {
Expand Down Expand Up @@ -53,13 +53,14 @@ public static void setUp() throws Exception {
"contracts",
"GreeterTest.kt");
classAsFile = new File(urlAsString);
File greeter = new File(urlAsString.substring(0, urlAsString.indexOf("org/")));
greeterContractClass =
new ClassProvider(greeter)
.getClasses().stream()
.filter(className -> className.getSimpleName().equals("Greeter"))
.findAny()
.orElse(null);
CompilerClassLoader compilerClassLoader =
new CompilerClassLoader(
temp,
org.web3j.codegen.unit.gen.java.Setup.class
.getClassLoader()
.getResource("java/"));

greeterContractClass = compilerClassLoader.loadClass("org.com.test.contract.Greeter");

filteredMethods = MethodFilter.extractValidMethods(greeterContractClass);
new KotlinClassGenerator(
Expand Down
Loading

0 comments on commit a183a7c

Please sign in to comment.