Skip to content

Commit

Permalink
StringParserTest added
Browse files Browse the repository at this point in the history
  • Loading branch information
rmhari committed Feb 29, 2024
1 parent a1c10e4 commit 453cf86
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/test/java/com/techatpark/sjson/core/StringParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.techatpark.sjson.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.io.StringReader;

import static org.junit.jupiter.api.Assertions.assertThrows;

class StringParserTest {

final ObjectMapper objectMapper = new ObjectMapper();

/**
* Tests Valid String values.
* <p>
* Steps:
* 1) Pass valid string value(original value).
* 2) Get JSON String from Jackson . (jsonString)
* 3) With this JSON String read java object using JSON.
* </p>
* Expected Result:
* This value should be equal to originalvalue.
* @param originalValue
* @throws IOException
*/
@ParameterizedTest
@ValueSource(strings = {
"Hari",
"Escapes \" \\",
"Tabnext \n \b \t \f \r",
"Emoji \uD83D\uDE00 \u0000"
})
void testValid(final String originalValue) throws IOException {

String jsonString = objectMapper.writeValueAsString(originalValue);
Assertions.assertEquals(originalValue, new Json().read(new StringReader(jsonString)));
}

/**
* Tests Invalid String values.
* <p>
* Steps:
* 1) Pass valid string value(original value).
* 2) Read java object using JSON.
* </p>
* Expected Result:
* IllegalArguementException should be thrown.
* @param invalidjson
*/
@ParameterizedTest
@ValueSource(strings = {
"\"\\s\"",
"\"dddd",
"\"\n",
"\"20\\r\n\""
})
void testInvalid(final String invalidjson) throws IOException {
assertThrows(IllegalArgumentException.class,
() -> new Json().read(new StringReader(invalidjson)));
}



}

0 comments on commit 453cf86

Please sign in to comment.