Clean all unused and redundant imports in controller.
[controller.git] / opendaylight / northbound / bundlescanner / implementation / src / test / java / org / opendaylight / controller / northbound / bundlescanner / internal / BundleScannerTest.java
1 package org.opendaylight.controller.northbound.bundlescanner.internal;
2
3 import static org.junit.Assert.*;
4
5 import java.io.File;
6 import java.io.FileFilter;
7 import java.net.MalformedURLException;
8 import java.net.URISyntaxException;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Dictionary;
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15 import java.util.List;
16 import java.util.regex.Pattern;
17
18 import org.junit.AfterClass;
19 import org.junit.Before;
20 import org.junit.BeforeClass;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.TestName;
24 import org.osgi.framework.Bundle;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.BundleEvent;
27 import org.osgi.framework.Constants;
28 import org.springframework.osgi.mock.MockBundle;
29 import org.springframework.osgi.mock.MockBundleContext;
30
31 public class BundleScannerTest {
32
33     private static BundleScanner bundleScanner;
34     private static List<Bundle> bundles;
35     @Rule
36     public final TestName testName = new TestName();
37
38     @BeforeClass
39     public static void init() throws Exception {
40         bundles = makeMockBundles();
41         bundleScanner = new BundleScanner(bundles.toArray(new Bundle[bundles.size()]));
42     }
43
44     @AfterClass
45     public static void destroy() throws Exception {
46     }
47
48     @Before
49     public void setup() {
50         System.out.println("==== " + testName.getMethodName());
51     }
52
53     @Test
54     public void testValidateBundles() {
55         assertNotNull(bundleScanner);
56         BundleContext context = bundles.get(0).getBundleContext();
57         assertNotNull(context.getBundle());
58         assertNotNull(context.getBundles());
59         assertNotNull(context.getBundles().length >= 4);
60     }
61
62     @Test
63     public void testBundleEvents() throws Exception {
64         MockBundle newBundle = new TestMockBundle("misc", "", "bundle_misc");
65         assertTrue(bundleScanner.getAnnotatedClasses(
66                 newBundle.getBundleContext(), null, false).size() == 0);
67         BundleEvent event = new BundleEvent(BundleEvent.RESOLVED, newBundle);
68         bundleScanner.bundleChanged(event);
69         assertTrue(bundleScanner.getAnnotatedClasses(
70                 newBundle.getBundleContext(), null, false).size() == 1);
71     }
72
73     @Test
74     public void testAnnotatedClassesWithDependencies() throws Exception {
75         for (Bundle bundle : bundles) {
76             List<Class<?>> classes = bundleScanner.getAnnotatedClasses(
77                     bundle.getBundleContext(), null, true);
78             String name = bundle.getSymbolicName();
79             System.out.println("name:" + name + " classes:" + classes.size());
80             if ("misc".equals(name)) {
81                 assertTrue(classes.size() == 1);
82             } else if ("base".equals(name)) {
83                 assertTrue(classes.size() == 5);
84             } else if ("sub1".equals(name)) {
85                 assertTrue(classes.size() == 6);
86             } else if ("sub2".equals(name)) {
87                 assertTrue(classes.size() == 7);
88             }
89         }
90     }
91
92     @Test
93     public void testExactFiltering() {
94         Bundle bundle = findBundle("sub1");
95         String[] annos = { "javax.xml.bind.annotation.XmlTransient" };
96         List<Class<?>> classes = bundleScanner.getAnnotatedClasses(
97                 bundle.getBundleContext(), annos, true);
98         assertTrue(classes.size() == 1);
99     }
100
101     @Test
102     public void testNonExactFiltering() {
103         Bundle bundle = findBundle("sub1");
104         String[] annos = { "javax.xml.bind.annotation.*" };
105         List<Class<?>> classes = bundleScanner.getAnnotatedClasses(
106                 bundle.getBundleContext(), annos, true);
107         assertTrue(classes.size() == 6);
108     }
109
110     @Test
111     public void testFilteringUnmatched() {
112         Bundle bundle = findBundle("sub1");
113         String[] annos = { "non.existent.pkg" };
114         List<Class<?>> classes = bundleScanner.getAnnotatedClasses(
115                 bundle.getBundleContext(), annos, true);
116         assertTrue(classes.size() == 0);
117     }
118
119     @Test
120     public void testRegexMerge() {
121         Pattern pattern = BundleScanner.mergePatterns(
122                 new String[] {
123                         "javax.xml.bind.annotation.*",
124                         "javax.ws.rs.Path"
125                     },
126                 true
127             );
128         assertTrue(pattern.matcher("Ljavax/xml/bind/annotation/FOO;").find());
129         assertFalse(pattern.matcher("Ljavax/servlet/FOO;").find());
130     }
131
132     private static Bundle findBundle(String symName) {
133         for (Bundle bundle : bundles) {
134             if (bundle.getSymbolicName().equals(symName)) return bundle;
135         }
136         return null;
137     }
138
139     private static List<Bundle> makeMockBundles() throws Exception {
140         List<Bundle> result = new ArrayList<Bundle>();
141         result.add(new MockBundle());
142         result.add(new TestMockBundle("base", "", "bundle_base"));
143         result.add(new TestMockBundle("sub1", "bundle_base", "bundle_sub1"));
144         result.add(new TestMockBundle("sub2", "bundle_base", "bundle_sub2"));
145         return result;
146     }
147
148     private static List<URL> findClasses(String pkg) throws URISyntaxException {
149         if (pkg == null) return Collections.EMPTY_LIST;
150         String npkg = pkg.replaceAll("\\.", "/");
151         URL dirUrl = BundleScannerTest.class.getClassLoader().getResource(npkg);
152         final List<URL> result = new ArrayList<URL>();
153         File dir = new File(dirUrl.toURI());
154         dir.listFiles(new FileFilter() {
155
156             @Override
157             public boolean accept(File file) {
158                 if (file.isFile() && file.getName().endsWith(".class")) {
159                     try {
160                         result.add(file.toURI().toURL());
161                     } catch (MalformedURLException e) {
162                         throw new IllegalStateException(e);
163                     }
164                 }
165                 return false;
166             }
167
168         });
169         return result;
170     }
171
172     public static class TestMockBundle extends MockBundle {
173         List<URL> classes;
174         public TestMockBundle(String name, String imports, String exports) throws Exception {
175             super(name, makeHeaders(name, imports, exports), new MockBundleContext() {
176                 @Override
177                 public Bundle[] getBundles() {
178                     return bundles.toArray(new Bundle[bundles.size()]);
179                 }
180             });
181             MockBundleContext ctx = (MockBundleContext) this.getBundleContext();
182             ctx.setBundle(this);
183             this.classes = findClasses(exports);
184         }
185
186         private static Dictionary<String,String> makeHeaders(
187                 String name, String imports, String exports)
188         {
189             Dictionary<String,String> headers = new Hashtable<String,String>();
190             headers.put(Constants.IMPORT_PACKAGE, imports);
191             headers.put(Constants.EXPORT_PACKAGE, exports);
192             headers.put(Constants.BUNDLE_SYMBOLICNAME, name);
193             return headers;
194         }
195
196         @Override
197         public Enumeration findEntries(String path, String filePattern, boolean recurse) {
198             return Collections.enumeration(classes);
199         }
200
201         @Override
202         public long getBundleId() {
203             return hashCode();
204         }
205     }
206 }