WIP: site + wagon
[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 /*
23    @Test
24    public void testFindResourceBase() throws Exception
25    {
26       URL url = ClasspathUrlFinder.findResourceBase("com/titan/domain/Address.class");
27       Assert.assertNotNull(url);
28       verify(url);
29    }
30 */
31
32 /*
33    @Test
34    public void testFindResourceBases() throws Exception
35    {
36       URL[] urls = ClasspathUrlFinder.findResourceBases("com/titan/domain/Address.class");
37       Assert.assertNotNull(urls);
38       verify(urls);
39    }
40 */
41
42    @Test
43    public void testFindClasspaths() throws Exception
44    {
45       Assert.assertNotNull(System.getProperty("java.class.path"));
46       if (System.getProperty("java.class.path").indexOf("titan-cruise-1.0.jar") == -1)
47       {
48          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");
49          return;
50       }
51
52       URL[] urls = ClasspathUrlFinder.findClassPaths("titan-cruise-1.0.jar");
53       Assert.assertNotNull(urls);
54       verify(urls);
55    }
56
57
58    @Test
59    public void testFindClasspaths2() throws Exception
60    {
61       Assert.assertNotNull(System.getProperty("java.class.path"));
62       if (System.getProperty("java.class.path").indexOf("titan-cruise-1.0.jar") == -1)
63       {
64          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");
65          return;
66       }
67
68       URL[] urls = ClasspathUrlFinder.findClassPaths();
69       Assert.assertNotNull(urls);
70       AnnotationDB db = verify(urls);
71
72       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
73       Set<String> tests = annotationIndex.get("org.junit.Test");
74       Assert.assertTrue(tests.contains(TestSmoke.class.getName()));
75
76    }
77
78    @Test
79    public void testFieldParameter() throws Exception
80    {
81       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
82       AnnotationDB db = new AnnotationDB();
83       db.scanArchives(url);
84
85       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
86       Set<String> simpleClasses = annotationIndex.get(SimpleAnnotation.class.getName());
87       Assert.assertTrue(simpleClasses.contains(ClassWithFieldAnnotation.class.getName()));
88       Assert.assertTrue(simpleClasses.contains(InterfaceWithParameterAnnotations.class.getName()));
89
90       Set<String> simpleAnnotations = db.getClassIndex().get(ClassWithFieldAnnotation.class.getName());
91       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
92       simpleAnnotations = db.getClassIndex().get(InterfaceWithParameterAnnotations.class.getName());
93       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
94
95
96
97    }
98
99    @Test
100    public void testCrossRef() throws Exception
101    {
102       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
103       AnnotationDB db = new AnnotationDB();
104       db.scanArchives(url);
105       db.crossReferenceImplementedInterfaces();
106
107       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
108       Set<String> simpleClasses = annotationIndex.get(SimpleAnnotation.class.getName());
109       Assert.assertTrue(simpleClasses.contains(CrossRef.class.getName()));
110
111       Set<String> simpleAnnotations = db.getClassIndex().get(CrossRef.class.getName());
112       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
113    }
114
115
116    @Test
117    public void testCrossRefMetaAnnotations() throws Exception
118    {
119       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
120       AnnotationDB db = new AnnotationDB();
121       db.scanArchives(url);
122       db.crossReferenceMetaAnnotations();
123
124       Assert.assertTrue(db.getAnnotationIndex().get(MetaAnnotation.class.getName()).contains(CrossRefMetaAnnotaiton.class.getName()));
125    }
126
127
128 /*
129    @Test
130    public void testByClass() throws Exception
131    {
132       URL url = ClasspathUrlFinder.findClassBase(Address.class);
133       Assert.assertNotNull(url);
134       verify(url);
135    }
136 */
137
138
139    private AnnotationDB verify(URL... urls)
140            throws IOException
141    {
142       AnnotationDB db = new AnnotationDB();
143       db.scanArchives(urls);
144
145       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
146       {
147          Set<String> entities = annotationIndex.get("javax.persistence.Entity");
148          Assert.assertNotNull(entities);
149
150          Assert.assertTrue(entities.contains("com.titan.domain.Address"));
151          Assert.assertTrue(entities.contains("com.titan.domain.Cabin"));
152          Assert.assertTrue(entities.contains("com.titan.domain.CreditCard"));
153          Assert.assertTrue(entities.contains("com.titan.domain.CreditCompany"));
154          Assert.assertTrue(entities.contains("com.titan.domain.Cruise"));
155          Assert.assertTrue(entities.contains("com.titan.domain.Customer"));
156          Assert.assertTrue(entities.contains("com.titan.domain.Phone"));
157          Assert.assertTrue(entities.contains("com.titan.domain.Reservation"));
158          Assert.assertTrue(entities.contains("com.titan.domain.Ship"));
159       }
160
161       {
162          Set<String> entities = annotationIndex.get("javax.persistence.GeneratedValue");
163          Assert.assertNotNull(entities);
164
165          Assert.assertTrue(entities.contains("com.titan.domain.Address"));
166          Assert.assertTrue(entities.contains("com.titan.domain.Cabin"));
167          Assert.assertTrue(entities.contains("com.titan.domain.CreditCard"));
168          Assert.assertTrue(entities.contains("com.titan.domain.CreditCompany"));
169          Assert.assertTrue(entities.contains("com.titan.domain.Cruise"));
170          Assert.assertTrue(entities.contains("com.titan.domain.Customer"));
171          Assert.assertTrue(entities.contains("com.titan.domain.Phone"));
172          Assert.assertTrue(entities.contains("com.titan.domain.Reservation"));
173          Assert.assertTrue(entities.contains("com.titan.domain.Ship"));
174       }
175
176       {
177          Set<String> entities = annotationIndex.get("javax.persistence.Id");
178          Assert.assertNotNull(entities);
179
180          Assert.assertTrue(entities.contains("com.titan.domain.Address"));
181          Assert.assertTrue(entities.contains("com.titan.domain.Cabin"));
182          Assert.assertTrue(entities.contains("com.titan.domain.CreditCard"));
183          Assert.assertTrue(entities.contains("com.titan.domain.CreditCompany"));
184          Assert.assertTrue(entities.contains("com.titan.domain.Cruise"));
185          Assert.assertTrue(entities.contains("com.titan.domain.Customer"));
186          Assert.assertTrue(entities.contains("com.titan.domain.Phone"));
187          Assert.assertTrue(entities.contains("com.titan.domain.Reservation"));
188          Assert.assertTrue(entities.contains("com.titan.domain.Ship"));
189       }
190
191       Map<String, Set<String>> classIndex = db.getClassIndex();
192       Set<String> annotations = classIndex.get("com.titan.domain.Address");
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.Cabin");
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.CreditCard");
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.CreditCompany");
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.Cruise");
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.Customer");
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.Phone");
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.Reservation");
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       annotations = classIndex.get("com.titan.domain.Ship");
241       Assert.assertNotNull(annotations);
242       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
243       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
244       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
245             
246       return db;
247    }
248 }