From 3eb711f1a58b508f30444067a3525c5024f3043b Mon Sep 17 00:00:00 2001 From: Matt Dolan Date: Fri, 12 Nov 2021 22:28:21 -0500 Subject: [PATCH 1/2] Updated project to JUnit 5 and fixed failing test in MessageTest --- pom.xml | 6 +-- .../p2p/solanaj/core/AccountKeysListTest.java | 8 ++-- .../org/p2p/solanaj/core/AccountTest.java | 12 +++--- .../org/p2p/solanaj/core/MainnetTest.java | 10 ++--- .../org/p2p/solanaj/core/MessageTest.java | 12 +++--- .../org/p2p/solanaj/core/PublicKeyTest.java | 40 +++++++++++-------- .../org/p2p/solanaj/core/TransactionTest.java | 10 ++--- .../solanaj/programs/SystemProgramTest.java | 13 +++--- .../org/p2p/solanaj/utils/ByteUtilsTest.java | 23 ++++++----- .../solanaj/utils/ShortvecEncodingTest.java | 9 +++-- 10 files changed, 77 insertions(+), 66 deletions(-) diff --git a/pom.xml b/pom.xml index 67f8c1d8..a8cd5201 100644 --- a/pom.xml +++ b/pom.xml @@ -24,9 +24,9 @@ - junit - junit - 4.13.1 + org.junit.jupiter + junit-jupiter-engine + 5.8.1 test diff --git a/src/test/java/org/p2p/solanaj/core/AccountKeysListTest.java b/src/test/java/org/p2p/solanaj/core/AccountKeysListTest.java index 779e07a6..1e513ee2 100644 --- a/src/test/java/org/p2p/solanaj/core/AccountKeysListTest.java +++ b/src/test/java/org/p2p/solanaj/core/AccountKeysListTest.java @@ -1,17 +1,17 @@ package org.p2p.solanaj.core; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.p2p.solanaj.programs.SystemProgram; -public class AccountKeysListTest { +class AccountKeysListTest { @Test - public void getSortedList() { + void getSortedList() { AccountKeysList list = new AccountKeysList(); list.addAll( diff --git a/src/test/java/org/p2p/solanaj/core/AccountTest.java b/src/test/java/org/p2p/solanaj/core/AccountTest.java index 489bce90..dca2b74c 100644 --- a/src/test/java/org/p2p/solanaj/core/AccountTest.java +++ b/src/test/java/org/p2p/solanaj/core/AccountTest.java @@ -1,16 +1,16 @@ package org.p2p.solanaj.core; -import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import org.bitcoinj.core.Base58; +import org.junit.jupiter.api.Test; -public class AccountTest { +class AccountTest { @Test - public void accountFromSecretKey() { + void accountFromSecretKey() { byte[] secretKey = Base58 .decode("4Z7cXSyeFR8wNGMVXUE1TwtKn5D5Vu7FzEv69dokLv7KrQk7h6pu4LF8ZRR9yQBhc7uSM6RTTZtU1fmaxiNrxXrs"); assertEquals("QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo", new Account(secretKey).getPublicKey().toString()); @@ -19,13 +19,13 @@ public void accountFromSecretKey() { } @Test - public void generateNewAccount() { + void generateNewAccount() { Account account = new Account(); assertEquals(64, account.getSecretKey().length); } @Test - public void fromMnemonic() { + void fromMnemonic() { Account acc = Account.fromMnemonic(Arrays.asList("spider", "federal", "bleak", "unable", "ask", "weasel", "diamond", "electric", "illness", "wheat", "uphold", "mind"), ""); diff --git a/src/test/java/org/p2p/solanaj/core/MainnetTest.java b/src/test/java/org/p2p/solanaj/core/MainnetTest.java index 6be2dcf5..fd3ef693 100644 --- a/src/test/java/org/p2p/solanaj/core/MainnetTest.java +++ b/src/test/java/org/p2p/solanaj/core/MainnetTest.java @@ -1,6 +1,8 @@ package org.p2p.solanaj.core; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; import org.p2p.solanaj.rpc.Cluster; import org.p2p.solanaj.rpc.RpcClient; import org.p2p.solanaj.rpc.RpcException; @@ -8,12 +10,10 @@ import java.util.List; -import static org.junit.Assert.assertTrue; - -public class MainnetTest { +class MainnetTest { @Test - public void connectToMainnet() { + void connectToMainnet() { final RpcClient client = new RpcClient(Cluster.MAINNET); final PublicKey publicKey = new PublicKey("skynetDj29GH6o6bAqoixCpDuYtWqi1rm8ZNx1hB3vq"); diff --git a/src/test/java/org/p2p/solanaj/core/MessageTest.java b/src/test/java/org/p2p/solanaj/core/MessageTest.java index 8716223c..c5bd72d3 100644 --- a/src/test/java/org/p2p/solanaj/core/MessageTest.java +++ b/src/test/java/org/p2p/solanaj/core/MessageTest.java @@ -1,15 +1,15 @@ package org.p2p.solanaj.core; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + import org.bitcoinj.core.Base58; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.p2p.solanaj.programs.SystemProgram; -import static org.junit.Assert.assertArrayEquals; - -public class MessageTest { +class MessageTest { @Test - public void serializeMessage() { + void serializeMessage() { PublicKey fromPublicKey = new PublicKey("QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo"); PublicKey toPublickKey = new PublicKey("GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5"); int lamports = 3000; @@ -20,7 +20,7 @@ public void serializeMessage() { Message message = new Message(); message.addInstruction(SystemProgram.transfer(fromPublicKey, toPublickKey, lamports)); message.setRecentBlockHash("Eit7RCyhUixAe2hGBS8oqnw59QK3kgMMjfLME5bm9wRn"); - message.setFeePayer(signer); + message.setFeePayer(fromPublicKey); assertArrayEquals(new int[] { 1, 0, 1, 3, 6, 26, 217, 208, 83, 135, 21, 72, 83, 126, 222, 62, 38, 24, 73, 163, 223, 183, 253, 2, 250, 188, 117, 178, 35, 200, 228, 106, 219, 133, 61, 12, 235, 122, 188, 208, 216, 117, diff --git a/src/test/java/org/p2p/solanaj/core/PublicKeyTest.java b/src/test/java/org/p2p/solanaj/core/PublicKeyTest.java index ff5ef28b..0759238e 100644 --- a/src/test/java/org/p2p/solanaj/core/PublicKeyTest.java +++ b/src/test/java/org/p2p/solanaj/core/PublicKeyTest.java @@ -1,25 +1,33 @@ package org.p2p.solanaj.core; -import org.junit.Test; -import org.p2p.solanaj.core.PublicKey.ProgramDerivedAddress; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; +import org.p2p.solanaj.core.PublicKey.ProgramDerivedAddress; import java.io.ByteArrayOutputStream; import java.util.Arrays; -public class PublicKeyTest { +class PublicKeyTest { - @Test(expected = IllegalArgumentException.class) - public void ivalidKeys() { - new PublicKey(new byte[] { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0 }); - new PublicKey("300000000000000000000000000000000000000000000000000000000000000000000"); - new PublicKey("300000000000000000000000000000000000000000000000000000000000000"); + @Test + void ivalidKeys() { + assertThrows(IllegalArgumentException.class, () -> new PublicKey( + new byte[]{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0})); + assertThrows(IllegalArgumentException.class, () -> new PublicKey( + "300000000000000000000000000000000000000000000000000000000000000000000")); + assertThrows(IllegalArgumentException.class, + () -> new PublicKey("300000000000000000000000000000000000000000000000000000000000000")); } @Test - public void validKeys() { + void validKeys() { PublicKey key = new PublicKey(new byte[] { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }); assertEquals("CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3", key.toString()); @@ -37,7 +45,7 @@ public void validKeys() { } @Test - public void equals() { + void equals() { PublicKey key = new PublicKey("11111111111111111111111111111111"); assertTrue(key.equals(key)); @@ -45,7 +53,7 @@ public void equals() { } @Test - public void readPubkey() { + void readPubkey() { PublicKey key = new PublicKey("11111111111111111111111111111111"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -57,7 +65,7 @@ public void readPubkey() { } @Test - public void createProgramAddress() throws Exception { + void createProgramAddress() throws Exception { PublicKey programId = new PublicKey("BPFLoader1111111111111111111111111111111111"); PublicKey programAddress = PublicKey.createProgramAddress( @@ -79,7 +87,7 @@ public void createProgramAddress() throws Exception { } @Test - public void findProgramAddress() throws Exception { + void findProgramAddress() throws Exception { PublicKey programId = new PublicKey("BPFLoader1111111111111111111111111111111111"); ProgramDerivedAddress programAddress = PublicKey.findProgramAddress(Arrays.asList("".getBytes()), programId); @@ -89,7 +97,7 @@ public void findProgramAddress() throws Exception { } @Test - public void findProgramAddress1() throws Exception { + void findProgramAddress1() throws Exception { PublicKey programId = new PublicKey("6Cust2JhvweKLh4CVo1dt21s2PJ86uNGkziudpkNPaCj"); PublicKey programId2 = new PublicKey("BPFLoader1111111111111111111111111111111111"); diff --git a/src/test/java/org/p2p/solanaj/core/TransactionTest.java b/src/test/java/org/p2p/solanaj/core/TransactionTest.java index 8a3fb0e0..149ab540 100644 --- a/src/test/java/org/p2p/solanaj/core/TransactionTest.java +++ b/src/test/java/org/p2p/solanaj/core/TransactionTest.java @@ -1,18 +1,18 @@ package org.p2p.solanaj.core; -import org.p2p.solanaj.programs.SystemProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; +import org.p2p.solanaj.programs.SystemProgram; import java.util.Base64; import org.bitcoinj.core.Base58; -public class TransactionTest { +class TransactionTest { @Test - public void signAndSerialize() { + void signAndSerialize() { PublicKey fromPublicKey = new PublicKey("QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo"); PublicKey toPublickKey = new PublicKey("GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5"); int lamports = 3000; diff --git a/src/test/java/org/p2p/solanaj/programs/SystemProgramTest.java b/src/test/java/org/p2p/solanaj/programs/SystemProgramTest.java index 1d1d0187..7a626925 100644 --- a/src/test/java/org/p2p/solanaj/programs/SystemProgramTest.java +++ b/src/test/java/org/p2p/solanaj/programs/SystemProgramTest.java @@ -1,17 +1,18 @@ package org.p2p.solanaj.programs; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; import org.p2p.solanaj.core.PublicKey; import org.p2p.solanaj.core.TransactionInstruction; -import org.junit.Test; -import static org.junit.Assert.*; - import org.bitcoinj.core.Base58; -public class SystemProgramTest { +class SystemProgramTest { @Test - public void transferInstruction() { + void transferInstruction() { PublicKey fromPublicKey = new PublicKey("QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo"); PublicKey toPublickKey = new PublicKey("GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5"); int lamports = 3000; @@ -26,7 +27,7 @@ public void transferInstruction() { } @Test - public void createAccountInstruction() { + void createAccountInstruction() { TransactionInstruction instruction = SystemProgram.createAccount(SystemProgram.PROGRAM_ID, SystemProgram.PROGRAM_ID, 2039280, 165, SystemProgram.PROGRAM_ID); diff --git a/src/test/java/org/p2p/solanaj/utils/ByteUtilsTest.java b/src/test/java/org/p2p/solanaj/utils/ByteUtilsTest.java index a5d36055..37c71ee1 100644 --- a/src/test/java/org/p2p/solanaj/utils/ByteUtilsTest.java +++ b/src/test/java/org/p2p/solanaj/utils/ByteUtilsTest.java @@ -1,16 +1,18 @@ package org.p2p.solanaj.utils; -import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigInteger; +import org.junit.jupiter.api.Test; -public class ByteUtilsTest { +class ByteUtilsTest { @Test - public void readBytes() { + void readBytes() { byte[] buf = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; assertArrayEquals(new byte[] { 1, 2, 3, 4 }, ByteUtils.readBytes(buf, 0, 4)); @@ -18,7 +20,7 @@ public void readBytes() { } @Test - public void readUint64() throws IOException { + void readUint64() throws IOException { String bigIntValue = "96351551052682965"; ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -29,13 +31,12 @@ public void readUint64() throws IOException { assertEquals(bigIntValue, bn.toString()); } - @Test(expected = RuntimeException.class) - public void uint64ToByteStreamLE() { + @Test + void uint64ToByteStreamLE() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try { - ByteUtils.uint64ToByteStreamLE(new BigInteger("137001898677442802701"), bos); - } catch (IOException e) { - } + assertThrows(RuntimeException.class, + () -> ByteUtils.uint64ToByteStreamLE(new BigInteger("137001898677442802701"), bos)); + } } diff --git a/src/test/java/org/p2p/solanaj/utils/ShortvecEncodingTest.java b/src/test/java/org/p2p/solanaj/utils/ShortvecEncodingTest.java index 12f8686e..04c77221 100644 --- a/src/test/java/org/p2p/solanaj/utils/ShortvecEncodingTest.java +++ b/src/test/java/org/p2p/solanaj/utils/ShortvecEncodingTest.java @@ -1,12 +1,13 @@ package org.p2p.solanaj.utils; -import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; -public class ShortvecEncodingTest { +import org.junit.jupiter.api.Test; + +class ShortvecEncodingTest { @Test - public void encodeLength() { + void encodeLength() { assertArrayEquals(new byte[] { 0 } /* [0] */, ShortvecEncoding.encodeLength(0)); assertArrayEquals(new byte[] { 1 } /* [1] */, ShortvecEncoding.encodeLength(1)); assertArrayEquals(new byte[] { 5 } /* [5] */, ShortvecEncoding.encodeLength(5)); From c0e48ba116186799b4870f79101bc44f0541d39a Mon Sep 17 00:00:00 2001 From: Matt Dolan Date: Fri, 12 Nov 2021 22:30:02 -0500 Subject: [PATCH 2/2] Updated signer in MessageTest to PublicKey --- src/test/java/org/p2p/solanaj/core/MessageTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/p2p/solanaj/core/MessageTest.java b/src/test/java/org/p2p/solanaj/core/MessageTest.java index c5bd72d3..4981412f 100644 --- a/src/test/java/org/p2p/solanaj/core/MessageTest.java +++ b/src/test/java/org/p2p/solanaj/core/MessageTest.java @@ -14,13 +14,13 @@ void serializeMessage() { PublicKey toPublickKey = new PublicKey("GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5"); int lamports = 3000; - Account signer = new Account(Base58 + PublicKey signer = new PublicKey(Base58 .decode("4Z7cXSyeFR8wNGMVXUE1TwtKn5D5Vu7FzEv69dokLv7KrQk7h6pu4LF8ZRR9yQBhc7uSM6RTTZtU1fmaxiNrxXrs")); Message message = new Message(); message.addInstruction(SystemProgram.transfer(fromPublicKey, toPublickKey, lamports)); message.setRecentBlockHash("Eit7RCyhUixAe2hGBS8oqnw59QK3kgMMjfLME5bm9wRn"); - message.setFeePayer(fromPublicKey); + message.setFeePayer(signer); assertArrayEquals(new int[] { 1, 0, 1, 3, 6, 26, 217, 208, 83, 135, 21, 72, 83, 126, 222, 62, 38, 24, 73, 163, 223, 183, 253, 2, 250, 188, 117, 178, 35, 200, 228, 106, 219, 133, 61, 12, 235, 122, 188, 208, 216, 117,