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

Fix for test java wrappers with duplicate name #2020

Merged
merged 4 commits into from
Mar 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions codegen/src/main/java/org/web3j/codegen/unit/gen/MethodFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

public static List<MethodSpec> generateMethodSpecsForEachTest(Class theContract) {
List<MethodSpec> listOfMethodSpecs = new ArrayList<>();
Map<String, Integer> methodNameCountMap = new HashMap<>();
extractValidMethods(theContract)
.forEach(
method ->
listOfMethodSpecs.add(
new MethodParser(method, theContract).getMethodSpec()));
method -> {
String uniqueName = getUniqueName(method, methodNameCountMap);
listOfMethodSpecs.add(
new MethodParser(method, theContract, uniqueName)
.getMethodSpec());
});

return listOfMethodSpecs;
}

public static List<FunSpec> generateFunctionSpecsForEachTest(Class theContract) {
List<FunSpec> listOfMethodSpecs = new ArrayList<>();
List<FunSpec> listOfFunSpecs = new ArrayList<>();
Map<String, Integer> functionNameCountMap = new HashMap<>();
extractValidMethods(theContract)
.forEach(
method ->
listOfMethodSpecs.add(
new FunParser(method, theContract).getFunSpec()));
return listOfMethodSpecs;
method -> {
String uniqueName = getUniqueName(method, functionNameCountMap);
listOfFunSpecs.add(
new FunParser(method, theContract, uniqueName).getFunSpec());
});

return listOfFunSpecs;
}

private static String getUniqueName(Method method, Map<String, Integer> nameCountMap) {
String baseName = method.getName();
int count = nameCountMap.getOrDefault(baseName, 0);
nameCountMap.put(baseName, count + 1);
return count > 0 ? baseName + count : baseName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@
public class MethodParser {
private final Method method;
private final Class theContract;
private final String uniqueMethodName;

public MethodParser(final Method method, final Class theContract) {
public MethodParser(final Method method, final Class theContract, String uniqueMethodName) {
this.method = method;
this.theContract = theContract;
this.uniqueMethodName = uniqueMethodName;
}

public MethodSpec getMethodSpec() {
return methodNeedsInjection()
? new MethodSpecGenerator(
method.getName(),
uniqueMethodName,
BeforeAll.class,
Modifier.STATIC,
defaultParameterSpecsForEachUnitTest(),
generateStatementBody())
.generate()
: new MethodSpecGenerator(method.getName(), generateStatementBody()).generate();
: new MethodSpecGenerator(uniqueMethodName, generateStatementBody()).generate();
}

private boolean methodNeedsInjection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@
public class FunParser {
private final Method method;
private final Class theContract;
private final String uniqueFunctionName;

public FunParser(final Method method, final Class theContract) {
public FunParser(final Method method, final Class theContract, String uniqueFunctionName) {
this.method = method;
this.theContract = theContract;
this.uniqueFunctionName = uniqueFunctionName;
}

public FunSpec getFunSpec() {
return methodNeedsInjection()
? new FunSpecGenerator(
method.getName(),
uniqueFunctionName,
BeforeAll.class,
defaultParameterSpecsForEachUnitTest(),
generateStatementBody())
.generate()
: new FunSpecGenerator(method.getName(), generateStatementBody()).generate();
: new FunSpecGenerator(uniqueFunctionName, generateStatementBody()).generate();
}

private boolean methodNeedsInjection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
package org.web3j.codegen.unit.gen.java;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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

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

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

public class MethodParserTest extends JavaTestSetup {
Expand All @@ -27,7 +32,8 @@ public void testThatDeployMethodWasGenerated() {
Optional<Method> deployMethod =
filteredMethods.stream().filter(m -> m.getName().equals("deploy")).findAny();
MethodSpec deployMethodSpec =
new MethodParser(deployMethod.get(), greeterContractClass).getMethodSpec();
new MethodParser(deployMethod.get(), greeterContractClass, "deploy")
.getMethodSpec();
assertEquals(
"@org.junit.jupiter.api.BeforeAll\n"
+ "static void deploy(org.web3j.protocol.Web3j web3j, org.web3j.tx.TransactionManager transactionManager, org.web3j.tx.gas.ContractGasProvider contractGasProvider) throws java.lang.Exception {\n"
Expand All @@ -42,10 +48,29 @@ public void testThatNewGreetingMethodWasGenerated() {
Optional<Method> deployMethod =
filteredMethods.stream().filter(m -> m.getName().equals("newGreeting")).findAny();
MethodSpec deployMethodSpec =
new MethodParser(deployMethod.get(), greeterContractClass).getMethodSpec();
new MethodParser(deployMethod.get(), greeterContractClass, "newGreeting")
.getMethodSpec();
assertEquals(
"org.web3j.protocol.core.methods.response.TransactionReceipt transactionReceiptVar = greeter.newGreeting(\"REPLACE_ME\").send();\n"
+ "org.junit.jupiter.api.Assertions.assertTrue(transactionReceiptVar.isStatusOK());\n",
deployMethodSpec.code.toString());
}

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

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

assertTrue(
greetMethodSpecs.stream().anyMatch(methodSpec -> methodSpec.name.equals("greet")));
assertTrue(
greetMethodSpecs.stream().anyMatch(methodSpec -> methodSpec.name.equals("greet1")));
assertEquals(2, greetMethodSpecs.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@
package org.web3j.codegen.unit.gen.kotlin;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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

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

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

public class FunParserTest extends KotlinTestSetup {
@Test
public void testThatDeployMethodWasGenerated() {
Optional<Method> deployFun =
filteredMethods.stream().filter(m -> m.getName().equals("deploy")).findAny();
FunSpec deployFunSpec = new FunParser(deployFun.get(), greeterContractClass).getFunSpec();
FunSpec deployFunSpec =
new FunParser(deployFun.get(), greeterContractClass, "deploy").getFunSpec();
assertEquals(
deployFunSpec.toString(),
"@org.junit.jupiter.api.BeforeAll\n"
Expand All @@ -42,7 +48,8 @@ public void testThatDeployMethodWasGenerated() {
public void testThatNewGreetingMethodWasGenerated() {
Optional<Method> deployFun =
filteredMethods.stream().filter(m -> m.getName().equals("newGreeting")).findAny();
FunSpec deployFunSpec = new FunParser(deployFun.get(), greeterContractClass).getFunSpec();
FunSpec deployFunSpec =
new FunParser(deployFun.get(), greeterContractClass, "newGreeting").getFunSpec();
assertEquals(
deployFunSpec.toString(),
"@org.junit.jupiter.api.Test\n"
Expand All @@ -51,4 +58,24 @@ public void testThatNewGreetingMethodWasGenerated() {
+ " org.junit.jupiter.api.Assertions.assertTrue(transactionReceiptVar.isStatusOK())\n"
+ "}\n");
}

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

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

assertTrue(
greetFunSpecs.stream()
.anyMatch(methodSpec -> methodSpec.getName().equals("greet")));
assertTrue(
greetFunSpecs.stream()
.anyMatch(methodSpec -> methodSpec.getName().equals("greet1")));
assertEquals(2, greetFunSpecs.size());
}
}
4 changes: 4 additions & 0 deletions codegen/src/test/resources/solidity/greeter/Greeter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ contract Greeter is Mortal {
function greet() public view returns (string) {
return greeting;
}

function greet(string newGreet) public pure returns (string) {
return newGreet;
}
}
Original file line number Diff line number Diff line change
@@ -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"}]
[{"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 number Diff line number Diff line change
@@ -1 +1 @@
608060405234801561001057600080fd5b5060405161040038038061040083398101604052805160008054600160a060020a0319163317905501805161004c906001906020840190610053565b50506100ee565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009457805160ff19168380011785556100c1565b828001600101855582156100c1579182015b828111156100c15782518255916020019190600101906100a6565b506100cd9291506100d1565b5090565b6100eb91905b808211156100cd57600081556001016100d7565b90565b610303806100fd6000396000f3006080604052600436106100565763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b5811461005b5780634ac0d66e14610072578063cfae3217146100cb575b600080fd5b34801561006757600080fd5b50610070610155565b005b34801561007e57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100709436949293602493928401919081908401838280828437509497506101929650505050505050565b3480156100d757600080fd5b506100e06101a9565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561011a578181015183820152602001610102565b50505050905090810190601f1680156101475780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60005473ffffffffffffffffffffffffffffffffffffffff163314156101905760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b80516101a590600190602084019061023f565b5050565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102345780601f1061020957610100808354040283529160200191610234565b820191906000526020600020905b81548152906001019060200180831161021757829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061028057805160ff19168380011785556102ad565b828001600101855582156102ad579182015b828111156102ad578251825591602001919060010190610292565b506102b99291506102bd565b5090565b61023c91905b808211156102b957600081556001016102c35600a165627a7a72305820a9bc86938894dc250f6ea25dd823d4472fad6087edcda429a3504e3713a9fc880029
608060405234801561001057600080fd5b5060405161046438038061046483398101604052805160008054600160a060020a0319163317905501805161004c906001906020840190610053565b50506100ee565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009457805160ff19168380011785556100c1565b828001600101855582156100c1579182015b828111156100c15782518255916020019190600101906100a6565b506100cd9291506100d1565b5090565b6100eb91905b808211156100cd57600081556001016100d7565b90565b610367806100fd6000396000f3006080604052600436106100615763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b581146100665780634ac0d66e1461007d578063cfae3217146100d6578063ead710c414610160575b600080fd5b34801561007257600080fd5b5061007b6101b9565b005b34801561008957600080fd5b506040805160206004803580820135601f810184900484028501840190955284845261007b9436949293602493928401919081908401838280828437509497506101f69650505050505050565b3480156100e257600080fd5b506100eb61020d565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561012557818101518382015260200161010d565b50505050905090810190601f1680156101525780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561016c57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100eb9436949293602493928401919081908401838280828437509497506102a09650505050505050565b60005473ffffffffffffffffffffffffffffffffffffffff163314156101f45760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b80516102099060019060208401906102a3565b5050565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102985780601f1061026d57610100808354040283529160200191610298565b820191906000526020600020905b81548152906001019060200180831161027b57829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102e457805160ff1916838001178555610311565b82800160010185558215610311579182015b828111156103115782518255916020019190600101906102f6565b5061031d929150610321565b5090565b6102a091905b8082111561031d57600081556001016103275600a165627a7a7230582002ef0e7cb360516c1f64403a364a524d0c4cf14ede2cb2db39e8ab45e96e3c770029
Original file line number Diff line number Diff line change
@@ -1 +1 @@
608060405234801561001057600080fd5b5060008054600160a060020a0319163317905560bf806100316000396000f300608060405260043610603e5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b581146043575b600080fd5b348015604e57600080fd5b5060556057565b005b60005473ffffffffffffffffffffffffffffffffffffffff1633141560915760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5600a165627a7a72305820625b7679c6c8ba6c2ba1468ffd850d828b06c0e18c1ea6ac511e8c903d747e7b0029
608060405234801561001057600080fd5b5060008054600160a060020a0319163317905560bf806100316000396000f300608060405260043610603e5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b581146043575b600080fd5b348015604e57600080fd5b5060556057565b005b60005473ffffffffffffffffffffffffffffffffffffffff1633141560915760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5600a165627a7a72305820b1e05d167881a64930ea4d3b13ed0a337a54a7505e082bcac336a2bc28ebfc620029
Loading
Loading