1 package com.atlassian.plugins.rest.module.scanner;
2
3 import com.atlassian.rest.test.jar.AnAnnotation;
4 import com.atlassian.rest.test.jar.AnnotatedClass;
5 import com.atlassian.rest.test.jar.ClassWithAnnotatedMethod;
6 import com.atlassian.rest.test.jar2.AnotherAnnotatedClass;
7 import com.atlassian.rest.test.jar2.jar3.YetAnotherAnnotatedClass;
8 import org.junit.Before;
9 import org.junit.Test;
10
11 import static org.junit.Assert.*;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import org.osgi.framework.Bundle;
16
17 import java.io.File;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.Set;
22
23 public class AnnotatedClassScannerTest {
24 private AnnotatedClassScanner scanner;
25 private Bundle bundle;
26
27 @Before
28 public void setUp() throws Exception {
29 bundle = mock(Bundle.class);
30 when(bundle.getLocation()).thenReturn(getTestJar().getAbsolutePath());
31 when(bundle.loadClass(AnnotatedClass.class.getName())).thenReturn((Class) AnnotatedClass.class);
32 when(bundle.loadClass(AnotherAnnotatedClass.class.getName())).thenReturn((Class) AnotherAnnotatedClass.class);
33 when(bundle.loadClass(YetAnotherAnnotatedClass.class.getName())).thenReturn((Class) YetAnotherAnnotatedClass.class);
34 when(bundle.loadClass(ClassWithAnnotatedMethod.class.getName())).thenReturn((Class) ClassWithAnnotatedMethod.class);
35
36 scanner = new AnnotatedClassScanner(bundle, AnAnnotation.class);
37 }
38
39 @Test
40 public void testScanWithNoBasePackage() {
41 testScan(Arrays.asList(AnnotatedClass.class, AnotherAnnotatedClass.class, YetAnotherAnnotatedClass.class, ClassWithAnnotatedMethod.class));
42 }
43
44 @Test
45 public void testScanWithBasePackage() {
46 testScan(Arrays.asList(AnnotatedClass.class, ClassWithAnnotatedMethod.class), "com.atlassian.rest.test.jar");
47 }
48
49 @Test
50 public void testScanWithBasePackageUsingSlashes() {
51 testScan(Arrays.asList(AnnotatedClass.class, ClassWithAnnotatedMethod.class), "com/atlassian/rest/test/jar");
52 }
53
54 @Test
55 public void testScanWithBasePackageAndSubPackages() {
56 testScan(Arrays.asList(AnotherAnnotatedClass.class, YetAnotherAnnotatedClass.class), "com/atlassian/rest/test/jar2");
57 }
58
59 @Test
60 public void testScanWithMultipleBasePackagesAndSubPackages() {
61 testScan(Arrays.asList(AnnotatedClass.class, ClassWithAnnotatedMethod.class, AnotherAnnotatedClass.class, YetAnotherAnnotatedClass.class), "com/atlassian/rest/test/jar", "com/atlassian/rest/test/jar2");
62 }
63
64 @Test
65 public void testScanWithMultipleBasePackages() {
66 testScan(Arrays.asList(AnnotatedClass.class, ClassWithAnnotatedMethod.class, YetAnotherAnnotatedClass.class), "com/atlassian/rest/test/jar", "com/atlassian/rest/test/jar2/jar3");
67 }
68
69 @Test
70 public void testGetBundleFileEscapesBundleLocationBeforeResolvingFile() {
71 final String bundlePath = "some%20path";
72 when(bundle.getLocation()).thenReturn("file:" + bundlePath);
73
74 final File bundleFile = scanner.getBundleFile(bundle);
75
76 assertEquals(bundlePath.replace("%20", " "), bundleFile.getPath());
77 }
78
79 @Test
80 public void testGetBundleFileRemovesReferenceProtocolBeforeResolvingFile() {
81 final String BUNDLE_PATH = new File("some/random/path").getPath();
82 when(bundle.getLocation()).thenReturn("reference:file:" + BUNDLE_PATH);
83
84 final File bundleFile = scanner.getBundleFile(bundle);
85
86 assertEquals(BUNDLE_PATH, bundleFile.getPath());
87 }
88
89 @Test
90 public void testGetBundleResolvesFileLocation() {
91 final String BUNDLE_PATH = new File("some/random/path").getPath();
92 when(bundle.getLocation()).thenReturn(BUNDLE_PATH);
93
94 final File bundleFile = scanner.getBundleFile(bundle);
95
96 assertEquals(BUNDLE_PATH, bundleFile.getPath());
97 }
98
99 public void testScan(List<Class<?>> expected, String... packages) {
100 final Set<Class<?>> classes = scanner.scan(packages);
101 assertCollectionEquals(expected, new ArrayList<Class<?>>(classes));
102 }
103
104 private static void assertCollectionEquals(List<Class<?>> expected, List<Class<?>> actual) {
105 try {
106 assertEquals(expected.size(), actual.size());
107 } catch (AssertionError error) {
108 System.err.println("Expected " + expected + " but got " + actual);
109 throw error;
110 }
111 for (Class<?> aClass : expected) {
112 try {
113 assertTrue(actual.contains(aClass));
114 } catch (AssertionError error) {
115 System.err.println("Expected <" + aClass + "> in " + actual);
116 throw error;
117 }
118 }
119 }
120
121 private File getTestJar() {
122 return new File(this.getClass().getClassLoader().getResource("atlassian-rest-test-jar.jar").getFile());
123 }
124 }