001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.proxy2.stub;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertNull;
024
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.junit.Test;
030
031/**
032 * Test {@link AnnotationBuilder}.
033 */
034public class AnnotationBuilderTest
035{
036    @Test
037    public void testDefaultAnnotation()
038    {
039        final CustomAnnotation customAnnotation = AnnotationBuilder.buildDefault(CustomAnnotation.class);
040        assertEquals(CustomAnnotation.class, customAnnotation.annotationType());
041        assertEquals("", customAnnotation.annString());
042        assertEquals(0, customAnnotation.finiteValues().length);
043        assertNull(customAnnotation.someType());
044    }
045
046    @Test
047    public void testStubbedAnnotation()
048    {
049        final CustomAnnotation customAnnotation = AnnotationBuilder.of(CustomAnnotation.class)
050                .train(new AnnotationTrainer<CustomAnnotation>()
051                {
052                    @Override
053                    protected void train(CustomAnnotation trainee)
054                    {
055                        when(trainee.someType()).thenReturn(Object.class).when(trainee.finiteValues())
056                                .thenReturn(FiniteValues.ONE, FiniteValues.THREE).when(trainee.annString())
057                                .thenReturn("hey");
058                    }
059                }).build();
060
061        assertEquals(CustomAnnotation.class, customAnnotation.annotationType());
062        assertEquals("hey", customAnnotation.annString());
063        assertArrayEquals(new FiniteValues[] { FiniteValues.ONE, FiniteValues.THREE }, customAnnotation.finiteValues());
064        assertEquals(Object.class, customAnnotation.someType());
065    }
066
067    @Test
068    public void testNestedStubbedAnnotation()
069    {
070        final NestingAnnotation nestingAnnotation = AnnotationBuilder.of(NestingAnnotation.class)
071                .train(new AnnotationTrainer<NestingAnnotation>()
072                {
073                    @Override
074                    protected void train(NestingAnnotation trainee)
075                    {
076                        when(trainee.child()).thenStub(CustomAnnotation.class).when(trainee.somethingElse())
077                                .thenReturn("somethingElse");
078                    }
079                }).build();
080
081        assertEquals("", nestingAnnotation.child().annString());
082        assertEquals(0, nestingAnnotation.child().finiteValues().length);
083        assertEquals(null, nestingAnnotation.child().someType());
084        assertEquals("somethingElse", nestingAnnotation.somethingElse());
085    }
086
087    @Test
088    public void testMemberMap()
089    {
090        final Map<String, Object> members = new HashMap<String, Object>();
091        members.put("annString", "foo");
092        members.put("finiteValues", FiniteValues.values());
093        members.put("someType", Object.class);
094
095        final CustomAnnotation customAnnotation = AnnotationBuilder.of(CustomAnnotation.class).withMembers(members)
096                .build();
097
098        assertNotNull(customAnnotation);
099        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}