|
5 | 5 |
|
6 | 6 | import com.fasterxml.jackson.core.JsonGenerator;
|
7 | 7 | import com.fasterxml.jackson.databind.*;
|
| 8 | +import com.fasterxml.jackson.databind.introspect.BasicBeanDescription; |
| 9 | +import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; |
8 | 10 | import com.fasterxml.jackson.annotation.*;
|
9 | 11 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
10 | 12 |
|
11 | 13 | /**
|
12 |
| - * Unit tests for checking whether JsonSerializerFactory.Feature |
13 |
| - * configuration works |
| 14 | + * Unit tests for checking handling of some of {@link MapperFeature}s |
| 15 | + * and {@link SerializationFeature}s for serialization. |
14 | 16 | */
|
15 |
| -public class TestFeatures |
| 17 | +public class SerializationFeaturesTest |
16 | 18 | extends BaseMapTest
|
17 | 19 | {
|
18 |
| - /* |
19 |
| - /********************************************************** |
20 |
| - /* Helper classes |
21 |
| - /********************************************************** |
22 |
| - */ |
23 |
| - |
24 | 20 | /**
|
25 | 21 | * Class with one explicitly defined getter, one name-based
|
26 | 22 | * auto-detectable getter.
|
@@ -192,49 +188,47 @@ public void testFlushingAutomatic() throws IOException
|
192 | 188 | assertTrue(mapper.getSerializationConfig().isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE));
|
193 | 189 | // default is to flush after writeValue()
|
194 | 190 | StringWriter sw = new StringWriter();
|
195 |
| - JsonGenerator jgen = mapper.getFactory().createGenerator(sw); |
196 |
| - mapper.writeValue(jgen, Integer.valueOf(13)); |
| 191 | + JsonGenerator g = mapper.getFactory().createGenerator(sw); |
| 192 | + mapper.writeValue(g, Integer.valueOf(13)); |
197 | 193 | assertEquals("13", sw.toString());
|
198 |
| - jgen.close(); |
| 194 | + g.close(); |
199 | 195 |
|
200 | 196 | // ditto with ObjectWriter
|
201 | 197 | sw = new StringWriter();
|
202 |
| - jgen = mapper.getFactory().createGenerator(sw); |
| 198 | + g = mapper.getFactory().createGenerator(sw); |
203 | 199 | ObjectWriter ow = mapper.writer();
|
204 |
| - ow.writeValue(jgen, Integer.valueOf(99)); |
| 200 | + ow.writeValue(g, Integer.valueOf(99)); |
205 | 201 | assertEquals("99", sw.toString());
|
206 |
| - jgen.close(); |
| 202 | + g.close(); |
207 | 203 | }
|
208 | 204 |
|
209 |
| - // Test for [JACKSON-401] |
210 | 205 | public void testFlushingNotAutomatic() throws IOException
|
211 | 206 | {
|
212 | 207 | // but should not occur if configured otherwise
|
213 | 208 | ObjectMapper mapper = new ObjectMapper();
|
214 | 209 | mapper.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false);
|
215 | 210 | StringWriter sw = new StringWriter();
|
216 |
| - JsonGenerator jgen = mapper.getFactory().createGenerator(sw); |
| 211 | + JsonGenerator g = mapper.getFactory().createGenerator(sw); |
217 | 212 |
|
218 |
| - mapper.writeValue(jgen, Integer.valueOf(13)); |
| 213 | + mapper.writeValue(g, Integer.valueOf(13)); |
219 | 214 | // no flushing now:
|
220 | 215 | assertEquals("", sw.toString());
|
221 | 216 | // except when actually flushing
|
222 |
| - jgen.flush(); |
| 217 | + g.flush(); |
223 | 218 | assertEquals("13", sw.toString());
|
224 |
| - jgen.close(); |
| 219 | + g.close(); |
225 | 220 | // Also, same should happen with ObjectWriter
|
226 | 221 | sw = new StringWriter();
|
227 |
| - jgen = mapper.getFactory().createGenerator(sw); |
| 222 | + g = mapper.getFactory().createGenerator(sw); |
228 | 223 | ObjectWriter ow = mapper.writer();
|
229 |
| - ow.writeValue(jgen, Integer.valueOf(99)); |
| 224 | + ow.writeValue(g, Integer.valueOf(99)); |
230 | 225 | assertEquals("", sw.toString());
|
231 | 226 | // except when actually flushing
|
232 |
| - jgen.flush(); |
| 227 | + g.flush(); |
233 | 228 | assertEquals("99", sw.toString());
|
234 |
| - jgen.close(); |
| 229 | + g.close(); |
235 | 230 | }
|
236 | 231 |
|
237 |
| - // Test for [JACKSON-805] |
238 | 232 | public void testSingleElementCollections() throws IOException
|
239 | 233 | {
|
240 | 234 | final ObjectWriter writer = objectWriter().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
|
@@ -264,6 +258,38 @@ public void testSingleElementCollections() throws IOException
|
264 | 258 | assertEquals("true", writer.writeValueAsString(new Boolean[] { Boolean.TRUE }));
|
265 | 259 | assertEquals("3", writer.writeValueAsString(new int[] { 3 }));
|
266 | 260 | assertEquals(quote("foo"), writer.writeValueAsString(new String[] { "foo" }));
|
267 |
| - |
| 261 | + } |
| 262 | + |
| 263 | + static class TCls { |
| 264 | + @JsonProperty("groupname") |
| 265 | + private String groupname; |
| 266 | + |
| 267 | + public void setName(String str) { |
| 268 | + this.groupname = str; |
| 269 | + } |
| 270 | + public String getName() { |
| 271 | + return groupname; |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + public void testVisibilityFeatures() throws Exception |
| 276 | + { |
| 277 | + ObjectMapper om = new ObjectMapper(); |
| 278 | + // Only use explicitly specified values to be serialized/deserialized (i.e., JSONProperty). |
| 279 | + om.configure(MapperFeature.AUTO_DETECT_FIELDS, false); |
| 280 | + om.configure(MapperFeature.AUTO_DETECT_GETTERS, false); |
| 281 | + om.configure(MapperFeature.AUTO_DETECT_SETTERS, false); |
| 282 | + om.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false); |
| 283 | + om.configure(MapperFeature.USE_GETTERS_AS_SETTERS, false); |
| 284 | + om.configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); |
| 285 | + om.configure(MapperFeature.INFER_PROPERTY_MUTATORS, false); |
| 286 | + om.configure(MapperFeature.USE_ANNOTATIONS, true); |
| 287 | + |
| 288 | + JavaType javaType = om.getTypeFactory().constructType(TCls.class); |
| 289 | + BeanDescription desc = (BeanDescription) om.getSerializationConfig().introspect(javaType); |
| 290 | + List<BeanPropertyDefinition> props = desc.findProperties(); |
| 291 | + if (props.size() != 1) { |
| 292 | + fail("Should find 1 property, not "+props.size()+"; properties = "+props); |
| 293 | + } |
268 | 294 | }
|
269 | 295 | }
|
0 commit comments