Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Junit test to Truth in the package com.google.gson of the module gson #2299

Merged
merged 6 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 2 additions & 3 deletions gson/src/test/java/com/google/gson/CommentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package com.google.gson;

import static org.junit.Assert.assertEquals;
import static com.google.common.truth.Truth.assertThat;

import com.google.gson.reflect.TypeToken;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

Expand All @@ -43,6 +42,6 @@ public void testParseComments() {
+ "]";

List<String> abc = new Gson().fromJson(json, new TypeToken<List<String>>() {}.getType());
assertEquals(Arrays.asList("a", "b", "c"), abc);
assertThat(abc).containsExactly("a", "b", "c").inOrder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.gson;

import static org.junit.Assert.assertEquals;
import static com.google.common.truth.Truth.assertThat;

import java.net.InetAddress;
import org.junit.Before;
Expand All @@ -39,9 +39,9 @@ public void setUp() throws Exception {
public void testInetAddressSerializationAndDeserialization() throws Exception {
InetAddress address = InetAddress.getByName("8.8.8.8");
String jsonAddress = gson.toJson(address);
assertEquals("\"8.8.8.8\"", jsonAddress);
assertThat(jsonAddress).isEqualTo("\"8.8.8.8\"");

InetAddress value = gson.fromJson(jsonAddress, InetAddress.class);
assertEquals(value, address);
}
assertThat(address).isEqualTo(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.gson;

import static org.junit.Assert.assertTrue;
import static com.google.common.truth.Truth.assertThat;

import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
Expand All @@ -37,9 +37,9 @@ public class DefaultMapJsonSerializerTest {
public void testEmptyMapNoTypeSerialization() {
Map<String, String> emptyMap = new HashMap<>();
JsonElement element = gson.toJsonTree(emptyMap, emptyMap.getClass());
assertTrue(element instanceof JsonObject);
assertThat(element).isInstanceOf(JsonObject.class);
JsonObject emptyMapJsonObject = (JsonObject) element;
assertTrue(emptyMapJsonObject.entrySet().isEmpty());
assertThat(emptyMapJsonObject.entrySet()).isEmpty();
}

@Test
Expand All @@ -48,9 +48,9 @@ public void testEmptyMapSerialization() {
Map<String, String> emptyMap = new HashMap<>();
JsonElement element = gson.toJsonTree(emptyMap, mapType);

assertTrue(element instanceof JsonObject);
assertThat(element).isInstanceOf(JsonObject.class);
JsonObject emptyMapJsonObject = (JsonObject) element;
assertTrue(emptyMapJsonObject.entrySet().isEmpty());
assertThat(emptyMapJsonObject.entrySet()).isEmpty();
}

@Test
Expand All @@ -62,8 +62,8 @@ public void testNonEmptyMapSerialization() {
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(myMap, mapType);

assertTrue(element.isJsonObject());
assertThat(element.isJsonObject()).isTrue();
JsonObject mapJsonObject = element.getAsJsonObject();
assertTrue(mapJsonObject.has(key));
assertThat(mapJsonObject.has(key)).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.google.gson;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static com.google.common.truth.Truth.assertThat;

import com.google.gson.annotations.Expose;
import com.google.gson.internal.Excluder;
Expand All @@ -33,44 +32,44 @@ public class ExposeAnnotationExclusionStrategyTest {
private Excluder excluder = Excluder.DEFAULT.excludeFieldsWithoutExposeAnnotation();

@Test
public void testNeverSkipClasses() throws Exception {
assertFalse(excluder.excludeClass(MockObject.class, true));
assertFalse(excluder.excludeClass(MockObject.class, false));
public void testNeverSkipClasses() {
assertThat(excluder.excludeClass(MockObject.class, true)).isFalse();
assertThat(excluder.excludeClass(MockObject.class, false)).isFalse();
}

@Test
public void testSkipNonAnnotatedFields() throws Exception {
Field f = createFieldAttributes("hiddenField");
assertTrue(excluder.excludeField(f, true));
assertTrue(excluder.excludeField(f, false));
assertThat(excluder.excludeField(f, true)).isTrue();
assertThat(excluder.excludeField(f, false)).isTrue();
}

@Test
public void testSkipExplicitlySkippedFields() throws Exception {
Field f = createFieldAttributes("explicitlyHiddenField");
assertTrue(excluder.excludeField(f, true));
assertTrue(excluder.excludeField(f, false));
assertThat(excluder.excludeField(f, true)).isTrue();
assertThat(excluder.excludeField(f, false)).isTrue();
}

@Test
public void testNeverSkipExposedAnnotatedFields() throws Exception {
Field f = createFieldAttributes("exposedField");
assertFalse(excluder.excludeField(f, true));
assertFalse(excluder.excludeField(f, false));
assertThat(excluder.excludeField(f, true)).isFalse();
assertThat(excluder.excludeField(f, false)).isFalse();
}

@Test
public void testNeverSkipExplicitlyExposedAnnotatedFields() throws Exception {
Field f = createFieldAttributes("explicitlyExposedField");
assertFalse(excluder.excludeField(f, true));
assertFalse(excluder.excludeField(f, false));
assertThat(excluder.excludeField(f, true)).isFalse();
assertThat(excluder.excludeField(f, false)).isFalse();
}

@Test
public void testDifferentSerializeAndDeserializeField() throws Exception {
Field f = createFieldAttributes("explicitlyDifferentModeField");
assertFalse(excluder.excludeField(f, true));
assertTrue(excluder.excludeField(f, false));
assertThat(excluder.excludeField(f, true)).isFalse();
assertThat(excluder.excludeField(f, false)).isTrue();
}

private static Field createFieldAttributes(String fieldName) throws Exception {
Expand Down
36 changes: 17 additions & 19 deletions gson/src/test/java/com/google/gson/FieldAttributesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.google.gson;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.gson.reflect.TypeToken;
Expand All @@ -44,40 +42,40 @@ public void setUp() throws Exception {

@SuppressWarnings("unused")
@Test
public void testNullField() throws Exception {
public void testNullField() {
try {
new FieldAttributes(null);
fail("Field parameter can not be null");
} catch (NullPointerException expected) { }
}

@Test
public void testDeclaringClass() throws Exception {
assertEquals(Foo.class, fieldAttributes.getDeclaringClass());
public void testDeclaringClass() {
assertThat(fieldAttributes.getDeclaringClass()).isAssignableTo(Foo.class);
}

@Test
public void testModifiers() throws Exception {
assertFalse(fieldAttributes.hasModifier(Modifier.STATIC));
assertFalse(fieldAttributes.hasModifier(Modifier.FINAL));
assertFalse(fieldAttributes.hasModifier(Modifier.ABSTRACT));
assertFalse(fieldAttributes.hasModifier(Modifier.VOLATILE));
assertFalse(fieldAttributes.hasModifier(Modifier.PROTECTED));
public void testModifiers() {
assertThat(fieldAttributes.hasModifier(Modifier.STATIC)).isFalse();
assertThat(fieldAttributes.hasModifier(Modifier.FINAL)).isFalse();
assertThat(fieldAttributes.hasModifier(Modifier.ABSTRACT)).isFalse();
assertThat(fieldAttributes.hasModifier(Modifier.VOLATILE)).isFalse();
assertThat(fieldAttributes.hasModifier(Modifier.PROTECTED)).isFalse();

assertTrue(fieldAttributes.hasModifier(Modifier.PUBLIC));
assertTrue(fieldAttributes.hasModifier(Modifier.TRANSIENT));
assertThat(fieldAttributes.hasModifier(Modifier.PUBLIC)).isTrue();
assertThat(fieldAttributes.hasModifier(Modifier.TRANSIENT)).isTrue();
}

@Test
public void testName() throws Exception {
assertEquals("bar", fieldAttributes.getName());
public void testName() {
assertThat(fieldAttributes.getName()).isEqualTo("bar");
}

@Test
public void testDeclaredTypeAndClass() throws Exception {
public void testDeclaredTypeAndClass() {
Type expectedType = new TypeToken<List<String>>() {}.getType();
assertEquals(expectedType, fieldAttributes.getDeclaredType());
assertEquals(List.class, fieldAttributes.getDeclaredClass());
assertThat(fieldAttributes.getDeclaredType()).isEqualTo(expectedType);
assertThat(fieldAttributes.getDeclaredClass()).isAssignableTo(List.class);
}

private static class Foo {
Expand Down
23 changes: 14 additions & 9 deletions gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.google.gson;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import java.lang.reflect.Field;
import java.util.Locale;
import org.junit.Test;
Expand All @@ -28,7 +29,7 @@ public void testSeparateCamelCase() {
};

for (String[] pair : argumentPairs) {
assertEquals(pair[1], FieldNamingPolicy.separateCamelCase(pair[0], '_'));
assertThat(FieldNamingPolicy.separateCamelCase(pair[0], '_')).isEqualTo(pair[1]);
}
}

Expand All @@ -51,12 +52,12 @@ public void testUpperCaseFirstLetter() {
};

for (String[] pair : argumentPairs) {
assertEquals(pair[1], FieldNamingPolicy.upperCaseFirstLetter(pair[0]));
assertThat(FieldNamingPolicy.upperCaseFirstLetter(pair[0])).isEqualTo(pair[1]);
}
}

/**
* Upper casing policies should be unaffected by default Locale.
* Upper-casing policies should be unaffected by default Locale.
*/
@Test
public void testUpperCasingLocaleIndependent() throws Exception {
Expand All @@ -81,11 +82,13 @@ class Dummy {

try {
// Verify that default Locale has different case conversion rules
assertNotEquals("Test setup is broken", expected, name.toUpperCase());
assertWithMessage("Test setup is broken")
.that(name.toUpperCase()).doesNotMatch(expected);

for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
assertEquals("Unexpected conversion for " + policy, expected, policy.translateName(field));
assertWithMessage("Unexpected conversion for %s", policy)
.that(policy.translateName(field)).matches(expected);
}
} finally {
Locale.setDefault(oldLocale);
Expand Down Expand Up @@ -118,11 +121,13 @@ class Dummy {

try {
// Verify that default Locale has different case conversion rules
assertNotEquals("Test setup is broken", expected, name.toLowerCase());
assertWithMessage("Test setup is broken")
.that(name.toLowerCase()).doesNotMatch(expected);

for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
assertEquals("Unexpected conversion for " + policy, expected, policy.translateName(field));
assertWithMessage("Unexpected conversion for %s", policy)
.that(policy.translateName(field)).matches(expected);
}
} finally {
Locale.setDefault(oldLocale);
Expand Down
13 changes: 6 additions & 7 deletions gson/src/test/java/com/google/gson/GenericArrayTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.google.gson;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static com.google.common.truth.Truth.assertThat;

import com.google.gson.internal.$Gson$Types;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -46,15 +45,15 @@ public void testOurTypeFunctionality() throws Exception {
Type parameterizedType = new TypeToken<List<String>>() {}.getType();
Type genericArrayType = new TypeToken<List<String>[]>() {}.getType();

assertEquals(parameterizedType, ourType.getGenericComponentType());
assertEquals(genericArrayType, ourType);
assertEquals(genericArrayType.hashCode(), ourType.hashCode());
assertThat(ourType.getGenericComponentType()).isEqualTo(parameterizedType);
assertThat(ourType).isEqualTo(genericArrayType);
assertThat(ourType.hashCode()).isEqualTo(genericArrayType.hashCode());
}

@Test
public void testNotEquals() throws Exception {
Type differentGenericArrayType = new TypeToken<List<String>[][]>() {}.getType();
assertFalse(differentGenericArrayType.equals(ourType));
assertFalse(ourType.equals(differentGenericArrayType));
assertThat(differentGenericArrayType.equals(ourType)).isFalse();
assertThat(ourType.equals(differentGenericArrayType)).isFalse();
}
}
Loading