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