ClientWeavingHook implements WeavingHook { @Override public void weave(WovenClass wovenClass) { Bundle consumerBundle = wovenClass.getBundleWiring().getBundle(); Set<WeavingData> wd = activator.getWeavingData(consumerBundle); if (wd != null) { activator.log(Level.FINE, "Weaving class " + wovenClass.getClassName()); ClassReader cr = new ClassReader(wovenClass.getBytes()); ClassWriter cw = new OSGiFriendlyClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES, wovenClass.getBundleWiring().getClassLoader()); TCCLSetterVisitor tsv = new TCCLSetterVisitor(cw, wovenClass.getClassName(), wd); cr.accept(tsv, ClassReader.SKIP_FRAMES); if (tsv.isWoven()) { wovenClass.setBytes(cw.toByteArray()); if (tsv.additionalImportRequired()) wovenClass.getDynamicImports().add(addedImport); if (activator.isLogEnabled(Level.FINEST)) { StringWriter stringWriter = new StringWriter(); ClassReader reader = new ClassReader(wovenClass.getBytes()); ClassVisitor tracer = new TraceClassVisitor(new PrintWriter(stringWriter)); ClassVisitor checker = new CheckClassAdapter(tracer, true); reader.accept(checker, 0); activator.log(Level.FINEST, "Woven class bytecode: \n" + stringWriter.toString()); } } } } ClientWeavingHook(BundleContext context, DynamicWeavingActivator dwActivator); @Override void weave(WovenClass wovenClass);  }
@Test public void testBasicServiceLoaderUsage() throws Exception { Dictionary<String, String> consumerHeaders = new Hashtable<String, String>(); consumerHeaders.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*"); Bundle providerBundle = mockProviderBundle("impl1", 1); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(consumerHeaders, providerBundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); Assert.assertNotNull("Precondition", clsUrl); String clientClassName = "org.apache.aries.spifly.dynamic.TestClient"; WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle); Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size()); wh.weave(wc); Assert.assertEquals(1, wc.getDynamicImports().size()); String di1 = "org.apache.aries.spifly"; String di = wc.getDynamicImports().get(0); Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di)); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Assert.assertEquals(Collections.singleton("olleh"), result); }
@Test public void test_ARIES_1755_ServiceLoaderUsage() throws Exception { Dictionary<String, String> consumerHeaders = new Hashtable<String, String>(); consumerHeaders.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*"); Bundle providerBundle = mockProviderBundle("impl1", 1); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(consumerHeaders, providerBundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient3$EnumLoader.class"); Assert.assertNotNull("Precondition", clsUrl); String clientClassName = "org.apache.aries.spifly.dynamic.TestClient3$EnumLoader"; WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle); Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size()); wh.weave(wc); Assert.assertEquals(1, wc.getDynamicImports().size()); String di1 = "org.apache.aries.spifly"; String di = wc.getDynamicImports().get(0); Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di)); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("load", new Class [] {Class.class}); @SuppressWarnings({ "unchecked", "rawtypes" }) Object result = method.invoke(Enum.valueOf((Class<? extends Enum>)cls, "INSTANCE"), MySPI.class); Assert.assertTrue("Either null or not a MySPI", result instanceof MySPI); MySPI instance = (MySPI)result; String someMethod = instance.someMethod("Hello"); Assert.assertEquals("olleH", someMethod); }
@Test public void testBasicServiceLoaderUsage2() throws Exception { Dictionary<String, String> consumerHeaders = new Hashtable<String, String>(); consumerHeaders.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*"); Bundle providerBundle = mockProviderBundle("impl1", 1); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(consumerHeaders, providerBundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); Assert.assertNotNull("Precondition", clsUrl); String clientClassName = "org.apache.aries.spifly.dynamic.TestClient"; WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle); Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size()); wh.weave(wc); Assert.assertEquals(1, wc.getDynamicImports().size()); String di1 = "org.apache.aries.spifly"; String di = wc.getDynamicImports().get(0); Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di)); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("testService2", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Assert.assertEquals(Collections.singleton("olleh"), result); }
@Test public void testBasicServiceLoaderUsage3() throws Exception { Dictionary<String, String> consumerHeaders = new Hashtable<String, String>(); consumerHeaders.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*"); Bundle providerBundle = mockProviderBundle("impl1", 1); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(consumerHeaders, providerBundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient2.class"); Assert.assertNotNull("Precondition", clsUrl); String clientClassName = "org.apache.aries.spifly.dynamic.TestClient2"; WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle); Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size()); wh.weave(wc); Assert.assertEquals(1, wc.getDynamicImports().size()); String di1 = "org.apache.aries.spifly"; String di = wc.getDynamicImports().get(0); Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di)); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Assert.assertEquals(Collections.singleton("olleh"), result); }
@Test public void testTCCLResetting() throws Exception { ClassLoader cl = new URLClassLoader(new URL [] {}); Thread.currentThread().setContextClassLoader(cl); Assert.assertSame("Precondition", cl, Thread.currentThread().getContextClassLoader()); Dictionary<String, String> consumerHeaders = new Hashtable<String, String>(); consumerHeaders.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*"); Bundle providerBundle = mockProviderBundle("impl1", 1); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(consumerHeaders, providerBundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); Assert.assertNotNull("Precondition", clsUrl); String clientClassName = "org.apache.aries.spifly.dynamic.TestClient"; WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle); Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size()); wh.weave(wc); Assert.assertEquals(1, wc.getDynamicImports().size()); String di1 = "org.apache.aries.spifly"; String di = wc.getDynamicImports().get(0); Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di)); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); method.invoke(cls.newInstance(), "hi there"); Assert.assertSame(cl, Thread.currentThread().getContextClassLoader()); }
@Test public void testAltServiceLoaderLoadUnprocessed() throws Exception { Bundle spiFlyBundle = mockSpiFlyBundle(); Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*"); Bundle consumerBundle = mockConsumerBundle(headers, spiFlyBundle); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("UnaffectedTestClient.class"); Assert.assertNotNull("Precondition", clsUrl); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.UnaffectedTestClient", consumerBundle); Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size()); wh.weave(wc); Assert.assertEquals("The client is not affected so no additional imports should have been added", 0, wc.getDynamicImports().size()); Bundle providerBundle = mockProviderBundle("impl1", 1); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>()); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Assert.assertEquals("impl4", result); }
@Test public void testMultipleProviders() throws Exception { Bundle spiFlyBundle = mockSpiFlyBundle(); Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*"); Bundle consumerBundle = mockConsumerBundle(headers, spiFlyBundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle); wh.weave(wc); Bundle providerBundle1 = mockProviderBundle("impl1", 1); Bundle providerBundle2 = mockProviderBundle("impl2", 2); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, new HashMap<String, Object>()); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Set<String> expected = new HashSet<String>(Arrays.asList("olleh", "HELLO", "5")); Assert.assertEquals("All three services should be invoked", expected, result); }
@Test public void testClientSpecifyingProvider() throws Exception { Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "java.util.ServiceLoader#load(java.lang.Class);bundle=impl2"); Bundle providerBundle1 = mockProviderBundle("impl1", 1); Bundle providerBundle2 = mockProviderBundle("impl2", 2); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Set<String> expected = new HashSet<String>(Arrays.asList("HELLO", "5")); Assert.assertEquals("Only the services from bundle impl2 should be selected", expected, result); }
@Test public void testClientSpecifyingProviderVersion() throws Exception { Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "java.util.ServiceLoader#load(java.lang.Class);bundle=impl2:version=1.2.3"); Bundle providerBundle1 = mockProviderBundle("impl1", 1); Bundle providerBundle2 = mockProviderBundle("impl2", 2); Bundle providerBundle3 = mockProviderBundle("impl2_123", 3, new Version(1, 2, 3)); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle3, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2, providerBundle3); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2, providerBundle3); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Assert.assertEquals("Only the services from bundle impl2 should be selected", Collections.singleton("Updated!hello!Updated"), result); }
@Test public void testClientMultipleTargetBundles() throws Exception { Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "java.util.ServiceLoader#load(java.lang.Class);bundle=impl1|impl4"); Bundle providerBundle1 = mockProviderBundle("impl1", 1); Bundle providerBundle2 = mockProviderBundle("impl2", 2); Bundle providerBundle4 = mockProviderBundle("impl4", 4); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle2, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle4, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle4, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2, providerBundle4); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2, providerBundle4); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Set<String> expected = new HashSet<String>(Arrays.asList("olleh", "impl4")); Assert.assertEquals("All providers should be selected for this one", expected, result); }
@Test public void testClientMultipleTargetBundles2() throws Exception { Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "java.util.ServiceLoader#load(java.lang.Class);bundleId=1|4"); Bundle providerBundle1 = mockProviderBundle("impl1", 1); Bundle providerBundle2 = mockProviderBundle("impl2", 2); Bundle providerBundle4 = mockProviderBundle("impl4", 4); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle2, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle4, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle4, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2, providerBundle4); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2, providerBundle4); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Set<String> expected = new HashSet<String>(Arrays.asList("olleh", "impl4")); Assert.assertEquals("All providers should be selected for this one", expected, result); }
@Test public void testClientSpecificProviderLoadArgument() throws Exception { Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "java.util.ServiceLoader#load(java.lang.Class[org.apache.aries.mytest.MySPI])," + "java.util.ServiceLoader#load(java.lang.Class[org.apache.aries.mytest.AltSPI]);bundle=impl4"); Bundle providerBundle1 = mockProviderBundle("impl1", 1); Bundle providerBundle2 = mockProviderBundle("impl2", 2); Bundle providerBundle4 = mockProviderBundle("impl4", 4); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle2, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle4, new HashMap<String, Object>()); activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle4, new HashMap<String, Object>()); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2, providerBundle4); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2, providerBundle4); WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator); URL clsUrl = getClass().getResource("TestClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {String.class}); Object result = method.invoke(cls.newInstance(), "hello"); Set<String> expected = new HashSet<String>(Arrays.asList("olleh", "impl4", "HELLO", "5")); Assert.assertEquals("All providers should be selected for this one", expected, result); URL cls2Url = getClass().getResource("AltTestClient.class"); WovenClass wc2 = new MyWovenClass(cls2Url, "org.apache.aries.spifly.dynamic.AltTestClient", consumerBundle); wh.weave(wc2); Class<?> cls2 = wc2.getDefinedClass(); Method method2 = cls2.getMethod("test", new Class [] {long.class}); Object result2 = method2.invoke(cls2.newInstance(), 4096); Assert.assertEquals("Only the services from bundle impl4 should be selected", -4096L, result2); }
@Test public void testJAXPClientWantsJREImplementation1() throws Exception { Bundle systembundle = mockSystemBundle(); Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "javax.xml.parsers.DocumentBuilderFactory#newInstance()"); Bundle consumerBundle = mockConsumerBundle(headers, systembundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); WeavingHook wh = new ClientWeavingHook(mockSpiFlyBundle(consumerBundle, systembundle).getBundleContext(), activator); URL clsUrl = getClass().getResource("JaxpClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.JaxpClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {}); Class<?> result = (Class<?>) method.invoke(cls.newInstance()); Assert.assertEquals("JAXP implementation from JRE", thisJVMsDBF, result.getName()); }
@Test public void testJAXPClientWantsAltImplementation1() throws Exception { Bundle systembundle = mockSystemBundle(); Bundle providerBundle = mockProviderBundle("impl3", 1); activator.registerProviderBundle("javax.xml.parsers.DocumentBuilderFactory", providerBundle, new HashMap<String, Object>()); Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "javax.xml.parsers.DocumentBuilderFactory#newInstance()"); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle, systembundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); WeavingHook wh = new ClientWeavingHook(mockSpiFlyBundle(consumerBundle, providerBundle, systembundle).getBundleContext(), activator); URL clsUrl = getClass().getResource("JaxpClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.JaxpClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {}); Class<?> result = (Class<?>) method.invoke(cls.newInstance()); Assert.assertEquals("JAXP implementation from JRE", "org.apache.aries.spifly.dynamic.impl3.MyAltDocumentBuilderFactory", result.getName()); }
@Test public void testJAXPClientWantsJREImplementation2() throws Exception { Bundle systembundle = mockSystemBundle(); Bundle providerBundle = mockProviderBundle("impl3", 1); activator.registerProviderBundle("javax.xml.parsers.DocumentBuilderFactory", providerBundle, new HashMap<String, Object>()); Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "javax.xml.parsers.DocumentBuilderFactory#newInstance();bundleId=0"); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle, systembundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); WeavingHook wh = new ClientWeavingHook(mockSpiFlyBundle(consumerBundle, providerBundle, systembundle).getBundleContext(), activator); URL clsUrl = getClass().getResource("JaxpClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.JaxpClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {}); Class<?> result = (Class<?>) method.invoke(cls.newInstance()); Assert.assertEquals("JAXP implementation from JRE", thisJVMsDBF, result.getName()); }
@Test public void testJAXPClientWantsAltImplementation2() throws Exception { Bundle systembundle = mockSystemBundle(); Bundle providerBundle = mockProviderBundle("impl3", 1); activator.registerProviderBundle("javax.xml.parsers.DocumentBuilderFactory", providerBundle, new HashMap<String, Object>()); Dictionary<String, String> headers = new Hashtable<String, String>(); headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "javax.xml.parsers.DocumentBuilderFactory#newInstance();bundle=impl3"); Bundle consumerBundle = mockConsumerBundle(headers, providerBundle, systembundle); activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER); WeavingHook wh = new ClientWeavingHook(mockSpiFlyBundle(consumerBundle, providerBundle, systembundle).getBundleContext(), activator); URL clsUrl = getClass().getResource("JaxpClient.class"); WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.JaxpClient", consumerBundle); wh.weave(wc); Class<?> cls = wc.getDefinedClass(); Method method = cls.getMethod("test", new Class [] {}); Class<?> result = (Class<?>) method.invoke(cls.newInstance()); Assert.assertEquals("JAXP implementation from alternative bundle", "org.apache.aries.spifly.dynamic.impl3.MyAltDocumentBuilderFactory", result.getName()); }