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