You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by sa...@apache.org on 2014/10/30 17:46:47 UTC
[1/2] git commit: JOHNZON-17 Thiago Veronezi:
RFC4627AwareInputStreamReader - Make sure that we read all the bytes before
throwing an exception
Repository: incubator-johnzon
Updated Branches:
refs/heads/master 80cf6a02f -> b7948d517
JOHNZON-17 Thiago Veronezi: RFC4627AwareInputStreamReader - Make sure that we read all the bytes before throwing an exception
Project: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/commit/399a368c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/tree/399a368c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/diff/399a368c
Branch: refs/heads/master
Commit: 399a368c4a15e2e6b53b7f87f1cfc9d16a2132d7
Parents: 80cf6a0
Author: Hendrik Saly <he...@gmail.com>
Authored: Thu Oct 30 16:22:34 2014 +0100
Committer: Hendrik Saly <he...@gmail.com>
Committed: Thu Oct 30 16:22:34 2014 +0100
----------------------------------------------------------------------
.../core/RFC4627AwareInputStreamReader.java | 90 +++++++++++---------
.../org/apache/johnzon/core/JsonParserTest.java | 66 +++++++++++++-
2 files changed, 112 insertions(+), 44 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/399a368c/johnzon-core/src/main/java/org/apache/johnzon/core/RFC4627AwareInputStreamReader.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/RFC4627AwareInputStreamReader.java b/johnzon-core/src/main/java/org/apache/johnzon/core/RFC4627AwareInputStreamReader.java
index e74e69d..645cf0e 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/RFC4627AwareInputStreamReader.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/RFC4627AwareInputStreamReader.java
@@ -37,6 +37,27 @@ final class RFC4627AwareInputStreamReader extends InputStreamReader {
}
+ /**
+ * According to the Java API "An attempt is made to read as many as len bytes, but a smaller number may be read".
+ * [http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[],%20int,%20int)]
+ * For this reason we need to ensure that we've read all the bytes that we need out of this stream.
+ */
+ private static byte[] readAllBytes(final PushbackInputStream inputStream) throws IOException {
+ final int first = inputStream.read();
+ final int second = inputStream.read();
+ if(first == -1|| second == -1) {
+ throw new JsonException("Invalid Json. Valid Json has at least 2 bytes");
+ }
+ final int third = inputStream.read();
+ final int fourth = inputStream.read();
+ if(third == -1) {
+ return new byte[] { (byte) first, (byte) second };
+ } else if(fourth == -1) {
+ return new byte[] { (byte) first, (byte) second, (byte) third };
+ } else {
+ return new byte[] { (byte) first, (byte) second, (byte) third, (byte) fourth };
+ }
+ }
/*
* RFC 4627
@@ -59,24 +80,18 @@ final class RFC4627AwareInputStreamReader extends InputStreamReader {
private static Charset getCharset(final PushbackInputStream inputStream) {
Charset charset = Charset.forName("UTF-8");
- final byte[] utfBytes = new byte[4];
int bomLength=0;
try {
- final int read = inputStream.read(utfBytes);
- if (read < 2) {
- throw new JsonException("Invalid Json. Valid Json has at least 2 bytes");
+ final byte[] utfBytes = readAllBytes(inputStream);
+ int first = (utfBytes[0] & 0xFF);
+ int second = (utfBytes[1] & 0xFF);
+ if (first == 0x00) {
+ charset = (second == 0x00) ? Charset.forName("UTF-32BE") : Charset.forName("UTF-16BE");
+ } else if (utfBytes.length > 2 && second == 0x00) {
+ int third = (utfBytes[2] & 0xFF);
+ charset = (third == 0x00) ? Charset.forName("UTF-32LE") : Charset.forName("UTF-16LE");
} else {
- int first = (utfBytes[0] & 0xFF);
- int second = (utfBytes[1] & 0xFF);
-
- if (first == 0x00) {
- charset = (second == 0x00) ? Charset.forName("UTF-32BE") : Charset.forName("UTF-16BE");
- } else if (read > 2 && second == 0x00) {
- int third = (utfBytes[2] & 0xFF);
- charset = (third == 0x00) ? Charset.forName("UTF-32LE") : Charset.forName("UTF-16LE");
- } else {
-
/*check BOM
Encoding hex byte order mark
@@ -86,42 +101,33 @@ final class RFC4627AwareInputStreamReader extends InputStreamReader {
UTF-32 (BE) 00 00 FE FF
UTF-32 (LE) FF FE 00 00
*/
-
- //We do not check for UTF-32BE because that is already covered above and we
- //do not to unread anything.
-
-
- if(first == 0xFE && second == 0xFF) {
- charset = Charset.forName("UTF-16BE");
+
+ //We do not check for UTF-32BE because that is already covered above and we
+ //do not to unread anything.
+
+ if(first == 0xFE && second == 0xFF) {
+ charset = Charset.forName("UTF-16BE");
+ bomLength=2;
+ } else if(first == 0xFF && second == 0xFE) {
+ if(utfBytes.length > 3 && (utfBytes[2]&0xff) == 0x00 && (utfBytes[3]&0xff) == 0x00) {
+ charset = Charset.forName("UTF-32LE");
+ bomLength=4;
+ }else {
+ charset = Charset.forName("UTF-16LE");
bomLength=2;
- } else if(first == 0xFF && second == 0xFE) {
-
- if(read > 3 && (utfBytes[2]&0xff) == 0x00 && (utfBytes[3]&0xff) == 0x00) {
- charset = Charset.forName("UTF-32LE");
- bomLength=4;
- }else {
- charset = Charset.forName("UTF-16LE");
- bomLength=2;
- }
-
- } else if (read > 2 && first == 0xEF && second == 0xBB && (utfBytes[2]&0xff) == 0xBF) {
-
- //UTF-8 with BOM
- bomLength=3;
}
-
+ } else if (utfBytes.length > 2 && first == 0xEF && second == 0xBB && (utfBytes[2]&0xff) == 0xBF) {
+ //UTF-8 with BOM
+ bomLength=3;
}
-
- //assume UTF8
-
}
-
+ //assume UTF8
if(bomLength > 0 && bomLength < 4) {
//do not unread BOM, only bytes after BOM
- inputStream.unread(utfBytes,bomLength,read-bomLength);
+ inputStream.unread(utfBytes,bomLength,utfBytes.length - bomLength);
} else {
//no BOM, unread all read bytes
- inputStream.unread(utfBytes,0,read);
+ inputStream.unread(utfBytes);
}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/399a368c/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
index e9cc0ea..07d22d7 100644
--- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
@@ -964,7 +964,29 @@ public class JsonParserTest {
}
@Test
+ public void testBinaryNullStreamBOM() {
+ ByteArrayInputStream bin = new ByteArrayInputStream("\ufeff\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".getBytes(UTF_32LE));
+ JsonParser parser = Json.createParser(bin);
+
+ try {
+ parser.next();
+ fail();
+ } catch (JsonParsingException e) {
+ //expected
+ }
+
+ }
+
+ @Test(expected=JsonParsingException.class)
+ public void testBinaryNullStream() {
+ ByteArrayInputStream bin = new ByteArrayInputStream("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".getBytes(UTF_8));
+ JsonParser parser = Json.createParser(bin);
+ parser.next();
+ }
+
+ @Test
public void testUTF32BEBOMStream() {
+
ByteArrayInputStream bin = new ByteArrayInputStream("\ufeff[\"UTF32BEBOM\"]".getBytes(UTF_32BE));
JsonParser parser = Json.createParser(bin);
parser.next();
@@ -988,6 +1010,18 @@ public class JsonParserTest {
}
@Test
+ public void testStreamReadNotAllBytes() {
+ AttemptingInputStream bin = new AttemptingInputStream("\ufeff[\"UTF8BOM\"]".getBytes(UTF_8));
+ JsonParser parser = Json.createParser(bin);
+ parser.next();
+ parser.next();
+ assertEquals("UTF8BOM", parser.getString());
+ parser.next();
+ assertTrue(!parser.hasNext());
+ parser.close();
+ }
+
+ @Test
public void shortestNonEmtyJsonFile() {
// using a reader as wrapper of parser
@@ -1573,6 +1607,34 @@ public class JsonParserTest {
}
}
-
-
+ class AttemptingInputStream extends ByteArrayInputStream {
+
+ public AttemptingInputStream(byte[] buf) {
+ super(buf);
+
+ }
+
+ @Override
+ public synchronized int read(byte b[], int off, int len) {
+
+ if (b == null) {
+ throw new NullPointerException();
+ } else if (off < 0 || 1 > b.length - off) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ if (pos >= count) {
+ return -1;
+ }
+
+ int avail = count - pos;
+
+ if (avail == 0) {
+ return 0;
+ }
+ System.arraycopy(buf, pos, b, off, 1);
+ pos++;
+ return 1;
+ }
+ }
}
[2/2] git commit: Update checkstyle configuration (and fixed some
check style errors), update contributorsrs, some minor pom.xml changes
Posted by sa...@apache.org.
Update checkstyle configuration (and fixed some check style errors), update contributorsrs, some minor pom.xml changes
Project: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/commit/b7948d51
Tree: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/tree/b7948d51
Diff: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/diff/b7948d51
Branch: refs/heads/master
Commit: b7948d5174018ddf04c95a4abd8107720d20155e
Parents: 399a368
Author: Hendrik Saly <he...@gmail.com>
Authored: Thu Oct 30 17:45:45 2014 +0100
Committer: Hendrik Saly <he...@gmail.com>
Committed: Thu Oct 30 17:45:45 2014 +0100
----------------------------------------------------------------------
.../jaxrs/xml/WadlDocumentToJsonTest.java | 7 +-
.../org/apache/johnzon/mapper/LiteralTest.java | 41 ++--
.../johnzon/mapper/MapperEnhancedTest.java | 13 +-
.../org/apache/johnzon/mapper/MapperTest.java | 226 ++++++++-----------
pom.xml | 40 +++-
5 files changed, 165 insertions(+), 162 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/b7948d51/johnzon-jaxrs/src/test/java/org/apache/johnzon/jaxrs/xml/WadlDocumentToJsonTest.java
----------------------------------------------------------------------
diff --git a/johnzon-jaxrs/src/test/java/org/apache/johnzon/jaxrs/xml/WadlDocumentToJsonTest.java b/johnzon-jaxrs/src/test/java/org/apache/johnzon/jaxrs/xml/WadlDocumentToJsonTest.java
index ba19b0d..cf82890 100644
--- a/johnzon-jaxrs/src/test/java/org/apache/johnzon/jaxrs/xml/WadlDocumentToJsonTest.java
+++ b/johnzon-jaxrs/src/test/java/org/apache/johnzon/jaxrs/xml/WadlDocumentToJsonTest.java
@@ -56,6 +56,11 @@ public class WadlDocumentToJsonTest {
"</application>").getBytes()));
final String json = new WadlDocumentToJson().convert(doc);
- assertEquals("{\"application\":{\"xmlns\":\"http://wadl.dev.java.net/2009/02\",\"resources\":[{\"base\":\"http://example.com/api\",\"resource\":[{\"path\":\"books\",\"method\":[{\"name\":\"GET\"}],\"resource\":[{\"path\":\"{bookId}\",\"param\":[{\"name\":\"bookId\",\"required\":\"true\",\"style\":\"template\"}],\"method\":[{\"name\":\"GET\"},{\"name\":\"DELETE\"}],\"resource\":[{\"path\":\"reviews\",\"method\":[{\"name\":\"GET\",\"request\":[{\"param\":[{\"default\":\"1\",\"name\":\"page\",\"required\":\"false\",\"style\":\"query\"},{\"default\":\"20\",\"name\":\"size\",\"required\":\"false\",\"style\":\"query\"}]}]}]}]}]},{\"path\":\"readers\",\"method\":[{\"name\":\"GET\"}]}]}]}}", json);
+ assertEquals("{\"application\":{\"xmlns\":\"http://wadl.dev.java.net/2009/02\",\"resources\":[{\"base\":\"http://example.com/api\""
+ + ",\"resource\":[{\"path\":\"books\",\"method\":[{\"name\":\"GET\"}],\"resource\":[{\"path\":\"{bookId}\",\"param\":[{\"name\""
+ + ":\"bookId\",\"required\":\"true\",\"style\":\"template\"}],\"method\":[{\"name\":\"GET\"},{\"name\":\"DELETE\"}],\"resource\""
+ + ":[{\"path\":\"reviews\",\"method\":[{\"name\":\"GET\",\"request\":[{\"param\":[{\"default\":\"1\",\"name\":\"page\",\"required\""
+ + ":\"false\",\"style\":\"query\"},{\"default\":\"20\",\"name\":\"size\",\"required\":\"false\",\"style\":\"query\"}]}]}]}]}]},{\"path\""
+ + ":\"readers\",\"method\":[{\"name\":\"GET\"}]}]}]}}", json);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/b7948d51/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/LiteralTest.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/LiteralTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/LiteralTest.java
index 4423559..147f71f 100644
--- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/LiteralTest.java
+++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/LiteralTest.java
@@ -18,7 +18,7 @@
*/
package org.apache.johnzon.mapper;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
import java.io.StringReader;
import java.io.StringWriter;
@@ -26,12 +26,12 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Comparator;
-import static org.junit.Assert.assertEquals;
+import org.junit.Test;
public class LiteralTest {
@Test
public void writeReadNumbers() {
- NumberClass nc = new NumberClass();
+ final NumberClass nc = new NumberClass();
final StringWriter sw = new StringWriter();
nc.setBg(new BigDecimal("123.123"));
nc.setBi(new BigInteger("123"));
@@ -43,7 +43,8 @@ public class LiteralTest {
nc.setLongNumber(123L);
nc.setIntNumber(123);
- final String expectedJson = "{\"shortNumber\":1,\"byteNumber\":1,\"intNumber\":123,\"floatNumber\":123.0,\"bg\":123.123,\"bool\":true,\"bi\":123,\"longNumber\":123,\"doubleNumber\":123.123}";
+ final String expectedJson = "{\"shortNumber\":1,\"byteNumber\":1,\"intNumber\":123,\"floatNumber\":123.0,\"bg\":123.123,\""
+ + "bool\":true,\"bi\":123,\"longNumber\":123,\"doubleNumber\":123.123}";
final Comparator<String> attributeOrder = new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
@@ -53,14 +54,14 @@ public class LiteralTest {
new MapperBuilder().setAttributeOrder(attributeOrder).build().writeObject(nc, sw);
assertEquals(expectedJson, sw.toString());
final NumberClass read = new MapperBuilder().setAttributeOrder(attributeOrder).build()
- .readObject(new StringReader(sw.toString()), NumberClass.class);
+ .readObject(new StringReader(sw.toString()), NumberClass.class);
assertEquals(nc, read);
}
@Test(expected = NumberFormatException.class)
public void writeReadNumbersInf() {
- NumberClass nc = new NumberClass();
+ final NumberClass nc = new NumberClass();
final StringWriter sw = new StringWriter();
nc.setBg(new BigDecimal("123.123"));
nc.setBi(new BigInteger("123"));
@@ -78,7 +79,7 @@ public class LiteralTest {
@Test(expected = NumberFormatException.class)
public void writeReadNumbersNaN() {
- NumberClass nc = new NumberClass();
+ final NumberClass nc = new NumberClass();
final StringWriter sw = new StringWriter();
nc.setBg(new BigDecimal("123.123"));
nc.setBi(new BigInteger("123"));
@@ -109,7 +110,7 @@ public class LiteralTest {
return bg;
}
- public void setBg(BigDecimal bg) {
+ public void setBg(final BigDecimal bg) {
this.bg = bg;
}
@@ -117,7 +118,7 @@ public class LiteralTest {
return bi;
}
- public void setBi(BigInteger bi) {
+ public void setBi(final BigInteger bi) {
this.bi = bi;
}
@@ -125,7 +126,7 @@ public class LiteralTest {
return intNumber;
}
- public void setIntNumber(int intNumber) {
+ public void setIntNumber(final int intNumber) {
this.intNumber = intNumber;
}
@@ -133,7 +134,7 @@ public class LiteralTest {
return longNumber;
}
- public void setLongNumber(long longNumber) {
+ public void setLongNumber(final long longNumber) {
this.longNumber = longNumber;
}
@@ -141,7 +142,7 @@ public class LiteralTest {
return byteNumber;
}
- public void setByteNumber(byte byteNumber) {
+ public void setByteNumber(final byte byteNumber) {
this.byteNumber = byteNumber;
}
@@ -149,7 +150,7 @@ public class LiteralTest {
return shortNumber;
}
- public void setShortNumber(short shortNumber) {
+ public void setShortNumber(final short shortNumber) {
this.shortNumber = shortNumber;
}
@@ -157,7 +158,7 @@ public class LiteralTest {
return doubleNumber;
}
- public void setDoubleNumber(double doubleNumber) {
+ public void setDoubleNumber(final double doubleNumber) {
this.doubleNumber = doubleNumber;
}
@@ -165,7 +166,7 @@ public class LiteralTest {
return floatNumber;
}
- public void setFloatNumber(float floatNumber) {
+ public void setFloatNumber(final float floatNumber) {
this.floatNumber = floatNumber;
}
@@ -173,7 +174,7 @@ public class LiteralTest {
return bool;
}
- public void setBool(boolean bool) {
+ public void setBool(final boolean bool) {
this.bool = bool;
}
@@ -196,7 +197,7 @@ public class LiteralTest {
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
@@ -206,7 +207,7 @@ public class LiteralTest {
if (getClass() != obj.getClass()) {
return false;
}
- NumberClass other = (NumberClass) obj;
+ final NumberClass other = (NumberClass) obj;
if (bg == null) {
if (other.bg != null) {
return false;
@@ -247,7 +248,9 @@ public class LiteralTest {
@Override
public String toString() {
- return "NumberClass [bg=" + bg + ", bi=" + bi + ", intNumber=" + intNumber + ", longNumber=" + longNumber + ", byteNumber=" + byteNumber + ", shortNumber=" + shortNumber + ", doubleNumber=" + doubleNumber + ", floatNumber=" + floatNumber + ", bool=" + bool + "]";
+ return "NumberClass [bg=" + bg + ", bi=" + bi + ", intNumber=" + intNumber + ", longNumber=" + longNumber + ", byteNumber="
+ + byteNumber + ", shortNumber=" + shortNumber + ", doubleNumber=" + doubleNumber + ", floatNumber=" + floatNumber
+ + ", bool=" + bool + "]";
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/b7948d51/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperEnhancedTest.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperEnhancedTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperEnhancedTest.java
index 12401f0..97ce8b0 100644
--- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperEnhancedTest.java
+++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperEnhancedTest.java
@@ -56,8 +56,17 @@ public class MapperEnhancedTest {
charClass.setCharArr(new char[]{'G','O'});
final StringWriter sw = new StringWriter();
- new MapperBuilder().build().writeObject(charClass, sw);
- assertEquals("{\"charArr\":[\"G\",\"O\"],\"charValue\":\"G\"}", sw.toString());
+ final String expectedJson = "{\"charArr\":[\"G\",\"O\"],\"charValue\":\"G\"}";
+
+ final Comparator<String> attributeOrder = new Comparator<String>() {
+ @Override
+ public int compare(final String o1, final String o2) {
+ return expectedJson.indexOf(o1) - expectedJson.indexOf(o2);
+ }
+ };
+
+ new MapperBuilder().setAttributeOrder(attributeOrder).build().writeObject(charClass, sw);
+ assertEquals(expectedJson, sw.toString());
CharClass read = new MapperBuilder().build().readObject(new StringReader(sw.toString()), CharClass.class);
Assert.assertNotNull(read);
Assert.assertEquals('G', read.getCharValue());
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/b7948d51/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
index fa58a1f..59711d8 100644
--- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
+++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
@@ -18,9 +18,11 @@
*/
package org.apache.johnzon.mapper;
-import junit.framework.AssertionFailedError;
-
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -30,51 +32,17 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import junit.framework.AssertionFailedError;
+
+import org.junit.Test;
public class MapperTest {
- private static final String BIG_OBJECT_STR = "{" +
- "\"name\":\"the string\"," +
- "\"integer\":56," +
- "\"longnumber\":118," +
- "\"bool\":true," +
- "\"nested\":{" +
- "\"name\":\"another value\"," +
- "\"integer\":97," +
- "\"longnumber\":34" +
- "}," +
- "\"array\":[" +
- "{" +
- "\"name\":\"a1\"," +
- "\"integer\":1," +
- "\"longnumber\":2" +
- "}," +
- "{" +
- "\"name\":\"a2\"," +
- "\"integer\":3," +
- "\"longnumber\":4" +
- "}" +
- "]," +
- "\"list\":[" +
- "{" +
- "\"name\":\"a3\"," +
- "\"integer\":5," +
- "\"longnumber\":6" +
- "}," +
- "{" +
- "\"name\":\"a4\"," +
- "\"integer\":7," +
- "\"longnumber\":8" +
- "}" +
- "]," +
- "\"primitives\":[1,2,3,4,5]," +
- "\"collectionWrapper\":[1,2,3,4,5]," +
- "\"map\":{\"uno\":true,\"duos\":false}" +
- "}";
+ private static final String BIG_OBJECT_STR = "{" + "\"name\":\"the string\"," + "\"integer\":56," + "\"longnumber\":118,"
+ + "\"bool\":true," + "\"nested\":{" + "\"name\":\"another value\"," + "\"integer\":97," + "\"longnumber\":34" + "},"
+ + "\"array\":[" + "{" + "\"name\":\"a1\"," + "\"integer\":1," + "\"longnumber\":2" + "}," + "{" + "\"name\":\"a2\","
+ + "\"integer\":3," + "\"longnumber\":4" + "}" + "]," + "\"list\":[" + "{" + "\"name\":\"a3\"," + "\"integer\":5,"
+ + "\"longnumber\":6" + "}," + "{" + "\"name\":\"a4\"," + "\"integer\":7," + "\"longnumber\":8" + "}" + "],"
+ + "\"primitives\":[1,2,3,4,5]," + "\"collectionWrapper\":[1,2,3,4,5]," + "\"map\":{\"uno\":true,\"duos\":false}" + "}";
@Test
public void writeEmptyObject() {
@@ -100,113 +68,106 @@ public class MapperTest {
@Test
public void writeMap() {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- new MapperBuilder().build().writeObject(new LinkedHashMap<String, Integer>() {{
- put("a", 1);
- put("b", 2);
- }}, baos);
+ new MapperBuilder().build().writeObject(new LinkedHashMap<String, Integer>() {
+ {
+ put("a", 1);
+ put("b", 2);
+ }
+ }, baos);
assertEquals("{\"a\":1,\"b\":2}", new String(baos.toByteArray()));
}
@Test
public void writeObject() {
- final TheObject instance = new MapperBuilder().build()
- .readObject(new ByteArrayInputStream(BIG_OBJECT_STR.getBytes()), TheObject.class); // suppose reader writes but this is tested
+ final TheObject instance = new MapperBuilder().build().readObject(new ByteArrayInputStream(BIG_OBJECT_STR.getBytes()),
+ TheObject.class); // suppose reader writes but this is tested
final StringWriter writer = new StringWriter();
new MapperBuilder().build().writeObject(instance, writer);
final String serialized = writer.toString();
assertTrue(serialized.contains("\"primitives\":[1,2,3,4,5]"));
assertTrue(serialized.contains("\"collectionWrapper\":[1,2,3,4,5]"));
assertTrue(serialized.contains("\"bool\":true"));
-
- //Assert fail with oracle java 1.7.0_45, works well with apple java 1.6.0_65
+
+ //Assert fail with oracle java 1.7.0_45, works well with apple java 1.6.0_65
//assertTrue(serialized.contains("\"map\":{\"uno\":true,\"duos\":false}"));
assertTrue(serialized.contains("\"map\":{"));
assertTrue(serialized.contains("\"uno\":true"));
assertTrue(serialized.contains("\"duos\":false"));
-
- TheObject instance2 = new MapperBuilder().build()
+
+ final TheObject instance2 = new MapperBuilder().build()
.readObject(new ByteArrayInputStream(serialized.getBytes()), TheObject.class); // suppose reader writes but this is tested
-
+
assertEquals(instance, instance2);
}
-
- static class Bool{
+
+ static class Bool {
boolean bool;
public boolean isBool() {
return bool;
}
- public void setBool(boolean bool) {
+ public void setBool(final boolean bool) {
this.bool = bool;
}
-
-
+
}
-
- static class Bool2{
+
+ static class Bool2 {
Map<String, Boolean> map;
public Map<String, Boolean> getMap() {
return map;
}
- public void setMap(Map<String, Boolean> map) {
+ public void setMap(final Map<String, Boolean> map) {
this.map = map;
}
-
-
-
}
-
+
@Test
public void literal() {
-
-
-
- final Bool instance = new MapperBuilder().build()
- .readObject(new ByteArrayInputStream("{\"bool\":true}".getBytes()), Bool.class);
-
+
+ final Bool instance = new MapperBuilder().build().readObject(new ByteArrayInputStream("{\"bool\":true}".getBytes()), Bool.class);
+
assertTrue(instance.bool);
-
+
final StringWriter writer = new StringWriter();
new MapperBuilder().build().writeObject(instance, writer);
final String serialized = writer.toString();
assertEquals("{\"bool\":true}", serialized);
-
+
}
-
- @Test(expected= MapperException.class)
+
+ @Test(expected = MapperException.class)
public void literalFail() {
-
+
final Bool instance = new MapperBuilder().build()
.readObject(new ByteArrayInputStream("{\"bool\":\"true\"}".getBytes()), Bool.class);
-
+
assertTrue(instance.bool);
-
+
}
-
- @Test(expected= MapperException.class)
+
+ @Test(expected = MapperException.class)
public void literalFail2() {
-
- final Bool2 instance = new MapperBuilder().build()
- .readObject(new ByteArrayInputStream("{\"map\":{\"key\":\"true\"}}".getBytes()), Bool2.class);
-
-
-
+
+ final Bool2 instance = new MapperBuilder().build().readObject(new ByteArrayInputStream("{\"map\":{\"key\":\"true\"}}".getBytes()),
+ Bool2.class);
+
}
@Test
public void writeArray() {
{ // integer
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- new MapperBuilder().build().writeArray(new Integer[]{1, 2}, baos);
+ new MapperBuilder().build().writeArray(new Integer[] { 1, 2 }, baos);
assertEquals("[1,2]", new String(baos.toByteArray()));
}
{ // object
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- new MapperBuilder().build().writeArray(new Pair[]{new Pair(1, "a"), new Pair(2, "b")}, baos);
+ new MapperBuilder().build().writeArray(new Pair[] { new Pair(1, "a"), new Pair(2, "b") }, baos);
try {
assertEquals("[{\"s\":\"a\",\"i\":1},{\"s\":\"b\",\"i\":2}]", new String(baos.toByteArray()));
} catch (final AssertionFailedError afe) { // a bit lazy but to avoid field ordering on java > 6
@@ -217,7 +178,8 @@ public class MapperTest {
@Test
public void readObject() {
- final TheObject object = new MapperBuilder().build().readObject(new ByteArrayInputStream(BIG_OBJECT_STR.getBytes()), TheObject.class);
+ final TheObject object = new MapperBuilder().build().readObject(new ByteArrayInputStream(BIG_OBJECT_STR.getBytes()),
+ TheObject.class);
assertNotNull(object);
assertEquals("the string", object.name);
@@ -261,18 +223,9 @@ public class MapperTest {
@Test
public void readArray() {
- final TheObject[] object = new MapperBuilder().build().readArray(new ByteArrayInputStream(("[" +
- "{" +
- "\"name\":\"a3\"," +
- "\"integer\":5," +
- "\"longnumber\":6" +
- "}," +
- "{" +
- "\"name\":\"a4\"," +
- "\"integer\":7," +
- "\"longnumber\":8" +
- "}" +
- "]").getBytes()), TheObject.class);
+ final TheObject[] object = new MapperBuilder().build().readArray(
+ new ByteArrayInputStream(("[" + "{" + "\"name\":\"a3\"," + "\"integer\":5," + "\"longnumber\":6" + "}," + "{"
+ + "\"name\":\"a4\"," + "\"integer\":7," + "\"longnumber\":8" + "}" + "]").getBytes()), TheObject.class);
assertNotNull(object);
assertEquals(2, object.length);
assertEquals("a3", object[0].name);
@@ -410,53 +363,70 @@ public class MapperTest {
}
@Override
- public boolean equals(Object obj) {
- if (this == obj)
+ public boolean equals(final Object obj) {
+ if (this == obj) {
return true;
- if (obj == null)
+ }
+ if (obj == null) {
return false;
- if (getClass() != obj.getClass())
+ }
+ if (getClass() != obj.getClass()) {
return false;
- TheObject other = (TheObject) obj;
- if (!Arrays.equals(array, other.array))
+ }
+ final TheObject other = (TheObject) obj;
+ if (!Arrays.equals(array, other.array)) {
return false;
- if (bool != other.bool)
+ }
+ if (bool != other.bool) {
return false;
+ }
if (collectionWrapper == null) {
- if (other.collectionWrapper != null)
+ if (other.collectionWrapper != null) {
return false;
- } else if (!collectionWrapper.equals(other.collectionWrapper))
+ }
+ } else if (!collectionWrapper.equals(other.collectionWrapper)) {
return false;
- if (integer != other.integer)
+ }
+ if (integer != other.integer) {
return false;
+ }
if (list == null) {
- if (other.list != null)
+ if (other.list != null) {
return false;
- } else if (!list.equals(other.list))
+ }
+ } else if (!list.equals(other.list)) {
return false;
- if (longnumber != other.longnumber)
+ }
+ if (longnumber != other.longnumber) {
return false;
+ }
if (map == null) {
- if (other.map != null)
+ if (other.map != null) {
return false;
- } else if (!map.equals(other.map))
+ }
+ } else if (!map.equals(other.map)) {
return false;
+ }
if (name == null) {
- if (other.name != null)
+ if (other.name != null) {
return false;
- } else if (!name.equals(other.name))
+ }
+ } else if (!name.equals(other.name)) {
return false;
+ }
if (nested == null) {
- if (other.nested != null)
+ if (other.nested != null) {
return false;
- } else if (!nested.equals(other.nested))
+ }
+ } else if (!nested.equals(other.nested)) {
return false;
- if (!Arrays.equals(primitives, other.primitives))
+ }
+ if (!Arrays.equals(primitives, other.primitives)) {
return false;
+ }
return true;
}
-
-
+
}
public static class Pair {
@@ -486,7 +456,7 @@ public class MapperTest {
}
@JohnzonConverter(ReverseConverter.class)
- public void setS(String v) {
+ public void setS(final String v) {
s = v;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/b7948d51/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 1e1e0f1..8522ff6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -66,7 +66,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
@@ -78,7 +77,6 @@
<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
- <version>0.11</version>
<configuration>
<includes>
<include>src/**/*</include>
@@ -110,7 +108,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-publish-plugin</artifactId>
- <version>1.1</version>
<configuration>
<automaticRemotePathCreation>true</automaticRemotePathCreation>
<content>${project.reporting.outputDirectory}</content>
@@ -132,7 +129,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
- <version>3.3</version>
<configuration>
<stagingDirectory>${staging.directory}</stagingDirectory>
</configuration>
@@ -147,7 +143,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
- <version>2.12.1</version>
+ <version>2.13</version>
<executions>
<execution>
<id>verify-style</id>
@@ -158,7 +154,13 @@
</execution>
</executions>
<configuration>
- <logViolationsToConsole>true</logViolationsToConsole>
+ <encoding>UTF-8</encoding>
+ <consoleOutput>true</consoleOutput>
+ <failOnViolation>true</failOnViolation>
+ <includeTestSourceDirectory>true</includeTestSourceDirectory>
+ <failsOnError>true</failsOnError>
+ <linkXRef>true</linkXRef>
+ <logViolationsToConsole>true</logViolationsToConsole>
<checkstyleRules>
<module name="Checker">
<module name="SuppressionCommentFilter" />
@@ -234,7 +236,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
- <version>2.17</version>
<configuration>
<args>-Dfile.encoding=UTF-8</args>
</configuration>
@@ -242,8 +243,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
- <version>2.5.1</version>
- <configuration>
+ <configuration>
<tagNameFormat>v@{project.version}</tagNameFormat>
<pushChanges>false</pushChanges>
<localCheckout>true</localCheckout>
@@ -389,6 +389,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.17</version>
+ <configuration>
+ <aggregate>true</aggregate>
+ </configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
@@ -401,7 +404,19 @@
<aggregate>true</aggregate>
</configuration>
</plugin>
- </plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-checkstyle-plugin</artifactId>
+ <version>2.13</version>
+ <reportSets>
+ <reportSet>
+ <reports>
+ <report>checkstyle</report>
+ </reports>
+ </reportSet>
+ </reportSets>
+ </plugin>
+ </plugins>
</reporting>
<licenses>
@@ -515,7 +530,8 @@
</developers>
<contributors>
-
-
+ <contributor>
+ <name>Thiago Veronezi</name>
+ </contributor>
</contributors>
</project>