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