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 all Junit assert to Truth asserts #2304

Merged
merged 15 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
104 changes: 50 additions & 54 deletions gson/src/test/java/com/google/gson/functional/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.google.gson.functional;

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

import com.google.gson.Gson;
Expand Down Expand Up @@ -51,14 +48,14 @@ public void setUp() throws Exception {
@Test
public void testTopLevelArrayOfIntsSerialization() {
int[] target = {1, 2, 3, 4, 5, 6, 7, 8, 9};
assertEquals("[1,2,3,4,5,6,7,8,9]", gson.toJson(target));
assertThat(gson.toJson(target)).isEqualTo("[1,2,3,4,5,6,7,8,9]");
}

@Test
public void testTopLevelArrayOfIntsDeserialization() {
int[] expected = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] actual = gson.fromJson("[1,2,3,4,5,6,7,8,9]", int[].class);
assertArrayEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Test
Expand All @@ -74,27 +71,27 @@ public void testInvalidArrayDeserialization() {
@Test
public void testEmptyArraySerialization() {
int[] target = {};
assertEquals("[]", gson.toJson(target));
assertThat(gson.toJson(target)).isEqualTo("[]");
}

@Test
public void testEmptyArrayDeserialization() {
int[] actualObject = gson.fromJson("[]", int[].class);
assertTrue(actualObject.length == 0);
assertThat(actualObject.length == 0).isTrue();
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved

Integer[] actualObject2 = gson.fromJson("[]", Integer[].class);
assertTrue(actualObject2.length == 0);
assertThat(actualObject2.length == 0).isTrue();

actualObject = gson.fromJson("[ ]", int[].class);
assertTrue(actualObject.length == 0);
assertThat(actualObject.length == 0).isTrue();
}

@Test
public void testNullsInArraySerialization() {
String[] array = {"foo", null, "bar"};
String expected = "[\"foo\",null,\"bar\"]";
String json = gson.toJson(array);
assertEquals(expected, json);
assertThat(json).isEqualTo(expected);
}

@Test
Expand All @@ -103,7 +100,7 @@ public void testNullsInArrayDeserialization() {
String[] expected = {"foo", null, "bar"};
String[] target = gson.fromJson(json, expected.getClass());
for (int i = 0; i < expected.length; ++i) {
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(expected[i], target[i]);
assertThat(target[i]).isEqualTo(expected[i]);
}
}

Expand All @@ -112,13 +109,13 @@ public void testSingleNullInArraySerialization() {
BagOfPrimitives[] array = new BagOfPrimitives[1];
array[0] = null;
String json = gson.toJson(array);
assertEquals("[null]", json);
assertThat(json).isEqualTo("[null]");
}

@Test
public void testSingleNullInArrayDeserialization() {
BagOfPrimitives[] array = gson.fromJson("[null]", BagOfPrimitives[].class);
assertNull(array[0]);
assertThat(array[0]).isNull();
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand All @@ -127,40 +124,40 @@ public void testNullsInArrayWithSerializeNullPropertySetSerialization() {
String[] array = {"foo", null, "bar"};
String expected = "[\"foo\",null,\"bar\"]";
String json = gson.toJson(array);
assertEquals(expected, json);
assertThat(json).isEqualTo(expected);
}

@Test
public void testArrayOfStringsSerialization() {
String[] target = {"Hello", "World"};
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target));
assertThat(gson.toJson(target)).isEqualTo("[\"Hello\",\"World\"]");
}

@Test
public void testArrayOfStringsDeserialization() {
String json = "[\"Hello\",\"World\"]";
String[] target = gson.fromJson(json, String[].class);
assertEquals("Hello", target[0]);
assertEquals("World", target[1]);
assertThat(target[0]).isEqualTo("Hello");
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
assertThat(target[1]).isEqualTo("World");
}

@Test
public void testSingleStringArraySerialization() throws Exception {
public void testSingleStringArraySerialization() {
String[] s = { "hello" };
String output = gson.toJson(s);
assertEquals("[\"hello\"]", output);
assertThat(output).isEqualTo("[\"hello\"]");
}

@Test
public void testSingleStringArrayDeserialization() throws Exception {
public void testSingleStringArrayDeserialization() {
String json = "[\"hello\"]";
String[] arrayType = gson.fromJson(json, String[].class);
assertEquals(1, arrayType.length);
assertEquals("hello", arrayType[0]);
assertThat(arrayType.length).isEqualTo(1);
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
assertThat(arrayType[0]).isEqualTo("hello");
}

@Test
public void testArrayOfCollectionSerialization() throws Exception {
public void testArrayOfCollectionSerialization() {
StringBuilder sb = new StringBuilder("[");
int arraySize = 3;

Expand All @@ -182,42 +179,42 @@ public void testArrayOfCollectionSerialization() throws Exception {
sb.append(']');

String json = gson.toJson(arrayOfCollection, typeToSerialize);
assertEquals(sb.toString(), json);
assertThat(json).isEqualTo(sb.toString());
}

@Test
public void testArrayOfCollectionDeserialization() throws Exception {
public void testArrayOfCollectionDeserialization() {
String json = "[[1,2],[3,4]]";
Type type = new TypeToken<Collection<Integer>[]>() {}.getType();
Collection<Integer>[] target = gson.fromJson(json, type);

assertEquals(2, target.length);
assertArrayEquals(new Integer[] {1, 2}, target[0].toArray(new Integer[0]));
assertArrayEquals(new Integer[] {3, 4}, target[1].toArray(new Integer[0]));
assertThat(target.length).isEqualTo(2);
assertThat(target[0].toArray(new Integer[0])).isEqualTo(new Integer[] {1, 2});
assertThat(target[1].toArray(new Integer[0])).isEqualTo(new Integer[] {3, 4});
}

@Test
public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception {
public void testArrayOfPrimitivesAsObjectsSerialization() {
Object[] objs = new Object[] {1, "abc", 0.3f, 5L};
String json = gson.toJson(objs);
assertTrue(json.contains("abc"));
assertTrue(json.contains("0.3"));
assertTrue(json.contains("5"));
assertThat(json.contains("abc")).isTrue();
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
assertThat(json.contains("0.3")).isTrue();
assertThat(json.contains("5")).isTrue();
}

@Test
public void testArrayOfPrimitivesAsObjectsDeserialization() throws Exception {
public void testArrayOfPrimitivesAsObjectsDeserialization() {
String json = "[1,'abc',0.3,1.1,5]";
Object[] objs = gson.fromJson(json, Object[].class);
assertEquals(1, ((Number)objs[0]).intValue());
assertEquals("abc", objs[1]);
assertEquals(0.3, ((Number)objs[2]).doubleValue(), 0);
assertEquals(new BigDecimal("1.1"), new BigDecimal(objs[3].toString()));
assertEquals(5, ((Number)objs[4]).shortValue());
assertThat(((Number)objs[0]).intValue()).isEqualTo(1);
assertThat(objs[1]).isEqualTo("abc");
assertThat(((Number)objs[2]).doubleValue()).isEqualTo(0.3);
assertThat(new BigDecimal(objs[3].toString())).isEqualTo(new BigDecimal("1.1"));
assertThat(((Number)objs[4]).shortValue()).isEqualTo(5);
}

@Test
public void testObjectArrayWithNonPrimitivesSerialization() throws Exception {
public void testObjectArrayWithNonPrimitivesSerialization() {
ClassWithObjects classWithObjects = new ClassWithObjects();
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();
String classWithObjectsJson = gson.toJson(classWithObjects);
Expand All @@ -226,21 +223,21 @@ public void testObjectArrayWithNonPrimitivesSerialization() throws Exception {
Object[] objects = {classWithObjects, bagOfPrimitives};
String json = gson.toJson(objects);

assertTrue(json.contains(classWithObjectsJson));
assertTrue(json.contains(bagOfPrimitivesJson));
assertThat(json.contains(classWithObjectsJson)).isTrue();
assertThat(json.contains(bagOfPrimitivesJson)).isTrue();
}

@Test
public void testArrayOfNullSerialization() {
Object[] array = {null};
String json = gson.toJson(array);
assertEquals("[null]", json);
assertThat(json).isEqualTo("[null]");
}

@Test
public void testArrayOfNullDeserialization() {
String[] values = gson.fromJson("[null]", String[].class);
assertNull(values[0]);
assertThat(values[0]).isNull();
}

/**
Expand All @@ -253,20 +250,20 @@ public void testMultidimensionalArraysSerialization() {
{"Alcoa Inc", "29.01", "0.42", "1.47", "4/1 12:00am", "Manufacturing"}
};
String json = gson.toJson(items);
assertTrue(json.contains("[[\"3m Co"));
assertTrue(json.contains("Manufacturing\"]]"));
assertThat(json.contains("[[\"3m Co")).isTrue();
assertThat(json.contains("Manufacturing\"]]")).isTrue();
}

@Test
public void testMultidimensionalObjectArraysSerialization() {
Object[][] array = {new Object[] { 1, 2 }};
assertEquals("[[1,2]]", gson.toJson(array));
assertThat(gson.toJson(array)).isEqualTo("[[1,2]]");
}

@Test
public void testMultidimensionalPrimitiveArraysSerialization() {
int[][] array = {{1, 2}, {3, 4}};
assertEquals("[[1,2],[3,4]]", gson.toJson(array));
assertThat(gson.toJson(array)).isEqualTo("[[1,2],[3,4]]");
}

/**
Expand All @@ -275,7 +272,7 @@ public void testMultidimensionalPrimitiveArraysSerialization() {
@Test
public void testMixingTypesInObjectArraySerialization() {
Object[] array = {1, 2, new Object[] {"one", "two", 3}};
assertEquals("[1,2,[\"one\",\"two\",3]]", gson.toJson(array));
assertThat(gson.toJson(array)).isEqualTo("[1,2,[\"one\",\"two\",3]]");
}

/**
Expand All @@ -286,15 +283,15 @@ public void testMultidimensionalArraysDeserialization() {
String json = "[['3m Co','71.72','0.02','0.03','4/2 12:00am','Manufacturing'],"
+ "['Alcoa Inc','29.01','0.42','1.47','4/1 12:00am','Manufacturing']]";
String[][] items = gson.fromJson(json, String[][].class);
assertEquals("3m Co", items[0][0]);
assertEquals("Manufacturing", items[1][5]);
assertThat(items[0][0]).isEqualTo("3m Co");
assertThat(items[1][5]).isEqualTo("Manufacturing");
}

@Test
public void testMultidimensionalPrimitiveArraysDeserialization() {
String json = "[[1,2],[3,4]]";
int[][] expected = {{1, 2}, {3, 4}};
assertArrayEquals(expected, gson.fromJson(json, int[][].class));
assertThat(gson.fromJson(json, int[][].class)).isEqualTo(expected);
}

/** http://code.google.com/p/google-gson/issues/detail?id=342 */
Expand All @@ -304,7 +301,6 @@ public void testArrayElementsAreArrays() {
new String[] {"test1", "test2"},
new String[] {"test3", "test4"}
};
assertEquals("[[\"test1\",\"test2\"],[\"test3\",\"test4\"]]",
new Gson().toJson(stringArrays));
assertThat(new Gson().toJson(stringArrays)).isEqualTo("[[\"test1\",\"test2\"],[\"test3\",\"test4\"]]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package com.google.gson.functional;

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

import com.google.gson.Gson;
Expand Down Expand Up @@ -48,7 +46,7 @@ public void setUp() throws Exception {
}

@Test
public void testCircularSerialization() throws Exception {
public void testCircularSerialization() {
ContainsReferenceToSelfType a = new ContainsReferenceToSelfType();
ContainsReferenceToSelfType b = new ContainsReferenceToSelfType();
a.children.add(b);
Expand All @@ -61,16 +59,16 @@ public void testCircularSerialization() throws Exception {
}

@Test
public void testSelfReferenceIgnoredInSerialization() throws Exception {
public void testSelfReferenceIgnoredInSerialization() {
ClassOverridingEquals objA = new ClassOverridingEquals();
objA.ref = objA;

String json = gson.toJson(objA);
assertFalse(json.contains("ref")); // self-reference is ignored
assertThat(json.contains("ref")).isFalse(); // self-reference is ignored
}

@Test
public void testSelfReferenceArrayFieldSerialization() throws Exception {
public void testSelfReferenceArrayFieldSerialization() {
ClassWithSelfReferenceArray objA = new ClassWithSelfReferenceArray();
objA.children = new ClassWithSelfReferenceArray[]{objA};

Expand All @@ -82,7 +80,7 @@ public void testSelfReferenceArrayFieldSerialization() throws Exception {
}

@Test
public void testSelfReferenceCustomHandlerSerialization() throws Exception {
public void testSelfReferenceCustomHandlerSerialization() {
ClassWithSelfReference obj = new ClassWithSelfReference();
obj.child = obj;
Gson gson = new GsonBuilder().registerTypeAdapter(ClassWithSelfReference.class, new JsonSerializer<ClassWithSelfReference>() {
Expand All @@ -102,22 +100,22 @@ public void testSelfReferenceCustomHandlerSerialization() throws Exception {
}

@Test
public void testDirectedAcyclicGraphSerialization() throws Exception {
public void testDirectedAcyclicGraphSerialization() {
ContainsReferenceToSelfType a = new ContainsReferenceToSelfType();
ContainsReferenceToSelfType b = new ContainsReferenceToSelfType();
ContainsReferenceToSelfType c = new ContainsReferenceToSelfType();
a.children.add(b);
a.children.add(c);
b.children.add(c);
assertNotNull(gson.toJson(a));
assertThat(gson.toJson(a)).isNotNull();
}

@Test
public void testDirectedAcyclicGraphDeserialization() throws Exception {
public void testDirectedAcyclicGraphDeserialization() {
String json = "{\"children\":[{\"children\":[{\"children\":[]}]},{\"children\":[]}]}";
ContainsReferenceToSelfType target = gson.fromJson(json, ContainsReferenceToSelfType.class);
assertNotNull(target);
assertEquals(2, target.children.size());
assertThat(target).isNotNull();
assertThat(target.children).hasSize(2);
}

private static class ContainsReferenceToSelfType {
Expand Down
Loading