Skip to content

Commit

Permalink
Updated Junit and mockito refactored the tests. (#1084)
Browse files Browse the repository at this point in the history
* Updated Junit and mockito refactored the tests.

* removed hamcrest as dependency replaced assertThat with assertEquals from JUnit

* fix conflict

* added messages back to assertTrue and assertFalse

* added benchmark option annotation to FastRawTransactionManagerIT

* refactored the code such that in the assertThrows only the line that causes the exception is declared.

* Reversed the argument order in assertEquals that was assertThat previously

* .

* spotless
  • Loading branch information
AlexandrouR authored and iikirilov committed Nov 7, 2019
1 parent aaebd8e commit 63facc5
Show file tree
Hide file tree
Showing 112 changed files with 1,891 additions and 1,989 deletions.
102 changes: 48 additions & 54 deletions abi/src/test/java/org/web3j/abi/DefaultFunctionEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.Arrays;
import java.util.Collections;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.DynamicArray;
Expand All @@ -27,56 +27,53 @@
import org.web3j.abi.datatypes.generated.Bytes10;
import org.web3j.abi.datatypes.generated.Uint32;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultFunctionEncoderTest {

@Test
public void testBuildMethodId() {
assertThat(DefaultFunctionEncoder.buildMethodId("baz(uint32,bool)"), is("0xcdcd77c0"));
assertEquals("0xcdcd77c0", DefaultFunctionEncoder.buildMethodId("baz(uint32,bool)"));
}

@Test
public void testBuildMessageSignature() {
assertThat(
assertEquals(
"baz(uint32,bool)",
DefaultFunctionEncoder.buildMethodSignature(
"baz", Arrays.asList(new Uint32(BigInteger.valueOf(69)), new Bool(true))),
is("baz(uint32,bool)"));
"baz", Arrays.asList(new Uint32(BigInteger.valueOf(69)), new Bool(true))));
}

@Test
public void testBuildEmptyMethodSignature() {
assertThat(
DefaultFunctionEncoder.buildMethodSignature("empty", Collections.emptyList()),
is("empty()"));
assertEquals(
"empty()",
DefaultFunctionEncoder.buildMethodSignature("empty", Collections.emptyList()));
}

@Test
public void testEncodeConstructorEmpty() {
assertThat(FunctionEncoder.encodeConstructor(Collections.emptyList()), is(""));
assertEquals("", FunctionEncoder.encodeConstructor(Collections.emptyList()));
}

@Test
public void testEncodeConstructorString() {
assertThat(
assertEquals(
"0000000000000000000000000000000000000000000000000000000000000020"
+ "000000000000000000000000000000000000000000000000000000000000000a"
+ "4772656574696e67732100000000000000000000000000000000000000000000",
FunctionEncoder.encodeConstructor(
Collections.singletonList(new Utf8String("Greetings!"))),
is(
"0000000000000000000000000000000000000000000000000000000000000020"
+ "000000000000000000000000000000000000000000000000000000000000000a"
+ "4772656574696e67732100000000000000000000000000000000000000000000"));
Collections.singletonList(new Utf8String("Greetings!"))));
}

@Test
public void testEncodeConstructorUint() {
assertThat(
assertEquals(
"0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000020",
FunctionEncoder.encodeConstructor(
Arrays.asList(
new Uint(BigInteger.ONE), new Uint(BigInteger.valueOf(0x20)))),
is(
"0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000020"));
new Uint(BigInteger.ONE), new Uint(BigInteger.valueOf(0x20)))));
}

@Test
Expand All @@ -87,12 +84,11 @@ public void testFunctionSimpleEncode() {
Arrays.asList(new Uint32(BigInteger.valueOf(69)), new Bool(true)),
Collections.emptyList());

assertThat(
FunctionEncoder.encode(function),
is(
"0xcdcd77c0"
+ "0000000000000000000000000000000000000000000000000000000000000045"
+ "0000000000000000000000000000000000000000000000000000000000000001"));
assertEquals(
"0xcdcd77c0"
+ "0000000000000000000000000000000000000000000000000000000000000045"
+ "0000000000000000000000000000000000000000000000000000000000000001",
FunctionEncoder.encode(function));
}

@Test
Expand All @@ -109,19 +105,18 @@ public void testFunctionMDynamicArrayEncode1() {
new Uint(BigInteger.valueOf(3)))),
Collections.emptyList());

assertThat(
FunctionEncoder.encode(function),
is(
"0xa5643bf2"
+ "0000000000000000000000000000000000000000000000000000000000000060"
+ "0000000000000000000000000000000000000000000000000000000000000001"
+ "00000000000000000000000000000000000000000000000000000000000000a0"
+ "0000000000000000000000000000000000000000000000000000000000000004"
+ "6461766500000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000003"
+ "0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000002"
+ "0000000000000000000000000000000000000000000000000000000000000003"));
assertEquals(
"0xa5643bf2"
+ "0000000000000000000000000000000000000000000000000000000000000060"
+ "0000000000000000000000000000000000000000000000000000000000000001"
+ "00000000000000000000000000000000000000000000000000000000000000a0"
+ "0000000000000000000000000000000000000000000000000000000000000004"
+ "6461766500000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000003"
+ "0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000002"
+ "0000000000000000000000000000000000000000000000000000000000000003",
FunctionEncoder.encode(function));
}

@Test
Expand All @@ -138,18 +133,17 @@ public void testFunctionMDynamicArrayEncode2() {
new DynamicBytes("Hello, world!".getBytes())),
Collections.emptyList());

assertThat(
FunctionEncoder.encode(function),
is(
"0x8be65246"
+ "0000000000000000000000000000000000000000000000000000000000000123"
+ "0000000000000000000000000000000000000000000000000000000000000080"
+ "3132333435363738393000000000000000000000000000000000000000000000"
+ "00000000000000000000000000000000000000000000000000000000000000e0"
+ "0000000000000000000000000000000000000000000000000000000000000002"
+ "0000000000000000000000000000000000000000000000000000000000000456"
+ "0000000000000000000000000000000000000000000000000000000000000789"
+ "000000000000000000000000000000000000000000000000000000000000000d"
+ "48656c6c6f2c20776f726c642100000000000000000000000000000000000000"));
assertEquals(
"0x8be65246"
+ "0000000000000000000000000000000000000000000000000000000000000123"
+ "0000000000000000000000000000000000000000000000000000000000000080"
+ "3132333435363738393000000000000000000000000000000000000000000000"
+ "00000000000000000000000000000000000000000000000000000000000000e0"
+ "0000000000000000000000000000000000000000000000000000000000000002"
+ "0000000000000000000000000000000000000000000000000000000000000456"
+ "0000000000000000000000000000000000000000000000000000000000000789"
+ "000000000000000000000000000000000000000000000000000000000000000d"
+ "48656c6c6f2c20776f726c642100000000000000000000000000000000000000",
FunctionEncoder.encode(function));
}
}
13 changes: 6 additions & 7 deletions abi/src/test/java/org/web3j/abi/EventEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
*/
package org.web3j.abi;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class EventEncoderTest {

@Test
public void testBuildEventSignature() {
assertThat(
assertEquals(
EventEncoder.buildEventSignature("Deposit(address,hash256,uint256)"),
is("0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20"));
("0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20"));

assertThat(
assertEquals(
EventEncoder.buildEventSignature("Notify(uint256,uint256)"),
is("0x71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed"));
("0x71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed"));
}
}
64 changes: 29 additions & 35 deletions abi/src/test/java/org/web3j/abi/FunctionReturnDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.DynamicBytes;
Expand All @@ -34,9 +34,7 @@
import org.web3j.crypto.Hash;
import org.web3j.utils.Numeric;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FunctionReturnDecoderTest {

Expand All @@ -48,11 +46,11 @@ public void testSimpleFunctionDecode() {
Collections.<Type>emptyList(),
Collections.singletonList(new TypeReference<Uint>() {}));

assertThat(
assertEquals(
FunctionReturnDecoder.decode(
"0x0000000000000000000000000000000000000000000000000000000000000037",
function.getOutputParameters()),
equalTo(Collections.singletonList(new Uint(BigInteger.valueOf(55)))));
(Collections.singletonList(new Uint(BigInteger.valueOf(55)))));
}

@Test
Expand All @@ -70,7 +68,7 @@ public void testSimpleFunctionStringResultDecode() {
+ "6f6e65206d6f72652074696d6500000000000000000000000000000000000000",
function.getOutputParameters());

assertThat(utf8Strings.get(0).getValue(), is("one more time"));
assertEquals(utf8Strings.get(0).getValue(), ("one more time"));
}

@Test
Expand All @@ -87,7 +85,7 @@ public void testFunctionEmptyStringResultDecode() {
+ "0000000000000000000000000000000000000000000000000000000000000000",
function.getOutputParameters());

assertThat(utf8Strings.get(0).getValue(), is(""));
assertEquals(utf8Strings.get(0).getValue(), (""));
}

@Test
Expand All @@ -98,15 +96,12 @@ public void testMultipleResultFunctionDecode() {
Collections.<Type>emptyList(),
Arrays.asList(new TypeReference<Uint>() {}, new TypeReference<Uint>() {}));

assertThat(
assertEquals(
FunctionReturnDecoder.decode(
"0x0000000000000000000000000000000000000000000000000000000000000037"
+ "0000000000000000000000000000000000000000000000000000000000000007",
function.getOutputParameters()),
equalTo(
Arrays.asList(
new Uint(BigInteger.valueOf(55)),
new Uint(BigInteger.valueOf(7)))));
(Arrays.asList(new Uint(BigInteger.valueOf(55)), new Uint(BigInteger.valueOf(7)))));
}

@Test
Expand All @@ -121,7 +116,7 @@ public void testDecodeMultipleStringValues() {
new TypeReference<Utf8String>() {},
new TypeReference<Utf8String>() {}));

assertThat(
assertEquals(
FunctionReturnDecoder.decode(
"0x0000000000000000000000000000000000000000000000000000000000000080"
+ "00000000000000000000000000000000000000000000000000000000000000c0"
Expand All @@ -136,10 +131,9 @@ public void testDecodeMultipleStringValues() {
+ "0000000000000000000000000000000000000000000000000000000000000004"
+ "6d6e6f3200000000000000000000000000000000000000000000000000000000",
function.getOutputParameters()),
equalTo(
Arrays.asList(
new Utf8String("def1"), new Utf8String("ghi1"),
new Utf8String("jkl1"), new Utf8String("mno2"))));
(Arrays.asList(
new Utf8String("def1"), new Utf8String("ghi1"),
new Utf8String("jkl1"), new Utf8String("mno2"))));
}

@Test
Expand All @@ -163,16 +157,16 @@ public void testDecodeStaticArrayValue() {
new Uint256(BigInteger.valueOf(55)), new Uint256(BigInteger.ONE));

List<Type> expected = Arrays.asList(uint256StaticArray2, new Uint256(BigInteger.TEN));
assertThat(decoded, equalTo(expected));
assertEquals(decoded, (expected));
}

@Test
public void testVoidResultFunctionDecode() {
Function function = new Function("test", Collections.emptyList(), Collections.emptyList());

assertThat(
assertEquals(
FunctionReturnDecoder.decode("0x", function.getOutputParameters()),
is(Collections.emptyList()));
(Collections.emptyList()));
}

@Test
Expand All @@ -183,19 +177,19 @@ public void testEmptyResultFunctionDecode() {
Collections.emptyList(),
Collections.singletonList(new TypeReference<Uint>() {}));

assertThat(
assertEquals(
FunctionReturnDecoder.decode("0x", function.getOutputParameters()),
is(Collections.emptyList()));
(Collections.emptyList()));
}

@Test
public void testDecodeIndexedUint256Value() {
Uint256 value = new Uint256(BigInteger.TEN);
String encoded = TypeEncoder.encodeNumeric(value);

assertThat(
assertEquals(
FunctionReturnDecoder.decodeIndexedValue(encoded, new TypeReference<Uint256>() {}),
equalTo(value));
(value));
}

@Test
Expand All @@ -204,29 +198,29 @@ public void testDecodeIndexedStringValue() {
String encoded = TypeEncoder.encodeString(string);
String hash = Hash.sha3(encoded);

assertThat(
assertEquals(
FunctionReturnDecoder.decodeIndexedValue(hash, new TypeReference<Utf8String>() {}),
equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
(new Bytes32(Numeric.hexStringToByteArray(hash))));
}

@Test
public void testDecodeIndexedBytes32Value() {
String rawInput = "0x1234567890123456789012345678901234567890123456789012345678901234";
byte[] rawInputBytes = Numeric.hexStringToByteArray(rawInput);

assertThat(
assertEquals(
FunctionReturnDecoder.decodeIndexedValue(rawInput, new TypeReference<Bytes32>() {}),
equalTo(new Bytes32(rawInputBytes)));
(new Bytes32(rawInputBytes)));
}

@Test
public void testDecodeIndexedBytes16Value() {
String rawInput = "0x1234567890123456789012345678901200000000000000000000000000000000";
byte[] rawInputBytes = Numeric.hexStringToByteArray(rawInput.substring(0, 34));

assertThat(
assertEquals(
FunctionReturnDecoder.decodeIndexedValue(rawInput, new TypeReference<Bytes16>() {}),
equalTo(new Bytes16(rawInputBytes)));
(new Bytes16(rawInputBytes)));
}

@Test
Expand All @@ -235,10 +229,10 @@ public void testDecodeIndexedDynamicBytesValue() {
String encoded = TypeEncoder.encodeDynamicBytes(bytes);
String hash = Hash.sha3(encoded);

assertThat(
assertEquals(
FunctionReturnDecoder.decodeIndexedValue(
hash, new TypeReference<DynamicBytes>() {}),
equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
(new Bytes32(Numeric.hexStringToByteArray(hash))));
}

@Test
Expand All @@ -249,9 +243,9 @@ public void testDecodeIndexedDynamicArrayValue() {
String encoded = TypeEncoder.encodeDynamicArray(array);
String hash = Hash.sha3(encoded);

assertThat(
assertEquals(
FunctionReturnDecoder.decodeIndexedValue(
hash, new TypeReference<DynamicArray>() {}),
equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
}
Loading

0 comments on commit 63facc5

Please sign in to comment.