Skip to content

Commit

Permalink
Modification in test cases (#2454)
Browse files Browse the repository at this point in the history
* Fixed Typo in GsonBuilder.java

* Suggestions on Test cases

* Modified test cases using assertThrows method (JUnit)

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/GsonTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

---------

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
  • Loading branch information
elevne and Marcono1234 committed Jul 29, 2023
1 parent 5055b62 commit 3d241ca
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 230 deletions.
26 changes: 7 additions & 19 deletions gson/src/test/java/com/google/gson/GsonTest.java
Expand Up @@ -17,7 +17,7 @@
package com.google.gson;

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

import com.google.gson.Gson.FutureTypeAdapter;
import com.google.gson.internal.Excluder;
Expand Down Expand Up @@ -104,12 +104,8 @@ private static final class TestTypeAdapter extends TypeAdapter<Object> {
@Test
public void testGetAdapter_Null() {
Gson gson = new Gson();
try {
gson.getAdapter((TypeToken<?>) null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("type must not be null");
}
NullPointerException e = assertThrows(NullPointerException.class, () -> gson.getAdapter((TypeToken<?>) null));
assertThat(e).hasMessageThat().isEqualTo("type must not be null");
}

@Test
Expand Down Expand Up @@ -282,13 +278,9 @@ public void testNewJsonWriter_Default() throws IOException {
jsonWriter.value(true);
jsonWriter.endObject();

try {
// Additional top-level value
jsonWriter.value(1);
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("JSON must have only one top-level value.");
}
// Additional top-level value
IllegalStateException e = assertThrows(IllegalStateException.class, () -> jsonWriter.value(1));
assertThat(e).hasMessageThat().isEqualTo("JSON must have only one top-level value.");

jsonWriter.close();
assertThat(writer.toString()).isEqualTo("{\"\\u003ctest2\":true}");
Expand Down Expand Up @@ -323,11 +315,7 @@ public void testNewJsonWriter_Custom() throws IOException {
public void testNewJsonReader_Default() throws IOException {
String json = "test"; // String without quotes
JsonReader jsonReader = new Gson().newJsonReader(new StringReader(json));
try {
jsonReader.nextString();
fail();
} catch (MalformedJsonException expected) {
}
assertThrows(MalformedJsonException.class, jsonReader::nextString);
jsonReader.close();
}

Expand Down
94 changes: 23 additions & 71 deletions gson/src/test/java/com/google/gson/JsonArrayAsListTest.java
Expand Up @@ -17,7 +17,7 @@
package com.google.gson;

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

import com.google.gson.common.MoreAsserts;
import java.util.Arrays;
Expand All @@ -37,17 +37,8 @@ public void testGet() {
List<JsonElement> list = a.asList();
assertThat(list.get(0)).isEqualTo(new JsonPrimitive(1));

try {
list.get(-1);
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.get(2);
fail();
} catch (IndexOutOfBoundsException e) {
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1));
assertThrows(IndexOutOfBoundsException.class, () -> list.get(2));

a.add((JsonElement) null);
assertThat(list.get(1)).isEqualTo(JsonNull.INSTANCE);
Expand Down Expand Up @@ -75,24 +66,11 @@ public void testSet() {
assertThat(list.get(0)).isEqualTo(new JsonPrimitive(2));
assertThat(a.get(0)).isEqualTo(new JsonPrimitive(2));

try {
list.set(-1, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.set(2, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.set(0, null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1)));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(2, new JsonPrimitive(1)));

NullPointerException e = assertThrows(NullPointerException.class, () -> list.set(0, null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}

@Test
Expand All @@ -115,30 +93,14 @@ public void testAdd() {
);
assertThat(list).isEqualTo(expectedList);

try {
list.set(-1, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.set(list.size(), new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.add(0, null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
try {
list.add(null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1)));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(list.size(), new JsonPrimitive(1)));

NullPointerException e = assertThrows(NullPointerException.class, () -> list.add(0, null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");

e = assertThrows(NullPointerException.class, () -> list.add(null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}

@Test
Expand All @@ -157,18 +119,11 @@ public void testAddAll() {
assertThat(list).isEqualTo(expectedList);
assertThat(list).isEqualTo(expectedList);

try {
list.addAll(0, Collections.<JsonElement>singletonList(null));
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
try {
list.addAll(Collections.<JsonElement>singletonList(null));
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
NullPointerException e = assertThrows(NullPointerException.class, () -> list.addAll(0, Collections.<JsonElement>singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");

e = assertThrows(NullPointerException.class, () -> list.addAll(Collections.<JsonElement>singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}

@Test
Expand All @@ -180,11 +135,8 @@ public void testRemoveIndex() {
assertThat(list.remove(0)).isEqualTo(new JsonPrimitive(1));
assertThat(list).hasSize(0);
assertThat(a).hasSize(0);
try {
list.remove(0);
fail();
} catch (IndexOutOfBoundsException e) {
}

assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0));
}

@Test
Expand Down
13 changes: 3 additions & 10 deletions gson/src/test/java/com/google/gson/JsonParserTest.java
Expand Up @@ -17,7 +17,7 @@
package com.google.gson;

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

import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.internal.Streams;
Expand All @@ -37,10 +37,7 @@ public class JsonParserTest {

@Test
public void testParseInvalidJson() {
try {
JsonParser.parseString("[[]");
fail();
} catch (JsonSyntaxException expected) { }
assertThrows(JsonSyntaxException.class, () -> JsonParser.parseString("[[]"));
}

@Test
Expand Down Expand Up @@ -81,11 +78,7 @@ public void testParseUnquotedSingleWordStringFails() {

@Test
public void testParseUnquotedMultiWordStringFails() {
String unquotedSentence = "Test is a test..blah blah";
try {
JsonParser.parseString(unquotedSentence);
fail();
} catch (JsonSyntaxException expected) { }
assertThrows(JsonSyntaxException.class, () -> JsonParser.parseString("Test is a test..blah blah"));
}

@Test
Expand Down
43 changes: 10 additions & 33 deletions gson/src/test/java/com/google/gson/JsonStreamParserTest.java
Expand Up @@ -16,7 +16,7 @@
package com.google.gson;

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

import java.io.EOFException;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -72,57 +72,34 @@ public void testNoSideEffectForHasNext() {
public void testCallingNextBeyondAvailableInput() {
JsonElement unused1 = parser.next();
JsonElement unused2 = parser.next();
try {
parser.next();
fail("Parser should not go beyond available input");
} catch (NoSuchElementException expected) {
}
// Parser should not go beyond available input
assertThrows(NoSuchElementException.class, parser::next);
}

@Test
public void testEmptyInput() {
JsonStreamParser parser = new JsonStreamParser("");
try {
parser.next();
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isInstanceOf(EOFException.class);
}
JsonIOException e = assertThrows(JsonIOException.class, parser::next);
assertThat(e).hasCauseThat().isInstanceOf(EOFException.class);

parser = new JsonStreamParser("");
try {
parser.hasNext();
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isInstanceOf(EOFException.class);
}
e = assertThrows(JsonIOException.class, parser::hasNext);
assertThat(e).hasCauseThat().isInstanceOf(EOFException.class);
}

@Test
public void testIncompleteInput() {
JsonStreamParser parser = new JsonStreamParser("[");
assertThat(parser.hasNext()).isTrue();
try {
parser.next();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::next);
}

@Test
public void testMalformedInput() {
JsonStreamParser parser = new JsonStreamParser(":");
try {
parser.hasNext();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::hasNext);

parser = new JsonStreamParser(":");
try {
parser.next();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::next);
}
}

0 comments on commit 3d241ca

Please sign in to comment.