Skip to content

Commit

Permalink
[JSTEP-10] migrate hppc, eclipse-collections, pcollections to J…
Browse files Browse the repository at this point in the history
…Unit 5 (#177)
  • Loading branch information
JooHyukKim authored Jan 22, 2025
1 parent 45724d5 commit 14d1ffb
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,22 @@
import org.eclipse.collections.impl.factory.primitive.ShortSets;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

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

public final class DeserializerTest extends ModuleTestBase {
private <T> void testCollection(T expected, String json, TypeReference<T> type) throws IOException {
ObjectMapper objectMapper = mapperWithModule();
T value = objectMapper.readValue(json, type);
Assert.assertEquals(expected, value);
Assert.assertTrue(objectMapper.getTypeFactory().constructType(type).getRawClass().isInstance(value));
assertEquals(expected, value);
assertTrue(objectMapper.getTypeFactory().constructType(type).getRawClass().isInstance(value));
}

private <T> void testCollection(T expected, String json, Class<T> type) throws IOException {
T value = mapperWithModule().readValue(json, type);
Assert.assertEquals(expected, value);
Assert.assertTrue(type.isInstance(value));
assertEquals(expected, value);
assertTrue(type.isInstance(value));
}

@Test
Expand Down Expand Up @@ -465,7 +466,7 @@ static void primitiveMaps0(ObjectMapper mapper, boolean serialize) throws Except
.getMethod("ofAll", baseMapType)
.invoke(immutableFactory, mutableSample);

Assert.assertEquals(mutableSample, immutableSample);
assertEquals(mutableSample, immutableSample);

Function<Class<?>, JavaType> generify;
if (key == Object.class || value == Object.class) {
Expand All @@ -483,21 +484,21 @@ static void primitiveMaps0(ObjectMapper mapper, boolean serialize) throws Except
mapper.writerFor(generify.apply(baseMapType)).writeValueAsString(mutableSample);
String polyPrinted = mapper.writeValueAsString(mutableSample);
// compare trees so property order doesn't matter
Assert.assertEquals(mapper.readTree(json), mapper.readTree(mutablePrinted));
Assert.assertEquals(mapper.readTree(json), mapper.readTree(immutablePrinted));
Assert.assertEquals(mapper.readTree(json), mapper.readTree(basePrinted));
Assert.assertEquals(mapper.readTree(json), mapper.readTree(polyPrinted));
assertEquals(mapper.readTree(json), mapper.readTree(mutablePrinted));
assertEquals(mapper.readTree(json), mapper.readTree(immutablePrinted));
assertEquals(mapper.readTree(json), mapper.readTree(basePrinted));
assertEquals(mapper.readTree(json), mapper.readTree(polyPrinted));
} else {
Object mutableParsed = mapper.readValue(json, generify.apply(mutableMapType));
Object immutableParsed = mapper.readValue(json, generify.apply(immutableMapType));
Object baseParsed = mapper.readValue(json, generify.apply(baseMapType));
Assert.assertEquals(mutableSample, mutableParsed);
Assert.assertEquals(immutableSample, immutableParsed);
Assert.assertEquals(mutableSample, baseParsed);
assertEquals(mutableSample, mutableParsed);
assertEquals(immutableSample, immutableParsed);
assertEquals(mutableSample, baseParsed);

Assert.assertTrue(mutableMapType.isInstance(mutableParsed));
Assert.assertTrue(immutableMapType.isInstance(immutableParsed));
Assert.assertTrue(baseMapType.isInstance(baseParsed));
assertTrue(mutableMapType.isInstance(mutableParsed));
assertTrue(immutableMapType.isInstance(immutableParsed));
assertTrue(baseMapType.isInstance(baseParsed));
}
}
}
Expand All @@ -509,19 +510,19 @@ private static String capitalize(String simpleName) {

@Test
public void objectObjectMaps() throws IOException {
Assert.assertEquals(
assertEquals(
mapperWithModule().readValue("{\"abc\":\"def\"}", new TypeReference<MutableMap<String, String>>() {}),
Maps.mutable.of("abc", "def")
);
Assert.assertEquals(
assertEquals(
mapperWithModule().readValue("{\"abc\":\"def\"}", new TypeReference<ImmutableMap<String, String>>() {}),
Maps.immutable.of("abc", "def")
);
Assert.assertEquals(
assertEquals(
mapperWithModule().readValue("{\"abc\":\"def\"}", new TypeReference<MapIterable<String, String>>() {}),
Maps.mutable.of("abc", "def")
);
Assert.assertEquals(
assertEquals(
mapperWithModule().readValue("{\"abc\":\"def\"}",
new TypeReference<UnsortedMapIterable<String, String>>() {}),
Maps.mutable.of("abc", "def")
Expand All @@ -544,7 +545,7 @@ private static Object randomSample(Class<?> type) {

@Test
public void typeInfoObjectMap() throws IOException {
Assert.assertEquals(
assertEquals(
mapperWithModule()
.readValue("{\"map\":{\"0\":{\"@c\":\".DeserializerTest$B\"}}}", Container.class).map,
IntObjectMaps.immutable.of(0, new B())
Expand Down Expand Up @@ -581,7 +582,7 @@ public void typeInfoNestedMapList() throws IOException {
// test case for jackson-datatypes-collections#71
ImmutableMap<String, ImmutableList<A>> property =
Maps.immutable.of("foo", Lists.immutable.of(new B()));
Assert.assertEquals(
assertEquals(
mapperWithModule().readValue(
"{\"foo\": [{\"@c\": \".DeserializerTest$B\"}]}",
new TypeReference<ImmutableMap<String, ImmutableList<A>>>() {}),
Expand All @@ -594,7 +595,7 @@ public void typeInfoNestedMapMap() throws IOException {
// auxiliary test case for jackson-datatypes-collections#71 - also worked before fix
ImmutableMap<String, ImmutableMap<String, A>> property =
Maps.immutable.of("foo", Maps.immutable.of("bar", new B()));
Assert.assertEquals(
assertEquals(
mapperWithModule().readValue(
"{\"foo\": {\"bar\": {\"@c\": \".DeserializerTest$B\"}}}",
new TypeReference<ImmutableMap<String, ImmutableMap<String, A>>>() {}),
Expand Down Expand Up @@ -656,8 +657,8 @@ public void primitivePairs() throws Exception {
+ ",\"two\":" + mapperWithModule().writeValueAsString(sampleTwo) + "}";
Object samplePair = factory.invoke(null, sampleOne, sampleTwo);

Assert.assertEquals(expectedJson, mapperWithModule().writeValueAsString(samplePair));
Assert.assertEquals(samplePair, mapperWithModule().readValue(expectedJson, pairType));
assertEquals(expectedJson, mapperWithModule().writeValueAsString(samplePair));
assertEquals(samplePair, mapperWithModule().readValue(expectedJson, pairType));
}
}
}
Expand All @@ -670,8 +671,8 @@ public void twin() throws Exception {
String expectedJson = "{\"one\":" + mapper.writeValueAsString(sampleOne)
+ ",\"two\":" + mapper.writeValueAsString(sampleTwo) + "}";
Twin<String> twin = Tuples.twin((String) sampleOne, (String) sampleTwo);
Assert.assertEquals(expectedJson, mapper.writeValueAsString(twin));
Assert.assertEquals(twin, mapper.readValue(expectedJson, new TypeReference<Twin<String>>() {}));
assertEquals(expectedJson, mapper.writeValueAsString(twin));
assertEquals(twin, mapper.readValue(expectedJson, new TypeReference<Twin<String>>() {}));
}

@Test
Expand All @@ -681,8 +682,8 @@ public void pairTyped() throws Exception {
final String actJson = mapper.writerFor(new TypeReference<ObjectIntPair<A>>() {})
.writeValueAsString(pair);
String expJson = "{\"one\":{\"@c\":\".DeserializerTest$B\"},\"two\":5}";
Assert.assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
Assert.assertEquals(pair,
assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
assertEquals(pair,
mapper.readValue(actJson, new TypeReference<ObjectIntPair<A>>() {})
);
}
Expand All @@ -693,8 +694,8 @@ public void nestedMap() throws Exception {
String json = "{\"a\":{\"b\":\"c\"}}";
TypeReference<MutableMap<String, MutableMap<String, String>>> type =
new TypeReference<MutableMap<String, MutableMap<String, String>>>() {};
Assert.assertEquals(json, mapperWithModule().writerFor(type).writeValueAsString(pair));
Assert.assertEquals(pair, mapperWithModule().readValue(json, type));
assertEquals(json, mapperWithModule().writerFor(type).writeValueAsString(pair));
assertEquals(pair, mapperWithModule().readValue(json, type));
}

@Test
Expand All @@ -704,8 +705,8 @@ public void triple() throws Exception {
String actJson = mapper.writerFor(new TypeReference<Triple<String, Integer, Boolean>>() {})
.writeValueAsString(triple);
String expJson = "{\"one\":\"a\",\"two\":2,\"three\":false}";
Assert.assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
Assert.assertEquals(
assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
assertEquals(
triple,
mapper.readValue(actJson, new TypeReference<Triple<String, Integer, Boolean>>() {})
);
Expand All @@ -718,8 +719,8 @@ public void triplet() throws Exception {
String actJson = mapper.writerFor(new TypeReference<Triplet<String>>() {})
.writeValueAsString(triple);
String expJson = "{\"one\":\"a\",\"two\":\"b\",\"three\":\"c\"}";
Assert.assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
Assert.assertEquals(
assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
assertEquals(
triple,
mapper.readValue(actJson, new TypeReference<Triplet<String>>() {})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.fasterxml.jackson.datatype.eclipsecollections;

import static org.junit.Assert.assertTrue;

import org.junit.Assert;
import org.junit.Test;

import org.eclipse.collections.api.map.primitive.MutableCharCharMap;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

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

/**
* Unit tests for verifying the fixes for OSS-Fuzz issues
* work as expected
Expand All @@ -25,7 +23,7 @@ public void testOSSFuzzIssue64629() throws Exception
// Invalid token {"x?":[x?]: where ? is not ascii characters
final char[] invalid = {123, 34, 824, 34, 58, 91, 120, 7, 93};

MismatchedInputException e = Assert.assertThrows(
MismatchedInputException e = assertThrows(
MismatchedInputException.class,
() -> MAPPER.readValue(new String(invalid), MutableCharCharMap.class));
assertTrue(e.getMessage().contains("Cannot convert a JSON Null into a char element of map"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

public abstract class ModuleTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,42 @@
import org.eclipse.collections.impl.factory.primitive.IntObjectMaps;
import org.eclipse.collections.impl.factory.primitive.LongLists;
import org.eclipse.collections.impl.factory.primitive.ShortLists;
import org.junit.Assert;
import org.junit.Test;

import org.junit.jupiter.api.Test;

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

public final class SerializerTest extends ModuleTestBase {
@Test
public void ref() throws IOException {
Assert.assertEquals(
assertEquals(
"[\"a\",\"b\",\"c\"]",
mapperWithModule().writeValueAsString(Sets.immutable.of("a", "b", "c"))
);
}

@Test
public void primitive() throws IOException {
Assert.assertEquals("[true,false,true]", mapperWithModule().writeValueAsString(
assertEquals("[true,false,true]", mapperWithModule().writeValueAsString(
BooleanLists.immutable.of(true, false, true)));
Assert.assertEquals("[1,2,3]", mapperWithModule().writeValueAsString(
assertEquals("[1,2,3]", mapperWithModule().writeValueAsString(
ShortLists.immutable.of((short) 1, (short) 2, (short) 3)));
Assert.assertEquals("[1,2,3]", mapperWithModule().writeValueAsString(
assertEquals("[1,2,3]", mapperWithModule().writeValueAsString(
IntLists.immutable.of(1, 2, 3)));
Assert.assertEquals("[1.1,2.3,3.5]", mapperWithModule().writeValueAsString(
assertEquals("[1.1,2.3,3.5]", mapperWithModule().writeValueAsString(
FloatLists.immutable.of(1.1F, 2.3F, 3.5F)));
Assert.assertEquals("[1,2,3]", mapperWithModule().writeValueAsString(
assertEquals("[1,2,3]", mapperWithModule().writeValueAsString(
LongLists.immutable.of(1, 2, 3)));
Assert.assertEquals("[1.1,2.3,3.5]", mapperWithModule().writeValueAsString(
assertEquals("[1.1,2.3,3.5]", mapperWithModule().writeValueAsString(
DoubleLists.immutable.of(1.1, 2.3, 3.5)));

Assert.assertEquals(
assertEquals(
mapperWithModule().writeValueAsString(new byte[]{ 1, 2, 3 }),
mapperWithModule().writeValueAsString(ByteLists.immutable.of((byte) 1, (byte) 2, (byte) 3)));
Assert.assertEquals(
assertEquals(
mapperWithModule().writeValueAsString(new char[]{ '1', '2', '3' }),
mapperWithModule().writeValueAsString(CharLists.immutable.of('1', '2', '3')));
Assert.assertEquals(
assertEquals(
mapperWithModule().configure(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS, true)
.writeValueAsString(new char[]{ '1', '2', '3' }),
mapperWithModule().configure(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS, true)
Expand All @@ -82,7 +84,7 @@ class Wrapper {
Wrapper wrapper = new Wrapper();
wrapper.object = iterable;

Assert.assertEquals("{\"object\":[\"" + iterable.getClass().getName() + "\"," + data + "]}",
assertEquals("{\"object\":[\"" + iterable.getClass().getName() + "\"," + data + "]}",
objectMapper.writeValueAsString(wrapper));
}

Expand Down Expand Up @@ -117,27 +119,27 @@ public void primitiveMaps() throws Exception

@Test
public void objectObjectMaps() throws IOException {
Assert.assertEquals(
assertEquals(
"{\"abc\":\"def\"}",
mapperWithModule().writerFor(MutableMap.class).writeValueAsString(Maps.mutable.of("abc", "def"))
);
Assert.assertEquals(
assertEquals(
"{\"abc\":\"def\"}",
mapperWithModule().writerFor(ImmutableMap.class).writeValueAsString(Maps.immutable.of("abc", "def"))
);
Assert.assertEquals(
assertEquals(
"{\"abc\":\"def\"}",
mapperWithModule().writerFor(MapIterable.class).writeValueAsString(Maps.immutable.of("abc", "def"))
);
Assert.assertEquals(
assertEquals(
"{\"abc\":\"def\"}",
mapperWithModule().writerFor(MutableMapIterable.class).writeValueAsString(Maps.mutable.of("abc", "def"))
);
Assert.assertEquals(
assertEquals(
"{\"abc\":\"def\"}",
mapperWithModule().writeValueAsString(Maps.immutable.of("abc", "def"))
);
Assert.assertEquals(
assertEquals(
"{\"abc\":\"def\"}",
mapperWithModule().writerFor(new TypeReference<MapIterable<String, String>>() {})
.writeValueAsString(Maps.immutable.of("abc", "def"))
Expand All @@ -146,7 +148,7 @@ public void objectObjectMaps() throws IOException {

@Test
public void typeInfoObjectMap() throws IOException {
Assert.assertEquals(
assertEquals(
"{\"map\":{\"0\":{\"@c\":\".SerializerTest$B\"}}}",
mapperWithModule().writeValueAsString(new Container())
);
Expand Down
7 changes: 7 additions & 0 deletions guava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ com.google.common.*;version="${version.guava.osgi}",
<artifactId>guava</artifactId>
<version>${version.guava}</version>
</dependency>

<!-- 2025-Jan-22 joohyukkim : temporarily isolate JUnit 4 here... -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class HppcTestBase extends junit.framework.TestCase
import static org.junit.jupiter.api.Assertions.fail;

public abstract class HppcTestBase
{
protected HppcTestBase() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.*;

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

public class TestVersions extends HppcTestBase
{
public void testMapperVersions() throws IOException
Expand Down
Loading

0 comments on commit 14d1ffb

Please sign in to comment.