Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,30 @@ public void stringConversion() throws Exception {

@Test
public void propertyConversions() {
JsonObject json = new JsonObject("{\"character\": \"v\", \"numeric\": 2, \"boolean\": true, \"string\": \"value\", \"bytes\": [1, 2]}");
JsonObject json = new JsonObject("{\"character\": \"v\", \"numeric\": 2, \"nullNumeric\": null, \"boolean\": true, \"nullBoolean\": null, \"string\": \"value\", \"bytes\": [1, 2]}");
assertEquals(true, json.get("boolean").asBoolean());
assertNull(json.get("nullNumeric").asObject(Boolean.class));
assertNull(json.get("nonexistent").asObject(Boolean.class));
assertEquals(2, json.get("numeric").asByte());
assertEquals('v', json.get("character").asCharacter());
assertEquals((char) 2, json.get("numeric").asCharacter());
assertEquals(2d, json.get("numeric").asDouble(), 0.001);
assertNull(json.get("nullNumeric").asObject(Double.class));
assertNull(json.get("nonexistent").asObject(Double.class));
assertEquals(2f, json.get("numeric").asFloat(), 0.001);
assertNull(json.get("nullNumeric").asObject(Float.class));
assertNull(json.get("nonexistent").asObject(Float.class));
assertEquals(2, json.get("numeric").asInteger());
assertNull(json.get("nullNumeric").asObject(Integer.class));
assertNull(json.get("nonexistent").asObject(Integer.class));
assertEquals(2l, json.get("numeric").asLong());
assertNull(json.get("nullNumeric").asObject(Long.class));
assertNull(json.get("nonexistent").asObject(Long.class));
assertEquals(2, json.get("numeric").asShort());
assertNull(json.get("nullNumeric").asObject(Short.class));
assertNull(json.get("nonexistent").asObject(Short.class));
assertEquals("value", json.get("string").asString());
assertNull(json.get("nonexistent").asObject(String.class));
assertArrayEquals("value".getBytes(), json.get("string").asByteArray());
assertArrayEquals(new byte[] {(byte) 1, (byte) 2}, json.get("bytes").asByteArray());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package foundation.stack.datamill.reflection.impl;

import foundation.stack.datamill.json.JsonObject;
import foundation.stack.datamill.reflection.Outline;
import foundation.stack.datamill.reflection.OutlineBuilder;
import foundation.stack.datamill.values.StringValue;
Expand All @@ -9,6 +10,7 @@

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -47,19 +49,29 @@ public void nonPropertyMethod() {

public class TestBeanClassWithVariousProperties {
private boolean booleanProperty;
private Boolean booleanWrapperProperty;
private byte byteProperty;
private char charProperty;
private short shortProperty;
private Short shortWrapperProperty;
private int intProperty;
private Integer intWrapperProperty;
private long longProperty;
private Long longWrapperProperty;
private float floatProperty;
private Float floatWrapperProperty;
private double doubleProperty;
private Double doubleWrapperProperty;
private String stringProperty;

public boolean isBooleanProperty() {
return booleanProperty;
}

public Boolean getBooleanWrapperProperty() {
return booleanWrapperProperty;
}

public byte getByteProperty() {
return byteProperty;
}
Expand All @@ -72,22 +84,42 @@ public short getShortProperty() {
return shortProperty;
}

public Short getShortWrapperProperty() {
return shortWrapperProperty;
}

public int getIntProperty() {
return intProperty;
}

public Integer getIntWrapperProperty() {
return intWrapperProperty;
}

public long getLongProperty() {
return longProperty;
}

public Long getLongWrapperProperty() {
return longWrapperProperty;
}

public float getFloatProperty() {
return floatProperty;
}

public Float getFloatWrapperProperty() {
return floatWrapperProperty;
}

public double getDoubleProperty() {
return doubleProperty;
}

public Double getDoubleWrapperProperty() {
return doubleWrapperProperty;
}

public String getStringProperty() {
return stringProperty;
}
Expand All @@ -96,6 +128,10 @@ public void setBooleanProperty(boolean booleanProperty) {
this.booleanProperty = booleanProperty;
}

public void setBooleanWrapperProperty(Boolean booleanWrapperProperty) {
this.booleanWrapperProperty = booleanWrapperProperty;
}

public void setByteProperty(byte byteProperty) {
this.byteProperty = byteProperty;
}
Expand All @@ -108,22 +144,42 @@ public void setShortProperty(short shortProperty) {
this.shortProperty = shortProperty;
}

public void setShortWrapperProperty(Short shortWrapperProperty) {
this.shortWrapperProperty = shortWrapperProperty;
}

public void setIntProperty(int intProperty) {
this.intProperty = intProperty;
}

public void setIntWrapperProperty(Integer intWrapperProperty) {
this.intWrapperProperty = intWrapperProperty;
}

public void setLongProperty(long longProperty) {
this.longProperty = longProperty;
}

public void setLongWrapperProperty(Long longWrapperProperty) {
this.longWrapperProperty = longWrapperProperty;
}

public void setFloatProperty(float floatProperty) {
this.floatProperty = floatProperty;
}

public void setFloatWrapperProperty(Float floatWrapperProperty) {
this.floatWrapperProperty = floatWrapperProperty;
}

public void setDoubleProperty(double doubleProperty) {
this.doubleProperty = doubleProperty;
}

public void setDoubleWrapperProperty(Double doubleWrapperProperty) {
this.doubleWrapperProperty = doubleWrapperProperty;
}

public void setStringProperty(String stringProperty) {
this.stringProperty = stringProperty;
}
Expand Down Expand Up @@ -273,24 +329,52 @@ public void wrapAndBeanSetCastedValue() {
TestBeanClassWithVariousProperties instance = new TestBeanClassWithVariousProperties();
outline.wrap(instance)
.set(m -> m.isBooleanProperty(), new StringValue("true"))
.set(m -> m.getBooleanWrapperProperty(), null)
.set(m -> m.getByteProperty(), new StringValue("10"))
.set(m -> m.getCharProperty(), new StringValue("c"))
.set(m -> m.getDoubleProperty(), new StringValue("1.0"))
.set(m -> m.getDoubleWrapperProperty(), null)
.set(m -> m.getFloatProperty(), new StringValue("2.0"))
.set(m -> m.getFloatWrapperProperty(), null)
.set(m -> m.getIntProperty(), new StringValue("1"))
.set(m -> m.getIntWrapperProperty(), null)
.set(m -> m.getLongProperty(), new StringValue("2"))
.set(m -> m.getLongWrapperProperty(), null)
.set(m -> m.getShortProperty(), new StringValue("3"))
.set(m -> m.getShortWrapperProperty(), null)
.set(m -> m.getStringProperty(), new StringValue("string"));

assertEquals(true, instance.isBooleanProperty());
assertNull(instance.getBooleanWrapperProperty());
assertEquals(10, instance.getByteProperty());
assertEquals('c', instance.getCharProperty());
assertEquals(1.0d, instance.getDoubleProperty(), 0.1);
assertNull(instance.getDoubleWrapperProperty());
assertEquals(2.0f, instance.getFloatProperty(), 0.1);
assertNull(instance.getFloatWrapperProperty());
assertEquals(1, instance.getIntProperty());
assertNull(instance.getIntWrapperProperty());
assertEquals(2, instance.getLongProperty());
assertNull(instance.getLongWrapperProperty());
assertEquals(3, instance.getShortProperty());
assertNull(instance.getShortWrapperProperty());
assertEquals("string", instance.getStringProperty());

JsonObject json = new JsonObject("{\"nullBoolean\": null, \"nullNumeric\": null}");
outline.wrap(instance)
.set(m -> m.getBooleanWrapperProperty(), json.get("nullBoolean"))
.set(m -> m.getDoubleWrapperProperty(), json.get("nullNumeric"))
.set(m -> m.getFloatWrapperProperty(), json.get("nullNumeric"))
.set(m -> m.getIntWrapperProperty(), json.get("nullNumeric"))
.set(m -> m.getLongWrapperProperty(), json.get("nullNumeric"))
.set(m -> m.getShortWrapperProperty(), json.get("nullNumeric"));

assertNull(instance.getBooleanWrapperProperty());
assertNull(instance.getDoubleWrapperProperty());
assertNull(instance.getFloatWrapperProperty());
assertNull(instance.getIntWrapperProperty());
assertNull(instance.getLongWrapperProperty());
assertNull(instance.getShortWrapperProperty());
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencyManagement>
Expand Down