* configurable ignored packages
[scannotation] / src / test / java / org / scannotation / test / TestSmoke.java
1 package org.scannotation.test;
2
3 import com.titan.domain.Address;
4 import org.junit.Assert;
5 import org.junit.Test;
6 import org.scannotation.AnnotationDB;
7 import org.scannotation.ClasspathUrlFinder;
8
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.net.URL;
12 import java.util.Map;
13 import java.util.Set;
14
15 /**
16  * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
17  * @version $Revision: 1 $
18  */
19 public class TestSmoke
20 {
21
22    @Test
23    public void testFindResourceBase() throws Exception
24    {
25       URL url = ClasspathUrlFinder.findResourceBase("com/titan/domain/Address.class");
26       Assert.assertNotNull(url);
27       verify(url);
28    }
29
30    @Test
31    public void testFindResourceBases() throws Exception
32    {
33       URL[] urls = ClasspathUrlFinder.findResourceBases("com/titan/domain/Address.class");
34       Assert.assertNotNull(urls);
35       verify(urls);
36    }
37
38    @Test
39    public void testFindClasspaths() throws Exception
40    {
41       Assert.assertNotNull(System.getProperty("java.class.path"));
42       if (System.getProperty("java.class.path").indexOf("titan-cruise-1.0.jar") == -1)
43       {
44          System.err.println("WARNING!!!!!!!! CANNOT TEST testFindClasspaths():  This is a Maven2 and Surefire problem in that it doesn't set java.class.path correctly.  I run this test within the IDE");
45          return;
46       }
47
48       URL[] urls = ClasspathUrlFinder.findClassPaths("titan-cruise-1.0.jar");
49       Assert.assertNotNull(urls);
50       verify(urls);
51    }
52
53
54    @Test
55    public void testFindClasspaths2() throws Exception
56    {
57       Assert.assertNotNull(System.getProperty("java.class.path"));
58       if (System.getProperty("java.class.path").indexOf("titan-cruise-1.0.jar") == -1)
59       {
60          System.err.println("WARNING!!!!!!! CANNOT TEST testFindClasspaths2():  This is a Maven2 and Surefire problem in that it doesn't set java.class.path correctly.  I run this test within the IDE");
61          return;
62       }
63
64       URL[] urls = ClasspathUrlFinder.findClassPaths();
65       Assert.assertNotNull(urls);
66       AnnotationDB db = verify(urls);
67
68       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
69       Set<String> tests = annotationIndex.get("org.junit.Test");
70       Assert.assertTrue(tests.contains(TestSmoke.class.getName()));
71
72    }
73
74    @Test
75    public void testFieldParameter() throws Exception
76    {
77       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
78       AnnotationDB db = new AnnotationDB();
79       db.scanArchives(url);
80
81       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
82       Set<String> simpleClasses = annotationIndex.get(SimpleAnnotation.class.getName());
83       Assert.assertTrue(simpleClasses.contains(ClassWithFieldAnnotation.class.getName()));
84       Assert.assertTrue(simpleClasses.contains(InterfaceWithParameterAnnotations.class.getName()));
85
86       Set<String> simpleAnnotations = db.getClassIndex().get(ClassWithFieldAnnotation.class.getName());
87       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
88       simpleAnnotations = db.getClassIndex().get(InterfaceWithParameterAnnotations.class.getName());
89       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
90
91
92
93    }
94
95    @Test
96    public void testCrossRef() throws Exception
97    {
98       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
99       AnnotationDB db = new AnnotationDB();
100       db.scanArchives(url);
101       db.crossReferenceImplementedInterfaces();
102
103       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
104       Set<String> simpleClasses = annotationIndex.get(SimpleAnnotation.class.getName());
105       Assert.assertTrue(simpleClasses.contains(CrossRef.class.getName()));
106
107       Set<String> simpleAnnotations = db.getClassIndex().get(CrossRef.class.getName());
108       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
109    }
110
111
112    @Test
113    public void testCrossRefMetaAnnotations() throws Exception
114    {
115       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
116       AnnotationDB db = new AnnotationDB();
117       db.scanArchives(url);
118       db.crossReferenceMetaAnnotations();
119
120       Assert.assertTrue(db.getAnnotationIndex().get(MetaAnnotation.class.getName()).contains(CrossRefMetaAnnotaiton.class.getName()));
121    }
122
123
124    @Test
125    public void testByClass() throws Exception
126    {
127       URL url = ClasspathUrlFinder.findClassBase(Address.class);
128       Assert.assertNotNull(url);
129       verify(url);
130    }
131
132
133    private AnnotationDB verify(URL... urls)
134            throws IOException
135    {
136       AnnotationDB db = new AnnotationDB();
137       db.scanArchives(urls);
138
139       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
140       {
141          Set<String> entities = annotationIndex.get("javax.persistence.Entity");
142          Assert.assertNotNull(entities);
143
144          Assert.assertTrue(entities.contains("com.titan.domain.Address"));
145          Assert.assertTrue(entities.contains("com.titan.domain.Cabin"));
146          Assert.assertTrue(entities.contains("com.titan.domain.CreditCard"));
147          Assert.assertTrue(entities.contains("com.titan.domain.CreditCompany"));
148          Assert.assertTrue(entities.contains("com.titan.domain.Cruise"));
149          Assert.assertTrue(entities.contains("com.titan.domain.Customer"));
150          Assert.assertTrue(entities.contains("com.titan.domain.Phone"));
151          Assert.assertTrue(entities.contains("com.titan.domain.Reservation"));
152          Assert.assertTrue(entities.contains("com.titan.domain.Ship"));
153       }
154
155       {
156          Set<String> entities = annotationIndex.get("javax.persistence.GeneratedValue");
157          Assert.assertNotNull(entities);
158
159          Assert.assertTrue(entities.contains("com.titan.domain.Address"));
160          Assert.assertTrue(entities.contains("com.titan.domain.Cabin"));
161          Assert.assertTrue(entities.contains("com.titan.domain.CreditCard"));
162          Assert.assertTrue(entities.contains("com.titan.domain.CreditCompany"));
163          Assert.assertTrue(entities.contains("com.titan.domain.Cruise"));
164          Assert.assertTrue(entities.contains("com.titan.domain.Customer"));
165          Assert.assertTrue(entities.contains("com.titan.domain.Phone"));
166          Assert.assertTrue(entities.contains("com.titan.domain.Reservation"));
167          Assert.assertTrue(entities.contains("com.titan.domain.Ship"));
168       }
169
170       {
171          Set<String> entities = annotationIndex.get("javax.persistence.Id");
172          Assert.assertNotNull(entities);
173
174          Assert.assertTrue(entities.contains("com.titan.domain.Address"));
175          Assert.assertTrue(entities.contains("com.titan.domain.Cabin"));
176          Assert.assertTrue(entities.contains("com.titan.domain.CreditCard"));
177          Assert.assertTrue(entities.contains("com.titan.domain.CreditCompany"));
178          Assert.assertTrue(entities.contains("com.titan.domain.Cruise"));
179          Assert.assertTrue(entities.contains("com.titan.domain.Customer"));
180          Assert.assertTrue(entities.contains("com.titan.domain.Phone"));
181          Assert.assertTrue(entities.contains("com.titan.domain.Reservation"));
182          Assert.assertTrue(entities.contains("com.titan.domain.Ship"));
183       }
184
185       Map<String, Set<String>> classIndex = db.getClassIndex();
186       Set<String> annotations = classIndex.get("com.titan.domain.Address");
187       Assert.assertNotNull(annotations);
188       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
189       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
190       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
191
192       annotations = classIndex.get("com.titan.domain.Cabin");
193       Assert.assertNotNull(annotations);
194       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
195       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
196       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
197
198       annotations = classIndex.get("com.titan.domain.CreditCard");
199       Assert.assertNotNull(annotations);
200       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
201       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
202       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
203
204       annotations = classIndex.get("com.titan.domain.CreditCompany");
205       Assert.assertNotNull(annotations);
206       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
207       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
208       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
209
210       annotations = classIndex.get("com.titan.domain.Cruise");
211       Assert.assertNotNull(annotations);
212       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
213       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
214       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
215
216       annotations = classIndex.get("com.titan.domain.Customer");
217       Assert.assertNotNull(annotations);
218       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
219       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
220       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
221
222       annotations = classIndex.get("com.titan.domain.Phone");
223       Assert.assertNotNull(annotations);
224       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
225       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
226       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
227
228       annotations = classIndex.get("com.titan.domain.Reservation");
229       Assert.assertNotNull(annotations);
230       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
231       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
232       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
233
234       annotations = classIndex.get("com.titan.domain.Ship");
235       Assert.assertNotNull(annotations);
236       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
237       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
238       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
239             
240       return db;
241    }
242 }