|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
4 | 4 |
|
| 5 | +import com.fasterxml.jackson.databind.JsonMappingException; |
5 | 6 | import com.fasterxml.jackson.databind.ObjectMapper;
|
6 | 7 | import com.fasterxml.jackson.datatype.guava.deser.util.RangeFactory;
|
7 | 8 |
|
| 9 | +import com.google.common.collect.BoundType; |
8 | 10 | import com.google.common.collect.Range;
|
9 | 11 |
|
10 | 12 | import java.io.IOException;
|
@@ -103,4 +105,148 @@ public void testUntyped() throws Exception
|
103 | 105 | assertNotNull(out);
|
104 | 106 | assertEquals(Range.class, out.range.getClass());
|
105 | 107 | }
|
| 108 | + |
| 109 | + public void testDefaultBoundTypeNoBoundTypeInformed() throws Exception |
| 110 | + { |
| 111 | + String json = "{\"lowerEndpoint\": 2, \"upperEndpoint\": 3}"; |
| 112 | + |
| 113 | + try { |
| 114 | + MAPPER.readValue(json, Range.class); |
| 115 | + fail("Should have failed"); |
| 116 | + } catch (JsonMappingException e) { |
| 117 | + verifyException(e, "'lowerEndpoint' field found, but not 'lowerBoundType'"); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public void testDefaultBoundTypeNoBoundTypeInformedWithClosedConfigured() throws Exception |
| 122 | + { |
| 123 | + String json = "{\"lowerEndpoint\": 2, \"upperEndpoint\": 3}"; |
| 124 | + |
| 125 | + GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED); |
| 126 | + ObjectMapper mapper = new ObjectMapper().registerModule(mod); |
| 127 | + |
| 128 | + @SuppressWarnings("unchecked") |
| 129 | + Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class); |
| 130 | + |
| 131 | + assertEquals(Integer.valueOf(2), r.lowerEndpoint()); |
| 132 | + assertEquals(Integer.valueOf(3), r.upperEndpoint()); |
| 133 | + assertEquals(BoundType.CLOSED, r.lowerBoundType()); |
| 134 | + assertEquals(BoundType.CLOSED, r.upperBoundType()); |
| 135 | + } |
| 136 | + |
| 137 | + public void testDefaultBoundTypeOnlyLowerBoundTypeInformed() throws Exception |
| 138 | + { |
| 139 | + String json = "{\"lowerEndpoint\": 2, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3}"; |
| 140 | + |
| 141 | + try { |
| 142 | + MAPPER.readValue(json, Range.class); |
| 143 | + fail("Should have failed"); |
| 144 | + } catch (JsonMappingException e) { |
| 145 | + verifyException(e, "'upperEndpoint' field found, but not 'upperBoundType'"); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public void testDefaultBoundTypeOnlyLowerBoundTypeInformedWithClosedConfigured() throws Exception |
| 150 | + { |
| 151 | + String json = "{\"lowerEndpoint\": 2, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3}"; |
| 152 | + |
| 153 | + GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED); |
| 154 | + ObjectMapper mapper = new ObjectMapper().registerModule(mod); |
| 155 | + |
| 156 | + @SuppressWarnings("unchecked") |
| 157 | + Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class); |
| 158 | + |
| 159 | + assertEquals(Integer.valueOf(2), r.lowerEndpoint()); |
| 160 | + assertEquals(Integer.valueOf(3), r.upperEndpoint()); |
| 161 | + assertEquals(BoundType.OPEN, r.lowerBoundType()); |
| 162 | + assertEquals(BoundType.CLOSED, r.upperBoundType()); |
| 163 | + } |
| 164 | + |
| 165 | + public void testDefaultBoundTypeOnlyUpperBoundTypeInformed() throws Exception |
| 166 | + { |
| 167 | + String json = "{\"lowerEndpoint\": 2, \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}"; |
| 168 | + |
| 169 | + try { |
| 170 | + MAPPER.readValue(json, Range.class); |
| 171 | + fail("Should have failed"); |
| 172 | + } catch (JsonMappingException e) { |
| 173 | + verifyException(e, "'lowerEndpoint' field found, but not 'lowerBoundType'"); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public void testDefaultBoundTypeOnlyUpperBoundTypeInformedWithClosedConfigured() throws Exception |
| 178 | + { |
| 179 | + String json = "{\"lowerEndpoint\": 1, \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}"; |
| 180 | + |
| 181 | + GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED); |
| 182 | + ObjectMapper mapper = new ObjectMapper().registerModule(mod); |
| 183 | + |
| 184 | + @SuppressWarnings("unchecked") |
| 185 | + Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class); |
| 186 | + |
| 187 | + assertEquals(Integer.valueOf(1), r.lowerEndpoint()); |
| 188 | + assertEquals(Integer.valueOf(3), r.upperEndpoint()); |
| 189 | + assertEquals(BoundType.CLOSED, r.lowerBoundType()); |
| 190 | + assertEquals(BoundType.OPEN, r.upperBoundType()); |
| 191 | + } |
| 192 | + |
| 193 | + public void testDefaultBoundTypeBothBoundTypesOpen() throws Exception |
| 194 | + { |
| 195 | + String json = "{\"lowerEndpoint\": 2, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}"; |
| 196 | + @SuppressWarnings("unchecked") |
| 197 | + Range<Integer> r = (Range<Integer>) MAPPER.readValue(json, Range.class); |
| 198 | + |
| 199 | + assertEquals(Integer.valueOf(2), r.lowerEndpoint()); |
| 200 | + assertEquals(Integer.valueOf(3), r.upperEndpoint()); |
| 201 | + |
| 202 | + assertEquals(BoundType.OPEN, r.lowerBoundType()); |
| 203 | + assertEquals(BoundType.OPEN, r.upperBoundType()); |
| 204 | + } |
| 205 | + |
| 206 | + public void testDefaultBoundTypeBothBoundTypesOpenWithClosedConfigured() throws Exception |
| 207 | + { |
| 208 | + String json = "{\"lowerEndpoint\": 1, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}"; |
| 209 | + |
| 210 | + GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED); |
| 211 | + ObjectMapper mapper = new ObjectMapper().registerModule(mod); |
| 212 | + |
| 213 | + @SuppressWarnings("unchecked") |
| 214 | + Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class); |
| 215 | + |
| 216 | + assertEquals(Integer.valueOf(1), r.lowerEndpoint()); |
| 217 | + assertEquals(Integer.valueOf(3), r.upperEndpoint()); |
| 218 | + |
| 219 | + assertEquals(BoundType.OPEN, r.lowerBoundType()); |
| 220 | + assertEquals(BoundType.OPEN, r.upperBoundType()); |
| 221 | + } |
| 222 | + |
| 223 | + public void testDefaultBoundTypeBothBoundTypesClosed() throws Exception |
| 224 | + { |
| 225 | + String json = "{\"lowerEndpoint\": 1, \"lowerBoundType\": \"CLOSED\", \"upperEndpoint\": 3, \"upperBoundType\": \"CLOSED\"}"; |
| 226 | + @SuppressWarnings("unchecked") |
| 227 | + Range<Integer> r = (Range<Integer>) MAPPER.readValue(json, Range.class); |
| 228 | + |
| 229 | + assertEquals(Integer.valueOf(1), r.lowerEndpoint()); |
| 230 | + assertEquals(Integer.valueOf(3), r.upperEndpoint()); |
| 231 | + |
| 232 | + assertEquals(BoundType.CLOSED, r.lowerBoundType()); |
| 233 | + assertEquals(BoundType.CLOSED, r.upperBoundType()); |
| 234 | + } |
| 235 | + |
| 236 | + public void testDefaultBoundTypeBothBoundTypesClosedWithOpenConfigured() throws Exception |
| 237 | + { |
| 238 | + String json = "{\"lowerEndpoint\": 12, \"lowerBoundType\": \"CLOSED\", \"upperEndpoint\": 33, \"upperBoundType\": \"CLOSED\"}"; |
| 239 | + |
| 240 | + GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED); |
| 241 | + ObjectMapper mapper = new ObjectMapper().registerModule(mod); |
| 242 | + |
| 243 | + @SuppressWarnings("unchecked") |
| 244 | + Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class); |
| 245 | + |
| 246 | + assertEquals(Integer.valueOf(12), r.lowerEndpoint()); |
| 247 | + assertEquals(Integer.valueOf(33), r.upperEndpoint()); |
| 248 | + |
| 249 | + assertEquals(BoundType.CLOSED, r.lowerBoundType()); |
| 250 | + assertEquals(BoundType.CLOSED, r.upperBoundType()); |
| 251 | + } |
106 | 252 | }
|
0 commit comments