|
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.io.StringWriter; |
| 5 | +import java.io.Writer; |
5 | 6 |
|
6 | 7 | import com.fasterxml.jackson.core.JsonFactory; |
7 | 8 | import com.fasterxml.jackson.core.JsonGenerator; |
|
11 | 12 |
|
12 | 13 | import org.junit.Before; |
13 | 14 | import org.junit.Test; |
| 15 | +import tec.uom.se.util.SI; |
| 16 | +import tec.uom.se.util.UCUM; |
14 | 17 |
|
15 | | -import org.unitsofmeasurement.ri.util.SI; |
16 | | -import org.unitsofmeasurement.ri.util.UCUM; |
17 | 18 | import javax.measure.Unit; |
18 | 19 |
|
19 | | -import static org.unitsofmeasurement.ri.util.SIPrefix.KILO; |
| 20 | +import static tec.uom.se.util.SIPrefix.KILO; |
20 | 21 | import static org.junit.Assert.*; |
21 | 22 |
|
22 | 23 | /** |
23 | 24 | * Unit tests for UnitJacksonModule |
24 | 25 | */ |
25 | 26 | 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 | + } |
99 | 111 | } |
0 commit comments