Skip to content

Commit

Permalink
Migrate guava module tests to JUnit 5 (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Jan 22, 2025
1 parent 14d1ffb commit 7169b99
Show file tree
Hide file tree
Showing 33 changed files with 338 additions and 42 deletions.
11 changes: 8 additions & 3 deletions guava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ com.google.common.*;version="${version.guava.osgi}",
<version>${version.guava}</version>
</dependency>

<!-- 2025-Jan-22 joohyukkim : temporarily isolate JUnit 4 here... -->
<!-- Use JUnit 5 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.Objects;

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

/**
* Unit tests for verifying deserialization of Guava's {@link Cache} type.
*
Expand Down Expand Up @@ -66,6 +69,7 @@ private enum MyEnum {

private final ObjectMapper MAPPER = mapperWithModule();

@Test
public void testGuavaCacheApi() throws Exception {
Cache<String, String> cache = CacheBuilder.newBuilder().build();
// Cache does not allow null key
Expand All @@ -81,6 +85,7 @@ public void testGuavaCacheApi() throws Exception {
} catch (NullPointerException e) {}
}

@Test
public void testCacheDeserializationSimple() throws Exception {
// Create a delegate cache using CacheBuilder
Cache<String, Integer> delegateCache = CacheBuilder.newBuilder().build();
Expand All @@ -93,6 +98,7 @@ public void testCacheDeserializationSimple() throws Exception {
assertEquals("foo", s.getIfPresent("a"));
}

@Test
public void testCacheDeserRoundTrip() throws Exception {
Cache<String, Integer> cache = CacheBuilder.newBuilder().build();
cache.put("key1", 1);
Expand All @@ -109,6 +115,7 @@ public void testCacheDeserRoundTrip() throws Exception {
}

// [datatype-collections#96]
@Test
public void testCacheSerialization() throws Exception {
Cache<Long, Integer> cache = CacheBuilder.newBuilder().build();
cache.put(1L, 1);
Expand All @@ -132,6 +139,7 @@ private void _verifySizeTwoAndContains(Map<Long, Integer> map) {
assertEquals(2, map.get(2L).intValue());
}

@Test
public void testEnumKey() throws Exception {
final TypeReference<Cache<MyEnum, Integer>> type = new TypeReference<Cache<MyEnum, Integer>>() {};
final Cache<MyEnum, Integer> cache = CacheBuilder.newBuilder().build();
Expand All @@ -155,11 +163,13 @@ static class CacheWrapper {
private Cache<String, String> cache = CacheBuilder.newBuilder().build();
}

@Test
public void testEmptyCacheExclusion() throws Exception {
String json = MAPPER.writeValueAsString(new CacheWrapper());
assertEquals("{}", json);
}

@Test
public void testWithGuavaOptional() throws Exception {
// set up
Cache<String, Optional<Double>> cache = CacheBuilder.newBuilder().build();
Expand All @@ -179,6 +189,7 @@ public void testWithGuavaOptional() throws Exception {
}

// [datatypes-collections#140]: handle null values
@Test
public void testCacheWithNulls() throws Exception {
Cache<String, Integer> cache;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import com.google.common.base.Objects;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

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

/**
* Unit tests for verifying serialization of Guava's {@link Cache} type.
*
Expand Down Expand Up @@ -188,6 +191,7 @@ public String toString() {

private final ObjectMapper ORDERED_MAPPER = mapperWithModule().enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);

@Test
public void testGuavaCacheApi() throws Exception {
Cache<String, String> cache = CacheBuilder.newBuilder().build();
// Cache does not allow null key
Expand All @@ -203,6 +207,7 @@ public void testGuavaCacheApi() throws Exception {
} catch (NullPointerException e) {}
}

@Test
public void testCacheSerialization() throws Exception {
// Create a Guava Cache
Cache<String, String> cache = CacheBuilder.newBuilder().build();
Expand All @@ -214,6 +219,7 @@ public void testCacheSerialization() throws Exception {
ORDERED_MAPPER.writeValueAsString(cache));
}

@Test
public void testCacheSerializationIgnoreProperties() throws Exception {
CacheContainerWithIgnores container = new CacheContainerWithIgnores();

Expand All @@ -222,6 +228,7 @@ public void testCacheSerializationIgnoreProperties() throws Exception {
ORDERED_MAPPER.writeValueAsString(container));
}

@Test
public void testCacheSerializationWithBean() throws Exception {
CacheContainerWithBean container = new CacheContainerWithBean();

Expand All @@ -230,6 +237,7 @@ public void testCacheSerializationWithBean() throws Exception {
ORDERED_MAPPER.writeValueAsString(container));
}

@Test
public void testCacheSerializationWithList() throws Exception {
CacheContainerWithList container = new CacheContainerWithList();

Expand All @@ -238,6 +246,7 @@ public void testCacheSerializationWithList() throws Exception {
ORDERED_MAPPER.writeValueAsString(container));
}

@Test
public void testCacheSerializationWithEmptyCache() throws Exception {
Cache<String, String> cache = CacheBuilder.newBuilder().build();

Expand All @@ -246,6 +255,7 @@ public void testCacheSerializationWithEmptyCache() throws Exception {
ORDERED_MAPPER.writeValueAsString(cache));
}

@Test
public void testCacheSerializationBeanKey() throws Exception {
Cache<BeanKey, String> cache = CacheBuilder.newBuilder().build();
cache.put(new BeanKey(1), "value1");
Expand All @@ -255,6 +265,7 @@ public void testCacheSerializationBeanKey() throws Exception {
ORDERED_MAPPER.writeValueAsString(cache));
}

@Test
public void testCacheSerializationBeanKeyEquals() throws Exception {
Cache<BeanKeyEquals, String> cache = CacheBuilder.newBuilder().build();
cache.put(new BeanKeyEquals(1), "value1");
Expand All @@ -267,12 +278,14 @@ public void testCacheSerializationBeanKeyEquals() throws Exception {
}


@Test
public void testEmptyCacheExclusion() throws Exception {
String json = ORDERED_MAPPER.writeValueAsString(new CacheWrapper());

assertEquals("{}", json);
}

@Test
public void testCacheSerializationWithTypeReference() throws Exception {
final Cache<MyEnum, Integer> cache = CacheBuilder.newBuilder().build();
cache.put(MyEnum.YAY, 5);
Expand All @@ -286,6 +299,7 @@ public void testCacheSerializationWithTypeReference() throws Exception {
assertEquals(expected, mapperSer);
}

@Test
public void testOrderByKeyViaProperty() throws Exception {
CacheOrderingBean input = new CacheOrderingBean("c", "b", "a");

Expand All @@ -294,6 +308,7 @@ public void testOrderByKeyViaProperty() throws Exception {
assertEquals(a2q("{'cache':{'a':3,'b':2,'c':1}}"), json);
}

@Test
public void testPolymorphicCacheSerialization() throws Exception {
Cache<String, Animal> cache = CacheBuilder.newBuilder().build();
cache.put("c", new Cat());
Expand All @@ -308,6 +323,7 @@ public void testPolymorphicCacheSerialization() throws Exception {
"'d':{'_type':'t_dog','name':'Woof'}}}"), json);
}

@Test
public void testNestedCacheSerialization() throws Exception {
Cache<String, Cache<String, String>> nestedCache = CacheBuilder.newBuilder().build();
nestedCache.put("a", _buildCacheWithKeys("a_x", "a_y"));
Expand All @@ -330,13 +346,15 @@ private Cache<String, String> _buildCacheWithKeys(String... keys) {
}

// [datatypes-collections#104]
@Test
public void testPolymorphicCacheEmpty() throws Exception {
final Cache<String, Object> cache = CacheBuilder.newBuilder().build();
cache.put("aKey", 1);
_testPolymorphicCache(cache,
a2q("{'aProperty':{'@type':'LocalCache$LocalManualCache','aKey':1}}"));
}

@Test
public void testPolymorphicCacheNonEmpty() throws Exception {
_testPolymorphicCache(CacheBuilder.newBuilder().build(),
a2q("{'aProperty':{'@type':'LocalCache$LocalManualCache'}}"));
Expand All @@ -351,6 +369,7 @@ private void _testPolymorphicCache(Cache<String, Object> cache, String expected)
assertEquals(expected, json);
}

@Test
public void testCacheSerializeOrderedByKey() throws Exception {
final Cache<String, String> cache = _buildCacheWithKeys("c_key", "d_key", "a_key", "e_key", "b_key");

Expand All @@ -362,6 +381,7 @@ public void testCacheSerializeOrderedByKey() throws Exception {
new TypeReference<Cache<String, String>>() {}).writeValueAsString(cache));
}

@Test
public void testPolymorphicCacheWrapperSerialization() throws Exception {
final Cache<String, String> cache = _buildCacheWithKeys("c_key", "a_key", "e_key", "b_key", "d_key");
PolymorphicWrapperBean outside = new PolymorphicWrapperBean();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.fasterxml.jackson.datatype.guava;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

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

public class EmptyCollectionsTest extends ModuleTestBase
{
// [datatypes-collections#67]
Expand All @@ -24,6 +28,7 @@ public static class ImmutableMapContainer67 {
.build();

// [datatypes-collections#67]
@Test
public void testEmptyForLists() throws Exception
{
ImmutableListContainer67 result;
Expand All @@ -47,6 +52,7 @@ public void testEmptyForLists() throws Exception
assertEquals(0, result.lists.size());
}

@Test
public void testEmptyForMaps() throws Exception
{
ImmutableMapContainer67 result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import java.util.Arrays;

import org.junit.jupiter.api.Test;

import com.google.common.collect.FluentIterable;

import com.fasterxml.jackson.databind.ObjectMapper;

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

/**
* Unit tests to verify serialization of {@link FluentIterable}s.
*/
Expand All @@ -27,18 +31,21 @@ static FluentIterable<Integer> createFluentIterable() {
* or Guava's implementation of FluentIterable changes.
* @throws Exception
*/
@Test
public void testSerializationWithoutModule() throws Exception {
ObjectMapper mapper = new ObjectMapper();
FluentHolder holder = new FluentHolder();
String json = mapper.writeValueAsString(holder);
assertEquals("{\"value\":{\"empty\":false}}", json);
}

@Test
public void testSerialization() throws Exception {
String json = MAPPER.writeValueAsString(createFluentIterable());
assertEquals("[1,2,3]", json);
}

@Test
public void testWrappedSerialization() throws Exception {
FluentHolder holder = new FluentHolder();
String json = MAPPER.writeValueAsString(holder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.fasterxml.jackson.datatype.guava;

import org.junit.jupiter.api.Test;

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

import com.google.common.hash.HashCode;

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

public class HashCodeTest extends ModuleTestBase
{
private final ObjectMapper MAPPER = mapperWithModule();

@Test
public void testSerialization() throws Exception
{
HashCode input = HashCode.fromString("cafebabe12345678");
String json = MAPPER.writeValueAsString(input);
assertEquals("\"cafebabe12345678\"", json);
}

@Test
public void testDeserialization() throws Exception
{
// success:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.fasterxml.jackson.datatype.guava;

import org.junit.jupiter.api.Test;

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

import com.google.common.net.HostAndPort;

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

public class HostAndPortTest extends ModuleTestBase
{
private final ObjectMapper MAPPER = mapperWithModule();

@Test
public void testSerialization() throws Exception
{
HostAndPort input = HostAndPort.fromParts("localhost", 80);
String json = MAPPER.writeValueAsString(input);
assertEquals("\"localhost:80\"", json);
}

@Test
public void testDeserializationOk() throws Exception
{
// Actually, let's support both old style and new style
Expand Down Expand Up @@ -44,6 +50,7 @@ public void testDeserializationOk() throws Exception
assertEquals(HostAndPort.fromHost(""), result);
}

@Test
public void testDeserializationFail() throws Exception
{
HostAndPort result = null;
Expand Down
Loading

0 comments on commit 7169b99

Please sign in to comment.