Skip to content

Commit

Permalink
Trying to reproduce #1353, not succeeding
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 31, 2016
1 parent d00e63c commit f08748f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,15 @@ protected Object _deserialize(String value, DeserializationContext ctxt) throws
int j = value.indexOf(':', i);
int port = j > -1 ? Integer.parseInt(value.substring(j + 1)) : 0;
return new InetSocketAddress(value.substring(0, i + 1), port);
} else {
int ix = value.indexOf(':');
if (ix >= 0 && value.indexOf(':', ix + 1) < 0) {
// host:port
int port = Integer.parseInt(value.substring(ix+1));
return new InetSocketAddress(value.substring(0, ix), port);
}
// host or unbracketed IPv6, without port number
return new InetSocketAddress(value, 0);
}
int ix = value.indexOf(':');
if (ix >= 0 && value.indexOf(':', ix + 1) < 0) {
// host:port
int port = Integer.parseInt(value.substring(ix+1));
return new InetSocketAddress(value.substring(0, ix), port);
}
// host or unbracketed IPv6, without port number
return new InetSocketAddress(value, 0);
}
throw new IllegalArgumentException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -58,7 +57,7 @@ public StackTraceElement deserialize(JsonParser jp,
}

}

/*
/**********************************************************
/* Test methods
Expand All @@ -67,9 +66,79 @@ public StackTraceElement deserialize(JsonParser jp,

private final ObjectMapper MAPPER = objectMapper();

/**
* Related to issues [JACKSON-155], [#170].
*/
public void testCharset() throws Exception
{
Charset UTF8 = Charset.forName("UTF-8");
assertSame(UTF8, MAPPER.readValue(quote("UTF-8"), Charset.class));
}

public void testClass() throws IOException
{
ObjectMapper mapper = new ObjectMapper();
assertSame(String.class, mapper.readValue(quote("java.lang.String"), Class.class));

// then primitive types
assertSame(Boolean.TYPE, mapper.readValue(quote("boolean"), Class.class));
assertSame(Byte.TYPE, mapper.readValue(quote("byte"), Class.class));
assertSame(Short.TYPE, mapper.readValue(quote("short"), Class.class));
assertSame(Character.TYPE, mapper.readValue(quote("char"), Class.class));
assertSame(Integer.TYPE, mapper.readValue(quote("int"), Class.class));
assertSame(Long.TYPE, mapper.readValue(quote("long"), Class.class));
assertSame(Float.TYPE, mapper.readValue(quote("float"), Class.class));
assertSame(Double.TYPE, mapper.readValue(quote("double"), Class.class));
assertSame(Void.TYPE, mapper.readValue(quote("void"), Class.class));
}

public void testClassWithParams() throws IOException
{
String json = MAPPER.writeValueAsString(new ParamClassBean("Foobar"));

ParamClassBean result = MAPPER.readValue(json, ParamClassBean.class);
assertEquals("Foobar", result.name);
assertSame(String.class, result.clazz);
}

public void testClassAsArray() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();
Class<?> result = mapper
.readerFor(Class.class)
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue(quote(String.class.getName()));
assertEquals(String.class, result);

try {
mapper
.readerFor(Class.class)
.without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[" + quote(String.class.getName()) + "]");
fail("Did not throw exception when UNWRAP_SINGLE_VALUE_ARRAYS feature was disabled and attempted to read a Class array containing one element");
} catch (JsonMappingException e) {
verifyException(e, "out of START_ARRAY token");
}

try {
mapper
.readerFor(Class.class)
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[" + quote(Object.class.getName()) + "," + quote(Object.class.getName()) +"]");
fail("Did not throw exception when UNWRAP_SINGLE_VALUE_ARRAYS feature was enabled and attempted to read a Class array containing two elements");
} catch (JsonMappingException e) {
verifyException(e, "more than a single value in");
}
result = mapper
.readerFor(Class.class)
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[" + quote(String.class.getName()) + "]");
assertEquals(String.class, result);
}

public void testCurrency() throws IOException
{
Currency usd = Currency.getInstance("USD");
assertEquals(usd, new ObjectMapper().readValue(quote("USD"), Currency.class));
}

public void testFile() throws Exception
{
// Not portable etc... has to do:
Expand All @@ -89,24 +158,6 @@ public void testFile() throws Exception
assertEquals(abs, result.getAbsolutePath());
}

public void testRegexps() throws IOException
{
final String PATTERN_STR = "abc:\\s?(\\d+)";
Pattern exp = Pattern.compile(PATTERN_STR);
/* Ok: easiest way is to just serialize first; problem
* is the backslash
*/
String json = MAPPER.writeValueAsString(exp);
Pattern result = MAPPER.readValue(json, Pattern.class);
assertEquals(exp.pattern(), result.pattern());
}

public void testCurrency() throws IOException
{
Currency usd = Currency.getInstance("USD");
assertEquals(usd, new ObjectMapper().readValue(quote("USD"), Currency.class));
}

public void testLocale() throws IOException
{
assertEquals(new Locale("en"), MAPPER.readValue(quote("en"), Locale.class));
Expand Down Expand Up @@ -164,113 +215,60 @@ public void testInetSocketAddress() throws IOException
assertEquals(80, address.getPort());
}

public void testRegexps() throws IOException
{
final String PATTERN_STR = "abc:\\s?(\\d+)";
Pattern exp = Pattern.compile(PATTERN_STR);
/* Ok: easiest way is to just serialize first; problem
* is the backslash
*/
String json = MAPPER.writeValueAsString(exp);
Pattern result = MAPPER.readValue(json, Pattern.class);
assertEquals(exp.pattern(), result.pattern());
}

public void testURI() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();

URI value = new URI("http://foo.com");
assertEquals(value, mapper.readValue("\""+value.toString()+"\"", URI.class));
final ObjectReader reader = MAPPER.readerFor(URI.class);
final URI value = new URI("http://foo.com");
assertEquals(value, reader.readValue("\""+value.toString()+"\""));

mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
try {
assertEquals(value, mapper.readValue("[\""+value.toString()+"\"]", URI.class));
fail("Did not throw exception for single value array when UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
} catch (JsonMappingException exp) {
//exception thrown successfully
}
// Also: empty String should be handled properly
URI result = reader.readValue(quote(""));
assertNotNull(result);
assertEquals(URI.create(""), result);

// and finally: broken URI should give proper failure
try {
assertEquals(value, mapper.readValue("[\""+value.toString()+"\",\""+value.toString()+"\"]", URI.class));
fail("Did not throw exception for single value array when there were multiple values");
} catch (JsonMappingException exp) {
//exception thrown successfully
result = reader.readValue(quote("a b"));
fail("Should not accept malformed URI, instead got: "+result);
} catch (InvalidFormatException e) {
verifyException(e, "not a valid textual representation");
}

mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
assertEquals(value, mapper.readValue("[\""+value.toString()+"\"]", URI.class));

value = mapper.readValue(quote(""), URI.class);
assertNotNull(value);
assertEquals(URI.create(""), value);
}

public void testClass() throws IOException
{
ObjectMapper mapper = new ObjectMapper();
assertSame(String.class, mapper.readValue(quote("java.lang.String"), Class.class));

// then primitive types
assertSame(Boolean.TYPE, mapper.readValue(quote("boolean"), Class.class));
assertSame(Byte.TYPE, mapper.readValue(quote("byte"), Class.class));
assertSame(Short.TYPE, mapper.readValue(quote("short"), Class.class));
assertSame(Character.TYPE, mapper.readValue(quote("char"), Class.class));
assertSame(Integer.TYPE, mapper.readValue(quote("int"), Class.class));
assertSame(Long.TYPE, mapper.readValue(quote("long"), Class.class));
assertSame(Float.TYPE, mapper.readValue(quote("float"), Class.class));
assertSame(Double.TYPE, mapper.readValue(quote("double"), Class.class));
assertSame(Void.TYPE, mapper.readValue(quote("void"), Class.class));
}

public void testClassWithParams() throws IOException
{
String json = MAPPER.writeValueAsString(new ParamClassBean("Foobar"));

ParamClassBean result = MAPPER.readValue(json, ParamClassBean.class);
assertEquals("Foobar", result.name);
assertSame(String.class, result.clazz);
}

public void testClassAsArray() throws Exception
public void testURIAsArray() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();
Class<?> result = mapper
.readerFor(Class.class)
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue(quote(String.class.getName()));
assertEquals(String.class, result);

final ObjectReader reader = MAPPER.readerFor(URI.class);
final URI value = new URI("http://foo.com");
try {
mapper
.readerFor(Class.class)
.without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[" + quote(String.class.getName()) + "]");
fail("Did not throw exception when UNWRAP_SINGLE_VALUE_ARRAYS feature was disabled and attempted to read a Class array containing one element");
reader.without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[\""+value.toString()+"\"]");
fail("Did not throw exception for single value array when UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
} catch (JsonMappingException e) {
verifyException(e, "out of START_ARRAY token");
}

try {
mapper
.readerFor(Class.class)
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[" + quote(Object.class.getName()) + "," + quote(Object.class.getName()) +"]");
fail("Did not throw exception when UNWRAP_SINGLE_VALUE_ARRAYS feature was enabled and attempted to read a Class array containing two elements");
reader.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[\""+value.toString()+"\",\""+value.toString()+"\"]");
fail("Did not throw exception for single value array when there were multiple values");
} catch (JsonMappingException e) {
verifyException(e, "more than a single value in");
}
result = mapper
.readerFor(Class.class)
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[" + quote(String.class.getName()) + "]");
assertEquals(String.class, result);
}

public void testUntypedWithJsonArrays() throws Exception
{
// by default we get:
Object ob = MAPPER.readValue("[1]", Object.class);
assertTrue(ob instanceof List<?>);

// but can change to produce Object[]:
MAPPER.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
ob = MAPPER.readValue("[1]", Object.class);
assertEquals(Object[].class, ob.getClass());
}

public void testCharset() throws Exception
{
Charset UTF8 = Charset.forName("UTF-8");
assertSame(UTF8, MAPPER.readValue(quote("UTF-8"), Charset.class));
verifyException(e, "more than a single value in the array");
}
assertEquals(value,
reader.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[\""+value.toString()+"\"]"));
}

public void testStackTraceElement() throws Exception
Expand Down Expand Up @@ -469,11 +467,4 @@ public void testURL() throws Exception
assertSame(value, MAPPER.readValue(buf.asParser(), URL.class));
buf.close();
}

public void testNull() throws Exception
{
// null doesn't really have a type, fake by assuming Object
Object result = MAPPER.readValue(" null", Object.class);
assertNull(result);
}
}
Loading

0 comments on commit f08748f

Please sign in to comment.