Search in sources :

Example 96 with BundleContext

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);
}
Also used : Expectations(org.jmock.Expectations) StringStartsWith(org.hamcrest.text.StringStartsWith) File(java.io.File) Mockery(org.jmock.Mockery) BundleContext(org.osgi.framework.BundleContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 97 with BundleContext

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());
}
Also used : ServiceEvent(org.osgi.framework.ServiceEvent) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 98 with BundleContext

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());
}
Also used : ServiceEvent(org.osgi.framework.ServiceEvent) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 99 with BundleContext

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());
}
Also used : Expectations(org.jmock.Expectations) ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URL(java.net.URL) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Version(org.osgi.framework.Version) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 100 with BundleContext

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;
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) ServiceListener(org.osgi.framework.ServiceListener) ComponentContext(org.osgi.service.component.ComponentContext) Filter(org.osgi.framework.Filter) MockAdapterFactory(org.apache.sling.adapter.mock.MockAdapterFactory) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

BundleContext (org.osgi.framework.BundleContext)433 Test (org.junit.Test)188 ServiceReference (org.osgi.framework.ServiceReference)124 Bundle (org.osgi.framework.Bundle)119 Hashtable (java.util.Hashtable)72 ArrayList (java.util.ArrayList)39 ServiceRegistration (org.osgi.framework.ServiceRegistration)38 Dictionary (java.util.Dictionary)29 HashMap (java.util.HashMap)28 Matchers.anyString (org.mockito.Matchers.anyString)28 File (java.io.File)23 Before (org.junit.Before)23 URL (java.net.URL)21 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)20 IOException (java.io.IOException)19 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)15 Map (java.util.Map)15 Configuration (org.osgi.service.cm.Configuration)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13