initial commit
[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.classpath.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       }
45
46       URL[] urls = ClasspathUrlFinder.findClassPaths("titan-cruise-1.0.jar");
47       Assert.assertNotNull(urls);
48       verify(urls);
49    }
50
51
52    @Test
53    public void testFindClasspaths2() throws Exception
54    {
55       Assert.assertNotNull(System.getProperty("java.class.path"));
56       if (System.getProperty("java.class.path").indexOf("titan-cruise-1.0.jar") == -1)
57       {
58          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");
59       }
60
61       URL[] urls = ClasspathUrlFinder.findClassPaths();
62       Assert.assertNotNull(urls);
63       AnnotationDB db = verify(urls);
64
65       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
66       Set<String> tests = annotationIndex.get("org.junit.Test");
67       Assert.assertTrue(tests.contains(TestSmoke.class.getName()));
68
69    }
70
71    @Test
72    public void testFieldParameter() throws Exception
73    {
74       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
75       AnnotationDB db = new AnnotationDB();
76       db.scanArchives(url);
77
78       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
79       Set<String> simpleClasses = annotationIndex.get(SimpleAnnotation.class.getName());
80       Assert.assertTrue(simpleClasses.contains(ClassWithFieldAnnotation.class.getName()));
81       Assert.assertTrue(simpleClasses.contains(InterfaceWithParameterAnnotations.class.getName()));
82
83       Set<String> simpleAnnotations = db.getClassIndex().get(ClassWithFieldAnnotation.class.getName());
84       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
85       simpleAnnotations = db.getClassIndex().get(InterfaceWithParameterAnnotations.class.getName());
86       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
87
88
89
90    }
91
92    @Test
93    public void testCrossRef() throws Exception
94    {
95       URL url = ClasspathUrlFinder.findClassBase(TestSmoke.class);
96       AnnotationDB db = new AnnotationDB();
97       db.scanArchives(url);
98       db.crossReferenceImplementedInterfaces();
99
100       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
101       Set<String> simpleClasses = annotationIndex.get(SimpleAnnotation.class.getName());
102       Assert.assertTrue(simpleClasses.contains(CrossRef.class.getName()));
103
104       Set<String> simpleAnnotations = db.getClassIndex().get(CrossRef.class.getName());
105       Assert.assertTrue(simpleAnnotations.contains(SimpleAnnotation.class.getName()));
106    }
107
108
109    @Test
110    public void testByClass() throws Exception
111    {
112       URL url = ClasspathUrlFinder.findClassBase(Address.class);
113       Assert.assertNotNull(url);
114       verify(url);
115    }
116
117
118    private AnnotationDB verify(URL... urls)
119            throws IOException
120    {
121       AnnotationDB db = new AnnotationDB();
122       db.scanArchives(urls);
123
124       Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
125       {
126          Set<String> entities = annotationIndex.get("javax.persistence.Entity");
127          Assert.assertNotNull(entities);
128
129          Assert.assertTrue(entities.contains("com.titan.domain.Address"));
130          Assert.assertTrue(entities.contains("com.titan.domain.Cabin"));
131          Assert.assertTrue(entities.contains("com.titan.domain.CreditCard"));
132          Assert.assertTrue(entities.contains("com.titan.domain.CreditCompany"));
133          Assert.assertTrue(entities.contains("com.titan.domain.Cruise"));
134          Assert.assertTrue(entities.contains("com.titan.domain.Customer"));
135          Assert.assertTrue(entities.contains("com.titan.domain.Phone"));
136          Assert.assertTrue(entities.contains("com.titan.domain.Reservation"));
137          Assert.assertTrue(entities.contains("com.titan.domain.Ship"));
138       }
139
140       {
141          Set<String> entities = annotationIndex.get("javax.persistence.GeneratedValue");
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.Id");
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       Map<String, Set<String>> classIndex = db.getClassIndex();
171       Set<String> annotations = classIndex.get("com.titan.domain.Address");
172       Assert.assertNotNull(annotations);
173       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
174       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
175       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
176
177       annotations = classIndex.get("com.titan.domain.Cabin");
178       Assert.assertNotNull(annotations);
179       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
180       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
181       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
182
183       annotations = classIndex.get("com.titan.domain.CreditCard");
184       Assert.assertNotNull(annotations);
185       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
186       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
187       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
188
189       annotations = classIndex.get("com.titan.domain.CreditCompany");
190       Assert.assertNotNull(annotations);
191       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
192       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
193       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
194
195       annotations = classIndex.get("com.titan.domain.Cruise");
196       Assert.assertNotNull(annotations);
197       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
198       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
199       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
200
201       annotations = classIndex.get("com.titan.domain.Customer");
202       Assert.assertNotNull(annotations);
203       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
204       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
205       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
206
207       annotations = classIndex.get("com.titan.domain.Phone");
208       Assert.assertNotNull(annotations);
209       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
210       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
211       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
212
213       annotations = classIndex.get("com.titan.domain.Reservation");
214       Assert.assertNotNull(annotations);
215       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
216       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
217       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
218
219       annotations = classIndex.get("com.titan.domain.Ship");
220       Assert.assertNotNull(annotations);
221       Assert.assertTrue(annotations.contains("javax.persistence.Entity"));
222       Assert.assertTrue(annotations.contains("javax.persistence.Id"));
223       Assert.assertTrue(annotations.contains("javax.persistence.GeneratedValue"));
224             
225       return db;
226    }
227 }