Skip to content

Commit

Permalink
Merge pull request #2025 from web3j/testWrappersFix
Browse files Browse the repository at this point in the history
Fix for test wrappers generation
  • Loading branch information
gtebrean committed Apr 4, 2024
2 parents 16b133b + 141f2aa commit 4f38cea
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -7,7 +7,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

### Bug Fixes

*
* Fix for test wrappers generation [#2025](https://github.com/web3j/web3j/pull/2025)

### Features

Expand Down
Expand Up @@ -672,7 +672,7 @@ private Set<String> getDuplicateFunctionNames(List<AbiDefinition> functionDefini
private static MethodSpec buildGetDeploymentBinaryMethod() {
MethodSpec.Builder toReturn =
MethodSpec.methodBuilder("getDeploymentBinary")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.returns(ClassName.get(String.class));

CodeBlock codeBlock =
Expand Down
16 changes: 11 additions & 5 deletions codegen/src/main/java/org/web3j/codegen/unit/gen/MethodFilter.java
Expand Up @@ -13,6 +13,7 @@
package org.web3j.codegen.unit.gen;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -61,9 +62,11 @@ public static List<MethodSpec> generateMethodSpecsForEachTest(Class theContract)
.forEach(
method -> {
String uniqueName = getUniqueName(method, methodNameCountMap);
listOfMethodSpecs.add(
new MethodParser(method, theContract, uniqueName)
.getMethodSpec());
if (!Modifier.isPrivate(method.getModifiers())) {
listOfMethodSpecs.add(
new MethodParser(method, theContract, uniqueName)
.getMethodSpec());
}
});

return listOfMethodSpecs;
Expand All @@ -76,8 +79,11 @@ public static List<FunSpec> generateFunctionSpecsForEachTest(Class theContract)
.forEach(
method -> {
String uniqueName = getUniqueName(method, functionNameCountMap);
listOfFunSpecs.add(
new FunParser(method, theContract, uniqueName).getFunSpec());
if (!Modifier.isPrivate(method.getModifiers())) {
listOfFunSpecs.add(
new FunParser(method, theContract, uniqueName)
.getFunSpec());
}
});

return listOfFunSpecs;
Expand Down
Expand Up @@ -73,4 +73,22 @@ public void testGeneratedDuplicateGreetingMethods() {
greetMethodSpecs.stream().anyMatch(methodSpec -> methodSpec.name.equals("greet1")));
assertEquals(2, greetMethodSpecs.size());
}

@Test
public void testGetDeploymentBinaryMethodNotGenerated() {
List<MethodSpec> allMethodSpecs =
MethodFilter.generateMethodSpecsForEachTest(greeterContractClass);

// Filter all MethodSpecs for those related to "getDeploymentBinary" method
List<MethodSpec> getDeploymentBinaryMethodSpecs =
allMethodSpecs.stream()
.filter(methodSpec -> methodSpec.name.startsWith("getDeploymentBinary"))
.collect(Collectors.toList());

// Ensure no MethodSpecs were generated for getDeploymentBinary method
assertEquals(
0,
getDeploymentBinaryMethodSpecs.size(),
"MethodSpec list should not contain getDeploymentBinary method");
}
}
Expand Up @@ -78,4 +78,22 @@ public void testGeneratedDuplicateGreetingMethods() {
.anyMatch(methodSpec -> methodSpec.getName().equals("greet1")));
assertEquals(2, greetFunSpecs.size());
}

@Test
public void testGetDeploymentBinaryMethodNotGenerated() {
List<FunSpec> allMethodSpecs =
MethodFilter.generateFunctionSpecsForEachTest(greeterContractClass);

// Filter all FunSpecs for those related to "getDeploymentBinary" method
List<FunSpec> getDeploymentBinaryFunSpecs =
allMethodSpecs.stream()
.filter(funSpec -> funSpec.getName().startsWith("getDeploymentBinary"))
.collect(Collectors.toList());

// Ensure no MethodSpecs were generated for getDeploymentBinary method
assertEquals(
0,
getDeploymentBinaryFunSpecs.size(),
"MethodSpec list should not contain getDeploymentBinary function");
}
}
Expand Up @@ -166,7 +166,7 @@ public static void linkLibraries(List<Contract.LinkReference> references) {
librariesLinkedBinary = linkBinaryWithReferences(BINARY, references);
}

public static String getDeploymentBinary() {
private static String getDeploymentBinary() {
if (librariesLinkedBinary != null) {
return librariesLinkedBinary;
} else {
Expand Down

0 comments on commit 4f38cea

Please sign in to comment.