View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 test.de.smartics.properties.report.qdox;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.when;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import com.thoughtworks.qdox.model.JavaAnnotation;
30  import com.thoughtworks.qdox.model.JavaClass;
31  
32  import de.smartics.properties.api.core.annotations.PropertyKeyName;
33  import de.smartics.properties.api.core.annotations.PropertySet;
34  import de.smartics.properties.report.qdox.PropertyDescriptorSearcher;
35  import de.smartics.testdoc.annotations.Uut;
36  
37  /**
38   * Tests {@link PropertyDescriptorSearcher}.
39   */
40  public class PropertyDescriptorSearcherTest
41  {
42    // ********************************* Fields *********************************
43  
44    // --- constants ------------------------------------------------------------
45  
46    // --- members --------------------------------------------------------------
47  
48    @Uut
49    private PropertyDescriptorSearcher uut;
50  
51    // ****************************** Inner Classes *****************************
52  
53    // ********************************* Methods ********************************
54  
55    // --- prepare --------------------------------------------------------------
56  
57    @Before
58    public void setUp()
59    {
60      uut = new PropertyDescriptorSearcher();
61    }
62  
63    // --- helper ---------------------------------------------------------------
64  
65    // --- tests ----------------------------------------------------------------
66  
67    @Test
68    public void recognizesInterfacesAnnotatedWithTheProjectSetAnnotation()
69    {
70      final JavaClass annotationClass = mock(JavaClass.class);
71      when(annotationClass.getFullyQualifiedName()).thenReturn(
72          PropertySet.class.getName());
73  
74      final JavaAnnotation annotation = mock(JavaAnnotation.class);
75      when(annotation.getType()).thenReturn(annotationClass);
76  
77      final JavaClass cls = mock(JavaClass.class);
78      when(cls.isInterface()).thenReturn(true);
79      final List<JavaAnnotation> annotations = new ArrayList<JavaAnnotation>(1);
80      annotations.add(annotation);
81      when(cls.getAnnotations()).thenReturn(annotations);
82  
83      final boolean match = uut.eval(cls);
84  
85      assertThat(match, is(true));
86    }
87  
88    @Test
89    public void rejectsIfTheAnnotationIsProvidedAtAClass()
90    {
91      final JavaClass annotationClass = mock(JavaClass.class);
92      when(annotationClass.getFullyQualifiedName()).thenReturn(
93          PropertySet.class.getName());
94  
95      final JavaAnnotation annotation = mock(JavaAnnotation.class);
96      when(annotation.getType()).thenReturn(annotationClass);
97  
98      final JavaClass cls = mock(JavaClass.class);
99      when(cls.isInterface()).thenReturn(false);
100     final List<JavaAnnotation> annotations = new ArrayList<JavaAnnotation>(1);
101     annotations.add(annotation);
102     when(cls.getAnnotations()).thenReturn(annotations);
103 
104     final boolean match = uut.eval(cls);
105 
106     assertThat(match, is(false));
107   }
108 
109   @Test
110   public void rejectsIfTheInterfaceIsNotAnnotatedWithThePropertySetAnnotation()
111   {
112     final JavaClass cls = mock(JavaClass.class);
113     when(cls.isInterface()).thenReturn(true);
114     final List<JavaAnnotation> annotations = new ArrayList<JavaAnnotation>(0);
115     when(cls.getAnnotations()).thenReturn(annotations);
116 
117     final boolean match = uut.eval(cls);
118 
119     assertThat(match, is(false));
120   }
121 
122   @Test
123   public void rejectsIfInterfaceIsAnnotatedWithOtherAnnotation()
124   {
125     final JavaClass annotationClass = mock(JavaClass.class);
126     when(annotationClass.getFullyQualifiedName()).thenReturn(
127         PropertyKeyName.class.getName());
128 
129     final JavaAnnotation annotation = mock(JavaAnnotation.class);
130     when(annotation.getType()).thenReturn(annotationClass);
131 
132     final JavaClass cls = mock(JavaClass.class);
133     when(cls.isInterface()).thenReturn(false);
134     final List<JavaAnnotation> annotations = new ArrayList<JavaAnnotation>(1);
135     annotations.add(annotation);
136     when(cls.getAnnotations()).thenReturn(annotations);
137 
138     final boolean match = uut.eval(cls);
139 
140     assertThat(match, is(false));
141   }
142 }