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

Modification in test cases #2454

Merged
merged 16 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
25 changes: 6 additions & 19 deletions gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
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,8 @@ 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.");
}
IllegalStateException e = assertThrows(IllegalStateException.class, () -> jsonWriter.value(1));
elevne marked this conversation as resolved.
Show resolved Hide resolved
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 +314,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
110 changes: 31 additions & 79 deletions gson/src/test/java/com/google/gson/JsonArrayAsListTest.java
Original file line number Diff line number Diff line change
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 @@ -107,38 +85,22 @@ public void testAdd() {
assertThat(list.add(JsonNull.INSTANCE)).isTrue();

List<JsonElement> expectedList = Arrays.<JsonElement>asList(
new JsonPrimitive(2),
new JsonPrimitive(3),
new JsonPrimitive(1),
new JsonPrimitive(4),
JsonNull.INSTANCE
new JsonPrimitive(2),
new JsonPrimitive(3),
new JsonPrimitive(1),
new JsonPrimitive(4),
JsonNull.INSTANCE
elevne marked this conversation as resolved.
Show resolved Hide resolved
);
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 @@ -150,25 +112,18 @@ public void testAddAll() {
list.addAll(Arrays.asList(new JsonPrimitive(2), new JsonPrimitive(3)));

List<JsonElement> expectedList = Arrays.<JsonElement>asList(
new JsonPrimitive(1),
new JsonPrimitive(2),
new JsonPrimitive(3)
new JsonPrimitive(1),
new JsonPrimitive(2),
new JsonPrimitive(3)
elevne marked this conversation as resolved.
Show resolved Hide resolved
);
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
Original file line number Diff line number Diff line change
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
42 changes: 9 additions & 33 deletions gson/src/test/java/com/google/gson/JsonStreamParserTest.java
Original file line number Diff line number Diff line change
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,33 @@ 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) {
}
assertThrows(NoSuchElementException.class, parser::next);
elevne marked this conversation as resolved.
Show resolved Hide resolved
}

@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.getCause()).isInstanceOf(EOFException.class);
elevne marked this conversation as resolved.
Show resolved Hide resolved

parser = new JsonStreamParser("");
try {
parser.hasNext();
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isInstanceOf(EOFException.class);
}
e = assertThrows(JsonIOException.class, parser::hasNext);
assertThat(e.getCause()).isInstanceOf(EOFException.class);
elevne marked this conversation as resolved.
Show resolved Hide resolved
}

@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);
}
}
Loading