Skip to content

Commit

Permalink
The BinsonArray.getXxx() method now throws FormatException for unexpe…
Browse files Browse the repository at this point in the history
…ted types. Release 1.5.
  • Loading branch information
franslundberg committed Aug 16, 2017
1 parent 398425e commit 56c0b90
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 24 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

6 changes: 3 additions & 3 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-->

<project name="binson" default="build" basedir=".">

<target name="init" description="Initializes properties and prints version, release, and build.">
<property name="release" value="1.4-dev"/> <!-- CHANGE for release build -->
<property name="release" value="1.5"/> <!-- CHANGE for release build -->

<tstamp>
<format property="dateString" pattern="yyMMdd" />
Expand Down
125 changes: 125 additions & 0 deletions src-test/org/binson/BinsonArrayTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 0 additions & 2 deletions src-test/org/binson/DoubleTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
7 changes: 4 additions & 3 deletions src/org/binson/Binson.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
* <pre> Binson obj = new Binson().put(&quot;name&quot;, &quot;Frans&quot;).put(&quot;height&quot;, 178);
* </pre>
*
* <p>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.
* <p>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.</p>
*
* <pre> obj.getString(&quot;name&quot;); // returns &quot;Frans&quot;
* obj.getInteger(&quot;height&quot;); // returns 178
Expand All @@ -49,7 +50,7 @@
* JSON (http://json.org/) objects. See the methods <code>fromJson()</code>
* and <code>toJson()</code>.</p>
*
* <p>The table below shows how Binson types are stored as Java object internally.</p>
* <p>The table below shows how Binson types are stored as Java objects internally.</p>
*
* <pre>
* Binson type Stored as
Expand Down
44 changes: 34 additions & 10 deletions src/org/binson/BinsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>A Binson array is a list of heterogeneous values.</p>
*
* <p>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.</p>
*
* @author Frans Lundberg
*/
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}

0 comments on commit 56c0b90

Please sign in to comment.