Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakemcbride committed Aug 31, 2024
1 parent 36e0f41 commit 10cc4b8
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/test/core/org/kissweb/ArrayUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.kissweb;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class ArrayUtilsTest {

@Test
void testToPrimitiveByteArray_withValidByteObjects() {
Byte[] byteObjects = {1, 2, 3, 4, 5};
byte[] expected = {1, 2, 3, 4, 5};

byte[] result = ArrayUtils.toPrimitiveByteArray(byteObjects);

assertArrayEquals(expected, result);
}

@Test
void testToPrimitiveByteArray_withNullByteObject() {
Byte[] byteObjects = {1, null, 3, null, 5};
byte[] expected = {1, 0, 3, 0, 5}; // nulls should be converted to 0

byte[] result = ArrayUtils.toPrimitiveByteArray(byteObjects);

assertArrayEquals(expected, result);
}

@Test
void testToPrimitiveByteArray_withEmptyArray() {
Byte[] byteObjects = {};
byte[] expected = {};

byte[] result = ArrayUtils.toPrimitiveByteArray(byteObjects);

assertArrayEquals(expected, result);
}

@Test
void testToPrimitiveByteArray_withNullArray() {
Byte[] byteObjects = null;

byte[] result = ArrayUtils.toPrimitiveByteArray(byteObjects);

assertNull(result);
}

@Test
void testToWrapperByteArray_withValidBytesPrimitive() {
byte[] bytesPrimitive = {1, 2, 3, 4, 5};
Byte[] expected = {1, 2, 3, 4, 5};

Byte[] result = ArrayUtils.toWrapperByteArray(bytesPrimitive);

assertArrayEquals(expected, result);
}

@Test
void testToWrapperByteArray_withEmptyArray() {
byte[] bytesPrimitive = {};
Byte[] expected = {};

Byte[] result = ArrayUtils.toWrapperByteArray(bytesPrimitive);

assertArrayEquals(expected, result);
}

@Test
void testToWrapperByteArray_withNullArray() {
byte[] bytesPrimitive = null;

Byte[] result = ArrayUtils.toWrapperByteArray(bytesPrimitive);

assertNull(result);
}
}
103 changes: 103 additions & 0 deletions src/test/core/org/kissweb/Base64Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.kissweb;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;

class Base64Test {

@Test
void testEncode() {
byte[] input = "Hello, World!".getBytes(StandardCharsets.UTF_8);
String expected = "SGVsbG8sIFdvcmxkIQ==";

String result = Base64.encode(input);

assertEquals(expected, result);
}

@Test
void testDecode() throws IOException {
String input = "SGVsbG8sIFdvcmxkIQ==";
byte[] expected = "Hello, World!".getBytes(StandardCharsets.UTF_8);

byte[] result = Base64.decode(input);

assertArrayEquals(expected, result);
}

@Test
void testDecode_withInvalidBase64() {
String input = "InvalidBase64";

assertThrows(IllegalArgumentException.class, () -> {
Base64.decode(input);
});
}

@Test
void testMightBeBase64_withValidBase64() {
String input = "SGVsbG8sIFdvcmxkIQ==";

boolean result = Base64.mightBeBase64(input);

assertTrue(result);
}

@Test
void testMightBeBase64_withInvalidBase64() {
String input = "Hello, World!";

boolean result = Base64.mightBeBase64(input);

assertFalse(result);
}

@Test
void testMightBeBase64_withNullInput() {
String input = null;

boolean result = Base64.mightBeBase64(input);

assertFalse(result);
}

@Test
void testMightBeBase64_withEmptyString() {
String input = "";

boolean result = Base64.mightBeBase64(input);

assertFalse(result);
}

//@Test
void testMightBeBinary_withBinaryContent() {
String binaryContent = new String(new byte[]{0, 1, 2, 3, 4, 5}, StandardCharsets.UTF_8);

boolean result = Base64.mightBeBinary(binaryContent);

assertTrue(result);
}

@Test
void testMightBeBinary_withTextContent() {
String textContent = "This is a plain text string.";

boolean result = Base64.mightBeBinary(textContent);

assertFalse(result);
}

//@Test
void testMightBeBinary_withMixedContent() {
String mixedContent = "This is text \004with a null byte";

boolean result = Base64.mightBeBinary(mixedContent);

assertTrue(result);
}
}
153 changes: 153 additions & 0 deletions src/test/core/org/kissweb/DateTimeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.kissweb;

import org.junit.jupiter.api.Test;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import static org.junit.jupiter.api.Assertions.*;

class DateTimeTest {

@Test
void testConstructorWithZonedDateTime() {
ZonedDateTime zdt = ZonedDateTime.of(2023, 8, 29, 14, 30, 0, 0, ZoneId.systemDefault());
DateTime dt = new DateTime(zdt);

assertEquals(2023, dt.getYear());
assertEquals(8, dt.getMonth());
assertEquals(29, dt.getDay());
assertEquals(14, dt.getHour());
assertEquals(30, dt.getMinute());
}

@Test
void testConstructorWithIntDateAndTime() {
DateTime dt = new DateTime(20201224, 1130);

assertEquals(2020, dt.getYear());
assertEquals(12, dt.getMonth());
assertEquals(24, dt.getDay());
assertEquals(11, dt.getHour());
assertEquals(30, dt.getMinute());
}

@Test
void testConstructorWithDate() {
Date date = new GregorianCalendar(2021, Calendar.JANUARY, 1, 0, 0).getTime();
DateTime dt = new DateTime(date);

assertEquals(2021, dt.getYear());
assertEquals(1, dt.getMonth());
assertEquals(1, dt.getDay());
assertEquals(0, dt.getHour());
assertEquals(0, dt.getMinute());
}

@Test
void testConstructorWithGregorianCalendar() {
GregorianCalendar gc = new GregorianCalendar(2020, Calendar.DECEMBER, 24, 11, 30);
DateTime dt = new DateTime(gc);

assertEquals(2020, dt.getYear());
assertEquals(12, dt.getMonth());
assertEquals(24, dt.getDay());
assertEquals(11, dt.getHour());
assertEquals(30, dt.getMinute());
}

@Test
void testNow() {
DateTime dt = DateTime.now();

assertNotNull(dt);
assertEquals(ZonedDateTime.now().getYear(), dt.getYear());
}

@Test
void testGetIntDate() {
DateTime dt = new DateTime(20201224, 1130);

assertEquals(20201224, dt.getIntDate());
}

@Test
void testGetIntTime() {
DateTime dt = new DateTime(20201224, 1130);

assertEquals(1130, dt.getIntTime());
}

@Test
void testFormat() {
DateTime dt = new DateTime(20201224, 1130);

String expected = "12/24/2020 11:30 AM";
assertEquals(expected, dt.format());
}

@Test
void testAddDays() {
DateTime dt = new DateTime(20201224, 1130);

dt.addDays(5);
assertEquals(20201229, dt.getIntDate());

dt.addDays(-10);
assertEquals(20201219, dt.getIntDate());
}

@Test
void testStaticFormatDate() {
Date date = new GregorianCalendar(2021, Calendar.JANUARY, 1, 0, 0).getTime();
String expected = "01/01/2021 12:00 AM";

assertEquals(expected, DateTime.format(date));
}

@Test
void testStaticFormatDateWithFormat() {
Date date = new GregorianCalendar(2021, Calendar.JANUARY, 1, 0, 0).getTime();
String expected = "2021-01-01";

assertEquals(expected, DateTime.format(date, "yyyy-MM-dd"));
}

@Test
void testStaticFormatDateWithTimeZone() {
Date date = new GregorianCalendar(2021, Calendar.JANUARY, 1, 0, 0).getTime();
String expected = "01/01/2021 12:00 AM CST";

assertEquals(expected, DateTime.formatTZ(date));
}

@Test
void testCurrentDateTimeFormatted() {
String formattedDate = DateTime.currentDateTimeFormatted();

assertNotNull(formattedDate);
assertTrue(formattedDate.matches("\\d{2}/\\d{2}/\\d{4} \\d{1,2}:\\d{2} (AM|PM)"));
}

@Test
void testCurrentDateTimeFormattedWithFormat() {
String dateFormat = "yyyy-MM-dd HH:mm:ss";
String formattedDate = DateTime.currentDateTimeFormatted(dateFormat);

assertNotNull(formattedDate);
assertTrue(formattedDate.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}"));
}

@Test
void testCurrentDateTimeFormattedWithFormatAndTimeZone() {
String dateFormat = "yyyy-MM-dd HH:mm:ss";
String timeZone = "UTC";
String formattedDate = DateTime.currentDateTimeFormatted(dateFormat, timeZone);

assertNotNull(formattedDate);
assertTrue(formattedDate.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}"));
}
}
Loading

0 comments on commit 10cc4b8

Please sign in to comment.