Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit 1e28266

Browse files
committed
added test for MalformedParametersException
fixes #33
1 parent 011f759 commit 1e28266

File tree

5 files changed

+97
-36
lines changed

5 files changed

+97
-36
lines changed

pom.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ introspection of method/constructor parameter names, without having to add expli
3737
<!-- Generate PackageVersion.java into this directory. -->
3838
<packageVersion.dir>com/fasterxml/jackson/module/paramnames</packageVersion.dir>
3939
<packageVersion.package>${project.groupId}.paramnames</packageVersion.package>
40-
<assertj-core.version>3.3.0</assertj-core.version>
40+
41+
<assertj-core.version>3.4.0</assertj-core.version>
42+
<mockito-core.version>1.10.19</mockito-core.version>
4143
</properties>
4244

4345
<dependencies>
@@ -58,6 +60,11 @@ introspection of method/constructor parameter names, without having to add expli
5860
<version>${assertj-core.version}</version>
5961
<scope>test</scope>
6062
</dependency>
63+
<dependency>
64+
<groupId>org.mockito</groupId>
65+
<artifactId>mockito-core</artifactId>
66+
<version>${mockito-core.version}</version>
67+
</dependency>
6168
</dependencies>
6269

6370
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.fasterxml.jackson.module.paramnames;
2+
3+
import java.lang.reflect.Executable;
4+
import java.lang.reflect.Parameter;
5+
6+
class ParameterExtractor {
7+
8+
public Parameter[] getParameters(Executable executable) {
9+
return executable.getParameters();
10+
}
11+
}

src/main/java/com/fasterxml/jackson/module/paramnames/ParameterNamesAnnotationIntrospector.java

+22-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import com.fasterxml.jackson.databind.AnnotationIntrospector;
55
import com.fasterxml.jackson.databind.introspect.*;
66

7-
import java.lang.reflect.*;
7+
import java.lang.reflect.MalformedParametersException;
8+
import java.lang.reflect.Parameter;
89

910
/**
1011
* Introspector that uses parameter name information provided by the Java Reflection API additions in Java 8 to
@@ -14,15 +15,16 @@
1415
* @see AnnotationIntrospector
1516
* @see Parameter
1617
*/
17-
class ParameterNamesAnnotationIntrospector extends NopAnnotationIntrospector
18-
{
18+
class ParameterNamesAnnotationIntrospector extends NopAnnotationIntrospector {
1919
private static final long serialVersionUID = 1L;
2020

2121
private final JsonCreator.Mode creatorBinding;
22+
private final ParameterExtractor parameterExtractor;
2223

23-
ParameterNamesAnnotationIntrospector(JsonCreator.Mode creatorBinding) {
24+
ParameterNamesAnnotationIntrospector(JsonCreator.Mode creatorBinding, ParameterExtractor parameterExtractor) {
2425

2526
this.creatorBinding = creatorBinding;
27+
this.parameterExtractor = parameterExtractor;
2628
}
2729

2830
@Override
@@ -44,26 +46,28 @@ public JsonCreator.Mode findCreatorBinding(Annotated a) {
4446
return creatorBinding;
4547
}
4648

47-
/**
48-
* Returns the parameter name, or {@code null} if it could not be determined.
49-
*
50-
* @param annotatedParameter containing constructor or method from which {@link Parameter} can be extracted
51-
*
52-
* @return name or {@code null} if parameter could not be determined
53-
*/
5449
private String findParameterName(AnnotatedParameter annotatedParameter) {
5550

56-
AnnotatedWithParams owner = annotatedParameter.getOwner();
5751
Parameter[] params;
58-
59-
if (owner instanceof AnnotatedConstructor) {
60-
params = ((AnnotatedConstructor) owner).getAnnotated().getParameters();
61-
} else if (owner instanceof AnnotatedMethod) {
62-
params = ((AnnotatedMethod) owner).getAnnotated().getParameters();
63-
} else {
52+
try {
53+
params = getParameters(annotatedParameter.getOwner());
54+
} catch (MalformedParametersException e) {
6455
return null;
6556
}
57+
6658
Parameter p = params[annotatedParameter.getIndex()];
6759
return p.isNamePresent() ? p.getName() : null;
6860
}
61+
62+
private Parameter[] getParameters(AnnotatedWithParams owner) {
63+
if (owner instanceof AnnotatedConstructor) {
64+
return parameterExtractor.getParameters(((AnnotatedConstructor) owner).getAnnotated());
65+
}
66+
67+
if (owner instanceof AnnotatedMethod) {
68+
return parameterExtractor.getParameters(((AnnotatedMethod) owner).getAnnotated());
69+
}
70+
71+
return null;
72+
}
6973
}

src/main/java/com/fasterxml/jackson/module/paramnames/ParameterNamesModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ParameterNamesModule() {
2222
@Override
2323
public void setupModule(SetupContext context) {
2424
super.setupModule(context);
25-
context.insertAnnotationIntrospector(new ParameterNamesAnnotationIntrospector(creatorBinding));
25+
context.insertAnnotationIntrospector(new ParameterNamesAnnotationIntrospector(creatorBinding, new ParameterExtractor()));
2626
}
2727

2828
@Override

src/test/java/com/fasterxml/jackson/module/paramnames/ParameterNamesAnnotationIntrospectorTest.java

+55-16
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,86 @@
44
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
55
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
66
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
7-
7+
import org.junit.Before;
88
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mockito.BDDMockito;
11+
import org.mockito.Mock;
12+
import org.mockito.runners.MockitoJUnitRunner;
913

1014
import java.lang.reflect.Constructor;
15+
import java.lang.reflect.MalformedParametersException;
1116
import java.lang.reflect.Method;
17+
import java.lang.reflect.Parameter;
1218

13-
import static org.junit.Assert.assertEquals;
19+
import static org.assertj.core.api.BDDAssertions.then;
20+
import static org.mockito.BDDMockito.given;
21+
import static org.mockito.Matchers.any;
1422

1523
/**
1624
* @author Lovro Pandzic
1725
*/
26+
@RunWith(MockitoJUnitRunner.class)
1827
public class ParameterNamesAnnotationIntrospectorTest {
1928

20-
private final ParameterNamesAnnotationIntrospector PN_AI = new ParameterNamesAnnotationIntrospector(JsonCreator.Mode.DEFAULT);
29+
@Mock
30+
private ParameterExtractor parameterExtractor;
31+
32+
private ParameterNamesAnnotationIntrospector introspector;
33+
34+
@Before
35+
public void setUp() throws Exception {
36+
introspector = new ParameterNamesAnnotationIntrospector(JsonCreator.Mode.DEFAULT, parameterExtractor);
37+
}
2138

2239
@Test
2340
public void shouldFindParameterNameFromConstructorForLegalIndex() throws Exception {
24-
Constructor<?> ctor = ImmutableBean.class.getConstructor(String.class, Integer.class);
25-
26-
assertEquals("name", ctor.getParameters()[0].getName());
2741

28-
AnnotatedConstructor owner = new AnnotatedConstructor(null, ctor, null, null);
42+
// given
43+
Constructor<?> givenConstructor = ImmutableBean.class.getConstructor(String.class, Integer.class);
44+
Parameter[] givenParameters = givenConstructor.getParameters();
45+
AnnotatedConstructor owner = new AnnotatedConstructor(null, givenConstructor, null, null);
2946
AnnotatedParameter annotatedParameter = new AnnotatedParameter(owner, null, null, 0);
30-
31-
String propertyName = PN_AI.findImplicitPropertyName(annotatedParameter);
47+
given(parameterExtractor.getParameters(any())).willReturn(givenParameters);
48+
49+
// when
50+
String actual = introspector.findImplicitPropertyName(annotatedParameter);
3251

33-
assertEquals("name", propertyName);
52+
then(actual).isEqualTo("name");
53+
BDDMockito.then(parameterExtractor).should().getParameters(givenConstructor);
3454
}
3555

3656
@Test
3757
public void shouldFindParameterNameFromMethodForLegalIndex() throws Exception {
3858

39-
Method method = ImmutableBeanWithStaticFactory.class.getMethod("of", String.class, Integer.class);
59+
// given
60+
Method givenMethod = ImmutableBeanWithStaticFactory.class.getMethod("of", String.class, Integer.class);
61+
Parameter[] givenParameters = givenMethod.getParameters();
62+
AnnotatedMethod owner = new AnnotatedMethod(null, givenMethod, null, null);
63+
AnnotatedParameter annotatedParameter = new AnnotatedParameter(owner, null, null, 0);
64+
given(parameterExtractor.getParameters(any())).willReturn(givenParameters);
65+
66+
// when
67+
String actual = introspector.findImplicitPropertyName(annotatedParameter);
68+
69+
// then
70+
then(actual).isEqualTo("name");
71+
BDDMockito.then(parameterExtractor).should().getParameters(givenMethod);
72+
}
73+
74+
@Test
75+
public void shouldReturnNullForMalformedParametersException() throws Exception {
4076

41-
assertEquals("name", method.getParameters()[0].getName());
42-
43-
AnnotatedMethod owner = new AnnotatedMethod(null, method, null, null);
77+
// given
78+
Constructor<?> givenConstructor = ImmutableBean.class.getConstructor(String.class, Integer.class);
79+
AnnotatedConstructor owner = new AnnotatedConstructor(null, givenConstructor, null, null);
4480
AnnotatedParameter annotatedParameter = new AnnotatedParameter(owner, null, null, 0);
81+
given(parameterExtractor.getParameters(any())).willThrow(new MalformedParametersException());
4582

46-
String propertyName = PN_AI.findImplicitPropertyName(annotatedParameter);
83+
// when
84+
String actual = introspector.findImplicitPropertyName(annotatedParameter);
4785

48-
assertEquals("name", propertyName);
86+
then(actual).isNull();
87+
BDDMockito.then(parameterExtractor).should().getParameters(givenConstructor);
4988
}
5089
}

0 commit comments

Comments
 (0)