Search in sources :

Example 1 with WovenClassListener

use of org.osgi.framework.hooks.weaving.WovenClassListener in project felix by apache.

the class BundleWiringImplTest method testFindClassBadWeave.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testFindClassBadWeave() throws Exception {
    Felix mockFramework = mock(Felix.class);
    Content mockContent = mock(Content.class);
    ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
    ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
    Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
    hooks.add(mockServiceReferenceWeavingHook);
    DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
    Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
    listeners.add(mockServiceReferenceWovenClassListener);
    Class testClass = TestClass.class;
    String testClassName = testClass.getName();
    String testClassAsPath = testClassName.replace('.', '/') + ".class";
    byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
    List<Content> contentPath = new ArrayList<Content>();
    contentPath.add(mockContent);
    initializeSimpleBundleWiring();
    when(mockBundle.getFramework()).thenReturn(mockFramework);
    when(mockFramework.getBootPackages()).thenReturn(new String[0]);
    when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
    when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(WeavingHook.class)).thenReturn(hooks);
    when(mockFramework.getHookRegistry()).thenReturn(hReg);
    when(mockFramework.getService(mockFramework, mockServiceReferenceWeavingHook, false)).thenReturn(new BadDummyWovenHook());
    when(hReg.getHooks(WovenClassListener.class)).thenReturn(listeners);
    when(mockFramework.getService(mockFramework, mockServiceReferenceWovenClassListener, false)).thenReturn(dummyWovenClassListener);
    BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoader.class, bundleWiring);
    assertNotNull(bundleClassLoader);
    try {
        bundleClassLoader.findClass(TestClass.class.getName());
        fail("Class should throw exception");
    } catch (Error e) {
    // This is expected
    }
    assertEquals("There should be 1 state changes fired by the weaving", 1, dummyWovenClassListener.stateList.size());
    assertEquals("The only state change should be a failed transform on the class", (Object) WovenClass.TRANSFORMING_FAILED, dummyWovenClassListener.stateList.get(0));
}
Also used : BundleClassLoader(org.apache.felix.framework.BundleWiringImpl.BundleClassLoader) ArrayList(java.util.ArrayList) WovenClassListener(org.osgi.framework.hooks.weaving.WovenClassListener) ServiceReference(org.osgi.framework.ServiceReference) Content(org.apache.felix.framework.cache.Content) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with WovenClassListener

use of org.osgi.framework.hooks.weaving.WovenClassListener in project felix by apache.

the class BundleWiringImplTest method testFindClassWeaveDefineError.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testFindClassWeaveDefineError() throws Exception {
    Felix mockFramework = mock(Felix.class);
    Content mockContent = mock(Content.class);
    ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
    ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
    Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
    hooks.add(mockServiceReferenceWeavingHook);
    DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
    Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
    listeners.add(mockServiceReferenceWovenClassListener);
    Class testClass = TestClass.class;
    String testClassName = testClass.getName();
    String testClassAsPath = testClassName.replace('.', '/') + ".class";
    byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
    List<Content> contentPath = new ArrayList<Content>();
    contentPath.add(mockContent);
    initializeSimpleBundleWiring();
    when(mockBundle.getFramework()).thenReturn(mockFramework);
    when(mockFramework.getBootPackages()).thenReturn(new String[0]);
    when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
    when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(WeavingHook.class)).thenReturn(hooks);
    when(mockFramework.getHookRegistry()).thenReturn(hReg);
    when(mockFramework.getService(mockFramework, mockServiceReferenceWeavingHook, false)).thenReturn(new BadDefineWovenHook());
    when(hReg.getHooks(WovenClassListener.class)).thenReturn(listeners);
    when(mockFramework.getService(mockFramework, mockServiceReferenceWovenClassListener, false)).thenReturn(dummyWovenClassListener);
    BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoader.class, bundleWiring);
    assertNotNull(bundleClassLoader);
    try {
        bundleClassLoader.findClass(TestClass.class.getName());
        fail("Class should throw exception");
    } catch (Throwable e) {
    }
    assertEquals("There should be 2 state changes fired by the weaving", 2, dummyWovenClassListener.stateList.size());
    assertEquals("The first state change should transform the class", (Object) WovenClass.TRANSFORMED, dummyWovenClassListener.stateList.get(0));
    assertEquals("The second state change failed the define on the class", (Object) WovenClass.DEFINE_FAILED, dummyWovenClassListener.stateList.get(1));
}
Also used : BundleClassLoader(org.apache.felix.framework.BundleWiringImpl.BundleClassLoader) ArrayList(java.util.ArrayList) WovenClassListener(org.osgi.framework.hooks.weaving.WovenClassListener) ServiceReference(org.osgi.framework.ServiceReference) Content(org.apache.felix.framework.cache.Content) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with WovenClassListener

use of org.osgi.framework.hooks.weaving.WovenClassListener in project felix by apache.

the class BundleWiringImplTest method testFindClassWeave.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testFindClassWeave() throws Exception {
    Felix mockFramework = mock(Felix.class);
    Content mockContent = mock(Content.class);
    ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
    ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
    Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
    hooks.add(mockServiceReferenceWeavingHook);
    DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
    Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
    listeners.add(mockServiceReferenceWovenClassListener);
    Class testClass = TestClass.class;
    String testClassName = testClass.getName();
    String testClassAsPath = testClassName.replace('.', '/') + ".class";
    byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
    List<Content> contentPath = new ArrayList<Content>();
    contentPath.add(mockContent);
    initializeSimpleBundleWiring();
    when(mockBundle.getFramework()).thenReturn(mockFramework);
    when(mockFramework.getBootPackages()).thenReturn(new String[0]);
    when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
    when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(WeavingHook.class)).thenReturn(hooks);
    when(mockFramework.getHookRegistry()).thenReturn(hReg);
    when(mockFramework.getService(mockFramework, mockServiceReferenceWeavingHook, false)).thenReturn(new GoodDummyWovenHook());
    when(hReg.getHooks(WovenClassListener.class)).thenReturn(listeners);
    when(mockFramework.getService(mockFramework, mockServiceReferenceWovenClassListener, false)).thenReturn(dummyWovenClassListener);
    BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoader.class, bundleWiring);
    assertNotNull(bundleClassLoader);
    Class foundClass = null;
    try {
        foundClass = bundleClassLoader.findClass(TestClass.class.getName());
    } catch (ClassNotFoundException e) {
        fail("Class should not throw exception");
    }
    assertNotNull("Class Should be found in this classloader", foundClass);
    assertEquals("Weaving should have added a field", 1, foundClass.getFields().length);
    assertEquals("There should be 2 state changes fired by the weaving", 2, dummyWovenClassListener.stateList.size());
    assertEquals("The first state change should transform the class", (Object) WovenClass.TRANSFORMED, dummyWovenClassListener.stateList.get(0));
    assertEquals("The second state change should define the class", (Object) WovenClass.DEFINED, dummyWovenClassListener.stateList.get(1));
}
Also used : BundleClassLoader(org.apache.felix.framework.BundleWiringImpl.BundleClassLoader) ArrayList(java.util.ArrayList) WovenClassListener(org.osgi.framework.hooks.weaving.WovenClassListener) ServiceReference(org.osgi.framework.ServiceReference) Content(org.apache.felix.framework.cache.Content) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 BundleClassLoader (org.apache.felix.framework.BundleWiringImpl.BundleClassLoader)3 Content (org.apache.felix.framework.cache.Content)3 Test (org.junit.Test)3 ServiceReference (org.osgi.framework.ServiceReference)3 WeavingHook (org.osgi.framework.hooks.weaving.WeavingHook)3 WovenClass (org.osgi.framework.hooks.weaving.WovenClass)3 WovenClassListener (org.osgi.framework.hooks.weaving.WovenClassListener)3