-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kyonRay/main
<fix>(v3): fix v3 codeGen wrapper in arrays in struct.
- Loading branch information
Showing
5 changed files
with
189 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.fisco.bcos.codegen.v3.test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import javax.tools.DiagnosticCollector; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileManager; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.SimpleJavaFileObject; | ||
import javax.tools.ToolProvider; | ||
import org.fisco.bcos.codegen.CodeGenMain; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class CodeGenV3Test { | ||
private static final String JAVA_OUTPUT_DIR = "sdk"; | ||
private static final String DEFAULT_PACKAGE = "com"; | ||
|
||
static class MyJavaFileObject extends SimpleJavaFileObject { | ||
|
||
private String source; | ||
private ByteArrayOutputStream outputStream; | ||
|
||
/** | ||
* Construct a SimpleJavaFileObject of the given kind and with the given URI. | ||
* | ||
* @param uri the URI for this file object | ||
* @param kind the kind of this file object | ||
*/ | ||
protected MyJavaFileObject(URI uri, Kind kind) { | ||
super(uri, kind); | ||
} | ||
|
||
public MyJavaFileObject(String name, String source) { | ||
this(URI.create("String:///" + name + Kind.SOURCE.extension), Kind.SOURCE); | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public OutputStream openOutputStream() throws IOException { | ||
outputStream = new ByteArrayOutputStream(); | ||
return outputStream; | ||
} | ||
|
||
@Override | ||
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { | ||
if (source == null) { | ||
throw new IOException("Empty source"); | ||
} | ||
return source; | ||
} | ||
} | ||
|
||
@Test | ||
public void complexABICodeGen() throws IOException { | ||
final String COMPLEX_ABI_FILE = "ComplexCodecTest.abi"; | ||
final String COMPLEX_NAME = "ComplexCodecTest"; | ||
codeGenTest(COMPLEX_ABI_FILE, COMPLEX_NAME); | ||
} | ||
|
||
@Test | ||
public void tableABICodeGen() throws IOException { | ||
final String ABI_FILE = "Table.abi"; | ||
final String CONTRACT_NAME = "Table"; | ||
codeGenTest(ABI_FILE, CONTRACT_NAME); | ||
} | ||
|
||
private void codeGenTest(String abiFileName, String contractName) throws IOException { | ||
String abiFile = CodeGenV3Test.class.getClassLoader().getResource(abiFileName).getPath(); | ||
String javaOutPut = new File(abiFile).getParent() + File.separator + JAVA_OUTPUT_DIR; | ||
CodeGenMain.main( | ||
Arrays.asList( | ||
"-v", "V3", | ||
"-a", abiFile, | ||
"-b", abiFile, | ||
"-s", abiFile, | ||
"-p", DEFAULT_PACKAGE, | ||
"-o", javaOutPut) | ||
.toArray(new String[0])); | ||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); | ||
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>(); | ||
JavaFileManager javaFileManager = | ||
compiler.getStandardFileManager(collector, null, StandardCharsets.UTF_8); | ||
String codeFileName = | ||
javaOutPut | ||
+ File.separator | ||
+ File.separator | ||
+ DEFAULT_PACKAGE | ||
+ File.separator | ||
+ contractName | ||
+ ".java"; | ||
|
||
File codeFile = new File(codeFileName); | ||
Long fileLength = codeFile.length(); | ||
byte[] fileContent = new byte[fileLength.intValue()]; | ||
FileInputStream in = new FileInputStream(codeFile); | ||
in.read(fileContent); | ||
in.close(); | ||
String code = new String(fileContent, StandardCharsets.UTF_8); | ||
|
||
JavaFileObject myJavaFileObject = new MyJavaFileObject(contractName, code); | ||
Boolean call = | ||
compiler.getTask( | ||
null, | ||
javaFileManager, | ||
collector, | ||
null, | ||
null, | ||
Collections.singletonList(myJavaFileObject)) | ||
.call(); | ||
collector.getDiagnostics().forEach(log -> System.out.println(log.toString())); | ||
Assert.assertTrue(call); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"conflictFields":[{"kind":3,"slot":1,"value":[0]},{"kind":3,"slot":1,"value":[1]},{"kind":4,"value":[1]}],"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"b32_array_array","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"selector":[361941394,1422834829],"stateMutability":"view","type":"function"},{"conflictFields":[{"kind":3,"slot":3,"value":[0]},{"kind":4,"value":[3]}],"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"b32_s_array_array","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"selector":[1849978875,3517985529],"stateMutability":"view","type":"function"},{"conflictFields":[{"kind":0},{"kind":3,"slot":0,"value":[0]},{"kind":3,"slot":0,"value":[1]},{"kind":4,"value":[0]}],"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"b_array_array","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"selector":[1350133985,3395322268],"stateMutability":"view","type":"function"},{"conflictFields":[{"kind":0},{"kind":3,"slot":2,"value":[0]},{"kind":4,"value":[2]}],"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"b_s_array_array","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"selector":[2512437014,1940483310],"stateMutability":"view","type":"function"},{"conflictFields":[{"kind":5}],"inputs":[{"components":[{"internalType":"int128","name":"i1","type":"int128"},{"internalType":"uint128","name":"u1","type":"uint128"},{"internalType":"int32[1]","name":"b1","type":"int32[1]"}],"internalType":"struct StaticStruct","name":"b","type":"tuple"}],"name":"buildStaticStruct","outputs":[{"components":[{"internalType":"int128","name":"i1","type":"int128"},{"internalType":"uint128","name":"u1","type":"uint128"},{"internalType":"int32[1]","name":"b1","type":"int32[1]"}],"internalType":"struct StaticStruct","name":"","type":"tuple"}],"selector":[1732779260,1182195354],"stateMutability":"pure","type":"function"},{"conflictFields":[{"kind":5}],"inputs":[{"internalType":"int128","name":"i1","type":"int128"},{"internalType":"uint128","name":"u1","type":"uint128"}],"name":"buildStaticStruct","outputs":[{"components":[{"internalType":"int128","name":"i1","type":"int128"},{"internalType":"uint128","name":"u1","type":"uint128"},{"internalType":"int32[1]","name":"b1","type":"int32[1]"}],"internalType":"struct StaticStruct","name":"","type":"tuple"}],"selector":[2268489121,274923318],"stateMutability":"pure","type":"function"},{"conflictFields":[{"kind":0},{"kind":4,"value":[4]},{"kind":4,"value":[5]}],"inputs":[{"internalType":"string","name":"value_str","type":"string"},{"internalType":"bytes32[]","name":"_b","type":"bytes32[]"}],"name":"buildStructA","outputs":[{"components":[{"internalType":"string[]","name":"value_str","type":"string[]"},{"internalType":"bytes32[]","name":"bytes32_in_struct","type":"bytes32[]"}],"internalType":"struct StructA","name":"","type":"tuple"}],"selector":[451944255,2748848611],"stateMutability":"nonpayable","type":"function"},{"conflictFields":[{"kind":0},{"kind":4,"value":[4]},{"kind":4,"value":[5]}],"inputs":[{"components":[{"internalType":"string[]","name":"value_str","type":"string[]"},{"internalType":"bytes32[]","name":"bytes32_in_struct","type":"bytes32[]"}],"internalType":"struct StructA","name":"a","type":"tuple"}],"name":"buildStructB","outputs":[{"components":[{"internalType":"string[]","name":"d_str","type":"string[]"},{"components":[{"internalType":"string[]","name":"value_str","type":"string[]"},{"internalType":"bytes32[]","name":"bytes32_in_struct","type":"bytes32[]"}],"internalType":"struct StructA[]","name":"a_struct","type":"tuple[]"}],"internalType":"struct StructB","name":"","type":"tuple"}],"selector":[2715886304,3473861522],"stateMutability":"nonpayable","type":"function"},{"conflictFields":[{"kind":0},{"kind":4,"value":[4]},{"kind":4,"value":[5]}],"inputs":[{"components":[{"internalType":"string[]","name":"d_str","type":"string[]"},{"components":[{"internalType":"string[]","name":"value_str","type":"string[]"},{"internalType":"bytes32[]","name":"bytes32_in_struct","type":"bytes32[]"}],"internalType":"struct StructA[]","name":"a_struct","type":"tuple[]"}],"internalType":"struct StructB","name":"b","type":"tuple"}],"name":"getStructAInStructB","outputs":[{"components":[{"internalType":"string[]","name":"value_str","type":"string[]"},{"internalType":"bytes32[]","name":"bytes32_in_struct","type":"bytes32[]"}],"internalType":"struct StructA","name":"","type":"tuple"}],"selector":[3302412820,2294370420],"stateMutability":"nonpayable","type":"function"},{"conflictFields":[{"kind":0},{"kind":4,"value":[1]}],"inputs":[{"internalType":"bytes32[][]","name":"b","type":"bytes32[][]"}],"name":"setBytes32ArrayArray","outputs":[{"internalType":"bytes32[][]","name":"","type":"bytes32[][]"}],"selector":[2425465556,973599251],"stateMutability":"nonpayable","type":"function"},{"conflictFields":[{"kind":0},{"kind":4,"value":[3]}],"inputs":[{"internalType":"bytes32[10][]","name":"b","type":"bytes32[10][]"}],"name":"setBytes32StaticArrayArray","outputs":[{"internalType":"bytes32[10][]","name":"","type":"bytes32[10][]"}],"selector":[3554602330,3667350222],"stateMutability":"nonpayable","type":"function"},{"conflictFields":[{"kind":0},{"kind":4,"value":[0]}],"inputs":[{"internalType":"bytes[][]","name":"b","type":"bytes[][]"}],"name":"setBytesArrayArray","outputs":[{"internalType":"bytes[][]","name":"","type":"bytes[][]"}],"selector":[68407213,3571060108],"stateMutability":"nonpayable","type":"function"},{"conflictFields":[{"kind":0},{"kind":4,"value":[2]}],"inputs":[{"internalType":"bytes[10][]","name":"b","type":"bytes[10][]"}],"name":"setBytesStaticArrayArray","outputs":[{"internalType":"bytes[10][]","name":"","type":"bytes[10][]"}],"selector":[2318966447,544764711],"stateMutability":"nonpayable","type":"function"}] |
Oops, something went wrong.