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));
}
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));
}
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));
}
Aggregations