Skip to content

Commit 3a5f276

Browse files
authored
Merge pull request #2020 from web3j/nicks/testJavaWrappersFix
Fix for test java wrappers with duplicate name
2 parents c6afb76 + b85bbf1 commit 3a5f276

File tree

11 files changed

+130
-35
lines changed

11 files changed

+130
-35
lines changed

codegen/src/main/java/org/web3j/codegen/unit/gen/MethodFilter.java

+26-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import java.lang.reflect.Method;
1616
import java.util.ArrayList;
1717
import java.util.Arrays;
18+
import java.util.HashMap;
1819
import java.util.List;
20+
import java.util.Map;
1921
import java.util.stream.Collectors;
2022

2123
import com.squareup.javapoet.MethodSpec;
@@ -54,21 +56,37 @@ private static boolean parametersAreMatching(final Method method) {
5456

5557
public static List<MethodSpec> generateMethodSpecsForEachTest(Class theContract) {
5658
List<MethodSpec> listOfMethodSpecs = new ArrayList<>();
59+
Map<String, Integer> methodNameCountMap = new HashMap<>();
5760
extractValidMethods(theContract)
5861
.forEach(
59-
method ->
60-
listOfMethodSpecs.add(
61-
new MethodParser(method, theContract).getMethodSpec()));
62+
method -> {
63+
String uniqueName = getUniqueName(method, methodNameCountMap);
64+
listOfMethodSpecs.add(
65+
new MethodParser(method, theContract, uniqueName)
66+
.getMethodSpec());
67+
});
68+
6269
return listOfMethodSpecs;
6370
}
6471

6572
public static List<FunSpec> generateFunctionSpecsForEachTest(Class theContract) {
66-
List<FunSpec> listOfMethodSpecs = new ArrayList<>();
73+
List<FunSpec> listOfFunSpecs = new ArrayList<>();
74+
Map<String, Integer> functionNameCountMap = new HashMap<>();
6775
extractValidMethods(theContract)
6876
.forEach(
69-
method ->
70-
listOfMethodSpecs.add(
71-
new FunParser(method, theContract).getFunSpec()));
72-
return listOfMethodSpecs;
77+
method -> {
78+
String uniqueName = getUniqueName(method, functionNameCountMap);
79+
listOfFunSpecs.add(
80+
new FunParser(method, theContract, uniqueName).getFunSpec());
81+
});
82+
83+
return listOfFunSpecs;
84+
}
85+
86+
private static String getUniqueName(Method method, Map<String, Integer> nameCountMap) {
87+
String baseName = method.getName();
88+
int count = nameCountMap.getOrDefault(baseName, 0);
89+
nameCountMap.put(baseName, count + 1);
90+
return count > 0 ? baseName + count : baseName;
7391
}
7492
}

codegen/src/main/java/org/web3j/codegen/unit/gen/java/MethodParser.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,24 @@
3636
public class MethodParser {
3737
private final Method method;
3838
private final Class theContract;
39+
private final String uniqueMethodName;
3940

40-
public MethodParser(final Method method, final Class theContract) {
41+
public MethodParser(final Method method, final Class theContract, String uniqueMethodName) {
4142
this.method = method;
4243
this.theContract = theContract;
44+
this.uniqueMethodName = uniqueMethodName;
4345
}
4446

4547
public MethodSpec getMethodSpec() {
4648
return methodNeedsInjection()
4749
? new MethodSpecGenerator(
48-
method.getName(),
50+
uniqueMethodName,
4951
BeforeAll.class,
5052
Modifier.STATIC,
5153
defaultParameterSpecsForEachUnitTest(),
5254
generateStatementBody())
5355
.generate()
54-
: new MethodSpecGenerator(method.getName(), generateStatementBody()).generate();
56+
: new MethodSpecGenerator(uniqueMethodName, generateStatementBody()).generate();
5557
}
5658

5759
private boolean methodNeedsInjection() {

codegen/src/main/java/org/web3j/codegen/unit/gen/kotlin/FunParser.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,23 @@
3434
public class FunParser {
3535
private final Method method;
3636
private final Class theContract;
37+
private final String uniqueFunctionName;
3738

38-
public FunParser(final Method method, final Class theContract) {
39+
public FunParser(final Method method, final Class theContract, String uniqueFunctionName) {
3940
this.method = method;
4041
this.theContract = theContract;
42+
this.uniqueFunctionName = uniqueFunctionName;
4143
}
4244

4345
public FunSpec getFunSpec() {
4446
return methodNeedsInjection()
4547
? new FunSpecGenerator(
46-
method.getName(),
48+
uniqueFunctionName,
4749
BeforeAll.class,
4850
defaultParameterSpecsForEachUnitTest(),
4951
generateStatementBody())
5052
.generate()
51-
: new FunSpecGenerator(method.getName(), generateStatementBody()).generate();
53+
: new FunSpecGenerator(uniqueFunctionName, generateStatementBody()).generate();
5254
}
5355

5456
private boolean methodNeedsInjection() {

codegen/src/test/java/org/web3j/codegen/unit/gen/java/MethodParserTest.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
package org.web3j.codegen.unit.gen.java;
1414

1515
import java.lang.reflect.Method;
16+
import java.util.List;
1617
import java.util.Optional;
18+
import java.util.stream.Collectors;
1719

1820
import com.squareup.javapoet.MethodSpec;
1921
import org.junit.jupiter.api.Test;
2022

23+
import org.web3j.codegen.unit.gen.MethodFilter;
24+
25+
import static org.junit.Assert.assertTrue;
2126
import static org.junit.jupiter.api.Assertions.assertEquals;
2227

2328
public class MethodParserTest extends JavaTestSetup {
@@ -27,7 +32,8 @@ public void testThatDeployMethodWasGenerated() {
2732
Optional<Method> deployMethod =
2833
filteredMethods.stream().filter(m -> m.getName().equals("deploy")).findAny();
2934
MethodSpec deployMethodSpec =
30-
new MethodParser(deployMethod.get(), greeterContractClass).getMethodSpec();
35+
new MethodParser(deployMethod.get(), greeterContractClass, "deploy")
36+
.getMethodSpec();
3137
assertEquals(
3238
"@org.junit.jupiter.api.BeforeAll\n"
3339
+ "static void deploy(org.web3j.protocol.Web3j web3j, org.web3j.tx.TransactionManager transactionManager, org.web3j.tx.gas.ContractGasProvider contractGasProvider) throws java.lang.Exception {\n"
@@ -42,10 +48,29 @@ public void testThatNewGreetingMethodWasGenerated() {
4248
Optional<Method> deployMethod =
4349
filteredMethods.stream().filter(m -> m.getName().equals("newGreeting")).findAny();
4450
MethodSpec deployMethodSpec =
45-
new MethodParser(deployMethod.get(), greeterContractClass).getMethodSpec();
51+
new MethodParser(deployMethod.get(), greeterContractClass, "newGreeting")
52+
.getMethodSpec();
4653
assertEquals(
4754
"org.web3j.protocol.core.methods.response.TransactionReceipt transactionReceiptVar = greeter.newGreeting(\"REPLACE_ME\").send();\n"
4855
+ "org.junit.jupiter.api.Assertions.assertTrue(transactionReceiptVar.isStatusOK());\n",
4956
deployMethodSpec.code.toString());
5057
}
58+
59+
@Test
60+
public void testGeneratedDuplicateGreetingMethods() {
61+
List<MethodSpec> allMethodSpecs =
62+
MethodFilter.generateMethodSpecsForEachTest(greeterContractClass);
63+
64+
// Filter all MethodSpecs for those related to "greet" methods
65+
List<MethodSpec> greetMethodSpecs =
66+
allMethodSpecs.stream()
67+
.filter(methodSpec -> methodSpec.name.startsWith("greet"))
68+
.collect(Collectors.toList());
69+
70+
assertTrue(
71+
greetMethodSpecs.stream().anyMatch(methodSpec -> methodSpec.name.equals("greet")));
72+
assertTrue(
73+
greetMethodSpecs.stream().anyMatch(methodSpec -> methodSpec.name.equals("greet1")));
74+
assertEquals(2, greetMethodSpecs.size());
75+
}
5176
}

codegen/src/test/java/org/web3j/codegen/unit/gen/kotlin/FunParserTest.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@
1313
package org.web3j.codegen.unit.gen.kotlin;
1414

1515
import java.lang.reflect.Method;
16+
import java.util.List;
1617
import java.util.Optional;
18+
import java.util.stream.Collectors;
1719

1820
import com.squareup.kotlinpoet.FunSpec;
1921
import org.junit.jupiter.api.Test;
2022

23+
import org.web3j.codegen.unit.gen.MethodFilter;
24+
25+
import static org.junit.Assert.assertTrue;
2126
import static org.junit.jupiter.api.Assertions.assertEquals;
2227

2328
public class FunParserTest extends KotlinTestSetup {
2429
@Test
2530
public void testThatDeployMethodWasGenerated() {
2631
Optional<Method> deployFun =
2732
filteredMethods.stream().filter(m -> m.getName().equals("deploy")).findAny();
28-
FunSpec deployFunSpec = new FunParser(deployFun.get(), greeterContractClass).getFunSpec();
33+
FunSpec deployFunSpec =
34+
new FunParser(deployFun.get(), greeterContractClass, "deploy").getFunSpec();
2935
assertEquals(
3036
deployFunSpec.toString(),
3137
"@org.junit.jupiter.api.BeforeAll\n"
@@ -42,7 +48,8 @@ public void testThatDeployMethodWasGenerated() {
4248
public void testThatNewGreetingMethodWasGenerated() {
4349
Optional<Method> deployFun =
4450
filteredMethods.stream().filter(m -> m.getName().equals("newGreeting")).findAny();
45-
FunSpec deployFunSpec = new FunParser(deployFun.get(), greeterContractClass).getFunSpec();
51+
FunSpec deployFunSpec =
52+
new FunParser(deployFun.get(), greeterContractClass, "newGreeting").getFunSpec();
4653
assertEquals(
4754
deployFunSpec.toString(),
4855
"@org.junit.jupiter.api.Test\n"
@@ -51,4 +58,24 @@ public void testThatNewGreetingMethodWasGenerated() {
5158
+ " org.junit.jupiter.api.Assertions.assertTrue(transactionReceiptVar.isStatusOK())\n"
5259
+ "}\n");
5360
}
61+
62+
@Test
63+
public void testGeneratedDuplicateGreetingMethods() {
64+
List<FunSpec> allMethodSpecs =
65+
MethodFilter.generateFunctionSpecsForEachTest(greeterContractClass);
66+
67+
// Filter all FunSpecs for those related to "greet" methods
68+
List<FunSpec> greetFunSpecs =
69+
allMethodSpecs.stream()
70+
.filter(funSpec -> funSpec.getName().startsWith("greet"))
71+
.collect(Collectors.toList());
72+
73+
assertTrue(
74+
greetFunSpecs.stream()
75+
.anyMatch(methodSpec -> methodSpec.getName().equals("greet")));
76+
assertTrue(
77+
greetFunSpecs.stream()
78+
.anyMatch(methodSpec -> methodSpec.getName().equals("greet1")));
79+
assertEquals(2, greetFunSpecs.size());
80+
}
5481
}

codegen/src/test/resources/solidity/greeter/Greeter.sol

+4
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ contract Greeter is Mortal {
3131
function greet() public view returns (string) {
3232
return greeting;
3333
}
34+
35+
function greet(string newGreet) public pure returns (string) {
36+
return newGreet;
37+
}
3438
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_greeting","type":"string"}],"name":"newGreeting","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
1+
[{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_greeting","type":"string"}],"name":"newGreeting","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"newGreet","type":"string"}],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
608060405234801561001057600080fd5b5060405161040038038061040083398101604052805160008054600160a060020a0319163317905501805161004c906001906020840190610053565b50506100ee565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009457805160ff19168380011785556100c1565b828001600101855582156100c1579182015b828111156100c15782518255916020019190600101906100a6565b506100cd9291506100d1565b5090565b6100eb91905b808211156100cd57600081556001016100d7565b90565b610303806100fd6000396000f3006080604052600436106100565763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b5811461005b5780634ac0d66e14610072578063cfae3217146100cb575b600080fd5b34801561006757600080fd5b50610070610155565b005b34801561007e57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100709436949293602493928401919081908401838280828437509497506101929650505050505050565b3480156100d757600080fd5b506100e06101a9565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561011a578181015183820152602001610102565b50505050905090810190601f1680156101475780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60005473ffffffffffffffffffffffffffffffffffffffff163314156101905760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b80516101a590600190602084019061023f565b5050565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102345780601f1061020957610100808354040283529160200191610234565b820191906000526020600020905b81548152906001019060200180831161021757829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061028057805160ff19168380011785556102ad565b828001600101855582156102ad579182015b828111156102ad578251825591602001919060010190610292565b506102b99291506102bd565b5090565b61023c91905b808211156102b957600081556001016102c35600a165627a7a72305820a9bc86938894dc250f6ea25dd823d4472fad6087edcda429a3504e3713a9fc880029
1+
608060405234801561001057600080fd5b5060405161046438038061046483398101604052805160008054600160a060020a0319163317905501805161004c906001906020840190610053565b50506100ee565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009457805160ff19168380011785556100c1565b828001600101855582156100c1579182015b828111156100c15782518255916020019190600101906100a6565b506100cd9291506100d1565b5090565b6100eb91905b808211156100cd57600081556001016100d7565b90565b610367806100fd6000396000f3006080604052600436106100615763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b581146100665780634ac0d66e1461007d578063cfae3217146100d6578063ead710c414610160575b600080fd5b34801561007257600080fd5b5061007b6101b9565b005b34801561008957600080fd5b506040805160206004803580820135601f810184900484028501840190955284845261007b9436949293602493928401919081908401838280828437509497506101f69650505050505050565b3480156100e257600080fd5b506100eb61020d565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561012557818101518382015260200161010d565b50505050905090810190601f1680156101525780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561016c57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100eb9436949293602493928401919081908401838280828437509497506102a09650505050505050565b60005473ffffffffffffffffffffffffffffffffffffffff163314156101f45760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b80516102099060019060208401906102a3565b5050565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102985780601f1061026d57610100808354040283529160200191610298565b820191906000526020600020905b81548152906001019060200180831161027b57829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102e457805160ff1916838001178555610311565b82800160010185558215610311579182015b828111156103115782518255916020019190600101906102f6565b5061031d929150610321565b5090565b6102a091905b8082111561031d57600081556001016103275600a165627a7a7230582002ef0e7cb360516c1f64403a364a524d0c4cf14ede2cb2db39e8ab45e96e3c770029
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
608060405234801561001057600080fd5b5060008054600160a060020a0319163317905560bf806100316000396000f300608060405260043610603e5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b581146043575b600080fd5b348015604e57600080fd5b5060556057565b005b60005473ffffffffffffffffffffffffffffffffffffffff1633141560915760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5600a165627a7a72305820625b7679c6c8ba6c2ba1468ffd850d828b06c0e18c1ea6ac511e8c903d747e7b0029
1+
608060405234801561001057600080fd5b5060008054600160a060020a0319163317905560bf806100316000396000f300608060405260043610603e5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b581146043575b600080fd5b348015604e57600080fd5b5060556057565b005b60005473ffffffffffffffffffffffffffffffffffffffff1633141560915760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5600a165627a7a72305820b1e05d167881a64930ea4d3b13ed0a337a54a7505e082bcac336a2bc28ebfc620029

0 commit comments

Comments
 (0)