From 665d209f5753350c24757830c830ee2b755899e8 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Mon, 27 May 2024 22:26:14 +0700 Subject: [PATCH] :white_check_mark: test: write unit test #5 --- .../groovy/org/unify4j/common/Numeric4j.java | 2 +- plugin/src/main/resources/sample.txt | 1 + .../test/groovy/org/unify4j/Class4jTest.java | 76 ++++++++++++++++++ .../groovy/org/unify4j/Encryption4jTest.java | 71 ++++++++++++++++ .../groovy/org/unify4j/Numeric4jTest.java | 77 ++++++++++++++++++ .../groovy/org/unify4j/Request4jTest.java | 80 +++++++++++++++++++ 6 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 plugin/src/main/resources/sample.txt create mode 100644 plugin/src/test/groovy/org/unify4j/Class4jTest.java create mode 100644 plugin/src/test/groovy/org/unify4j/Encryption4jTest.java create mode 100644 plugin/src/test/groovy/org/unify4j/Numeric4jTest.java create mode 100644 plugin/src/test/groovy/org/unify4j/Request4jTest.java diff --git a/plugin/src/main/groovy/org/unify4j/common/Numeric4j.java b/plugin/src/main/groovy/org/unify4j/common/Numeric4j.java index 871d0f8..28b0806 100644 --- a/plugin/src/main/groovy/org/unify4j/common/Numeric4j.java +++ b/plugin/src/main/groovy/org/unify4j/common/Numeric4j.java @@ -306,7 +306,7 @@ public static Number tryNumeric(String numStr) { } } if (hasDecimalPoint || hasExponent) { - if (mantissaSize < 17 && (exponentValue.length() == 0 || Math.abs(Integer.parseInt(exponentValue.toString())) < 308)) { + if (mantissaSize < 17 && (String4j.isEmpty(exponentValue) || Math.abs(Integer.parseInt(exponentValue.toString())) < 308)) { return Double.parseDouble(numStr); } else { return new BigDecimal(numStr); diff --git a/plugin/src/main/resources/sample.txt b/plugin/src/main/resources/sample.txt new file mode 100644 index 0000000..80e6792 --- /dev/null +++ b/plugin/src/main/resources/sample.txt @@ -0,0 +1 @@ +@Author: pnguyen215 \ No newline at end of file diff --git a/plugin/src/test/groovy/org/unify4j/Class4jTest.java b/plugin/src/test/groovy/org/unify4j/Class4jTest.java new file mode 100644 index 0000000..cbf1510 --- /dev/null +++ b/plugin/src/test/groovy/org/unify4j/Class4jTest.java @@ -0,0 +1,76 @@ +package org.unify4j; + +import org.junit.Test; +import org.unify4j.common.Class4j; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +public class Class4jTest { + + @Test + public void testComputeInheritanceDistance_sameClass() { + assertEquals(0, Class4j.computeInheritanceDistance(Integer.class, Integer.class)); + } + + @Test + public void testComputeInheritanceDistance_superclass() { + assertEquals(1, Class4j.computeInheritanceDistance(Integer.class, Number.class)); + } + + @Test + public void testComputeInheritanceDistance_interface() { + assertEquals(1, Class4j.computeInheritanceDistance(HashMap.class, Map.class)); + } + + @Test + public void testComputeInheritanceDistance_unrelatedClasses() { + assertEquals(-1, Class4j.computeInheritanceDistance(Integer.class, String.class)); + } + + @Test + public void testIsPrimitive() { + assertTrue(Class4j.isPrimitive(int.class)); + assertTrue(Class4j.isPrimitive(Integer.class)); + assertFalse(Class4j.isPrimitive(String.class)); + } + + @Test + public void testForName() { + assertEquals(String.class, Class4j.forName("java.lang.String", ClassLoader.getSystemClassLoader())); + assertEquals(Date.class, Class4j.forName("java.util.Date", ClassLoader.getSystemClassLoader())); + assertNull(Class4j.forName("non.existent.Class", ClassLoader.getSystemClassLoader())); + } + + @Test + public void testIsClassFinal() { + assertTrue(Class4j.isClassFinal(String.class)); + assertFalse(Class4j.isClassFinal(HashMap.class)); + } + + @Test + public void testAreAllConstructorsPrivate() { + assertTrue(Class4j.areAllConstructorsPrivate(PrivateConstructorsClass.class)); + assertFalse(Class4j.areAllConstructorsPrivate(String.class)); + } + + @Test + public void testToPrimitiveWrapperClass() { + assertEquals(Integer.class, Class4j.toPrimitiveWrapperClass(int.class)); + assertEquals(String.class, Class4j.toPrimitiveWrapperClass(String.class)); + } + + @Test + public void testDoesOneWrapTheOther() { + assertTrue(Class4j.doesOneWrapTheOther(Integer.class, int.class)); + assertFalse(Class4j.doesOneWrapTheOther(String.class, int.class)); + } + + private static class PrivateConstructorsClass { + private PrivateConstructorsClass() { + } + } +} diff --git a/plugin/src/test/groovy/org/unify4j/Encryption4jTest.java b/plugin/src/test/groovy/org/unify4j/Encryption4jTest.java new file mode 100644 index 0000000..30ab891 --- /dev/null +++ b/plugin/src/test/groovy/org/unify4j/Encryption4jTest.java @@ -0,0 +1,71 @@ +package org.unify4j; + +import org.junit.Test; +import org.unify4j.common.Encryption4j; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; + +import static org.junit.Assert.*; + +public class Encryption4jTest { + + @Test + public void testCalculateMD5Hash() { + String input = "test"; + String expectedHash = "098F6BCD4621D373CADE4E832627B4F6"; // Precomputed MD5 hash of "test" + String actualHash = Encryption4j.calculateMD5Hash(input.getBytes(StandardCharsets.UTF_8)); + System.out.println(actualHash); + assertEquals(expectedHash, actualHash); + } + + @Test + public void testCalculateSHA1Hash() { + String input = "test"; + String expectedHash = "A94A8FE5CCB19BA61C4C0873D391E987982FBBD3"; // Precomputed SHA-1 hash of "test" + String actualHash = Encryption4j.calculateSHA1Hash(input.getBytes(StandardCharsets.UTF_8)); + System.out.println(actualHash); + assertEquals(expectedHash, actualHash); + } + + @Test + public void testEncryptDecrypt() { + String key = "key"; + String content = "Hello, World!"; + String encrypted = Encryption4j.encrypt(key, content); + String decrypted = Encryption4j.decrypt(key, encrypted); + assertEquals(content, decrypted); + } + + @Test + public void testFastMD5() { + File file = new File("src/main/resources/sample.txt"); + assertTrue(file.exists()); + String expectedHash = "6FEA7ECEC6AA4622AFA8DB071B426F35"; // Precomputed MD5 hash of file content + String actualHash = Encryption4j.fastMD5(file); + System.out.println(actualHash); + assertEquals(expectedHash, actualHash); + } + + @Test + public void testFastSHA256() { + File file = new File("src/main/resources/sample.txt"); + assertTrue(file.exists()); + String expectedHash = "6F78263FD0F8D1FD47747ED9B9AAE6476E6A560FADA212A6A2174E91AD063CF2"; // Precomputed SHA-256 hash of file content + String actualHash = Encryption4j.fastSHA256(file); + System.out.println(actualHash); + assertEquals(expectedHash, actualHash); + } + + @Test + public void testGetDigest() { + MessageDigest md5Digest = Encryption4j.getDigest("MD5"); + assertNotNull(md5Digest); + assertEquals("MD5", md5Digest.getAlgorithm()); + + MessageDigest sha256Digest = Encryption4j.getDigest("SHA-256"); + assertNotNull(sha256Digest); + assertEquals("SHA-256", sha256Digest.getAlgorithm()); + } +} diff --git a/plugin/src/test/groovy/org/unify4j/Numeric4jTest.java b/plugin/src/test/groovy/org/unify4j/Numeric4jTest.java new file mode 100644 index 0000000..db61ba6 --- /dev/null +++ b/plugin/src/test/groovy/org/unify4j/Numeric4jTest.java @@ -0,0 +1,77 @@ +package org.unify4j; + +import org.junit.Test; +import org.unify4j.common.Numeric4j; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; + +public class Numeric4jTest { + @Test + public void testCurrencyWithLocale() { + assertEquals("$1,234.56", Numeric4j.currency(1234.56, Locale.US)); + } + + @Test + public void testCurrencyWithLanguageAndCountry() { + assertEquals("$1,234.56", Numeric4j.currency(1234.56, "en", "US")); + } + + @Test + public void testCurrencyWithGrouping() { + assertEquals("$1,234.56", Numeric4j.currency(1234.56, Locale.US, true)); + assertEquals("$1234.56", Numeric4j.currency(1234.56, Locale.US, false)); + } + + @Test + public void testCurrencyWithPattern() { + assertEquals("1,234.5600", Numeric4j.currency(1234.56, 4, "#,##0.0000", Locale.US)); + } + + @Test + public void testCurrencyWithPrecision() { + assertEquals("$1,234.5600", Numeric4j.currency(1234.56, 4, Locale.US)); + } + + @Test + public void testMinimumLong() { + assertEquals(1L, Numeric4j.minimum(3L, 5L, 1L, 4L)); + } + + @Test + public void testMaximumLong() { + assertEquals(5L, Numeric4j.maximum(3L, 5L, 1L, 4L)); + } + + @Test + public void testMinimumBigInteger() { + assertEquals(BigInteger.ONE, Numeric4j.minimum(BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.ONE, BigInteger.valueOf(4))); + } + + @Test + public void testMaximumBigInteger() { + assertEquals(BigInteger.valueOf(5), Numeric4j.maximum(BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.ONE, BigInteger.valueOf(4))); + } + + @Test + public void testMinimumBigDecimal() { + assertEquals(BigDecimal.ONE, Numeric4j.minimum(BigDecimal.valueOf(3), BigDecimal.valueOf(5), BigDecimal.ONE, BigDecimal.valueOf(4))); + } + + @Test + public void testMaximumBigDecimal() { + assertEquals(BigDecimal.valueOf(5), Numeric4j.maximum(BigDecimal.valueOf(3), BigDecimal.valueOf(5), BigDecimal.ONE, BigDecimal.valueOf(4))); + } + + @Test + public void testTryNumeric() { + assertEquals(123L, Numeric4j.tryNumeric("123")); + assertEquals(-123L, Numeric4j.tryNumeric("-123")); + assertEquals(123.45, Numeric4j.tryNumeric("123.45")); + assertEquals(new BigInteger("12345678901234567890"), Numeric4j.tryNumeric("12345678901234567890")); + assertEquals(new BigDecimal("1234567890.1234567890"), Numeric4j.tryNumeric("1234567890.1234567890")); + } +} diff --git a/plugin/src/test/groovy/org/unify4j/Request4jTest.java b/plugin/src/test/groovy/org/unify4j/Request4jTest.java new file mode 100644 index 0000000..fde8898 --- /dev/null +++ b/plugin/src/test/groovy/org/unify4j/Request4jTest.java @@ -0,0 +1,80 @@ +package org.unify4j; + +import org.junit.Test; +import org.unify4j.common.Request4j; + +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +public class Request4jTest { + + @Test + public void testAppendQueryParams() throws UnsupportedEncodingException { + String url = "http://example.com"; + Map params = new HashMap<>(); + params.put("key1", "value1"); + params.put("key2", "value2"); + + String result = Request4j.appendQueryParams(url, params); + assertTrue(result.contains("key1=value1")); + assertTrue(result.contains("key2=value2")); + assertTrue(result.startsWith("http://example.com?")); + } + + @Test + public void testAppendQueryParams_emptyParams() throws UnsupportedEncodingException { + String url = "http://example.com"; + Map params = new HashMap<>(); + + String result = Request4j.appendQueryParams(url, params); + assertEquals(url, result); + } + + @Test + public void testGetQueryParamsFromUrl() throws Exception { + URL url = new URL("http://example.com?key1=value1&key2=value2"); + Map params = Request4j.getQueryParams(url); + + assertEquals(2, params.size()); + assertEquals("value1", params.get("key1")); + assertEquals("value2", params.get("key2")); + } + + @Test + public void testGetQueryParams_emptyUrl() { + Map params = Request4j.getQueryParams((URL) null); + assertTrue(params.isEmpty()); + } + + @Test + public void testGetUrlWithoutParameters() { + String url = "http://example.com?key1=value1&key2=value2"; + String result = Request4j.getUrlWithoutParameters(url); + assertEquals("http://example.com", result); + } + + @Test + public void testGetUrlWithoutParameters_noParams() { + String url = "http://example.com"; + String result = Request4j.getUrlWithoutParameters(url); + assertEquals(url, result); + } + + @Test + public void testCanConnect() { + String url = "http://www.google.com"; // Assuming this URL is always reachable + boolean canConnect = Request4j.canConnect(url); + assertTrue(canConnect); + } + + @Test + public void testCanConnect_invalidUrl() { + String url = "http://invalid.url"; + boolean canConnect = Request4j.canConnect(url); + assertFalse(canConnect); + } +}