|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
| 4 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 5 | +import com.fasterxml.jackson.databind.*; |
| 6 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; |
| 7 | +import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; |
| 8 | +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 9 | + |
| 10 | +public class MultiArgConstructorTest extends BaseMapTest |
| 11 | +{ |
| 12 | + static class MultiArgCtorBean |
| 13 | + { |
| 14 | + protected int a, b; |
| 15 | + |
| 16 | + public MultiArgCtorBean(int a, int b) { |
| 17 | + this.a = a; |
| 18 | + this.b = b; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + /* Before JDK8, we won't have parameter names available, so let's |
| 23 | + * fake it before that... |
| 24 | + */ |
| 25 | + @SuppressWarnings("serial") |
| 26 | + static class MyParamIntrospector extends JacksonAnnotationIntrospector |
| 27 | + { |
| 28 | + @Override |
| 29 | + public String findImplicitPropertyName(AnnotatedMember param) { |
| 30 | + if (param instanceof AnnotatedParameter) { |
| 31 | + AnnotatedParameter ap = (AnnotatedParameter) param; |
| 32 | + switch (ap.getIndex()) { |
| 33 | + case 0: return "a"; |
| 34 | + case 1: return "b"; |
| 35 | + default: |
| 36 | + return "param"+ap.getIndex(); |
| 37 | + } |
| 38 | + } |
| 39 | + return super.findImplicitPropertyName(param); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /* |
| 44 | + /********************************************************************** |
| 45 | + /* Test methods |
| 46 | + /********************************************************************** |
| 47 | + */ |
| 48 | + |
| 49 | + public void testMultiArgVisible() throws Exception |
| 50 | + { |
| 51 | + final ObjectMapper mapper = new ObjectMapper(); |
| 52 | + mapper.setAnnotationIntrospector(new MyParamIntrospector()); |
| 53 | + MultiArgCtorBean bean = mapper.readValue(aposToQuotes("{'b':13, 'a':-99}"), |
| 54 | + MultiArgCtorBean.class); |
| 55 | + assertNotNull(bean); |
| 56 | + assertEquals(13, bean.b); |
| 57 | + assertEquals(-99, bean.a); |
| 58 | + } |
| 59 | + |
| 60 | + // but let's also ensure that it is possible to prevent use of that constructor |
| 61 | + // with different visibility |
| 62 | + public void testMultiArgNotVisible() throws Exception |
| 63 | + { |
| 64 | + final ObjectMapper mapper = new ObjectMapper(); |
| 65 | + mapper.setAnnotationIntrospector(new MyParamIntrospector()); |
| 66 | + mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE); |
| 67 | + try { |
| 68 | + /*MultiArgCtorBean bean =*/ mapper.readValue(aposToQuotes("{'b':13, 'a':-99}"), |
| 69 | + MultiArgCtorBean.class); |
| 70 | + fail("Should not have passed"); |
| 71 | + } catch (JsonMappingException e) { |
| 72 | + verifyException(e, "No suitable constructor"); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments