use of org.osgi.framework.BundleContext in project sling by apache.
the class FormAuthenticationHandlerTest method test_getTokenFile.
@Test
public void test_getTokenFile() {
final File root = new File("bundle999").getAbsoluteFile();
final SlingHomeAction slingHome = new SlingHomeAction();
slingHome.setSlingHome(new File("sling").getAbsolutePath());
Mockery context = new Mockery();
final BundleContext bundleContext = context.mock(BundleContext.class);
context.checking(new Expectations() {
{
// mock access to sling.home framework property
allowing(bundleContext).getProperty("sling.home");
will(slingHome);
// mock no data file support with file names starting with sl
allowing(bundleContext).getDataFile(with(new StringStartsWith("sl")));
will(returnValue(null));
// mock data file support for any other name
allowing(bundleContext).getDataFile(with(any(String.class)));
will(new RVA(root));
}
});
final FormAuthenticationHandler handler = new FormAuthenticationHandler();
// test files relative to bundle context
File relFile0 = handler.getTokenFile("", bundleContext);
assertEquals(root, relFile0);
String relName1 = "rel/path";
File relFile1 = handler.getTokenFile(relName1, bundleContext);
assertEquals(new File(root, relName1), relFile1);
// test file relative to sling.home if no data file support
String relName2 = "sl/rel_to_sling.home";
File relFile2 = handler.getTokenFile(relName2, bundleContext);
assertEquals(new File(slingHome.getSlingHome(), relName2), relFile2);
// test file relative to current working directory
String relName3 = "sl/test";
slingHome.setSlingHome(null);
File relFile3 = handler.getTokenFile(relName3, bundleContext);
assertEquals(new File(relName3).getAbsoluteFile(), relFile3);
// test absolute file return
File absFile = new File("test").getAbsoluteFile();
File absFile0 = handler.getTokenFile(absFile.getPath(), bundleContext);
assertEquals(absFile, absFile0);
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class SlingAuthenticatorServiceListenerTest method testDuplicateRegistration.
@Test
public void testDuplicateRegistration() {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, cache);
final ServiceReference<?> ref1 = createServiceReference(new String[] { "/path1", "/path1", "/path2" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref1));
final ServiceReference<?> ref2 = createServiceReference(new String[] { "/path2", "/path3" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref2));
assertPaths(cache, new String[] { "/path1", "/path2", "/path2", "/path3" }, new ServiceReference<?>[] { ref1, ref1, ref2, ref2 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref2));
assertPaths(cache, new String[] { "/path1", "/path2" }, new ServiceReference<?>[] { ref1, ref1 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref1));
assertTrue(cache.getHolders().isEmpty());
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class SlingAuthenticatorServiceListenerTest method testAddRemoveRegistration.
@Test
public void testAddRemoveRegistration() {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, cache);
assertTrue(cache.getHolders().isEmpty());
final ServiceReference<?> ref = createServiceReference(new String[] { "/path1" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
assertEquals(1, cache.getHolders().size());
assertPaths(cache, new String[] { "/path1" }, new ServiceReference<?>[] { ref });
assertEquals(ref, cache.getHolders().get(0).serviceReference);
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));
assertTrue(cache.getHolders().isEmpty());
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class ClassLoadingTest method testLoading_SLING_6258.
@Test
public void testLoading_SLING_6258() throws Exception {
final BundleContext bundleContext = this.context.mock(BundleContext.class);
final PackageAdmin packageAdmin = this.context.mock(PackageAdmin.class);
final ExportedPackage ep1 = this.context.mock(ExportedPackage.class, "ep1");
final ExportedPackage ep2 = this.context.mock(ExportedPackage.class, "ep2");
final ExportedPackage ep3 = this.context.mock(ExportedPackage.class, "ep3");
final Bundle bundle1 = this.context.mock(Bundle.class, "bundle1");
final Bundle bundle2 = this.context.mock(Bundle.class, "bundle2");
final Bundle bundle3 = this.context.mock(Bundle.class, "bundle3");
final ClassNotFoundException cnfe = new ClassNotFoundException();
this.context.checking(new Expectations() {
{
allowing(bundleContext).createFilter(with(any(String.class)));
will(returnValue(null));
allowing(bundleContext).getServiceReferences(with(any(String.class)), with((String) null));
will(returnValue(null));
allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
allowing(bundleContext).removeServiceListener(with(any(ServiceListener.class)));
allowing(packageAdmin).getExportedPackages(with("org.apache.sling.test"));
will(returnValue(new ExportedPackage[] { ep1, ep2 }));
allowing(packageAdmin).getExportedPackages(with("org.apache.sling.test3"));
will(returnValue(new ExportedPackage[] { ep3 }));
allowing(ep1).getExportingBundle();
will(returnValue(bundle1));
allowing(ep1).isRemovalPending();
will(returnValue(false));
allowing(ep2).getExportingBundle();
will(returnValue(bundle2));
allowing(ep2).isRemovalPending();
will(returnValue(false));
allowing(ep3).getExportingBundle();
will(returnValue(bundle3));
allowing(ep3).isRemovalPending();
will(returnValue(false));
allowing(bundle1).getBundleId();
will(returnValue(1L));
allowing(bundle1).getState();
will(returnValue(Bundle.ACTIVE));
allowing(bundle1).getVersion();
will(returnValue(new Version("1.0.0")));
allowing(bundle1).getSymbolicName();
will(returnValue("org.apache.sling.test1"));
allowing(bundle2).getBundleId();
will(returnValue(2L));
allowing(bundle2).getState();
will(returnValue(Bundle.ACTIVE));
allowing(bundle2).getVersion();
will(returnValue(new Version("2.0.0")));
allowing(bundle2).getSymbolicName();
will(returnValue("org.apache.sling.test2"));
allowing(bundle3).getBundleId();
will(returnValue(3L));
allowing(bundle3).getState();
will(returnValue(Bundle.ACTIVE));
allowing(bundle3).getVersion();
will(returnValue(new Version("1.2.3")));
allowing(bundle3).getSymbolicName();
will(returnValue("org.apache.sling.test3"));
allowing(bundle1).loadClass("org.apache.sling.test.T1");
will(throwException(cnfe));
allowing(bundle1).getResource("org/apache/sling/test/T3.class");
will(returnValue(new URL("jar:file:/ws/org.apache.sling.test1.jar!/org/apache/sling/test/T3.class")));
allowing(bundle2).loadClass("org.apache.sling.test.T1");
will(returnValue(ArrayList.class));
allowing(bundle2).loadClass("org.apache.sling.test.T2");
will(throwException(new ClassNotFoundException()));
allowing(bundle2).getResource("org/apache/sling/test/T3.class");
will(returnValue(null));
allowing(bundle2).getResources("org/apache/sling/test/T3.class");
will(returnValue(null));
allowing(bundle3).getResource("org/apache/sling/test3/T4.class");
will(returnValue(new URL("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class")));
allowing(bundle3).getResources("org/apache/sling/test3/T4.class");
will(returnValue(Collections.enumeration(new ArrayList<URL>() {
{
add(new URL("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class"));
}
})));
}
});
DynamicClassLoaderManagerImpl manager = new DynamicClassLoaderManagerImpl(bundleContext, packageAdmin, null, new DynamicClassLoaderManagerFactory(bundleContext, packageAdmin));
final ClassLoader cl = manager.getDynamicClassLoader();
Assert.assertEquals(ArrayList.class, cl.loadClass("org.apache.sling.test.T1"));
try {
cl.loadClass("org.apache.sling.test.T2");
} catch (Exception e) {
Assert.assertEquals(ClassNotFoundException.class, e.getClass());
}
Assert.assertNull(cl.getResource("org/apache/sling/test/T3.class"));
Assert.assertFalse(cl.getResources("org/apache/sling/test/T3.class").hasMoreElements());
Assert.assertEquals("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class", cl.getResource("org/apache/sling/test3/T4.class").toString());
Enumeration<URL> resourceURLs = cl.getResources("org/apache/sling/test3/T4.class");
int count = 0;
URL lastURL = new URL("https://sling.apache.org");
while (resourceURLs.hasMoreElements()) {
count++;
lastURL = resourceURLs.nextElement();
}
Assert.assertEquals(1, count);
Assert.assertEquals("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class", lastURL.toString());
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class AdapterManagerTest method createComponentContext.
/**
* Helper method to create a mock component context
*/
protected ComponentContext createComponentContext() throws Exception {
final BundleContext bundleCtx = this.context.mock(BundleContext.class);
final Filter filter = this.context.mock(Filter.class);
final ComponentContext ctx = this.context.mock(ComponentContext.class);
this.context.checking(new Expectations() {
{
allowing(ctx).locateService(with(any(String.class)), with(any(ServiceReference.class)));
will(returnValue(new MockAdapterFactory()));
allowing(ctx).getBundleContext();
will(returnValue(bundleCtx));
allowing(bundleCtx).createFilter(with(any(String.class)));
will(returnValue(filter));
allowing(bundleCtx).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
allowing(bundleCtx).getServiceReferences(with(any(String.class)), with(any(String.class)));
will(returnValue(null));
allowing(bundleCtx).removeServiceListener(with(any(ServiceListener.class)));
allowing(bundleCtx).registerService(with(Adaption.class), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
will(returnValue(null));
}
});
return ctx;
}
Aggregations