Skip to content

Commit

Permalink
Test google#43: Changed tests to make use of assertThrows
Browse files Browse the repository at this point in the history
* test google#43: Changed tests to make use of assertThrows as per feedback

* Test google#43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* Test google#43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* test google#43: Resolve PR recommendations

* Test google#43: Mini change to TC

* Test google#43: Mini change to TC

---------

Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>
  • Loading branch information
gustajoh and marten-voorberg authored Mar 2, 2023
1 parent 0d65c62 commit 2b812f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 75 deletions.
109 changes: 38 additions & 71 deletions gson/src/test/java/com/google/gson/stream/JsonReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.io.EOFException;
import java.io.IOException;
Expand Down Expand Up @@ -66,25 +67,17 @@ public void testSetStrictness() {
@Test
public void testSetStrictnessNull() {
JsonReader reader = new JsonReader(reader("{}"));
try {
reader.setStrictness(null);
fail();
} catch (NullPointerException expected) {
//OK: It should not be possible to set the strictness to null.
}
assertThrows(NullPointerException.class, () -> reader.setStrictness(null));
}

@Test
public void testEscapedNewlineNotAllowedInStrictMode() throws IOException {
String json = "\"\\\n\"";
JsonReader reader = new JsonReader(reader(json));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextString();
fail();
} catch (IOException expected) {
assertThat(expected.getMessage()).contains("Cannot escape a newline character in strict mode!");
}

IOException expected = assertThrows(IOException.class, reader::nextString);
assertThat(expected.getMessage()).contains("Cannot escape a newline character in strict mode!");
}

@Test
Expand All @@ -99,12 +92,9 @@ public void testStrictModeFailsToParseUnescapedControlCharacter() {
String json = "\"\t\"";
JsonReader reader = new JsonReader(reader(json));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextString();
fail();
} catch (IOException e) {
assertThat(e.getMessage()).contains("strict");
}

IOException expected = assertThrows(IOException.class, reader::nextString);
assertThat(expected.getMessage()).contains("Unescaped control characters (\\u0000-\\u001F) are not allowed in strict mode.");
}

@Test
Expand All @@ -118,69 +108,51 @@ public void testNonStrictModeParsesUnescapedControlCharacter() throws IOExceptio
public void testCapitalizedTrueFailWhenStrict() throws IOException {
JsonReader reader = new JsonReader(reader("TRUE"));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextBoolean();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

IOException expected = assertThrows(IOException.class, reader::nextString);
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");

reader = new JsonReader(reader("True"));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextBoolean();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

expected = assertThrows(IOException.class, reader::nextString);
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

@Test
public void testCapitalizedNullFailWhenStrict() throws IOException {
JsonReader reader = new JsonReader(reader("NULL"));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextNull();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

IOException expected = assertThrows(IOException.class, reader::nextNull);
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");

reader = new JsonReader(reader("nulL"));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextNull();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

expected = assertThrows(IOException.class, reader::nextNull);
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

@Test
public void testCapitalizedFalseFailWhenStrict() throws IOException {
JsonReader reader = new JsonReader(reader("FALSE"));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextBoolean();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

IOException expected = assertThrows(IOException.class, reader::nextBoolean);
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");

reader = new JsonReader(reader("FaLse"));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextBoolean();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

expected = assertThrows(IOException.class, reader::nextBoolean);
assertThat(expected).hasMessageThat().isEqualTo("Use JsonReader.setLenient(true) to accept malformed" +
" JSON at line 1 column 1 path $");
}

@Test
Expand Down Expand Up @@ -497,12 +469,9 @@ public void testEscapeCharacterQuoteInStrictMode() throws IOException {
String json = "\"\\'\"";
JsonReader reader = new JsonReader(reader(json));
reader.setStrictness(Strictness.STRICT);
try {
reader.nextString();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().contains("Invalid escaped character \"'\" in strict mode");
}

IOException expected = assertThrows(IOException.class, reader::nextString);
assertThat(expected).hasMessageThat().contains("Invalid escaped character \"'\" in strict mode");
}

@Test
Expand Down Expand Up @@ -530,11 +499,9 @@ public void testUnescapedControlCharactersInStrictMode() throws IOException {
JsonReader reader = new JsonReader(reader(json));
reader.setStrictness(Strictness.STRICT);
reader.beginArray();
try {
reader.nextString();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().contains("Unescaped control characters");
}

IOException expected = assertThrows(IOException.class, reader::nextString);
assertThat(expected).hasMessageThat().contains("Unescaped control characters");
}

@Test
Expand Down
6 changes: 2 additions & 4 deletions gson/src/test/java/com/google/gson/stream/JsonWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.gson.stream;

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

import com.google.gson.FormattingStyle;
Expand Down Expand Up @@ -70,10 +71,7 @@ public void testSetStrictness() throws IOException {
public void testSetStrictnessNull() throws IOException {
JsonWriter jsonWriter = new JsonWriter(new StringWriter());
try {
jsonWriter.setStrictness(null);
fail();
} catch (NullPointerException expected) {
// OK: Setting the strictness to null should throw a null pointer exception!
assertThrows(NullPointerException.class, () -> jsonWriter.setStrictness(null));
} finally {
jsonWriter.value(false);
jsonWriter.close();
Expand Down

0 comments on commit 2b812f1

Please sign in to comment.