View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy2.stub;
19  
20  import static org.junit.Assert.assertArrayEquals;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.junit.Test;
30  
31  /**
32   * Test {@link AnnotationBuilder}.
33   */
34  public class AnnotationBuilderTest
35  {
36      @Test
37      public void testDefaultAnnotation()
38      {
39          final CustomAnnotation customAnnotation = AnnotationBuilder.buildDefault(CustomAnnotation.class);
40          assertEquals(CustomAnnotation.class, customAnnotation.annotationType());
41          assertEquals("", customAnnotation.annString());
42          assertEquals(0, customAnnotation.finiteValues().length);
43          assertNull(customAnnotation.someType());
44      }
45  
46      @Test
47      public void testStubbedAnnotation()
48      {
49          final CustomAnnotation customAnnotation = AnnotationBuilder.of(CustomAnnotation.class)
50                  .train(new AnnotationTrainer<CustomAnnotation>()
51                  {
52                      @Override
53                      protected void train(CustomAnnotation trainee)
54                      {
55                          when(trainee.someType()).thenReturn(Object.class).when(trainee.finiteValues())
56                                  .thenReturn(FiniteValues.ONE, FiniteValues.THREE).when(trainee.annString())
57                                  .thenReturn("hey");
58                      }
59                  }).build();
60  
61          assertEquals(CustomAnnotation.class, customAnnotation.annotationType());
62          assertEquals("hey", customAnnotation.annString());
63          assertArrayEquals(new FiniteValues[] { FiniteValues.ONE, FiniteValues.THREE }, customAnnotation.finiteValues());
64          assertEquals(Object.class, customAnnotation.someType());
65      }
66  
67      @Test
68      public void testNestedStubbedAnnotation()
69      {
70          final NestingAnnotation nestingAnnotation = AnnotationBuilder.of(NestingAnnotation.class)
71                  .train(new AnnotationTrainer<NestingAnnotation>()
72                  {
73                      @Override
74                      protected void train(NestingAnnotation trainee)
75                      {
76                          when(trainee.child()).thenStub(CustomAnnotation.class).when(trainee.somethingElse())
77                                  .thenReturn("somethingElse");
78                      }
79                  }).build();
80  
81          assertEquals("", nestingAnnotation.child().annString());
82          assertEquals(0, nestingAnnotation.child().finiteValues().length);
83          assertEquals(null, nestingAnnotation.child().someType());
84          assertEquals("somethingElse", nestingAnnotation.somethingElse());
85      }
86  
87      @Test
88      public void testMemberMap()
89      {
90          final Map<String, Object> members = new HashMap<String, Object>();
91          members.put("annString", "foo");
92          members.put("finiteValues", FiniteValues.values());
93          members.put("someType", Object.class);
94  
95          final CustomAnnotation customAnnotation = AnnotationBuilder.of(CustomAnnotation.class).withMembers(members)
96                  .build();
97  
98          assertNotNull(customAnnotation);
99          assertEquals(CustomAnnotation.class, customAnnotation.annotationType());
100         assertEquals("foo", customAnnotation.annString());
101         assertEquals(3, customAnnotation.finiteValues().length);
102         assertEquals(Object.class, customAnnotation.someType());
103     }
104 
105     @Test
106     public void testNestedStubbedAnnotationArray()
107     {
108         final NestingAnnotation nestingAnnotation = AnnotationBuilder.of(NestingAnnotation.class)
109                 .train(new AnnotationTrainer<NestingAnnotation>()
110                 {
111 
112                     @Override
113                     protected void train(NestingAnnotation trainee)
114                     {
115                         when(trainee.children()).thenBuildArray().addElement(new AnnotationTrainer<CustomAnnotation>()
116                         {
117                             @Override
118                             protected void train(CustomAnnotation trainee)
119                             {
120                                 when(trainee.finiteValues()).thenReturn(FiniteValues.ONE, FiniteValues.THREE);
121                             }
122                         }).addElement(new AnnotationTrainer<CustomAnnotation>()
123                         {
124                             @Override
125                             protected void train(CustomAnnotation trainee)
126                             {
127                                 when(trainee.finiteValues()).thenReturn(FiniteValues.TWO);
128                             }
129                         }).build();
130                     }
131                 }).build();
132 
133         assertNull(nestingAnnotation.child());
134         assertEquals(2, nestingAnnotation.children().length);
135         assertArrayEquals(new FiniteValues[] { FiniteValues.ONE, FiniteValues.THREE },
136                 nestingAnnotation.children()[0].finiteValues());
137         assertArrayEquals(new FiniteValues[] { FiniteValues.TWO }, nestingAnnotation.children()[1].finiteValues());
138     }
139 
140     @Test(expected = IllegalArgumentException.class)
141     public void testBadMemberMap()
142     {
143         AnnotationBuilder.of(CustomAnnotation.class).withMembers(
144                 Collections.singletonMap("annString", Integer.valueOf(100)));
145     }
146 
147     public @interface NestingAnnotation
148     {
149         CustomAnnotation child();
150 
151         String somethingElse();
152 
153         CustomAnnotation[] children() default {};
154     }
155 
156     public @interface CustomAnnotation
157     {
158         String annString() default "";
159 
160         FiniteValues[] finiteValues() default {};
161 
162         Class<?> someType();
163     }
164 
165     public enum FiniteValues
166     {
167         ONE, TWO, THREE;
168     }
169 
170 }