Skip to content

Commit

Permalink
✅ test: write unit test #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed May 27, 2024
1 parent 5be14d3 commit 665d209
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugin/src/main/groovy/org/unify4j/common/Numeric4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions plugin/src/main/resources/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@Author: pnguyen215
76 changes: 76 additions & 0 deletions plugin/src/test/groovy/org/unify4j/Class4jTest.java
Original file line number Diff line number Diff line change
@@ -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() {
}
}
}
71 changes: 71 additions & 0 deletions plugin/src/test/groovy/org/unify4j/Encryption4jTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
77 changes: 77 additions & 0 deletions plugin/src/test/groovy/org/unify4j/Numeric4jTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
80 changes: 80 additions & 0 deletions plugin/src/test/groovy/org/unify4j/Request4jTest.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String, Object> 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<String, String> params = Request4j.getQueryParams(url);

assertEquals(2, params.size());
assertEquals("value1", params.get("key1"));
assertEquals("value2", params.get("key2"));
}

@Test
public void testGetQueryParams_emptyUrl() {
Map<String, String> 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);
}
}

0 comments on commit 665d209

Please sign in to comment.