diff --git a/README.md b/README.md index f01dfe7..c4e5414 100644 --- a/README.md +++ b/README.md @@ -39,27 +39,35 @@ Log Log entries, latest entry first, format: YYMMDD. +## 170816 + +Frans: Release 1.5. The BinsonArray.getXxx() method now throws +FormatException for unexpected element types. + ## 170507 -Release 1.4. Minor changes from 1.3. +Frans: Release 1.4. Minor changes from previous release. +Note, this release was not added to the Github releases. ## 160128 -Bug in field sort order fixed thanks to a bug report by Alexander Reshniuk. +Frans: Bug in field sort order fixed thanks to a bug report +by Alexander Reshniuk. ## 151004 -Release 1.1. +Frans: Release 1.1. ## 150925 -Release 1.0. +Frans: Release 1.0. ## 150912 -Added Binson Schema validation (might be called BINSON-SCHEMA-1) in 40 lines of code. +Frans: Added Binson Schema validation (might be called BINSON-SCHEMA-1) +in 40 lines of code. ## 140911 -First upload to github.com. +Frans: First upload to github.com. Before that I stored it locally. diff --git a/build.xml b/build.xml index 9b76579..dd6fb75 100644 --- a/build.xml +++ b/build.xml @@ -7,15 +7,15 @@ Instructions: * Modify stuff marked with "CHANGE" in this file. * Set JAVA_HOME environment variable. -* Use (for example) "1.4-dev" while developing version 1.4, remove "1.4" - for final release. +* Use (for example) "1.4-dev" while developing version 1.4, + remove "1.4" for final release. --> - + diff --git a/src-test/org/binson/BinsonArrayTest.java b/src-test/org/binson/BinsonArrayTest.java new file mode 100644 index 0000000..2192853 --- /dev/null +++ b/src-test/org/binson/BinsonArrayTest.java @@ -0,0 +1,125 @@ +package org.binson; + +import org.junit.Assert; +import org.junit.Test; + +public class BinsonArrayTest { + + @Test + public void testSanity() { + BinsonArray a = new BinsonArray().add(1234); + Assert.assertEquals(1234, a.getInteger(0)); + } + + @Test(expected=FormatException.class) + public void testThatGetIntegerThrowsExceptionWhenFieldHasUnexpectedType() { + BinsonArray a = new BinsonArray().add(12.33); + a.getInteger(0); + } + + @Test + public void testGetBoolean1() { + BinsonArray a = new BinsonArray().add(true); + Assert.assertEquals(true, a.getBoolean(0)); + } + + @Test + public void testGetBoolean2() { + BinsonArray a = new BinsonArray().add(false).add(true).add(true); + Assert.assertEquals(false, a.getBoolean(0)); + Assert.assertEquals(true, a.getBoolean(1)); + Assert.assertEquals(true, a.getBoolean(2)); + } + + @Test(expected=FormatException.class) + public void testGetBoolean3() { + BinsonArray a = new BinsonArray().add(false).add(1234); + Assert.assertEquals(false, a.getBoolean(0)); + Assert.assertEquals(true, a.getBoolean(1)); + } + + @Test + public void testGetInteger1() { + BinsonArray a = new BinsonArray().add("hello").add(1234); + Assert.assertEquals(1234, a.getInteger(1)); + } + + @Test(expected=FormatException.class) + public void testGetInteger2() { + BinsonArray a = new BinsonArray().add("hello").add(1234); + a.getInteger(0); + } + + @Test + public void testGetDouble1() { + BinsonArray a = new BinsonArray().add(12.33); + Assert.assertEquals(12.33, a.getDouble(0), 1e-9); + } + + @Test(expected=FormatException.class) + public void testGetDouble2() { + BinsonArray a = new BinsonArray().add(12.33).add("hello"); + a.getDouble(1); + } + + @Test + public void testGetString1() { + BinsonArray a = new BinsonArray().add("hello"); + Assert.assertEquals("hello", a.getString(0)); + } + + @Test(expected=FormatException.class) + public void testGetString2() { + BinsonArray a = new BinsonArray().add(1234); + a.getString(0); + } + + @Test + public void testGetBytes1() { + BinsonArray a = new BinsonArray().add("hello").add(new byte[]{1, 2, 3}); + Assert.assertArrayEquals(new byte[]{1, 2, 3}, a.getBytes(1)); + } + + @Test(expected=FormatException.class) + public void testGetBytes2() { + BinsonArray a = new BinsonArray().add("hello").add(new byte[]{1, 2, 3}); + a.getBytes(0); + } + + @Test + public void testGetArray1() { + BinsonArray a = new BinsonArray() + .add("hello") + .add(new BinsonArray().add(1).add(2)); + Assert.assertEquals("hello", a.getString(0)); + BinsonArray inner = a.getArray(1); + Assert.assertEquals(1, inner.getInteger(0)); + Assert.assertEquals(2, inner.getInteger(1)); + } + + @Test(expected=FormatException.class) + public void testGetArray2() { + BinsonArray a = new BinsonArray() + .add("hello") + .add(new BinsonArray().add(1).add(2)); + a.getArray(0); + } + + @Test + public void testGetObject1() { + BinsonArray a = new BinsonArray() + .add("hello") + .add(new Binson().put("one", 11).put("two", 22)); + Binson inner = a.getObject(1); + Assert.assertEquals(11, inner.getInteger("one")); + Assert.assertEquals(22, inner.getInteger("two")); + } + + @Test(expected=FormatException.class) + public void testGetObject2() { + BinsonArray a = new BinsonArray() + .add("hello") + .add(new Binson().put("one", 11).put("two", 22)); + a.getObject(0); + } +} diff --git a/src-test/org/binson/DoubleTest.java b/src-test/org/binson/DoubleTest.java index 6992ed1..b949f8e 100644 --- a/src-test/org/binson/DoubleTest.java +++ b/src-test/org/binson/DoubleTest.java @@ -1,8 +1,6 @@ package org.binson; import static org.junit.Assert.*; - -import org.binson.lowlevel.Hex; import org.junit.Assert; import org.junit.Test; diff --git a/src/org/binson/Binson.java b/src/org/binson/Binson.java index c01fb37..ac004bc 100644 --- a/src/org/binson/Binson.java +++ b/src/org/binson/Binson.java @@ -33,8 +33,9 @@ *
 Binson obj = new Binson().put("name", "Frans").put("height", 178);
  * 
* - *

The getX() methods gets a value of the type X. If the value does not exist, a FormatException - * is thrown. To check whether a field of a particular type exist, use the hasX() methods. + *

The getX() methods gets a value of the type X. If the expected value does not exist, + * a FormatException is thrown. To check whether a field of a particular type exists, + * use the hasX() methods.

* *
 obj.getString("name");       // returns "Frans"
  * obj.getInteger("height");    // returns 178 
@@ -49,7 +50,7 @@
  * JSON (http://json.org/) objects. See the methods fromJson() 
  * and toJson().

* - *

The table below shows how Binson types are stored as Java object internally.

+ *

The table below shows how Binson types are stored as Java objects internally.

* *
  * Binson type      Stored as
diff --git a/src/org/binson/BinsonArray.java b/src/org/binson/BinsonArray.java
index 3d2c3d3..9a39446 100644
--- a/src/org/binson/BinsonArray.java
+++ b/src/org/binson/BinsonArray.java
@@ -3,8 +3,11 @@
 import java.util.ArrayList;
 
 /**
- * A Binson array, an ArrayList of heterogenous values.
- * See Binson class for how Binson types are mapped to Java classes.
+ * 

A Binson array is a list of heterogeneous values.

+ * + *

The getX() methods gets a value of the type X. If the expected value does not exist, + * a FormatException is thrown. To check whether a field of a particular type exists, + * use the hasX() methods.

* * @author Frans Lundberg */ @@ -47,7 +50,10 @@ public boolean isBoolean(int index) { public boolean getBoolean(int index) { Object obj = get(index); - return obj instanceof Boolean ? ((Boolean) obj) : false; + if (!(obj instanceof Boolean)) { + throw new FormatException("No boolean in Binson array at index " + index + "."); + } + return ((Boolean) obj).booleanValue(); } // long @@ -62,9 +68,12 @@ public boolean isInteger(int index) { return obj instanceof Long; } - public long getInteger(int index) { + public long getInteger(int index) { Object obj = get(index); - return obj instanceof Long ? ((Long) obj).longValue() : 0; + if (!(obj instanceof Long)) { + throw new FormatException("No integer in Binson array at index " + index + "."); + } + return ((Long) obj).longValue(); } // double @@ -81,7 +90,10 @@ public boolean isDouble(int index) { public double getDouble(int index) { Object obj = get(index); - return obj instanceof Double ? ((Double) obj).doubleValue() : 0.0; + if (!(obj instanceof Double)) { + throw new FormatException("No Double in Binson array at index " + index + "."); + } + return ((Double) obj).doubleValue(); } // string @@ -101,7 +113,10 @@ public boolean isString(int index) { public String getString(int index) { Object obj = get(index); - return obj instanceof String ? (String) obj : null; + if (!(obj instanceof String)) { + throw new FormatException("No String in Binson array at index " + index + "."); + } + return (String) obj; } // bytes @@ -121,7 +136,10 @@ public boolean isBytes(int index) { public byte[] getBytes(int index) { Object obj = get(index); - return obj instanceof byte[] ? (byte[]) obj : null; + if (!(obj instanceof byte[])) { + throw new FormatException("No bytes element in Binson array at index " + index + "."); + } + return (byte[]) obj; } // array @@ -141,7 +159,10 @@ public boolean isArray(int index) { public BinsonArray getArray(int index) { Object obj = get(index); - return obj instanceof BinsonArray ? (BinsonArray) obj : null; + if (!(obj instanceof BinsonArray)) { + throw new FormatException("No BinsonArray in Binson array at index " + index + "."); + } + return (BinsonArray) obj; } // object @@ -161,6 +182,9 @@ public boolean isObject(int index) { public Binson getObject(int index) { Object obj = get(index); - return obj instanceof Binson ? (Binson) obj : null; + if (!(obj instanceof Binson)) { + throw new FormatException("No Binson object in Binson array at index " + index + "."); + } + return (Binson) obj; } }