diff --git a/build.gradle b/build.gradle index 0186994b6..15a466048 100644 --- a/build.gradle +++ b/build.gradle @@ -114,6 +114,10 @@ sourceSets { } } +tasks.withType(ProcessResources) { + duplicatesStrategy = DuplicatesStrategy.INCLUDE +} + googleJavaFormat { toolVersion = '1.7' options style: 'AOSP' diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/config/exceptions/ConfigExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/config/exceptions/ConfigExceptionTest.java new file mode 100644 index 000000000..7b3d51c21 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/config/exceptions/ConfigExceptionTest.java @@ -0,0 +1,77 @@ +package org.fisco.bcos.sdk.v3.test.config.exceptions; + +import org.fisco.bcos.sdk.v3.config.exceptions.ConfigException; +import org.junit.Assert; +import org.junit.Test; + +public class ConfigExceptionTest { + + @Test + public void testConstructorWithMessage() { + String message = "Configuration error occurred"; + ConfigException exception = new ConfigException(message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testConstructorWithCause() { + Exception cause = new RuntimeException("Root cause"); + ConfigException exception = new ConfigException(cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(cause, exception.getCause()); + Assert.assertTrue(exception.getMessage().contains("Root cause")); + } + + @Test + public void testConstructorWithMessageAndCause() { + String message = "Configuration error"; + Exception cause = new IllegalArgumentException("Invalid argument"); + ConfigException exception = new ConfigException(message, cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testExceptionIsThrowable() { + ConfigException exception = new ConfigException("Test"); + Assert.assertTrue(exception instanceof Exception); + Assert.assertTrue(exception instanceof Throwable); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new ConfigException("Test exception"); + } catch (ConfigException e) { + Assert.assertEquals("Test exception", e.getMessage()); + } catch (Exception e) { + Assert.fail("Should have caught ConfigException specifically"); + } + } + + @Test + public void testNullMessage() { + ConfigException exception = new ConfigException((String) null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + } + + @Test + public void testNullCause() { + ConfigException exception = new ConfigException((Throwable) null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getCause()); + } + + @Test + public void testEmptyMessage() { + String message = ""; + ConfigException exception = new ConfigException(message); + Assert.assertEquals(message, exception.getMessage()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/HashExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/HashExceptionTest.java new file mode 100644 index 000000000..fc63e4e01 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/HashExceptionTest.java @@ -0,0 +1,67 @@ +package org.fisco.bcos.sdk.v3.test.crypto.exceptions; + +import org.fisco.bcos.sdk.v3.crypto.exceptions.HashException; +import org.junit.Assert; +import org.junit.Test; + +public class HashExceptionTest { + + @Test + public void testConstructorWithMessage() { + String message = "Hash computation failed"; + HashException exception = new HashException(message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testConstructorWithMessageAndCause() { + String message = "Hash error occurred"; + Exception cause = new IllegalArgumentException("Invalid input"); + HashException exception = new HashException(message, cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testExceptionIsRuntimeException() { + HashException exception = new HashException("Test"); + Assert.assertTrue(exception instanceof RuntimeException); + Assert.assertTrue(exception instanceof Exception); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new HashException("Hash exception"); + } catch (HashException e) { + Assert.assertEquals("Hash exception", e.getMessage()); + } catch (Exception e) { + Assert.fail("Should have caught HashException specifically"); + } + } + + @Test + public void testNullMessage() { + HashException exception = new HashException(null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + } + + @Test + public void testEmptyMessage() { + String message = ""; + HashException exception = new HashException(message); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testNullCause() { + HashException exception = new HashException("Message", null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getCause()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/KeyPairExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/KeyPairExceptionTest.java new file mode 100644 index 000000000..d4d6354e0 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/KeyPairExceptionTest.java @@ -0,0 +1,67 @@ +package org.fisco.bcos.sdk.v3.test.crypto.exceptions; + +import org.fisco.bcos.sdk.v3.crypto.exceptions.KeyPairException; +import org.junit.Assert; +import org.junit.Test; + +public class KeyPairExceptionTest { + + @Test + public void testConstructorWithMessage() { + String message = "Key pair generation failed"; + KeyPairException exception = new KeyPairException(message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testConstructorWithMessageAndCause() { + String message = "Key pair error occurred"; + Exception cause = new IllegalStateException("Invalid state"); + KeyPairException exception = new KeyPairException(message, cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testExceptionIsRuntimeException() { + KeyPairException exception = new KeyPairException("Test"); + Assert.assertTrue(exception instanceof RuntimeException); + Assert.assertTrue(exception instanceof Exception); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new KeyPairException("Key pair exception"); + } catch (KeyPairException e) { + Assert.assertEquals("Key pair exception", e.getMessage()); + } catch (Exception e) { + Assert.fail("Should have caught KeyPairException specifically"); + } + } + + @Test + public void testNullMessage() { + KeyPairException exception = new KeyPairException(null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + } + + @Test + public void testEmptyMessage() { + String message = ""; + KeyPairException exception = new KeyPairException(message); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testNullCause() { + KeyPairException exception = new KeyPairException("Message", null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getCause()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/LoadKeyStoreExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/LoadKeyStoreExceptionTest.java new file mode 100644 index 000000000..47f0575c1 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/LoadKeyStoreExceptionTest.java @@ -0,0 +1,67 @@ +package org.fisco.bcos.sdk.v3.test.crypto.exceptions; + +import org.fisco.bcos.sdk.v3.crypto.exceptions.LoadKeyStoreException; +import org.junit.Assert; +import org.junit.Test; + +public class LoadKeyStoreExceptionTest { + + @Test + public void testConstructorWithMessage() { + String message = "Failed to load keystore"; + LoadKeyStoreException exception = new LoadKeyStoreException(message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testConstructorWithMessageAndCause() { + String message = "Keystore load error"; + Exception cause = new java.io.IOException("File not found"); + LoadKeyStoreException exception = new LoadKeyStoreException(message, cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testExceptionIsRuntimeException() { + LoadKeyStoreException exception = new LoadKeyStoreException("Test"); + Assert.assertTrue(exception instanceof RuntimeException); + Assert.assertTrue(exception instanceof Exception); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new LoadKeyStoreException("Load exception"); + } catch (LoadKeyStoreException e) { + Assert.assertEquals("Load exception", e.getMessage()); + } catch (Exception e) { + Assert.fail("Should have caught LoadKeyStoreException specifically"); + } + } + + @Test + public void testNullMessage() { + LoadKeyStoreException exception = new LoadKeyStoreException(null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + } + + @Test + public void testEmptyMessage() { + String message = ""; + LoadKeyStoreException exception = new LoadKeyStoreException(message); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testNullCause() { + LoadKeyStoreException exception = new LoadKeyStoreException("Message", null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getCause()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/SignatureExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/SignatureExceptionTest.java new file mode 100644 index 000000000..771d83ce2 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/SignatureExceptionTest.java @@ -0,0 +1,67 @@ +package org.fisco.bcos.sdk.v3.test.crypto.exceptions; + +import org.fisco.bcos.sdk.v3.crypto.exceptions.SignatureException; +import org.junit.Assert; +import org.junit.Test; + +public class SignatureExceptionTest { + + @Test + public void testConstructorWithMessage() { + String message = "Signature verification failed"; + SignatureException exception = new SignatureException(message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testConstructorWithMessageAndCause() { + String message = "Signature error occurred"; + Exception cause = new IllegalArgumentException("Invalid signature"); + SignatureException exception = new SignatureException(message, cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testExceptionIsRuntimeException() { + SignatureException exception = new SignatureException("Test"); + Assert.assertTrue(exception instanceof RuntimeException); + Assert.assertTrue(exception instanceof Exception); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new SignatureException("Signature exception"); + } catch (SignatureException e) { + Assert.assertEquals("Signature exception", e.getMessage()); + } catch (Exception e) { + Assert.fail("Should have caught SignatureException specifically"); + } + } + + @Test + public void testNullMessage() { + SignatureException exception = new SignatureException(null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + } + + @Test + public void testEmptyMessage() { + String message = ""; + SignatureException exception = new SignatureException(message); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testNullCause() { + SignatureException exception = new SignatureException("Message", null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getCause()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/UnsupportedCryptoTypeExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/UnsupportedCryptoTypeExceptionTest.java new file mode 100644 index 000000000..b98a8f887 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/crypto/exceptions/UnsupportedCryptoTypeExceptionTest.java @@ -0,0 +1,67 @@ +package org.fisco.bcos.sdk.v3.test.crypto.exceptions; + +import org.fisco.bcos.sdk.v3.crypto.exceptions.UnsupportedCryptoTypeException; +import org.junit.Assert; +import org.junit.Test; + +public class UnsupportedCryptoTypeExceptionTest { + + @Test + public void testConstructorWithMessage() { + String message = "Unsupported crypto type"; + UnsupportedCryptoTypeException exception = new UnsupportedCryptoTypeException(message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testConstructorWithMessageAndCause() { + String message = "Crypto type not supported"; + Exception cause = new IllegalArgumentException("Invalid type"); + UnsupportedCryptoTypeException exception = new UnsupportedCryptoTypeException(message, cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testExceptionIsRuntimeException() { + UnsupportedCryptoTypeException exception = new UnsupportedCryptoTypeException("Test"); + Assert.assertTrue(exception instanceof RuntimeException); + Assert.assertTrue(exception instanceof Exception); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new UnsupportedCryptoTypeException("Unsupported exception"); + } catch (UnsupportedCryptoTypeException e) { + Assert.assertEquals("Unsupported exception", e.getMessage()); + } catch (Exception e) { + Assert.fail("Should have caught UnsupportedCryptoTypeException specifically"); + } + } + + @Test + public void testNullMessage() { + UnsupportedCryptoTypeException exception = new UnsupportedCryptoTypeException(null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + } + + @Test + public void testEmptyMessage() { + String message = ""; + UnsupportedCryptoTypeException exception = new UnsupportedCryptoTypeException(message); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testNullCause() { + UnsupportedCryptoTypeException exception = new UnsupportedCryptoTypeException("Message", null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getCause()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/ConstantConfigTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/ConstantConfigTest.java new file mode 100644 index 000000000..052146980 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/ConstantConfigTest.java @@ -0,0 +1,43 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.ConstantConfig; +import org.junit.Assert; +import org.junit.Test; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class ConstantConfigTest { + + @Test + public void testConfigFileName() { + Assert.assertEquals("config.toml", ConstantConfig.CONFIG_FILE_NAME); + } + + @Test + public void testDefaultCharset() { + Assert.assertEquals(StandardCharsets.UTF_8, ConstantConfig.DEFAULT_CHARSET); + } + + @Test + public void testConfigFileNameNotNull() { + Assert.assertNotNull(ConstantConfig.CONFIG_FILE_NAME); + } + + @Test + public void testConfigFileNameNotEmpty() { + Assert.assertFalse(ConstantConfig.CONFIG_FILE_NAME.isEmpty()); + } + + @Test + public void testDefaultCharsetNotNull() { + Assert.assertNotNull(ConstantConfig.DEFAULT_CHARSET); + } + + @Test + public void testDefaultCharsetIsUtf8() { + Charset utf8 = StandardCharsets.UTF_8; + Assert.assertEquals(utf8, ConstantConfig.DEFAULT_CHARSET); + Assert.assertEquals("UTF-8", ConstantConfig.DEFAULT_CHARSET.name()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/CryptoTypeTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/CryptoTypeTest.java new file mode 100644 index 000000000..c7a4fb0fa --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/CryptoTypeTest.java @@ -0,0 +1,46 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.CryptoType; +import org.junit.Assert; +import org.junit.Test; + +public class CryptoTypeTest { + + @Test + public void testEcdsaType() { + Assert.assertEquals(0, CryptoType.ECDSA_TYPE); + } + + @Test + public void testSmType() { + Assert.assertEquals(1, CryptoType.SM_TYPE); + } + + @Test + public void testEd25519VrfType() { + Assert.assertEquals(2, CryptoType.ED25519_VRF_TYPE); + } + + @Test + public void testHsmType() { + Assert.assertEquals(3, CryptoType.HSM_TYPE); + } + + @Test + public void testAllTypesAreDifferent() { + Assert.assertNotEquals(CryptoType.ECDSA_TYPE, CryptoType.SM_TYPE); + Assert.assertNotEquals(CryptoType.ECDSA_TYPE, CryptoType.ED25519_VRF_TYPE); + Assert.assertNotEquals(CryptoType.ECDSA_TYPE, CryptoType.HSM_TYPE); + Assert.assertNotEquals(CryptoType.SM_TYPE, CryptoType.ED25519_VRF_TYPE); + Assert.assertNotEquals(CryptoType.SM_TYPE, CryptoType.HSM_TYPE); + Assert.assertNotEquals(CryptoType.ED25519_VRF_TYPE, CryptoType.HSM_TYPE); + } + + @Test + public void testConstantsAreNonNegative() { + Assert.assertTrue(CryptoType.ECDSA_TYPE >= 0); + Assert.assertTrue(CryptoType.SM_TYPE >= 0); + Assert.assertTrue(CryptoType.ED25519_VRF_TYPE >= 0); + Assert.assertTrue(CryptoType.HSM_TYPE >= 0); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/EnumNodeVersionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/EnumNodeVersionTest.java new file mode 100644 index 000000000..1ef809554 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/EnumNodeVersionTest.java @@ -0,0 +1,235 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.EnumNodeVersion; +import org.junit.Assert; +import org.junit.Test; + +public class EnumNodeVersionTest { + + @Test + public void testEnumValues() { + EnumNodeVersion[] values = EnumNodeVersion.values(); + Assert.assertNotNull(values); + Assert.assertTrue(values.length > 0); // At least some versions exist + } + + @Test + public void testUnknownVersion() { + Assert.assertEquals(-1, EnumNodeVersion.UNKNOWN.getVersion().intValue()); + } + + @Test + public void testBcos300RC4() { + Assert.assertEquals(4, EnumNodeVersion.BCOS_3_0_0_RC4.getVersion().intValue()); + } + + @Test + public void testBcos300() { + Assert.assertEquals(0x03000000, EnumNodeVersion.BCOS_3_0_0.getVersion().intValue()); + } + + @Test + public void testBcos310() { + Assert.assertEquals(0x03010000, EnumNodeVersion.BCOS_3_1_0.getVersion().intValue()); + } + + @Test + public void testValueOfKnownVersion() { + Assert.assertEquals(EnumNodeVersion.BCOS_3_0_0, EnumNodeVersion.valueOf(0x03000000)); + Assert.assertEquals(EnumNodeVersion.BCOS_3_1_0, EnumNodeVersion.valueOf(0x03010000)); + Assert.assertEquals(EnumNodeVersion.BCOS_3_2_0, EnumNodeVersion.valueOf(0x03020000)); + } + + @Test + public void testValueOfUnknownVersion() { + Assert.assertEquals(EnumNodeVersion.UNKNOWN, EnumNodeVersion.valueOf(999999)); + } + + @Test + public void testGetVersionString() { + Assert.assertEquals("3.0.0-rc4", EnumNodeVersion.BCOS_3_0_0_RC4.getVersionString()); + Assert.assertEquals("3.0.0", EnumNodeVersion.BCOS_3_0_0.getVersionString()); + Assert.assertEquals("3.1.0", EnumNodeVersion.BCOS_3_1_0.getVersionString()); + Assert.assertEquals("3.2.0", EnumNodeVersion.BCOS_3_2_0.getVersionString()); + Assert.assertEquals("0.0.0", EnumNodeVersion.UNKNOWN.getVersionString()); + } + + @Test + public void testCompareToVersion() { + EnumNodeVersion v1 = EnumNodeVersion.BCOS_3_0_0; + EnumNodeVersion v2 = EnumNodeVersion.BCOS_3_1_0; + EnumNodeVersion v3 = EnumNodeVersion.BCOS_3_0_0; + + Assert.assertTrue(v2.compareToVersion(v1) > 0); + Assert.assertTrue(v1.compareToVersion(v2) < 0); + Assert.assertEquals(0, v1.compareToVersion(v3)); + } + + @Test + public void testCompareTo() { + EnumNodeVersion v1 = EnumNodeVersion.BCOS_3_0_0; + EnumNodeVersion v2 = EnumNodeVersion.BCOS_3_1_0; + + Assert.assertTrue(EnumNodeVersion.compareTo(v2, v1) > 0); + Assert.assertTrue(EnumNodeVersion.compareTo(v1, v2) < 0); + Assert.assertEquals(0, EnumNodeVersion.compareTo(v1, v1)); + } + + @Test + public void testToVersionObj() { + EnumNodeVersion.Version version = EnumNodeVersion.BCOS_3_1_0.toVersionObj(); + Assert.assertNotNull(version); + Assert.assertEquals(3, version.getMajor()); + Assert.assertEquals(1, version.getMinor()); + Assert.assertEquals(0, version.getPatch()); + } + + @Test + public void testConvertToVersion() { + EnumNodeVersion.Version version = EnumNodeVersion.convertToVersion(0x03010000); + Assert.assertNotNull(version); + Assert.assertEquals(3, version.getMajor()); + Assert.assertEquals(1, version.getMinor()); + } + + @Test + public void testGetClassVersion() { + EnumNodeVersion.Version version = EnumNodeVersion.getClassVersion("3.1.0"); + Assert.assertEquals(3, version.getMajor()); + Assert.assertEquals(1, version.getMinor()); + Assert.assertEquals(0, version.getPatch()); + Assert.assertEquals("", version.getExt()); + } + + @Test + public void testGetClassVersionWithExt() { + EnumNodeVersion.Version version = EnumNodeVersion.getClassVersion("3.0.0-rc4"); + Assert.assertEquals(3, version.getMajor()); + Assert.assertEquals(0, version.getMinor()); + Assert.assertEquals(0, version.getPatch()); + Assert.assertEquals("rc4", version.getExt()); + } + + @Test(expected = IllegalStateException.class) + public void testGetClassVersionInvalidFormat() { + EnumNodeVersion.getClassVersion("invalid"); + } + + @Test + public void testVersionToVersionString() { + EnumNodeVersion.Version version = new EnumNodeVersion.Version(); + version.setMajor(3); + version.setMinor(1); + version.setPatch(0); + Assert.assertEquals("3.1.0", version.toVersionString()); + } + + @Test + public void testVersionToVersionStringWithExt() { + EnumNodeVersion.Version version = new EnumNodeVersion.Version(); + version.setMajor(3); + version.setMinor(0); + version.setPatch(0); + version.setExt("rc4"); + Assert.assertEquals("3.0.0-rc4", version.toVersionString()); + } + + @Test + public void testVersionToCompatibilityVersion() { + EnumNodeVersion.Version version = new EnumNodeVersion.Version(); + version.setMajor(3); + version.setMinor(1); + version.setPatch(0); + Assert.assertEquals(0x03010000, version.toCompatibilityVersion()); + } + + @Test + public void testVersionCompareTo() { + EnumNodeVersion.Version v1 = new EnumNodeVersion.Version(); + v1.setMajor(3); + v1.setMinor(0); + v1.setPatch(0); + + EnumNodeVersion.Version v2 = new EnumNodeVersion.Version(); + v2.setMajor(3); + v2.setMinor(1); + v2.setPatch(0); + + Assert.assertTrue(v2.compareTo(v1) > 0); + Assert.assertTrue(v1.compareTo(v2) < 0); + Assert.assertEquals(0, v1.compareTo(v1)); + } + + @Test + public void testVersionEquals() { + EnumNodeVersion.Version v1 = new EnumNodeVersion.Version(); + v1.setMajor(3); + v1.setMinor(1); + v1.setPatch(0); + + EnumNodeVersion.Version v2 = new EnumNodeVersion.Version(); + v2.setMajor(3); + v2.setMinor(1); + v2.setPatch(0); + + EnumNodeVersion.Version v3 = new EnumNodeVersion.Version(); + v3.setMajor(3); + v3.setMinor(2); + v3.setPatch(0); + + Assert.assertEquals(v1, v2); + Assert.assertNotEquals(v1, v3); + Assert.assertNotEquals(v1, null); + Assert.assertNotEquals(v1, "string"); + } + + @Test + public void testVersionToString() { + EnumNodeVersion.Version version = new EnumNodeVersion.Version(); + version.setMajor(3); + version.setMinor(1); + version.setPatch(0); + version.setExt("test"); + + String result = version.toString(); + Assert.assertNotNull(result); + Assert.assertTrue(result.contains("3")); + Assert.assertTrue(result.contains("1")); + Assert.assertTrue(result.contains("0")); + Assert.assertTrue(result.contains("test")); + } + + @Test + public void testValueFromCompatibilityVersion() { + EnumNodeVersion.Version version = EnumNodeVersion.valueFromCompatibilityVersion(0x03010000); + Assert.assertEquals(3, version.getMajor()); + Assert.assertEquals(1, version.getMinor()); + Assert.assertEquals(0, version.getPatch()); + } + + @Test + public void testValueFromCompatibilityVersionRC4() { + EnumNodeVersion.Version version = EnumNodeVersion.valueFromCompatibilityVersion(4); + Assert.assertEquals(3, version.getMajor()); + Assert.assertEquals(0, version.getMinor()); + Assert.assertEquals(0, version.getPatch()); + Assert.assertEquals("rc4", version.getExt()); + } + + @Test + public void testVersionGettersAndSetters() { + EnumNodeVersion.Version version = new EnumNodeVersion.Version(); + + version.setMajor(3); + Assert.assertEquals(3, version.getMajor()); + + version.setMinor(2); + Assert.assertEquals(2, version.getMinor()); + + version.setPatch(5); + Assert.assertEquals(5, version.getPatch()); + + version.setExt("beta"); + Assert.assertEquals("beta", version.getExt()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/MerkleProofUnitTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/MerkleProofUnitTest.java new file mode 100644 index 000000000..8cdfad7e0 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/MerkleProofUnitTest.java @@ -0,0 +1,101 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.MerkleProofUnit; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +public class MerkleProofUnitTest { + + @Test + public void testGettersAndSetters() { + MerkleProofUnit unit = new MerkleProofUnit(); + + List left = Arrays.asList("left1", "left2"); + unit.setLeft(left); + Assert.assertEquals(left, unit.getLeft()); + + List right = Arrays.asList("right1", "right2"); + unit.setRight(right); + Assert.assertEquals(right, unit.getRight()); + } + + @Test + public void testEquals() { + MerkleProofUnit unit1 = new MerkleProofUnit(); + unit1.setLeft(Arrays.asList("a", "b")); + unit1.setRight(Arrays.asList("c", "d")); + + MerkleProofUnit unit2 = new MerkleProofUnit(); + unit2.setLeft(Arrays.asList("a", "b")); + unit2.setRight(Arrays.asList("c", "d")); + + MerkleProofUnit unit3 = new MerkleProofUnit(); + unit3.setLeft(Arrays.asList("x", "y")); + unit3.setRight(Arrays.asList("c", "d")); + + // Test equality + Assert.assertEquals(unit1, unit2); + + // Test same object + Assert.assertEquals(unit1, unit1); + + // Test different left + Assert.assertNotEquals(unit1, unit3); + + // Test null + Assert.assertNotEquals(unit1, null); + + // Test different class + Assert.assertNotEquals(unit1, "string"); + } + + @Test + public void testHashCode() { + MerkleProofUnit unit1 = new MerkleProofUnit(); + unit1.setLeft(Arrays.asList("a", "b")); + unit1.setRight(Arrays.asList("c", "d")); + + MerkleProofUnit unit2 = new MerkleProofUnit(); + unit2.setLeft(Arrays.asList("a", "b")); + unit2.setRight(Arrays.asList("c", "d")); + + Assert.assertEquals(unit1.hashCode(), unit2.hashCode()); + } + + @Test + public void testToString() { + MerkleProofUnit unit = new MerkleProofUnit(); + unit.setLeft(Arrays.asList("left1")); + unit.setRight(Arrays.asList("right1")); + + String result = unit.toString(); + Assert.assertNotNull(result); + Assert.assertTrue(result.contains("left1")); + Assert.assertTrue(result.contains("right1")); + Assert.assertTrue(result.contains("MerkleProofUnit")); + } + + @Test + public void testNullLists() { + MerkleProofUnit unit1 = new MerkleProofUnit(); + MerkleProofUnit unit2 = new MerkleProofUnit(); + + Assert.assertEquals(unit1, unit2); + Assert.assertEquals(unit1.hashCode(), unit2.hashCode()); + } + + @Test + public void testEmptyLists() { + MerkleProofUnit unit = new MerkleProofUnit(); + unit.setLeft(Arrays.asList()); + unit.setRight(Arrays.asList()); + + Assert.assertNotNull(unit.getLeft()); + Assert.assertNotNull(unit.getRight()); + Assert.assertTrue(unit.getLeft().isEmpty()); + Assert.assertTrue(unit.getRight().isEmpty()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/NodeTypeTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/NodeTypeTest.java new file mode 100644 index 000000000..c172d8367 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/NodeTypeTest.java @@ -0,0 +1,69 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.NodeType; +import org.junit.Assert; +import org.junit.Test; + +public class NodeTypeTest { + + @Test + public void testEnumValues() { + NodeType[] values = NodeType.values(); + Assert.assertNotNull(values); + Assert.assertEquals(4, values.length); + } + + @Test + public void testConsensusSealer() { + NodeType type = NodeType.CONSENSUS_SEALER; + Assert.assertNotNull(type); + Assert.assertEquals("CONSENSUS_SEALER", type.name()); + } + + @Test + public void testConsensusObserver() { + NodeType type = NodeType.CONSENSUS_OBSERVER; + Assert.assertNotNull(type); + Assert.assertEquals("CONSENSUS_OBSERVER", type.name()); + } + + @Test + public void testConsensusCandidateSealer() { + NodeType type = NodeType.CONSENSUS_CANDIDATE_SEALER; + Assert.assertNotNull(type); + Assert.assertEquals("CONSENSUS_CANDIDATE_SEALER", type.name()); + } + + @Test + public void testUnknown() { + NodeType type = NodeType.UNKNOWN; + Assert.assertNotNull(type); + Assert.assertEquals("UNKNOWN", type.name()); + } + + @Test + public void testValueOf() { + NodeType type = NodeType.valueOf("CONSENSUS_SEALER"); + Assert.assertEquals(NodeType.CONSENSUS_SEALER, type); + + type = NodeType.valueOf("CONSENSUS_OBSERVER"); + Assert.assertEquals(NodeType.CONSENSUS_OBSERVER, type); + + type = NodeType.valueOf("CONSENSUS_CANDIDATE_SEALER"); + Assert.assertEquals(NodeType.CONSENSUS_CANDIDATE_SEALER, type); + + type = NodeType.valueOf("UNKNOWN"); + Assert.assertEquals(NodeType.UNKNOWN, type); + } + + @Test + public void testEnumComparison() { + Assert.assertSame(NodeType.CONSENSUS_SEALER, NodeType.valueOf("CONSENSUS_SEALER")); + Assert.assertNotSame(NodeType.CONSENSUS_SEALER, NodeType.CONSENSUS_OBSERVER); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidValueOf() { + NodeType.valueOf("INVALID_TYPE"); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/NodeVersionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/NodeVersionTest.java new file mode 100644 index 000000000..e2fd0bb8b --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/NodeVersionTest.java @@ -0,0 +1,129 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.NodeVersion; +import org.junit.Assert; +import org.junit.Test; + +public class NodeVersionTest { + + @Test + public void testGetNodeVersion() { + NodeVersion nodeVersion = new NodeVersion(); + NodeVersion.ClientVersion clientVersion = new NodeVersion.ClientVersion(); + + nodeVersion.setResult(clientVersion); + Assert.assertEquals(clientVersion, nodeVersion.getNodeVersion()); + } + + @Test + public void testClientVersionGettersAndSetters() { + NodeVersion.ClientVersion cv = new NodeVersion.ClientVersion(); + + cv.setVersion("3.0.0"); + Assert.assertEquals("3.0.0", cv.getVersion()); + + cv.setSupportedVersion("3.0.0"); + Assert.assertEquals("3.0.0", cv.getSupportedVersion()); + + cv.setChainId("chain0"); + Assert.assertEquals("chain0", cv.getChainId()); + + cv.setBuildTime("2023-01-01"); + Assert.assertEquals("2023-01-01", cv.getBuildTime()); + + cv.setBuildType("Release"); + Assert.assertEquals("Release", cv.getBuildType()); + + cv.setGitBranch("master"); + Assert.assertEquals("master", cv.getGitBranch()); + + cv.setGitCommitHash("abc123"); + Assert.assertEquals("abc123", cv.getGitCommitHash()); + } + + @Test + public void testClientVersionEquals() { + NodeVersion.ClientVersion cv1 = new NodeVersion.ClientVersion(); + cv1.setVersion("3.0.0"); + cv1.setSupportedVersion("3.0.0"); + cv1.setChainId("chain0"); + cv1.setBuildTime("2023-01-01"); + cv1.setBuildType("Release"); + cv1.setGitBranch("master"); + cv1.setGitCommitHash("abc123"); + + NodeVersion.ClientVersion cv2 = new NodeVersion.ClientVersion(); + cv2.setVersion("3.0.0"); + cv2.setSupportedVersion("3.0.0"); + cv2.setChainId("chain0"); + cv2.setBuildTime("2023-01-01"); + cv2.setBuildType("Release"); + cv2.setGitBranch("master"); + cv2.setGitCommitHash("abc123"); + + NodeVersion.ClientVersion cv3 = new NodeVersion.ClientVersion(); + cv3.setVersion("3.1.0"); + + // Test equality + Assert.assertEquals(cv1, cv2); + + // Test same object + Assert.assertEquals(cv1, cv1); + + // Test different version + Assert.assertNotEquals(cv1, cv3); + + // Test null + Assert.assertNotEquals(cv1, null); + + // Test different class + Assert.assertNotEquals(cv1, "string"); + } + + @Test + public void testClientVersionHashCode() { + NodeVersion.ClientVersion cv1 = new NodeVersion.ClientVersion(); + cv1.setVersion("3.0.0"); + cv1.setSupportedVersion("3.0.0"); + + NodeVersion.ClientVersion cv2 = new NodeVersion.ClientVersion(); + cv2.setVersion("3.0.0"); + cv2.setSupportedVersion("3.0.0"); + + Assert.assertEquals(cv1.hashCode(), cv2.hashCode()); + } + + @Test + public void testClientVersionToString() { + NodeVersion.ClientVersion cv = new NodeVersion.ClientVersion(); + cv.setVersion("3.0.0"); + cv.setChainId("chain0"); + + String result = cv.toString(); + Assert.assertNotNull(result); + Assert.assertTrue(result.contains("3.0.0")); + Assert.assertTrue(result.contains("chain0")); + Assert.assertTrue(result.contains("ClientVersion")); + } + + @Test + public void testClientVersionAllFieldsNull() { + NodeVersion.ClientVersion cv1 = new NodeVersion.ClientVersion(); + NodeVersion.ClientVersion cv2 = new NodeVersion.ClientVersion(); + + Assert.assertEquals(cv1, cv2); + Assert.assertEquals(cv1.hashCode(), cv2.hashCode()); + } + + @Test + public void testClientVersionPartialFields() { + NodeVersion.ClientVersion cv = new NodeVersion.ClientVersion(); + cv.setVersion("3.0.0"); + cv.setChainId("chain0"); + + Assert.assertNotNull(cv.getVersion()); + Assert.assertNotNull(cv.getChainId()); + Assert.assertNull(cv.getBuildTime()); + Assert.assertNull(cv.getGitBranch()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/PrecompiledConstantTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/PrecompiledConstantTest.java new file mode 100644 index 000000000..14e5e69b5 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/PrecompiledConstantTest.java @@ -0,0 +1,79 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.PrecompiledConstant; +import org.junit.Assert; +import org.junit.Test; + +public class PrecompiledConstantTest { + + @Test + public void testTableKeyMaxLength() { + Assert.assertEquals(255, PrecompiledConstant.TABLE_KEY_MAX_LENGTH); + } + + @Test + public void testTableFieldNameMaxLength() { + Assert.assertEquals(64, PrecompiledConstant.TABLE_FIELD_NAME_MAX_LENGTH); + } + + @Test + public void testUserTableNameMaxLength() { + Assert.assertEquals(48, PrecompiledConstant.USER_TABLE_NAME_MAX_LENGTH); + } + + @Test + public void testTableValueFieldMaxLength() { + Assert.assertEquals(1024, PrecompiledConstant.TABLE_VALUE_FIELD_MAX_LENGTH); + } + + @Test + public void testTableKeyValueMaxLength() { + Assert.assertEquals(255, PrecompiledConstant.TABLE_KEY_VALUE_MAX_LENGTH); + } + + @Test + public void testUserTableFieldValueMaxLength() { + Assert.assertEquals(16 * 1024 * 1024 - 1, PrecompiledConstant.USER_TABLE_FIELD_VALUE_MAX_LENGTH); + } + + @Test + public void testSyncKeepUpThreshold() { + Assert.assertEquals(10, PrecompiledConstant.SYNC_KEEP_UP_THRESHOLD); + } + + @Test + public void testKeyOrderConstant() { + Assert.assertEquals("key_order", PrecompiledConstant.KEY_ORDER); + } + + @Test + public void testKeyFieldNameConstant() { + Assert.assertEquals("key_field", PrecompiledConstant.KEY_FIELD_NAME); + } + + @Test + public void testValueFieldNameConstant() { + Assert.assertEquals("value_field", PrecompiledConstant.VALUE_FIELD_NAME); + } + + @Test + public void testConstantsAreConsistent() { + // Ensure KEY_MAX_LENGTH and KEY_VALUE_MAX_LENGTH have the same value + Assert.assertEquals(PrecompiledConstant.TABLE_KEY_MAX_LENGTH, + PrecompiledConstant.TABLE_KEY_VALUE_MAX_LENGTH); + } + + @Test + public void testStringConstantsNotNull() { + Assert.assertNotNull(PrecompiledConstant.KEY_ORDER); + Assert.assertNotNull(PrecompiledConstant.KEY_FIELD_NAME); + Assert.assertNotNull(PrecompiledConstant.VALUE_FIELD_NAME); + } + + @Test + public void testStringConstantsNotEmpty() { + Assert.assertFalse(PrecompiledConstant.KEY_ORDER.isEmpty()); + Assert.assertFalse(PrecompiledConstant.KEY_FIELD_NAME.isEmpty()); + Assert.assertFalse(PrecompiledConstant.VALUE_FIELD_NAME.isEmpty()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/model/PrecompiledRetCodeTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/model/PrecompiledRetCodeTest.java new file mode 100644 index 000000000..f3053de99 --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/model/PrecompiledRetCodeTest.java @@ -0,0 +1,156 @@ +package org.fisco.bcos.sdk.v3.test.model; + +import org.fisco.bcos.sdk.v3.model.PrecompiledRetCode; +import org.fisco.bcos.sdk.v3.model.RetCode; +import org.junit.Assert; +import org.junit.Test; + +public class PrecompiledRetCodeTest { + + @Test + public void testSuccessCode() { + Assert.assertNotNull(PrecompiledRetCode.CODE_SUCCESS); + Assert.assertEquals(0, PrecompiledRetCode.CODE_SUCCESS.getCode()); + Assert.assertEquals("Success", PrecompiledRetCode.CODE_SUCCESS.getMessage()); + } + + @Test + public void testUnknownFailedCode() { + Assert.assertNotNull(PrecompiledRetCode.CODE_UNKNOWN_FAILED); + Assert.assertEquals(-1, PrecompiledRetCode.CODE_UNKNOWN_FAILED.getCode()); + Assert.assertEquals("Unknown failed", PrecompiledRetCode.CODE_UNKNOWN_FAILED.getMessage()); + } + + @Test + public void testFileSystemPrecompiledCodes() { + Assert.assertEquals(-53005, PrecompiledRetCode.CODE_FILE_INVALID_PATH.getCode()); + Assert.assertEquals(-53004, PrecompiledRetCode.CODE_FILE_SET_WASM_FAILED.getCode()); + Assert.assertEquals(-53003, PrecompiledRetCode.CODE_FILE_BUILD_DIR_FAILED.getCode()); + Assert.assertEquals(-53002, PrecompiledRetCode.CODE_FILE_ALREADY_EXIST.getCode()); + Assert.assertEquals(-53001, PrecompiledRetCode.CODE_FILE_NOT_EXIST.getCode()); + } + + @Test + public void testChainGovernancePrecompiledCodes() { + Assert.assertEquals(-52012, PrecompiledRetCode.CODE_CURRENT_VALUE_IS_EXPECTED_VALUE.getCode()); + Assert.assertEquals(-52011, PrecompiledRetCode.CODE_ACCOUNT_FROZEN.getCode()); + Assert.assertEquals(-52010, PrecompiledRetCode.CODE_ACCOUNT_ALREADY_AVAILABLE.getCode()); + Assert.assertEquals(-52009, PrecompiledRetCode.CODE_INVALID_ACCOUNT_ADDRESS.getCode()); + Assert.assertEquals(-52008, PrecompiledRetCode.CODE_ACCOUNT_NOT_EXIST.getCode()); + } + + @Test + public void testConsensusPrecompiledCodes() { + Assert.assertEquals(-51104, PrecompiledRetCode.CODE_ADD_SEALER_SHOULD_IN_OBSERVER.getCode()); + Assert.assertEquals(-51103, PrecompiledRetCode.CODE_NODE_NOT_EXIST.getCode()); + Assert.assertEquals(-51102, PrecompiledRetCode.CODE_INVALID_WEIGHT.getCode()); + Assert.assertEquals(-51101, PrecompiledRetCode.CODE_LAST_SEALER.getCode()); + Assert.assertEquals(-51100, PrecompiledRetCode.CODE_INVALID_NODEID.getCode()); + } + + @Test + public void testCRUDPrecompiledCodes() { + Assert.assertEquals(-51508, PrecompiledRetCode.CODE_REMOVE_KEY_NOT_EXIST.getCode()); + Assert.assertEquals(-51507, PrecompiledRetCode.CODE_UPDATE_KEY_NOT_EXIST.getCode()); + Assert.assertEquals(-51506, PrecompiledRetCode.CODE_INSERT_KEY_EXIST.getCode()); + Assert.assertEquals(-51505, PrecompiledRetCode.CODE_KEY_NOT_EXIST_IN_COND.getCode()); + Assert.assertEquals(-51504, PrecompiledRetCode.CODE_KEY_NOT_EXIST_IN_ENTRY.getCode()); + } + + @Test + public void testCommonErrorCodes() { + Assert.assertEquals(-50105, PrecompiledRetCode.CODE_TABLE_OPEN_ERROR.getCode()); + Assert.assertEquals(-50104, PrecompiledRetCode.CODE_TABLE_CREATE_ERROR.getCode()); + Assert.assertEquals(-50103, PrecompiledRetCode.CODE_TABLE_SET_ROW_ERROR.getCode()); + Assert.assertEquals(-50102, PrecompiledRetCode.CODE_ADDRESS_INVALID.getCode()); + Assert.assertEquals(-50101, PrecompiledRetCode.CODE_UNKNOWN_FUNCTION_CALL.getCode()); + Assert.assertEquals(-50100, PrecompiledRetCode.CODE_TABLE_NOT_EXIST.getCode()); + } + + @Test + public void testTableErrorCodes() { + Assert.assertEquals(-50000, PrecompiledRetCode.CODE_NO_AUTHORIZED.getCode()); + Assert.assertEquals(-50001, PrecompiledRetCode.CODE_TABLE_NAME_ALREADY_EXIST.getCode()); + Assert.assertEquals(-50002, PrecompiledRetCode.CODE_TABLE_NAME_LENGTH_OVERFLOW.getCode()); + Assert.assertEquals(-50003, PrecompiledRetCode.CODE_TABLE_FILED_LENGTH_OVERFLOW.getCode()); + } + + @Test + public void testGetPrecompiledResponseWithKnownCode() { + RetCode retCode = PrecompiledRetCode.getPrecompiledResponse(0, "Test Message"); + Assert.assertNotNull(retCode); + Assert.assertEquals(PrecompiledRetCode.CODE_SUCCESS, retCode); + } + + @Test + public void testGetPrecompiledResponseWithUnknownCode() { + int unknownCode = -99999; + String message = "Custom error message"; + RetCode retCode = PrecompiledRetCode.getPrecompiledResponse(unknownCode, message); + + Assert.assertNotNull(retCode); + Assert.assertEquals(unknownCode, retCode.getCode()); + Assert.assertEquals(message, retCode.getMessage()); + } + + @Test + public void testGetPrecompiledResponseForMultipleCodes() { + // Test a few different known codes + Assert.assertEquals(PrecompiledRetCode.CODE_FILE_NOT_EXIST, + PrecompiledRetCode.getPrecompiledResponse(-53001, "")); + Assert.assertEquals(PrecompiledRetCode.CODE_INVALID_NODEID, + PrecompiledRetCode.getPrecompiledResponse(-51100, "")); + Assert.assertEquals(PrecompiledRetCode.CODE_TABLE_NOT_EXIST, + PrecompiledRetCode.getPrecompiledResponse(-50100, "")); + } + + @Test + public void testErrorMessageConstants() { + Assert.assertEquals("The operated node must be in the list returned by getGroupPeers", + PrecompiledRetCode.MUST_EXIST_IN_NODE_LIST); + Assert.assertEquals("The node already exists in the sealerList", + PrecompiledRetCode.ALREADY_EXISTS_IN_SEALER_LIST); + Assert.assertEquals("The node already exists in the observerList", + PrecompiledRetCode.ALREADY_EXISTS_IN_OBSERVER_LIST); + Assert.assertEquals("The node already has been removed from the group", + PrecompiledRetCode.ALREADY_REMOVED_FROM_THE_GROUP); + } + + @Test + public void testOverTableKeyLengthLimit() { + Assert.assertNotNull(PrecompiledRetCode.OVER_TABLE_KEY_LENGTH_LIMIT); + Assert.assertTrue(PrecompiledRetCode.OVER_TABLE_KEY_LENGTH_LIMIT.contains("255")); + } + + @Test + public void testAllRetCodesHaveMessages() { + Assert.assertNotNull(PrecompiledRetCode.CODE_SUCCESS.getMessage()); + Assert.assertNotNull(PrecompiledRetCode.CODE_FILE_NOT_EXIST.getMessage()); + Assert.assertNotNull(PrecompiledRetCode.CODE_INVALID_NODEID.getMessage()); + Assert.assertNotNull(PrecompiledRetCode.CODE_TABLE_NOT_EXIST.getMessage()); + } + + @Test + public void testNegativeErrorCodes() { + // All error codes should be negative except success + Assert.assertTrue(PrecompiledRetCode.CODE_SUCCESS.getCode() >= 0); + Assert.assertTrue(PrecompiledRetCode.CODE_UNKNOWN_FAILED.getCode() < 0); + Assert.assertTrue(PrecompiledRetCode.CODE_FILE_NOT_EXIST.getCode() < 0); + Assert.assertTrue(PrecompiledRetCode.CODE_INVALID_NODEID.getCode() < 0); + } + + @Test + public void testContractLifeCycleCodes() { + Assert.assertEquals(-51907, PrecompiledRetCode.CODE_INVALID_REVOKE_LAST_AUTHORIZATION.getCode()); + Assert.assertEquals(-51906, PrecompiledRetCode.CODE_INVALID_NON_EXIST_AUTHORIZATION.getCode()); + Assert.assertEquals(-51905, PrecompiledRetCode.CODE_INVALID_NO_AUTHORIZED.getCode()); + Assert.assertEquals(-51904, PrecompiledRetCode.CODE_INVALID_TABLE_NOT_EXIST.getCode()); + Assert.assertEquals(-51903, PrecompiledRetCode.CODE_INVALID_CONTRACT_ADDRESS.getCode()); + } + + @Test + public void testVersionErrorCodes() { + Assert.assertEquals(-51202, PrecompiledRetCode.CODE_ADDRESS_OR_VERSION_ERROR.getCode()); + Assert.assertEquals(-51201, PrecompiledRetCode.CODE_VERSION_LENGTH_OVERFLOW.getCode()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/model/exception/JsonExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/model/exception/JsonExceptionTest.java new file mode 100644 index 000000000..bba18454d --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/model/exception/JsonExceptionTest.java @@ -0,0 +1,109 @@ +package org.fisco.bcos.sdk.v3.test.transaction.model.exception; + +import org.fisco.bcos.sdk.v3.transaction.model.exception.JsonException; +import org.junit.Assert; +import org.junit.Test; + +public class JsonExceptionTest { + + @Test + public void testDefaultConstructor() { + JsonException exception = new JsonException(); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + Assert.assertNull(exception.getCause()); + } + + @Test + public void testConstructorWithMessage() { + String message = "JSON parsing error"; + JsonException exception = new JsonException(message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testConstructorWithCause() { + Exception cause = new IllegalArgumentException("Invalid JSON format"); + JsonException exception = new JsonException(cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testConstructorWithMessageAndCause() { + String message = "Failed to parse JSON"; + Exception cause = new RuntimeException("Root cause"); + JsonException exception = new JsonException(message, cause); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertEquals(cause, exception.getCause()); + } + + @Test + public void testExceptionIsRuntimeException() { + JsonException exception = new JsonException("Test"); + Assert.assertTrue(exception instanceof RuntimeException); + Assert.assertTrue(exception instanceof Exception); + Assert.assertTrue(exception instanceof Throwable); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new JsonException("JSON error"); + } catch (JsonException e) { + Assert.assertEquals("JSON error", e.getMessage()); + } catch (Exception e) { + Assert.fail("Should have caught JsonException specifically"); + } + } + + @Test + public void testExceptionDoesNotRequireChecked() { + // Since JsonException extends RuntimeException, it can be thrown without being declared + JsonException exception = new JsonException("Unchecked exception"); + Assert.assertTrue(exception instanceof RuntimeException); + } + + @Test + public void testSerialVersionUID() { + // Test that the exception is serializable + JsonException exception = new JsonException("Test"); + Assert.assertTrue(exception instanceof java.io.Serializable); + } + + @Test + public void testNullMessage() { + JsonException exception = new JsonException((String) null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getMessage()); + } + + @Test + public void testNullCause() { + JsonException exception = new JsonException((Throwable) null); + Assert.assertNotNull(exception); + Assert.assertNull(exception.getCause()); + } + + @Test + public void testEmptyMessage() { + String message = ""; + JsonException exception = new JsonException(message); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testChainedExceptions() { + Exception rootCause = new IllegalStateException("Root"); + Exception middleCause = new RuntimeException("Middle", rootCause); + JsonException exception = new JsonException("Top", middleCause); + + Assert.assertEquals(middleCause, exception.getCause()); + Assert.assertEquals(rootCause, exception.getCause().getCause()); + } +} diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/model/exception/TransactionBaseExceptionTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/model/exception/TransactionBaseExceptionTest.java new file mode 100644 index 000000000..6f975397c --- /dev/null +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/model/exception/TransactionBaseExceptionTest.java @@ -0,0 +1,101 @@ +package org.fisco.bcos.sdk.v3.test.transaction.model.exception; + +import org.fisco.bcos.sdk.v3.model.RetCode; +import org.fisco.bcos.sdk.v3.transaction.model.exception.TransactionBaseException; +import org.junit.Assert; +import org.junit.Test; + +public class TransactionBaseExceptionTest { + + @Test + public void testConstructorWithRetCode() { + RetCode retCode = new RetCode(100, "Test error message"); + TransactionBaseException exception = new TransactionBaseException(retCode); + + Assert.assertNotNull(exception); + Assert.assertEquals("Test error message", exception.getMessage()); + Assert.assertEquals(retCode, exception.getRetCode()); + Assert.assertEquals(100, exception.getRetCode().getCode()); + } + + @Test + public void testConstructorWithCodeAndMessage() { + int code = 200; + String message = "Custom error"; + TransactionBaseException exception = new TransactionBaseException(code, message); + + Assert.assertNotNull(exception); + Assert.assertEquals(message, exception.getMessage()); + Assert.assertNotNull(exception.getRetCode()); + Assert.assertEquals(code, exception.getRetCode().getCode()); + Assert.assertEquals(message, exception.getRetCode().getMessage()); + } + + @Test + public void testGetRetCode() { + RetCode retCode = new RetCode(404, "Not found"); + TransactionBaseException exception = new TransactionBaseException(retCode); + + RetCode returnedRetCode = exception.getRetCode(); + Assert.assertNotNull(returnedRetCode); + Assert.assertEquals(404, returnedRetCode.getCode()); + Assert.assertEquals("Not found", returnedRetCode.getMessage()); + } + + @Test + public void testExceptionIsThrowable() { + TransactionBaseException exception = new TransactionBaseException(100, "Test"); + Assert.assertTrue(exception instanceof Exception); + Assert.assertTrue(exception instanceof Throwable); + } + + @Test + public void testExceptionCanBeCaught() { + try { + throw new TransactionBaseException(500, "Server error"); + } catch (TransactionBaseException e) { + Assert.assertEquals("Server error", e.getMessage()); + Assert.assertEquals(500, e.getRetCode().getCode()); + } catch (Exception e) { + Assert.fail("Should have caught TransactionBaseException specifically"); + } + } + + @Test + public void testSerialVersionUID() { + // Test that the exception is serializable + TransactionBaseException exception = new TransactionBaseException(100, "Test"); + Assert.assertTrue(exception instanceof java.io.Serializable); + } + + @Test + public void testNegativeErrorCode() { + int code = -1; + String message = "Negative error code"; + TransactionBaseException exception = new TransactionBaseException(code, message); + + Assert.assertEquals(code, exception.getRetCode().getCode()); + Assert.assertEquals(message, exception.getMessage()); + } + + @Test + public void testZeroErrorCode() { + TransactionBaseException exception = new TransactionBaseException(0, "Success"); + Assert.assertEquals(0, exception.getRetCode().getCode()); + } + + @Test + public void testEmptyMessage() { + TransactionBaseException exception = new TransactionBaseException(100, ""); + Assert.assertNotNull(exception); + Assert.assertEquals("", exception.getMessage()); + } + + @Test + public void testRetCodeWithNullMessage() { + RetCode retCode = new RetCode(100, null); + TransactionBaseException exception = new TransactionBaseException(retCode); + Assert.assertNull(exception.getMessage()); + Assert.assertEquals(retCode, exception.getRetCode()); + } +}