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

Removal of magic numbers in CollectionTest #2091

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
999e279
Merge pull request #1 from google/master
Lhandi29 Mar 1, 2022
51e723f
fix: remove Magic number in CollectionTest
Lhandi29 Mar 15, 2022
996eca1
fix: remove Magic number in CollectionTest
Lhandi29 Mar 15, 2022
abf30d5
fix: remove Magic number in CollectionTest
Lhandi29 Mar 22, 2022
f776d6c
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
feefe22
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
10ea6e8
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
1c359de
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
e8776a2
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
3c7feb8
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
38b729d
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
e9e9e67
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
f443276
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
3cafb34
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
0c660b4
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
a3e6cd7
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
1d50e3a
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
49e355d
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
7819e74
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
372852b
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
8fa2869
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
098d7bb
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
d879fe5
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
2a25a9a
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
5cfcda6
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
0a4dfb9
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
3d83b70
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
7b5b11d
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
9f491ec
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
3bc33d9
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
62dd893
JUnit assertion to Truth assertion
Lhandi29 Apr 10, 2022
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
8 changes: 8 additions & 0 deletions gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -41,6 +45,10 @@
</configuration>
</execution>
</executions>
<configuration>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required for your change to work?

<source>9</source>
<target>9</target>
</configuration>
</plugin>
<!-- Note: Javadoc plugin has to be run in combination with >= `package`
phase, e.g. `mvn package javadoc:javadoc`, otherwise it fails with
Expand Down
139 changes: 73 additions & 66 deletions gson/src/test/java/com/google/gson/functional/CollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import com.google.gson.reflect.TypeToken;

import junit.framework.TestCase;

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

/**
* Functional tests for Json serialization and deserialization of collections.
Expand All @@ -62,15 +64,15 @@ public void testTopLevelCollectionOfIntegersSerialization() {
Collection<Integer> target = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Type targetType = new TypeToken<Collection<Integer>>() {}.getType();
String json = gson.toJson(target, targetType);
assertEquals("[1,2,3,4,5,6,7,8,9]", json);
assertThat("[1,2,3,4,5,6,7,8,9]").isEqualTo(json);
}

public void testTopLevelCollectionOfIntegersDeserialization() {
String json = "[0,1,2,3,4,5,6,7,8,9]";
Type collectionType = new TypeToken<Collection<Integer>>() { }.getType();
Collection<Integer> target = gson.fromJson(json, collectionType);
int[] expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assertArrayEquals(expected, toIntArray(target));
assertThat(toIntArray(target)).isEqualTo(expected);
}

public void testTopLevelListOfIntegerCollectionsDeserialization() throws Exception {
Expand All @@ -86,7 +88,7 @@ public void testTopLevelListOfIntegerCollectionsDeserialization() throws Excepti
}

for (int i = 0; i < 3; i++) {
assertArrayEquals(expected[i], toIntArray(target.get(i)));
assertThat(toIntArray(target.get(i))).isEqualTo(expected[i]);
}
}

Expand All @@ -96,16 +98,16 @@ public void testLinkedListSerialization() {
list.add("a2");
Type linkedListType = new TypeToken<LinkedList<String>>() {}.getType();
String json = gson.toJson(list, linkedListType);
assertTrue(json.contains("a1"));
assertTrue(json.contains("a2"));
assertThat(json.contains("a1")).isTrue();
assertThat(json.contains("a2")).isTrue();
}

public void testLinkedListDeserialization() {
String json = "['a1','a2']";
Type linkedListType = new TypeToken<LinkedList<String>>() {}.getType();
List<String> list = gson.fromJson(json, linkedListType);
assertEquals("a1", list.get(0));
assertEquals("a2", list.get(1));
assertThat(list.get(0)).isEqualTo("a1");
assertThat(list.get(1)).isEqualTo("a2");
}

public void testQueueSerialization() {
Expand All @@ -114,50 +116,53 @@ public void testQueueSerialization() {
queue.add("a2");
Type queueType = new TypeToken<Queue<String>>() {}.getType();
String json = gson.toJson(queue, queueType);
assertTrue(json.contains("a1"));
assertTrue(json.contains("a2"));
assertThat(json.contains("a1")).isTrue();
assertThat(json.contains("a2")).isTrue();
}

public void testQueueDeserialization() {
String json = "['a1','a2']";
Type queueType = new TypeToken<Queue<String>>() {}.getType();
Queue<String> queue = gson.fromJson(json, queueType);
assertEquals("a1", queue.element());
assertThat(queue.element()).isEqualTo("a1");
queue.remove();
assertEquals("a2", queue.element());
assertThat(queue.element()).isEqualTo("a2");
}

public void testPriorityQueue() throws Exception {
int[] value = {10, 20, 22};
Type type = new TypeToken<PriorityQueue<Integer>>(){}.getType();
PriorityQueue<Integer> queue = gson.fromJson("[10, 20, 22]", type);
assertEquals(3, queue.size());
assertThat(queue.size()).isEqualTo(value.length);
String json = gson.toJson(queue);
assertEquals(10, queue.remove().intValue());
assertEquals(20, queue.remove().intValue());
assertEquals(22, queue.remove().intValue());
assertEquals("[10,20,22]", json);
for (int i = 0; i < value.length; i++){
assertThat(queue.remove().intValue()).isEqualTo(value[i]);
}
assertThat(json).isEqualTo("[10,20,22]");
}

public void testVector() {
int[] value = {10, 20, 31};
Type type = new TypeToken<Vector<Integer>>(){}.getType();
Vector<Integer> target = gson.fromJson("[10, 20, 31]", type);
assertEquals(3, target.size());
assertEquals(10, target.get(0).intValue());
assertEquals(20, target.get(1).intValue());
assertEquals(31, target.get(2).intValue());
assertThat(target.size()).isEqualTo(value.length);
for (int i = 0; i < value.length; i++){
assertThat(target.get(i).intValue()).isEqualTo(value[i]);
}
String json = gson.toJson(target);
assertEquals("[10,20,31]", json);
assertThat(json).isEqualTo("[10,20,31]");
}

public void testStack() {
int[] value = {11, 13, 17};
Type type = new TypeToken<Stack<Integer>>(){}.getType();
Stack<Integer> target = gson.fromJson("[11, 13, 17]", type);
assertEquals(3, target.size());
assertThat(target.size()).isEqualTo(value.length);
String json = gson.toJson(target);
assertEquals(17, target.pop().intValue());
assertEquals(13, target.pop().intValue());
assertEquals(11, target.pop().intValue());
assertEquals("[11,13,17]", json);
for (int i = (value.length - 1); i >= 0; i--){
assertThat(target.pop().intValue()).isEqualTo(value[i]);
}
assertThat(json).isEqualTo("[11,13,17]");
}

public void testNullsInListSerialization() {
Expand All @@ -168,7 +173,7 @@ public void testNullsInListSerialization() {
String expected = "[\"foo\",null,\"bar\"]";
Type typeOfList = new TypeToken<List<String>>() {}.getType();
String json = gson.toJson(list, typeOfList);
assertEquals(expected, json);
assertThat(json).isEqualTo(expected);
}

public void testNullsInListDeserialization() {
Expand All @@ -180,36 +185,37 @@ public void testNullsInListDeserialization() {
Type expectedType = new TypeToken<List<String>>() {}.getType();
List<String> target = gson.fromJson(json, expectedType);
for (int i = 0; i < expected.size(); ++i) {
assertEquals(expected.get(i), target.get(i));
assertThat(target.get(i)).isEqualTo(expected.get(i));
}
}

public void testCollectionOfObjectSerialization() {
List<Object> target = new ArrayList<Object>();
target.add("Hello");
target.add("World");
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target));
assertThat(gson.toJson(target)).isEqualTo("[\"Hello\",\"World\"]");

Type type = new TypeToken<List<Object>>() {}.getType();
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target, type));
assertThat(gson.toJson(target, type)).isEqualTo("[\"Hello\",\"World\"]");
}

public void testCollectionOfObjectWithNullSerialization() {
List<Object> target = new ArrayList<Object>();
target.add("Hello");
target.add(null);
target.add("World");
assertEquals("[\"Hello\",null,\"World\"]", gson.toJson(target));
assertThat(gson.toJson(target)).isEqualTo("[\"Hello\",null,\"World\"]");

Type type = new TypeToken<List<Object>>() {}.getType();
assertEquals("[\"Hello\",null,\"World\"]", gson.toJson(target, type));
assertThat(gson.toJson(target, type)).isEqualTo("[\"Hello\",null,\"World\"]");

}

public void testCollectionOfStringsSerialization() {
List<String> target = new ArrayList<String>();
target.add("Hello");
target.add("World");
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target));
assertThat(gson.toJson(target)).isEqualTo("[\"Hello\",\"World\"]");
}

public void testCollectionOfBagOfPrimitivesSerialization() {
Expand All @@ -220,10 +226,10 @@ public void testCollectionOfBagOfPrimitivesSerialization() {
target.add(objB);

String result = gson.toJson(target);
assertTrue(result.startsWith("["));
assertTrue(result.endsWith("]"));
assertThat(result.startsWith("[")).isTrue();
assertThat(result.endsWith("]")).isTrue();
for (BagOfPrimitives obj : target) {
assertTrue(result.contains(obj.getExpectedJson()));
assertThat(result.contains(obj.getExpectedJson())).isTrue();
}
}

Expand All @@ -232,68 +238,69 @@ public void testCollectionOfStringsDeserialization() {
Type collectionType = new TypeToken<Collection<String>>() { }.getType();
Collection<String> target = gson.fromJson(json, collectionType);

assertTrue(target.contains("Hello"));
assertTrue(target.contains("World"));
//assertTrue(target.contains("Hello"));
assertThat(target.contains("Hello")).isTrue();
assertThat(target.contains("World")).isTrue();
}

public void testRawCollectionOfIntegersSerialization() {
Collection<Integer> target = Arrays.asList(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)).contains("[1,2,3,4,5,6,7,8,9]");
}

@SuppressWarnings("rawtypes")
public void testRawCollectionSerialization() {
BagOfPrimitives bag1 = new BagOfPrimitives();
Collection target = Arrays.asList(bag1, bag1);
String json = gson.toJson(target);
assertTrue(json.contains(bag1.getExpectedJson()));
assertThat(json.contains(bag1.getExpectedJson())).isTrue();
}

@SuppressWarnings("rawtypes")
public void testRawCollectionDeserializationNotAlllowed() {
String json = "[0,1,2,3,4,5,6,7,8,9]";
Collection integers = gson.fromJson(json, Collection.class);
// JsonReader converts numbers to double by default so we need a floating point comparison
assertEquals(Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0), integers);
assertThat(integers).containsAtLeast(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);

json = "[\"Hello\", \"World\"]";
Collection strings = gson.fromJson(json, Collection.class);
assertTrue(strings.contains("Hello"));
assertTrue(strings.contains("World"));
assertThat(strings.contains("Hello")).isTrue();
assertThat(strings.contains("World")).isTrue();
}

@SuppressWarnings({"rawtypes", "unchecked"})
public void testRawCollectionOfBagOfPrimitivesNotAllowed() {
BagOfPrimitives bag = new BagOfPrimitives(10, 20, false, "stringValue");
String json = '[' + bag.getExpectedJson() + ',' + bag.getExpectedJson() + ']';
Collection target = gson.fromJson(json, Collection.class);
assertEquals(2, target.size());
assertThat(target.size()).isEqualTo(2);
for (Object bag1 : target) {
// Gson 2.0 converts raw objects into maps
Map<String, Object> values = (Map<String, Object>) bag1;
assertTrue(values.containsValue(10.0));
assertTrue(values.containsValue(20.0));
assertTrue(values.containsValue("stringValue"));
assertThat(values.containsValue(10.0)).isTrue();
assertThat(values.containsValue(20.0)).isTrue();
assertThat(values.containsValue("stringValue")).isTrue();
}
}

public void testWildcardPrimitiveCollectionSerilaization() throws Exception {
Collection<? extends Integer> target = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Type collectionType = new TypeToken<Collection<? extends Integer>>() { }.getType();
String json = gson.toJson(target, collectionType);
assertEquals("[1,2,3,4,5,6,7,8,9]", json);

assertThat(json).isEqualTo("[1,2,3,4,5,6,7,8,9]");
json = gson.toJson(target);
assertEquals("[1,2,3,4,5,6,7,8,9]", json);
assertThat(json).isEqualTo("[1,2,3,4,5,6,7,8,9]");
}

public void testWildcardPrimitiveCollectionDeserilaization() throws Exception {
String json = "[1,2,3,4,5,6,7,8,9]";
Type collectionType = new TypeToken<Collection<? extends Integer>>() { }.getType();
Collection<? extends Integer> target = gson.fromJson(json, collectionType);
assertEquals(9, target.size());
assertTrue(target.contains(1));
assertTrue(target.contains(9));
assertThat(target.size()).isEqualTo(9);

assertThat(target.contains(1)).isTrue();
assertThat(target.contains(9)).isTrue();
}

public void testWildcardCollectionField() throws Exception {
Expand All @@ -305,24 +312,24 @@ public void testWildcardCollectionField() throws Exception {

ObjectWithWildcardCollection target = new ObjectWithWildcardCollection(collection);
String json = gson.toJson(target);
assertTrue(json.contains(objA.getExpectedJson()));
assertTrue(json.contains(objB.getExpectedJson()));
assertThat(json.contains(objA.getExpectedJson())).isTrue();
assertThat(json.contains(objB.getExpectedJson())).isTrue();

target = gson.fromJson(json, ObjectWithWildcardCollection.class);
Collection<? extends BagOfPrimitives> deserializedCollection = target.getCollection();
assertEquals(2, deserializedCollection.size());
assertTrue(deserializedCollection.contains(objA));
assertTrue(deserializedCollection.contains(objB));
assertThat(deserializedCollection.size()).isEqualTo(2);
assertThat(deserializedCollection.contains(objA)).isTrue();
assertThat(deserializedCollection.contains(objB)).isTrue();
}

public void testFieldIsArrayList() {
HasArrayListField object = new HasArrayListField();
object.longs.add(1L);
object.longs.add(3L);
String json = gson.toJson(object, HasArrayListField.class);
assertEquals("{\"longs\":[1,3]}", json);
assertThat(json).isEqualTo("{\"longs\":[1,3]}");
HasArrayListField copy = gson.fromJson("{\"longs\":[1,3]}", HasArrayListField.class);
assertEquals(Arrays.asList(1L, 3L), copy.longs);
assertThat(copy.longs).isEqualTo(Arrays.asList(1L, 3L));
}

public void testUserCollectionTypeAdapter() {
Expand Down Expand Up @@ -381,16 +388,16 @@ public void testSetSerialization() {
set.add(new Entry(1));
set.add(new Entry(2));
String json = gson.toJson(set);
assertTrue(json.contains("1"));
assertTrue(json.contains("2"));
assertThat(json.contains("1")).isTrue();
assertThat(json.contains("2")).isTrue();
}
public void testSetDeserialization() {
String json = "[{value:1},{value:2}]";
Type type = new TypeToken<Set<Entry>>() {}.getType();
Set<Entry> set = gson.fromJson(json, type);
assertEquals(2, set.size());
assertThat(set.size()).isEqualTo(2);
for (Entry entry : set) {
assertTrue(entry.value == 1 || entry.value == 2);
assertThat(entry.value == 1 || entry.value == 2).isTrue();
}
}

Expand All @@ -408,8 +415,8 @@ public void testIssue1107() {
"}";
BigClass bigClass = new Gson().fromJson(json, BigClass.class);
SmallClass small = bigClass.inBig.get("key").get(0);
assertNotNull(small);
assertEquals("hello", small.inSmall);
assertThat(small).isNotNull();
assertThat(small.inSmall).isEqualTo("hello");
}

}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down