@ -19,7 +19,9 @@ package org.springframework.boot.json;
import java.util.List ;
import java.util.Map ;
import org.junit.Rule ;
import org.junit.Test ;
import org.junit.rules.ExpectedException ;
import static org.junit.Assert.assertEquals ;
@ -27,15 +29,19 @@ import static org.junit.Assert.assertEquals;
* Base for { @link JsonParser } tests .
*
* @author Dave Syer
* @author Jean de Klerk
* /
public abstract class AbstractJsonParserTests {
@Rule
public ExpectedException thrown = ExpectedException . none ( ) ;
private final JsonParser parser = getParser ( ) ;
protected abstract JsonParser getParser ( ) ;
@Test
public void te stS impleMap( ) {
public void simpleMap( ) {
Map < String , Object > map = this . parser . parseMap ( "{\"foo\":\"bar\",\"spam\":1}" ) ;
assertEquals ( 2 , map . size ( ) ) ;
assertEquals ( "bar" , map . get ( "foo" ) ) ;
@ -43,7 +49,7 @@ public abstract class AbstractJsonParserTests {
}
@Test
public void testD oubleValue( ) {
public void d oubleValue( ) {
Map < String , Object > map = this . parser . parseMap ( "{\"foo\":\"bar\",\"spam\":1.23}" ) ;
assertEquals ( 2 , map . size ( ) ) ;
assertEquals ( "bar" , map . get ( "foo" ) ) ;
@ -51,27 +57,27 @@ public abstract class AbstractJsonParserTests {
}
@Test
public void t estE mptyMap( ) {
public void emptyMap( ) {
Map < String , Object > map = this . parser . parseMap ( "{}" ) ;
assertEquals ( 0 , map . size ( ) ) ;
}
@Test
public void te stS impleList( ) {
public void simpleList( ) {
List < Object > list = this . parser . parseList ( "[\"foo\",\"bar\",1]" ) ;
assertEquals ( 3 , list . size ( ) ) ;
assertEquals ( "bar" , list . get ( 1 ) ) ;
}
@Test
public void t estE mptyList( ) {
public void emptyList( ) {
List < Object > list = this . parser . parseList ( "[]" ) ;
assertEquals ( 0 , list . size ( ) ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testL istOfMaps( ) {
public void l istOfMaps( ) {
List < Object > list = this . parser
. parseList ( "[{\"foo\":\"bar\",\"spam\":1},{\"foo\":\"baz\",\"spam\":2}]" ) ;
assertEquals ( 2 , list . size ( ) ) ;
@ -80,11 +86,73 @@ public abstract class AbstractJsonParserTests {
@SuppressWarnings ( "unchecked" )
@Test
public void testM apOfLists( ) {
public void m apOfLists( ) {
Map < String , Object > map = this . parser . parseMap (
"{\"foo\":[{\"foo\":\"bar\",\"spam\":1},{\"foo\":\"baz\",\"spam\":2}]}" ) ;
assertEquals ( 1 , map . size ( ) ) ;
assertEquals ( 2 , ( ( List < Object > ) map . get ( "foo" ) ) . size ( ) ) ;
}
@Test
public void mapWithNullThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseMap ( null ) ;
}
@Test
public void listWithNullThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseList ( null ) ;
}
@Test
public void mapWithEmptyStringThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseMap ( "" ) ;
}
@Test
public void listWithEmptyStringThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseList ( "" ) ;
}
@Test
public void mapWithListThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseMap ( "[]" ) ;
}
@Test
public void listWithMapThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseList ( "{}" ) ;
}
@Test
public void listWithLeadingWhitespace ( ) {
List < Object > list = this . parser . parseList ( "\n\t[\"foo\"]" ) ;
assertEquals ( 1 , list . size ( ) ) ;
assertEquals ( "foo" , list . get ( 0 ) ) ;
}
@Test
public void mapWithLeadingWhitespace ( ) {
Map < String , Object > map = this . parser . parseMap ( "\n\t{\"foo\":\"bar\"}" ) ;
assertEquals ( 1 , map . size ( ) ) ;
assertEquals ( "bar" , map . get ( "foo" ) ) ;
}
@Test
public void mapWithLeadingWhitespaceListThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseMap ( "\n\t[]" ) ;
}
@Test
public void listWithLeadingWhitespaceMapThrowsARuntimeException ( ) {
this . thrown . expect ( RuntimeException . class ) ;
this . parser . parseList ( "\n\t{}" ) ;
}
}