Skip to content

Commit

Permalink
More tests, fix issue with UTC support on Unix date type.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Jan 8, 2024
1 parent 5747389 commit 19eff1d
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 58 deletions.
12 changes: 10 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<!-- <maven.compiler.release>11</maven.compiler.release>-->

<!-- testing only -->
<version.junit>5.10.1</version.junit>
<version.junit-jupiter-api>5.10.1</version.junit-jupiter-api>
<version.junit-jupiter-params>5.10.1</version.junit-jupiter-params>
<version.assertj>3.25.1</version.assertj>
<version.json.io>4.19.1</version.json.io>
<version.mockito.inline>4.11.0</version.mockito.inline>
Expand Down Expand Up @@ -208,7 +209,14 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.junit}</version>
<version>${version.junit-jupiter-api}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${version.junit-jupiter-params}</version>
<scope>test</scope>
</dependency>

Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/cedarsoftware/util/MapUtilities.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.cedarsoftware.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
* Usefule utilities for Maps
Expand Down Expand Up @@ -85,4 +91,63 @@ public static <T extends Throwable> Object getOrThrow(Map<?, ?> map, Object key,
public static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}

/**
* Duplicate a map of Set to Class, possibly as unmodifiable
*
* @param other map to duplicate
* @param unmodifiable will the result be unmodifiable
* @return duplicated map
*/
public static <T> Map<Class<?>, Set<T>> dupe(Map<Class<?>, Set<T>> other, boolean unmodifiable) {
final Map<Class<?>, Set<T>> newItemsAssocToClass = new LinkedHashMap<>();
for (Map.Entry<Class<?>, Set<T>> entry : other.entrySet()) {
final Set<T> itemsAssocToClass = new LinkedHashSet<>(entry.getValue());
if (unmodifiable) {
newItemsAssocToClass.computeIfAbsent(entry.getKey(), k -> Collections.unmodifiableSet(itemsAssocToClass));
} else {
newItemsAssocToClass.computeIfAbsent(entry.getKey(), k -> itemsAssocToClass);
}
}
if (unmodifiable) {
return Collections.unmodifiableMap(newItemsAssocToClass);
} else {
return newItemsAssocToClass;
}
}

// Keeping next two methods in case we need to make certain sets unmodifiable still.
public static <T, V> Map<T, Set<V>> cloneMapOfSets(final Map<T, Set<V>> original, final boolean immutable) {
final Map<T, Set<V>> result = new HashMap<>();

for (Map.Entry<T, Set<V>> entry : original.entrySet()) {
final T key = entry.getKey();
final Set<V> value = entry.getValue();

final Set<V> clonedSet = immutable
? Collections.unmodifiableSet(value)
: new HashSet<>(value);

result.put(key, clonedSet);
}

return immutable ? Collections.unmodifiableMap(result) : result;
}

public static <T, U, V> Map<T, Map<U, V>> cloneMapOfMaps(final Map<T, Map<U, V>> original, final boolean immutable) {
final Map<T, Map<U, V>> result = new LinkedHashMap<>();

for (Map.Entry<T, Map<U, V>> entry : original.entrySet()) {
final T key = entry.getKey();
final Map<U, V> value = entry.getValue();

final Map<U, V> clonedMap = immutable
? Collections.unmodifiableMap(value)
: new LinkedHashMap<>(value);

result.put(key, clonedMap);
}

return immutable ? Collections.unmodifiableMap(result) : result;
}
}
61 changes: 39 additions & 22 deletions src/test/java/com/cedarsoftware/util/TestDateUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.lang.reflect.Modifier;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -538,24 +541,16 @@ void testDatePrecision()
assertTrue(x.compareTo(y) < 0);
}

@Test
void testTimeZoneValidShortNames() {
@ParameterizedTest
@ValueSource(strings = {"JST", "IST", "CET", "BST", "EST", "CST", "MST", "PST", "CAT", "EAT", "ART", "ECT", "NST", "AST", "HST"})
void testTimeZoneValidShortNames(String timeZoneId) {
// Support for some of the oldie but goodies (when the TimeZone returned does not have a 0 offset)
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 JST"); // Japan
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 IST"); // India
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 CET"); // France
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 BST"); // British Summer Time
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 EST"); // Eastern Standard
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 CST"); // Central Standard
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 MST"); // Mountain Standard
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 PST"); // Pacific Standard
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 CAT"); // Central Africa Time
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 EAT"); // Eastern Africa Time
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 ART"); // Argentina Time
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 ECT"); // Ecuador Time
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 NST"); // Newfoundland Time
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 AST"); // Atlantic Standard Time
DateUtilities.parseDate("2021-01-13T13:01:54.6747552 HST"); // Hawaii Standard Time
Date date = DateUtilities.parseDate("2021-01-13T13:01:54.6747552 " + timeZoneId);
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(timeZoneId));
calendar.clear();
calendar.set(2021, Calendar.JANUARY, 13, 13, 1, 54);
assert date.getTime() - calendar.getTime().getTime() == 674; // less than 1000 millis
}

@Test
Expand Down Expand Up @@ -679,16 +674,16 @@ void testParseErrors()
} catch (Exception ignored) {}
}

@Test
void testMacUnixDateFormat()
@ParameterizedTest
@ValueSource(strings = {"JST", "IST", "CET", "BST", "EST", "CST", "MST", "PST", "CAT", "EAT", "ART", "ECT", "NST", "AST", "HST"})
void testMacUnixDateFormat(String timeZoneId)
{
Date date = DateUtilities.parseDate("Sat Jan 6 20:06:58 EST 2024");
Date date = DateUtilities.parseDate("Sat Jan 6 20:06:58 " + timeZoneId + " 2024");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(timeZoneId));
calendar.clear();
calendar.set(2024, Calendar.JANUARY, 6, 20, 6, 58);
assertEquals(calendar.getTime(), date);
Date date2 = DateUtilities.parseDate("Sat Jan 6 20:06:58 PST 2024");
assertEquals(date2.getTime(), date.getTime() + 3*60*60*1000);
}

@Test
Expand All @@ -712,4 +707,26 @@ void testInconsistentDateSeparators()
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Unable to parse: 1996-12/24 as a date");
}

@Test
void testBadTimeSeparators()
{
assertThatThrownBy(() -> DateUtilities.parseDate("12/24/1996 12.49.58"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Issue parsing data/time, other characters present: 12.49.58");

assertThatThrownBy(() -> DateUtilities.parseDate("12.49.58 12/24/1996"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Issue parsing data/time, other characters present: 12.49.58");

Date date = DateUtilities.parseDate("12:49:58 12/24/1996"); // time with valid separators before date
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(1996, Calendar.DECEMBER, 24, 12, 49, 58);
assertEquals(calendar.getTime(), date);

assertThatThrownBy(() -> DateUtilities.parseDate("12/24/1996 12-49-58"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Issue parsing data/time, other characters present: 12-49-58");
}
}
56 changes: 22 additions & 34 deletions src/test/java/com/cedarsoftware/util/TestUniqueIdGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
*/
public class TestUniqueIdGenerator
{
private static int bucketSize = 200000;
private static int maxIdGen = 100000;
private static final int bucketSize = 200000;

@Test
public void testIdLengths()
Expand All @@ -66,6 +65,7 @@ public void testIDtoDate()
@Test
public void testUniqueIdGeneration()
{
int maxIdGen = 100000;
int testSize = maxIdGen;
Long[] keep = new Long[testSize];
Long[] keep19 = new Long[testSize];
Expand Down Expand Up @@ -131,44 +131,32 @@ public void testConcurrency()
final Set<Long> bucketC = new LinkedHashSet<>();
final Set<Long> bucketD = new LinkedHashSet<>();

Runnable test1 = new Runnable() {
public void run()
{
await(startLatch);
fillBucket(bucket1);
fillBucket19(bucketA);
finishedLatch.countDown();
}
Runnable test1 = () -> {
await(startLatch);
fillBucket(bucket1);
fillBucket19(bucketA);
finishedLatch.countDown();
};

Runnable test2 = new Runnable() {
public void run()
{
await(startLatch);
fillBucket(bucket2);
fillBucket19(bucketB);
finishedLatch.countDown();
}
Runnable test2 = () -> {
await(startLatch);
fillBucket(bucket2);
fillBucket19(bucketB);
finishedLatch.countDown();
};

Runnable test3 = new Runnable() {
public void run()
{
await(startLatch);
fillBucket(bucket3);
fillBucket19(bucketC);
finishedLatch.countDown();
}
Runnable test3 = () -> {
await(startLatch);
fillBucket(bucket3);
fillBucket19(bucketC);
finishedLatch.countDown();
};

Runnable test4 = new Runnable() {
public void run()
{
await(startLatch);
fillBucket(bucket4);
fillBucket19(bucketD);
finishedLatch.countDown();
}
Runnable test4 = () -> {
await(startLatch);
fillBucket(bucket4);
fillBucket19(bucketD);
finishedLatch.countDown();
};

long start = System.nanoTime();
Expand Down

0 comments on commit 19eff1d

Please sign in to comment.