| package com.fasterxml.jackson.databind.introspect; |
| |
| import com.fasterxml.jackson.databind.BaseMapTest; |
| import com.fasterxml.jackson.databind.JsonMappingException; |
| import com.fasterxml.jackson.databind.MapperFeature; |
| import com.fasterxml.jackson.databind.ObjectMapper; |
| |
| public class TestInferredMutators extends BaseMapTest |
| { |
| public static class Point { |
| protected int x; |
| |
| public int getX() { return x; } |
| } |
| |
| public static class FixedPoint { |
| protected final int x; |
| |
| public FixedPoint() { x = 0; } |
| |
| public int getX() { return x; } |
| } |
| |
| /* |
| /********************************************************** |
| /* Unit tests |
| /********************************************************** |
| */ |
| |
| // for #190 |
| public void testFinalFieldIgnoral() throws Exception |
| { |
| ObjectMapper mapper = new ObjectMapper(); |
| // default value is 'enabled', for backwards compatibility |
| assertTrue(mapper.isEnabled(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)); |
| mapper = jsonMapperBuilder() |
| .disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS) |
| .build(); |
| try { |
| /*p =*/ mapper.readValue("{\"x\":2}", FixedPoint.class); |
| fail("Should not try to use final field"); |
| } catch (JsonMappingException e) { |
| verifyException(e, "unrecognized field \"x\""); |
| } |
| } |
| |
| // for #195 |
| public void testDeserializationInference() throws Exception |
| { |
| final String JSON = "{\"x\":2}"; |
| ObjectMapper mapper = new ObjectMapper(); |
| // First: default case, inference enabled: |
| assertTrue(mapper.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS)); |
| Point p = mapper.readValue(JSON, Point.class); |
| assertEquals(2, p.x); |
| |
| // but without it, should fail: |
| mapper = jsonMapperBuilder() |
| .disable(MapperFeature.INFER_PROPERTY_MUTATORS) |
| .build(); |
| try { |
| p = mapper.readValue(JSON, Point.class); |
| fail("Should not succeeed"); |
| } catch (JsonMappingException e) { |
| verifyException(e, "unrecognized field \"x\""); |
| } |
| } |
| } |