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

Commit 5356af7

Browse files
committed
5: Improve demos
Migrate Jackson PoC to SE Task-Url: https://github.com/unitsofmeasurement/uom-demos/issues/issue/5
1 parent 5f738d2 commit 5356af7

3 files changed

Lines changed: 103 additions & 107 deletions

File tree

pom.xml

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
<modelVersion>4.0.0</modelVersion>
77
<parent>
8-
<version>0.5</version>
9-
<artifactId>unitsofmeasurement-parent</artifactId>
10-
<groupId>org.unitsofmeasurement</groupId>
8+
<version>0.7</version>
9+
<artifactId>uom-parent</artifactId>
10+
<groupId>tec.uom</groupId>
1111
</parent>
1212
<groupId>com.opower.unitsofmeasure</groupId>
1313
<artifactId>jackson-module-unitsofmeasure</artifactId>
1414
<packaging>jar</packaging>
15-
<version>1.3.1-uom</version>
15+
<version>1.3.2-uom</version>
1616

1717
<name>Jackson Units of Measurement Module</name>
1818
<description>Contains custom serializers and deserializers for JSR 363.</description>
@@ -25,41 +25,25 @@
2525
<jdkVersion>1.8</jdkVersion>
2626
<maven.compile.targetLevel>1.6</maven.compile.targetLevel>
2727
<maven.compile.sourceLevel>${jdkVersion}</maven.compile.sourceLevel>
28-
<jsr.version>0.5</jsr.version>
29-
<ri.version>0.2</ri.version>
30-
<lib.version>0.1</lib.version> <!-- currently unused -->
28+
<jsr.version>0.7</jsr.version>
29+
<impl.version>0.6</impl.version>
30+
<lib.version>0.5</lib.version> <!-- currently unused -->
3131
<jackson.version>2.2.2</jackson.version>
3232
</properties>
3333

3434
<dependencies>
3535
<!-- =========================================================================================================== -->
36-
<!-- The units of measurement interface is in the org.unitsofmeasurement package. -->
37-
<!-- The implementation is provided by JScience -->
36+
<!-- The units of measurement interface is in the javax.measure package. -->
37+
<!-- The implementation is provided by JSR 363 -->
3838
<!-- =========================================================================================================== -->
3939
<dependency>
4040
<groupId>javax.measure</groupId>
41-
<artifactId>unit-api-core</artifactId>
42-
<version>${jsr.version}</version>
41+
<artifactId>unit-api</artifactId>
4342
</dependency>
4443
<dependency>
45-
<groupId>javax.measure</groupId>
46-
<artifactId>unit-api-format</artifactId>
47-
<version>${jsr.version}</version>
48-
</dependency>
49-
<dependency>
50-
<groupId>javax.measure</groupId>
51-
<artifactId>unit-api-quantity</artifactId>
52-
<version>${jsr.version}</version>
53-
</dependency>
54-
<dependency>
55-
<groupId>javax.measure</groupId>
56-
<artifactId>unit-api-util</artifactId>
57-
<version>${jsr.version}</version>
58-
</dependency>
59-
<dependency>
60-
<groupId>org.unitsofmeasurement</groupId>
61-
<artifactId>unit-ri</artifactId>
62-
<version>${ri.version}</version>
44+
<groupId>tec.uom</groupId>
45+
<artifactId>uom-se</artifactId>
46+
<version>${impl.version}</version>
6347
</dependency>
6448

6549
<!-- =========================================================================================================== -->

src/main/java/com/opower/unitsofmeasure/UnitJacksonModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import com.fasterxml.jackson.databind.module.SimpleModule;
1414
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
1515

16-
import org.unitsofmeasurement.ri.format.UCUMFormat;
17-
import org.unitsofmeasurement.ri.format.UCUMFormat.Variant;
16+
import tec.uom.se.format.UCUMFormat;
17+
import tec.uom.se.format.UCUMFormat.Variant;
1818

1919
import javax.measure.Unit;
2020

src/test/java/com/opower/unitsofmeasure/TestUnitJacksonModule.java

Lines changed: 88 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.io.StringWriter;
5+
import java.io.Writer;
56

67
import com.fasterxml.jackson.core.JsonFactory;
78
import com.fasterxml.jackson.core.JsonGenerator;
@@ -11,89 +12,100 @@
1112

1213
import org.junit.Before;
1314
import org.junit.Test;
15+
import tec.uom.se.util.SI;
16+
import tec.uom.se.util.UCUM;
1417

15-
import org.unitsofmeasurement.ri.util.SI;
16-
import org.unitsofmeasurement.ri.util.UCUM;
1718
import javax.measure.Unit;
1819

19-
import static org.unitsofmeasurement.ri.util.SIPrefix.KILO;
20+
import static tec.uom.se.util.SIPrefix.KILO;
2021
import static org.junit.Assert.*;
2122

2223
/**
2324
* Unit tests for UnitJacksonModule
2425
*/
2526
public class TestUnitJacksonModule {
26-
// can't directly unit test the Jackson Module classes; need to go through JsonFactory
27-
private JsonFactory jsonFactory;
28-
29-
@Before
30-
public void setUp() throws Exception {
31-
ObjectMapper mapper = new ObjectMapper();
32-
33-
mapper.registerModule(new UnitJacksonModule());
34-
35-
this.jsonFactory = new JsonFactory(mapper);
36-
}
37-
38-
@Test
39-
public void testSerializeArea() throws Exception {
40-
assertEquals("Expected JSON with a UCUM representation of the area unit", "\"m2\"", serialize(SI.SQUARE_METRE));
41-
}
42-
43-
@Test
44-
public void testSerializeTemperature() throws Exception {
45-
assertEquals("Expected JSON with a UCUM representation of the temperature unit",
46-
"\"[degF]\"", serialize(UCUM.FAHRENHEIT));
47-
assertEquals("Expected JSON with a UCUM representation of the temperature unit",
48-
"\"Cel\"", serialize(SI.CELSIUS));
49-
}
50-
@Test
51-
public void testSerializeLength() throws Exception {
52-
assertEquals("Expected JSON with a UCUM representation of the length unit",
53-
"\"[mi_i]\"", serialize(UCUM.MILE_INTERNATIONAL));
54-
55-
assertEquals("Expected JSON with a UCUM representation of the length unit", "\"km\"", serialize(KILO(SI.METRE)));
56-
}
57-
58-
@Test
59-
public void testParseArea() throws Exception {
60-
Unit parsedAmount = parse("\"[sft_i]\"", Unit.class);
61-
62-
assertEquals("The Unit<Area> in the parsed JSON doesn't match", UCUM.SQUARE_FOOT_INTERNATIONAL, parsedAmount);
63-
}
64-
65-
@Test
66-
public void testParseTemperature() throws Exception {
67-
Unit parsedAmount = parse("\"Cel\"", Unit.class);
68-
assertEquals("The Unit<Temperature> in the parsed JSON doesn't match", SI.CELSIUS, parsedAmount);
69-
}
70-
71-
@Test
72-
public void testParseLength() throws Exception {
73-
Unit parsedAmount = parse("\"km\"", Unit.class);
74-
75-
assertEquals("The Unit<Length> in the parsed JSON doesn't match", KILO(SI.METRE), parsedAmount);
76-
}
77-
78-
@Test(expected = JsonParseException.class)
79-
public void testParseWithUnrecognizedField() throws Exception {
80-
parse("foobar", Unit.class);
81-
}
82-
83-
protected String serialize(Object objectToSerialize) throws IOException {
84-
StringWriter writer = new StringWriter();
85-
JsonGenerator generator = this.jsonFactory.createJsonGenerator(writer);
86-
87-
generator.writeObject(objectToSerialize);
88-
generator.close();
89-
return writer.toString();
90-
}
91-
92-
protected <T> T parse(String json, Class<T> aClass) throws IOException {
93-
JsonParser parser = this.jsonFactory.createJsonParser(json);
94-
T object = parser.readValueAs(aClass);
95-
96-
parser.close();
97-
return object;
98-
}
27+
// can't directly unit test the Jackson Module classes; need to go through JsonFactory
28+
private JsonFactory jsonFactory;
29+
30+
@Before
31+
public void setUp() throws Exception {
32+
final ObjectMapper mapper = new ObjectMapper();
33+
34+
mapper.registerModule(new UnitJacksonModule());
35+
36+
this.jsonFactory = new JsonFactory(mapper);
37+
}
38+
39+
@Test
40+
public void testSerializeArea() throws Exception {
41+
assertEquals(
42+
"Expected JSON with a UCUM representation of the area unit",
43+
"\"m2\"", serialize(SI.SQUARE_METRE));
44+
}
45+
46+
@Test
47+
public void testSerializeTemperature() throws Exception {
48+
assertEquals(
49+
"Expected JSON with a UCUM representation of the temperature unit",
50+
"\"[degF]\"", serialize(UCUM.FAHRENHEIT));
51+
assertEquals(
52+
"Expected JSON with a UCUM representation of the temperature unit",
53+
"\"Cel\"", serialize(SI.CELSIUS));
54+
}
55+
56+
@Test
57+
public void testSerializeLength() throws Exception {
58+
assertEquals(
59+
"Expected JSON with a UCUM representation of the length unit",
60+
"\"[mi_i]\"", serialize(UCUM.MILE_INTERNATIONAL));
61+
62+
assertEquals(
63+
"Expected JSON with a UCUM representation of the length unit",
64+
"\"km\"", serialize(KILO(SI.METRE)));
65+
}
66+
67+
@Test
68+
public void testParseArea() throws Exception {
69+
Unit<?> parsedUnit = parse("\"[sft_i]\"", Unit.class);
70+
71+
assertEquals("The Unit<Area> in the parsed JSON doesn't match",
72+
UCUM.SQUARE_FOOT_INTERNATIONAL, parsedUnit);
73+
}
74+
75+
@Test
76+
public void testParseTemperature() throws Exception {
77+
Unit<?> parsedUnit = parse("\"Cel\"", Unit.class);
78+
assertEquals("The Unit<Temperature> in the parsed JSON doesn't match",
79+
SI.CELSIUS, parsedUnit);
80+
}
81+
82+
@Test
83+
public void testParseLength() throws Exception {
84+
Unit<?> parsedUnit = parse("\"km\"", Unit.class);
85+
86+
assertEquals("The Unit<Length> in the parsed JSON doesn't match",
87+
KILO(SI.METRE), parsedUnit);
88+
}
89+
90+
@Test(expected = JsonParseException.class)
91+
public void testParseWithUnrecognizedField() throws Exception {
92+
parse("foobar", Unit.class);
93+
}
94+
95+
protected String serialize(Object objectToSerialize) throws IOException {
96+
final Writer writer = new StringWriter();
97+
final JsonGenerator generator = this.jsonFactory.createJsonGenerator(writer);
98+
99+
generator.writeObject(objectToSerialize);
100+
generator.close();
101+
return writer.toString();
102+
}
103+
104+
protected <T> T parse(String json, Class<T> aClass) throws IOException {
105+
final JsonParser parser = this.jsonFactory.createJsonParser(json);
106+
T object = parser.readValueAs(aClass);
107+
108+
parser.close();
109+
return object;
110+
}
99111
}

0 commit comments

Comments
 (0)