View Javadoc
1   /*
2    * Copyright (C) 2015 Sven von Pluto - javanarior (a) gmail dot com
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.javanarior.utils.lang.reflect;
17  
18  import static de.javanarior.utils.lang.reflect.Retrieve.annotationValueOnMethod;
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.equalTo;
21  import static org.hamcrest.Matchers.is;
22  import static org.hamcrest.Matchers.notNullValue;
23  
24  import javax.annotation.Resource;
25  
26  import org.testng.annotations.Test;
27  
28  @Resource(name = "RetrieveTest")
29  @Testeria("attributeValue")
30  public class RetrieveTest {
31  
32      @Test
33      public void testAnnotationValueOnClass() {
34          Object attributeValue = Retrieve.annotationValueOnClass(Resource.class, "name", RetrieveTest.class);
35          assertThat(attributeValue, notNullValue());
36          assertThat((String)attributeValue, equalTo("RetrieveTest"));
37      }
38  
39      @Test
40      public void testAnnotationValueOnClassWithWrongNamedAttribute() {
41          try {
42              Retrieve.annotationValueOnClass(Resource.class, "wrongName", RetrieveTest.class);
43              assertThat("ReflectionException is expected", false);
44          } catch (ReflectionException exception) {
45              assertThat("ReflectionException is thrown", true);
46              assertThat(exception.getMessage(), equalTo("Attribute 'wrongName' not "
47                              + "found on 'javax.annotation.Resource'"));
48          }
49      }
50  
51      @Test
52      public void testAnnotationValueOnClassWithDefault() {
53          Object attributeValue = Retrieve.annotationValueOnClass(Testeria.class, RetrieveTest.class);
54          assertThat(attributeValue, notNullValue());
55          assertThat((String)attributeValue, is("attributeValue"));
56      }
57  
58      @Test
59      public void testAnnotationValueOnClassWithDefaultAnnotationIsMissing() {
60          try {
61              Retrieve.annotationValueOnClass(Test.class, RetrieveTest.class);
62          } catch (ReflectionException exception) {
63              assertThat("ReflectionException is thrown", true);
64              assertThat(exception.getMessage(),
65                              equalTo("Annotation 'interface org.testng.annotations.Test' not found in"
66                                              + " Class 'de.javanarior.utils.lang.reflect.RetrieveTest'"));
67          }
68      }
69  
70      @Test(alwaysRun = true)
71      public void testAnnotationValueOnMethod() {
72          Object attributeValue = Retrieve.annotationValueOnMethod(Test.class, "alwaysRun", RetrieveTest.class,
73                          "testAnnotationValueOnMethod");
74          assertThat(attributeValue, notNullValue());
75          assertThat((Boolean)attributeValue, is(Boolean.TRUE));
76      }
77  
78      @Test
79      public void testAnnotationValueOnMethodWithParameter() {
80          Object attributeValue = Retrieve.annotationValueOnMethod(Resource.class, "name", RetrieveTest.class,
81                          "methodForTestingPurpuse");
82          assertThat(attributeValue, notNullValue());
83          assertThat((String)attributeValue, equalTo("attributeValueWithParameter"));
84      }
85  
86      @Resource(name = "attributeValueWithParameter")
87      public void methodForTestingPurpuse(String unused) {
88          /* Do nothing */
89      }
90  
91      @Test(invocationCount = 5)
92      public void testAnnotationValueOnMethodWithParameterPolymorphismString() {
93          Object attributeValue = Retrieve.annotationValueOnMethod(Resource.class, "name", RetrieveTest.class,
94                          "methodForTestingPurposePolymorphism", String.class);
95          assertThat(attributeValue, notNullValue());
96          assertThat((String)attributeValue, equalTo("attributeValueString"));
97      }
98  
99      @Test(invocationCount = 5)
100     public void testAnnotationValueOnMethodWithParameterPolymorphismInteger() {
101         Object attributeValue = Retrieve.annotationValueOnMethod(Resource.class, "name", RetrieveTest.class,
102                         "methodForTestingPurposePolymorphism", Integer.TYPE);
103         assertThat(attributeValue, notNullValue());
104         assertThat((String)attributeValue, equalTo("attributeValueInteger"));
105     }
106 
107     @Test(invocationCount = 5)
108     public void testAnnotationValueOnMethodWithParameterPolymorphismAndWrongType() {
109         try {
110             Retrieve.annotationValueOnMethod(Resource.class, "name", RetrieveTest.class,
111                             "methodForTestingPurposePolymorphism", Long.class);
112             assertThat("ReflectionException is expected", false);
113         } catch (ReflectionException exception) {
114             assertThat("ReflectionException is thrown", true);
115             assertThat(exception.getMessage(), equalTo("Method name 'methodForTestingPurposePolymorphism' "
116                             + "not found in Class 'de.javanarior.utils.lang.reflect.RetrieveTest'"));
117         }
118     }
119 
120     @Test(invocationCount = 5)
121     public void testAnnotationValueOnMethodWithParameterPolymorphismAndEmptyTypes() {
122         try {
123             Retrieve.annotationValueOnMethod(Resource.class, "name", RetrieveTest.class,
124                             "methodForTestingPurposePolymorphism", new Class[0]);
125             assertThat("ReflectionException is expected", false);
126         } catch (ReflectionException exception) {
127             assertThat("ReflectionException is thrown", true);
128             assertThat(exception.getMessage(), equalTo("Method name 'methodForTestingPurposePolymorphism' "
129                             + "not found in Class 'de.javanarior.utils.lang.reflect.RetrieveTest'"));
130         }
131     }
132 
133     @Resource(name = "attributeValueString")
134     public void methodForTestingPurposePolymorphism(String unused) {
135         /* Do nothing */
136     }
137 
138     @Resource(name = "attributeValueInteger")
139     public void methodForTestingPurposePolymorphism(int unused) {
140         /* Do nothing */
141     }
142 
143     @Test
144     public void testAnnotationValueOnMethodAnnotationIsMissing() {
145         try {
146             Retrieve.annotationValueOnMethod(Resource.class, RetrieveTest.class,
147                             "testAnnotationValueOnMethodAnnotationIsMissing");
148             assertThat("ReflectionException is expected", false);
149         } catch (ReflectionException exception) {
150             assertThat("ReflectionException is thrown", true);
151             assertThat(exception.getMessage(),
152                             equalTo("Annotation 'interface javax.annotation.Resource' not found on Method "
153                                             + "'public void de.javanarior.utils.lang.reflect."
154                                             + "RetrieveTest.testAnnotationValueOnMethodAnnotationIsMissing()'"));
155         }
156     }
157 
158     @Test
159     public void testAnnotationValueOnMethodWithWrongMethodName() {
160         try {
161             Retrieve.annotationValueOnMethod(Test.class, "alwaysRun", RetrieveTest.class, "testWrongName");
162             assertThat("ReflectionException is expected", false);
163         } catch (ReflectionException exception) {
164             assertThat("ReflectionException is thrown", true);
165             assertThat(exception.getMessage(), equalTo("Method name 'testWrongName' not found in "
166                             + "Class 'de.javanarior.utils.lang.reflect.RetrieveTest'"));
167         }
168     }
169 
170     @Test
171     public void testAnnotationValueOnMethodWithWrongAttributeName() {
172         try {
173             annotationValueOnMethod(Test.class, "wrongName", RetrieveTest.class, "testAnnotationValueOnMethod");
174             assertThat("ReflectionException is expected", false);
175         } catch (ReflectionException exception) {
176             assertThat("ReflectionException is thrown", true);
177             assertThat(exception.getMessage(),
178                             equalTo("Attribute 'wrongName' not found on 'org.testng.annotations.Test'"));
179         }
180     }
181 
182     @Test
183     @Testeria("attributeValue")
184     public void testAnnotationValueOnMethodWithDefault() {
185         Object attributeValue = Retrieve.annotationValueOnMethod(Testeria.class, RetrieveTest.class,
186                         "testAnnotationValueOnMethodWithDefault");
187         assertThat(attributeValue, notNullValue());
188         assertThat((String)attributeValue, is("attributeValue"));
189     }
190 
191     @Test
192     @Testeria("attributeValue")
193     public void testAnnotationValueOnMethodWrongMethodName() {
194         try {
195             Retrieve.annotationValueOnMethod(Testeria.class, RetrieveTest.class, "wrongMethodName");
196             assertThat("ReflectionException is expected", false);
197         } catch (ReflectionException exception) {
198             assertThat("ReflectionException is thrown", true);
199             assertThat(exception.getMessage(),
200                             /* CHECKSTYLE:OFF */
201                             equalTo("Method name 'wrongMethodName' not found in "
202                                             + "Class 'de.javanarior.utils.lang.reflect.RetrieveTest'"));
203             /* CHECKSTYLE:ON */
204         }
205     }
206 
207     @Test
208     public void testAnnotationValueOnParameter() {
209         Object annotationValueOnParameter = Retrieve.annotationValueOnParameter(Testeria.class, "value",
210                         RetrieveTest.class, "methodForTestingPurpuseParameter", "unused");
211         assertThat(annotationValueOnParameter, notNullValue());
212         assertThat((String)annotationValueOnParameter, is("attributeValueOnParameter"));
213     }
214 
215     @Test
216     public void testAnnotationValueOnParameterWithDefaultAttributeNameAndNoParameterTypes() {
217         Object annotationValueOnParameter = Retrieve.annotationValueOnParameter(Testeria.class,
218                         RetrieveTest.class, "methodForTestingPurpuseParameter", "unused");
219         assertThat(annotationValueOnParameter, notNullValue());
220         assertThat((String)annotationValueOnParameter, is("attributeValueOnParameter"));
221     }
222 
223     @Test
224     public void testAnnotationValueOnParameterWithMissingAnnoation() {
225         try {
226             Retrieve.annotationValueOnParameter(Resource.class, "value",
227                             RetrieveTest.class, "methodForTestingPurpuseParameter", "unused");
228             assertThat("ReflectionException is expected", false);
229         } catch (ReflectionException exception) {
230             assertThat("ReflectionException is thrown", true);
231             assertThat(exception.getMessage(),
232                             equalTo("Annotation 'javax.annotation.Resource' not found in Parameter list of Method "
233                                             + "'public void de.javanarior.utils.lang.reflect."
234                                             + "RetrieveTest.methodForTestingPurpuseParameter(java.lang.String)'"));
235         }
236     }
237 
238     public void methodForTestingPurpuseParameter(@Testeria("attributeValueOnParameter") String unused) {
239 
240     }
241 
242 }