1 package org.opendaylight.controller.northbound.bundlescanner.internal;
3 import static org.junit.Assert.*;
6 import java.io.FileFilter;
7 import java.net.MalformedURLException;
8 import java.net.URISyntaxException;
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;
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;
31 public class BundleScannerTest {
33 private static BundleScanner bundleScanner;
34 private static List<Bundle> bundles;
36 public final TestName testName = new TestName();
39 public static void init() throws Exception {
40 bundles = makeMockBundles();
41 bundleScanner = new BundleScanner(bundles.toArray(new Bundle[bundles.size()]));
45 public static void destroy() throws Exception {
50 System.out.println("==== " + testName.getMethodName());
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);
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);
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);
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);
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);
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);
120 public void testRegexMerge() {
121 Pattern pattern = BundleScanner.mergePatterns(
123 "javax.xml.bind.annotation.*",
128 assertTrue(pattern.matcher("Ljavax/xml/bind/annotation/FOO;").find());
129 assertFalse(pattern.matcher("Ljavax/servlet/FOO;").find());
132 private static Bundle findBundle(String symName) {
133 for (Bundle bundle : bundles) {
134 if (bundle.getSymbolicName().equals(symName)) return bundle;
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"));
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() {
157 public boolean accept(File file) {
158 if (file.isFile() && file.getName().endsWith(".class")) {
160 result.add(file.toURI().toURL());
161 } catch (MalformedURLException e) {
162 throw new IllegalStateException(e);
172 public static class TestMockBundle extends MockBundle {
174 public TestMockBundle(String name, String imports, String exports) throws Exception {
175 super(name, makeHeaders(name, imports, exports), new MockBundleContext() {
177 public Bundle[] getBundles() {
178 return bundles.toArray(new Bundle[bundles.size()]);
181 MockBundleContext ctx = (MockBundleContext) this.getBundleContext();
183 this.classes = findClasses(exports);
186 private static Dictionary<String,String> makeHeaders(
187 String name, String imports, String exports)
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);
197 public Enumeration findEntries(String path, String filePattern, boolean recurse) {
198 return Collections.enumeration(classes);
202 public long getBundleId() {