Skip to content

Commit

Permalink
Moved over tests for ListOf...SetOf...and ClassUtilities
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 26, 2024
1 parent 9723bef commit 5d2e406
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 62 deletions.
71 changes: 71 additions & 0 deletions src/test/java/com/cedarsoftware/util/ClassUtilitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,75 @@ public void testPrimitives() {
assert 1 == ClassUtilities.computeInheritanceDistance(java.sql.Date.class, Date.class);
}

@Test
public void testClassForName()
{
Class<?> testObjectClass = ClassUtilities.forName(SubClass.class.getName(), ClassUtilities.class.getClassLoader());
assert testObjectClass instanceof Class<?>;
assert SubClass.class.getName().equals(testObjectClass.getName());
}

@Test
public void testClassForNameWithClassloader()
{
Class<?> testObjectClass = ClassUtilities.forName("ReallyLong", new AlternateNameClassLoader("ReallyLong", Long.class));
assert testObjectClass instanceof Class<?>;
assert "java.lang.Long".equals(testObjectClass.getName());
}

@Test
public void testClassForNameNullClassErrorHandling()
{
assert null == ClassUtilities.forName(null, ClassUtilities.class.getClassLoader());
assert null == ClassUtilities.forName("Smith&Wesson", ClassUtilities.class.getClassLoader());
}

@Test
public void testClassForNameFailOnClassLoaderErrorTrue()
{
assert null == ClassUtilities.forName("foo.bar.baz.Qux", ClassUtilities.class.getClassLoader());
}

@Test
public void testClassForNameFailOnClassLoaderErrorFalse()
{
Class<?> testObjectClass = ClassUtilities.forName("foo.bar.baz.Qux", ClassUtilities.class.getClassLoader());
assert testObjectClass == null;
}

private static class AlternateNameClassLoader extends ClassLoader
{
AlternateNameClassLoader(String alternateName, Class<?> clazz)
{
super(AlternateNameClassLoader.class.getClassLoader());
this.alternateName = alternateName;
this.clazz = clazz;
}

public Class<?> loadClass(String className) throws ClassNotFoundException
{
return findClass(className);
}

protected Class<?> findClass(String className)
{
try
{
return findSystemClass(className);
}
catch (Exception ignored)
{ }

if (alternateName.equals(className))
{
return Long.class;
}

return null;
}

private final String alternateName;
private final Class<?> clazz;
}

}
105 changes: 105 additions & 0 deletions src/test/java/com/cedarsoftware/util/CollectionUtilitiesTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.cedarsoftware.util;

import com.cedarsoftware.util.io.MetaUtils;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CollectionUtilitiesTests {
static class Rec {
final String s;
final int i;
Rec(String s, int i) {
this.s = s;
this.i = i;
}

Rec link;
List<Rec> ilinks;
List<Rec> mlinks;

Map<String, Rec> smap;
}

@Test
void testListOf() {
final List<String> list = CollectionUtilities.listOf();
assertEquals(0, list.size());
}

@Test
void testListOf_producesImmutableList() {
final List<String> list = CollectionUtilities.listOf();
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> list.add("One"));
}

@Test
void testListOfOne() {
final List<String> list = CollectionUtilities.listOf("One");
assertEquals(1, list.size());
assertEquals("One", list.get(0));
}

@Test
void testListOfTwo() {
final List<String> list = CollectionUtilities.listOf("One", "Two");
assertEquals(2, list.size());
assertEquals("One", list.get(0));
assertEquals("Two", list.get(1));
}

@Test
void testListOfThree() {
final List<String> list = CollectionUtilities.listOf("One", "Two", "Three");
assertEquals(3, list.size());
assertEquals("One", list.get(0));
assertEquals("Two", list.get(1));
assertEquals("Three", list.get(2));
}

@Test
void testSetOf() {
final Set<?> set = CollectionUtilities.setOf();
assertEquals(0, set.size());
}

@Test
void testSetOf_producesImmutableSet() {
final Set<String> set = CollectionUtilities.setOf();
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> set.add("One"));
}


@Test
void testSetOfOne() {
final Set<String> set = CollectionUtilities.setOf("One");
assertEquals(1, set.size());
assertTrue(set.contains("One"));
}

@Test
public void testSetOfTwo() {
final Set<String> set = CollectionUtilities.setOf("One", "Two");
assertEquals(2, set.size());
assertTrue(set.contains("One"));
assertTrue(set.contains("Two"));
}

@Test
public void testSetOfThree() {
final Set<String> set = CollectionUtilities.setOf("One", "Two", "Three");
assertEquals(3, set.size());
assertTrue(set.contains("One"));
assertTrue(set.contains("Two"));
assertTrue(set.contains("Three"));
}
}
147 changes: 85 additions & 62 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,52 @@ void testZonedDateTimeToLocalDateTime(long epochMilli, ZoneId zoneId, LocalDateT
}


@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testZonedDateTimeToLocalTime(long epochMilli, ZoneId zoneId, LocalDateTime expected)
{
ZonedDateTime time = Instant.ofEpochMilli(epochMilli).atZone(zoneId);

LocalTime actual = this.converter.convert(time, LocalTime.class, createConvertOptions(zoneId, zoneId));

assertThat(actual).isEqualTo(expected.toLocalTime());
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testZonedDateTimeToLocalDate(long epochMilli, ZoneId zoneId, LocalDateTime expected)
{
ZonedDateTime time = Instant.ofEpochMilli(epochMilli).atZone(zoneId);

LocalDate actual = this.converter.convert(time, LocalDate.class, createConvertOptions(zoneId, zoneId));

assertThat(actual).isEqualTo(expected.toLocalDate());
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testZonedDateTimeToInstant(long epochMilli, ZoneId zoneId, LocalDateTime expected)
{
ZonedDateTime time = Instant.ofEpochMilli(epochMilli).atZone(zoneId);

Instant actual = this.converter.convert(time, Instant.class, createConvertOptions(zoneId, zoneId));

assertThat(actual).isEqualTo(time.toInstant());
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testZonedDateTimeToCalendar(long epochMilli, ZoneId zoneId, LocalDateTime expected)
{
ZonedDateTime time = Instant.ofEpochMilli(epochMilli).atZone(zoneId);

Calendar actual = this.converter.convert(time, Calendar.class, createConvertOptions(zoneId, zoneId));

assertThat(actual.getTime().getTime()).isEqualTo(time.toInstant().toEpochMilli());
assertThat(actual.getTimeZone()).isEqualTo(TimeZone.getTimeZone(zoneId));
}


@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testZonedDateTimeToLong(long epochMilli, ZoneId zoneId, LocalDateTime localDateTime)
Expand Down Expand Up @@ -1492,7 +1538,18 @@ void testLocalDateTimeToLong(long epochMilli, ZoneId sourceZoneId, LocalDateTime
long milli = this.converter.convert(initial, long.class, createConvertOptions(sourceZoneId, targetZoneId));
assertThat(milli).isEqualTo(epochMilli);

LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(sourceZoneId, targetZoneId));
LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(null, targetZoneId));
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateTimeConversion_params")
void testLocalDateTimeToInstant(long epochMilli, ZoneId sourceZoneId, LocalDateTime initial, ZoneId targetZoneId, LocalDateTime expected)
{
Instant intermediate = this.converter.convert(initial, Instant.class, createConvertOptions(sourceZoneId, targetZoneId));
assertThat(intermediate.toEpochMilli()).isEqualTo(epochMilli);

LocalDateTime actual = this.converter.convert(intermediate, LocalDateTime.class, createConvertOptions(null, targetZoneId));
assertThat(actual).isEqualTo(expected);
}

Expand All @@ -1503,18 +1560,38 @@ void testLocalDateTimeToAtomicLong(long epochMilli, ZoneId sourceZoneId, LocalDa
AtomicLong milli = this.converter.convert(initial, AtomicLong.class, createConvertOptions(sourceZoneId, targetZoneId));
assertThat(milli.longValue()).isEqualTo(epochMilli);

LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(sourceZoneId, targetZoneId));
LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(null, targetZoneId));
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateTimeConversion_params")
void testLocalDateTimeToZonedDateTime(long epochMilli, ZoneId sourceZoneId, LocalDateTime initial, ZoneId targetZoneId, LocalDateTime expected)
{
ZonedDateTime intermediate = this.converter.convert(initial, ZonedDateTime.class, createConvertOptions(sourceZoneId, targetZoneId));
assertThat(intermediate.toInstant().toEpochMilli()).isEqualTo(epochMilli);

LocalDateTime actual = this.converter.convert(intermediate, LocalDateTime.class, createConvertOptions(null, targetZoneId));
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateTimeConversion_params")
void testLocalDateTimeToLocalTime(long epochMilli, ZoneId sourceZoneId, LocalDateTime initial, ZoneId targetZoneId, LocalDateTime expected)
{
LocalTime intermediate = this.converter.convert(initial, LocalTime.class, createConvertOptions(sourceZoneId, targetZoneId));

assertThat(intermediate).isEqualTo(expected.toLocalTime());
}

@ParameterizedTest
@MethodSource("localDateTimeConversion_params")
void testLocalDateTimeToBigInteger(long epochMilli, ZoneId sourceZoneId, LocalDateTime initial, ZoneId targetZoneId, LocalDateTime expected)
{
BigInteger milli = this.converter.convert(initial, BigInteger.class, createConvertOptions(sourceZoneId, targetZoneId));
assertThat(milli.longValue()).isEqualTo(epochMilli);

LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(sourceZoneId, targetZoneId));
LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(null, targetZoneId));
assertThat(actual).isEqualTo(expected);

}
Expand All @@ -1526,7 +1603,7 @@ void testLocalDateTimeToBigDecimal(long epochMilli, ZoneId sourceZoneId, LocalDa
BigDecimal milli = this.converter.convert(initial, BigDecimal.class, createConvertOptions(sourceZoneId, targetZoneId));
assertThat(milli.longValue()).isEqualTo(epochMilli);

LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(sourceZoneId, targetZoneId));
LocalDateTime actual = this.converter.convert(milli, LocalDateTime.class, createConvertOptions(null, targetZoneId));
assertThat(actual).isEqualTo(expected);
}

Expand Down Expand Up @@ -2379,66 +2456,12 @@ void testConversions_whenClassTypeMatchesObjectType_stillCreatesNewObject(Object
}

@Test
void testLocalDateTimeToOthers()
{
// String to LocalDateTime
// String strDate = this.converter.convert(now, String.class);
// localDateTime = this.converter.convert(strDate, LocalDateTime.class);
// String strDate2 = this.converter.convert(localDateTime, String.class);
// assert strDate.startsWith(strDate2);
//
// // Other direction --> LocalDateTime to other date types
//
// // LocalDateTime to Date
// localDateTime = this.converter.convert(now, LocalDateTime.class);
// Date date = this.converter.convert(localDateTime, Date.class);
// assertEquals(localDateTimeToMillis(localDateTime), date.getTime());
//
// // LocalDateTime to SqlDate
// sqlDate = this.converter.convert(localDateTime, java.sql.Date.class);
// assertEquals(localDateTimeToMillis(localDateTime), sqlDate.getTime());
//
// // LocalDateTime to Timestamp
// timestamp = this.converter.convert(localDateTime, Timestamp.class);
// assertEquals(localDateTimeToMillis(localDateTime), timestamp.getTime());
//
// // LocalDateTime to Long
// long tnow = this.converter.convert(localDateTime, long.class);
// assertEquals(localDateTimeToMillis(localDateTime), tnow);
//
// // LocalDateTime to AtomicLong
// atomicLong = this.converter.convert(localDateTime, AtomicLong.class);
// assertEquals(localDateTimeToMillis(localDateTime), atomicLong.get());
//
// // LocalDateTime to String
// strDate = this.converter.convert(localDateTime, String.class);
// strDate2 = this.converter.convert(now, String.class);
// assert strDate2.startsWith(strDate);
//
// // LocalDateTime to BigInteger
// bigInt = this.converter.convert(localDateTime, BigInteger.class);
// assertEquals(now.getTime(), bigInt.longValue());
//
// // LocalDateTime to BigDecimal
// bigDec = this.converter.convert(localDateTime, BigDecimal.class);
// assertEquals(now.getTime(), bigDec.longValue());
//
// // Error handling
// try
// {
// this.converter.convert("2020-12-40", LocalDateTime.class);
// fail();
// }
// catch (IllegalArgumentException e)
// {
// TestUtil.assertContainsIgnoreCase(e.getMessage(), "day must be between 1 and 31");
// }
//
// assert this.converter.convert(null, LocalDateTime.class) == null;
void testConvertStringToLocalDateTime_withParseError() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> this.converter.convert("2020-12-40", LocalDateTime.class))
.withMessageContaining("Day must be between 1 and 31");
}



@Test
void testDateErrorHandlingBadInput()
{
Expand Down

0 comments on commit 5d2e406

Please sign in to comment.