Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
5: Improve demos
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Feb 8, 2015
1 parent 5f738d2 commit 5356af7
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 107 deletions.
42 changes: 13 additions & 29 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

<modelVersion>4.0.0</modelVersion>
<parent>
<version>0.5</version>
<artifactId>unitsofmeasurement-parent</artifactId>
<groupId>org.unitsofmeasurement</groupId>
<version>0.7</version>
<artifactId>uom-parent</artifactId>
<groupId>tec.uom</groupId>
</parent>
<groupId>com.opower.unitsofmeasure</groupId>
<artifactId>jackson-module-unitsofmeasure</artifactId>
<packaging>jar</packaging>
<version>1.3.1-uom</version>
<version>1.3.2-uom</version>

<name>Jackson Units of Measurement Module</name>
<description>Contains custom serializers and deserializers for JSR 363.</description>
Expand All @@ -25,41 +25,25 @@
<jdkVersion>1.8</jdkVersion>
<maven.compile.targetLevel>1.6</maven.compile.targetLevel>
<maven.compile.sourceLevel>${jdkVersion}</maven.compile.sourceLevel>
<jsr.version>0.5</jsr.version>
<ri.version>0.2</ri.version>
<lib.version>0.1</lib.version> <!-- currently unused -->
<jsr.version>0.7</jsr.version>
<impl.version>0.6</impl.version>
<lib.version>0.5</lib.version> <!-- currently unused -->
<jackson.version>2.2.2</jackson.version>
</properties>

<dependencies>
<!-- =========================================================================================================== -->
<!-- The units of measurement interface is in the org.unitsofmeasurement package. -->
<!-- The implementation is provided by JScience -->
<!-- The units of measurement interface is in the javax.measure package. -->
<!-- The implementation is provided by JSR 363 -->
<!-- =========================================================================================================== -->
<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api-core</artifactId>
<version>${jsr.version}</version>
<artifactId>unit-api</artifactId>
</dependency>
<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api-format</artifactId>
<version>${jsr.version}</version>
</dependency>
<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api-quantity</artifactId>
<version>${jsr.version}</version>
</dependency>
<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api-util</artifactId>
<version>${jsr.version}</version>
</dependency>
<dependency>
<groupId>org.unitsofmeasurement</groupId>
<artifactId>unit-ri</artifactId>
<version>${ri.version}</version>
<groupId>tec.uom</groupId>
<artifactId>uom-se</artifactId>
<version>${impl.version}</version>
</dependency>

<!-- =========================================================================================================== -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;

import org.unitsofmeasurement.ri.format.UCUMFormat;
import org.unitsofmeasurement.ri.format.UCUMFormat.Variant;
import tec.uom.se.format.UCUMFormat;
import tec.uom.se.format.UCUMFormat.Variant;

import javax.measure.Unit;

Expand Down
164 changes: 88 additions & 76 deletions src/test/java/com/opower/unitsofmeasure/TestUnitJacksonModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -11,89 +12,100 @@

import org.junit.Before;
import org.junit.Test;
import tec.uom.se.util.SI;
import tec.uom.se.util.UCUM;

import org.unitsofmeasurement.ri.util.SI;
import org.unitsofmeasurement.ri.util.UCUM;
import javax.measure.Unit;

import static org.unitsofmeasurement.ri.util.SIPrefix.KILO;
import static tec.uom.se.util.SIPrefix.KILO;
import static org.junit.Assert.*;

/**
* Unit tests for UnitJacksonModule
*/
public class TestUnitJacksonModule {
// can't directly unit test the Jackson Module classes; need to go through JsonFactory
private JsonFactory jsonFactory;

@Before
public void setUp() throws Exception {
ObjectMapper mapper = new ObjectMapper();

mapper.registerModule(new UnitJacksonModule());

this.jsonFactory = new JsonFactory(mapper);
}

@Test
public void testSerializeArea() throws Exception {
assertEquals("Expected JSON with a UCUM representation of the area unit", "\"m2\"", serialize(SI.SQUARE_METRE));
}

@Test
public void testSerializeTemperature() throws Exception {
assertEquals("Expected JSON with a UCUM representation of the temperature unit",
"\"[degF]\"", serialize(UCUM.FAHRENHEIT));
assertEquals("Expected JSON with a UCUM representation of the temperature unit",
"\"Cel\"", serialize(SI.CELSIUS));
}
@Test
public void testSerializeLength() throws Exception {
assertEquals("Expected JSON with a UCUM representation of the length unit",
"\"[mi_i]\"", serialize(UCUM.MILE_INTERNATIONAL));

assertEquals("Expected JSON with a UCUM representation of the length unit", "\"km\"", serialize(KILO(SI.METRE)));
}

@Test
public void testParseArea() throws Exception {
Unit parsedAmount = parse("\"[sft_i]\"", Unit.class);

assertEquals("The Unit<Area> in the parsed JSON doesn't match", UCUM.SQUARE_FOOT_INTERNATIONAL, parsedAmount);
}

@Test
public void testParseTemperature() throws Exception {
Unit parsedAmount = parse("\"Cel\"", Unit.class);
assertEquals("The Unit<Temperature> in the parsed JSON doesn't match", SI.CELSIUS, parsedAmount);
}

@Test
public void testParseLength() throws Exception {
Unit parsedAmount = parse("\"km\"", Unit.class);

assertEquals("The Unit<Length> in the parsed JSON doesn't match", KILO(SI.METRE), parsedAmount);
}

@Test(expected = JsonParseException.class)
public void testParseWithUnrecognizedField() throws Exception {
parse("foobar", Unit.class);
}

protected String serialize(Object objectToSerialize) throws IOException {
StringWriter writer = new StringWriter();
JsonGenerator generator = this.jsonFactory.createJsonGenerator(writer);

generator.writeObject(objectToSerialize);
generator.close();
return writer.toString();
}

protected <T> T parse(String json, Class<T> aClass) throws IOException {
JsonParser parser = this.jsonFactory.createJsonParser(json);
T object = parser.readValueAs(aClass);

parser.close();
return object;
}
// can't directly unit test the Jackson Module classes; need to go through JsonFactory
private JsonFactory jsonFactory;

@Before
public void setUp() throws Exception {
final ObjectMapper mapper = new ObjectMapper();

mapper.registerModule(new UnitJacksonModule());

this.jsonFactory = new JsonFactory(mapper);
}

@Test
public void testSerializeArea() throws Exception {
assertEquals(
"Expected JSON with a UCUM representation of the area unit",
"\"m2\"", serialize(SI.SQUARE_METRE));
}

@Test
public void testSerializeTemperature() throws Exception {
assertEquals(
"Expected JSON with a UCUM representation of the temperature unit",
"\"[degF]\"", serialize(UCUM.FAHRENHEIT));
assertEquals(
"Expected JSON with a UCUM representation of the temperature unit",
"\"Cel\"", serialize(SI.CELSIUS));
}

@Test
public void testSerializeLength() throws Exception {
assertEquals(
"Expected JSON with a UCUM representation of the length unit",
"\"[mi_i]\"", serialize(UCUM.MILE_INTERNATIONAL));

assertEquals(
"Expected JSON with a UCUM representation of the length unit",
"\"km\"", serialize(KILO(SI.METRE)));
}

@Test
public void testParseArea() throws Exception {
Unit<?> parsedUnit = parse("\"[sft_i]\"", Unit.class);

assertEquals("The Unit<Area> in the parsed JSON doesn't match",
UCUM.SQUARE_FOOT_INTERNATIONAL, parsedUnit);
}

@Test
public void testParseTemperature() throws Exception {
Unit<?> parsedUnit = parse("\"Cel\"", Unit.class);
assertEquals("The Unit<Temperature> in the parsed JSON doesn't match",
SI.CELSIUS, parsedUnit);
}

@Test
public void testParseLength() throws Exception {
Unit<?> parsedUnit = parse("\"km\"", Unit.class);

assertEquals("The Unit<Length> in the parsed JSON doesn't match",
KILO(SI.METRE), parsedUnit);
}

@Test(expected = JsonParseException.class)
public void testParseWithUnrecognizedField() throws Exception {
parse("foobar", Unit.class);
}

protected String serialize(Object objectToSerialize) throws IOException {
final Writer writer = new StringWriter();
final JsonGenerator generator = this.jsonFactory.createJsonGenerator(writer);

generator.writeObject(objectToSerialize);
generator.close();
return writer.toString();
}

protected <T> T parse(String json, Class<T> aClass) throws IOException {
final JsonParser parser = this.jsonFactory.createJsonParser(json);
T object = parser.readValueAs(aClass);

parser.close();
return object;
}
}

0 comments on commit 5356af7

Please sign in to comment.