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