Search in sources :

Example 81 with InitialContext

use of javax.naming.InitialContext in project aries by apache.

the class ServiceRegistryContextTest method checkProxyDynamism.

@Test
public void checkProxyDynamism() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    String className = Runnable.class.getName();
    Runnable t = Skeleton.newMock(Runnable.class);
    Runnable t2 = Skeleton.newMock(Runnable.class);
    // we don't want the default service
    reg.unregister();
    ServiceRegistration reg = bc.registerService(className, t, null);
    bc.registerService(className, t2, null);
    Runnable r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(t2).assertNotCalled(new MethodCall(Runnable.class, "run"));
    reg.unregister();
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 82 with InitialContext

use of javax.naming.InitialContext in project aries by apache.

the class ServiceRegistryContextTest method jndiLookupWithFilter.

/**
   * This test checks that we can pass a filter in without things blowing up.
   * Right now our mock service registry does not implement filtering, so the
   * effect is the same as simpleJNDILookup, but we at least know it is not
   * blowing up.
   * 
   * @throws NamingException
   */
@Test
public void jndiLookupWithFilter() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    Object s = ctx.lookup("osgi:service/java.lang.Runnable/(rubbish=smelly)");
    assertNotNull("We didn't get a service back from our lookup :(", s);
    service.run();
    Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(mock.getBundleContext()).assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", "(rubbish=smelly)"));
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 83 with InitialContext

use of javax.naming.InitialContext in project aries by apache.

the class BlueprintURLContextTest method testList.

/**
   * Validate that list() function works for BlueprintURLContext. 
   * This returns an enumeration of component id -> component class name pairs
   */
@Test
public void testList() throws Exception {
    InitialContext ctx = new InitialContext();
    NamingEnumeration<NameClassPair> compList = ctx.list("blueprint:comp");
    Set<String> expectedCompIds = new BlueprintContainerStub().getComponentIds();
    while (compList.hasMore()) {
        NameClassPair ncp = compList.next();
        String compId = ncp.getName();
        String compClass = ncp.getClassName();
        if (compId.equals("comp1")) {
            assertEquals("comp1 class wrong in list", SimpleComponent.class.getName(), compClass);
        } else if (compId.equals("comp2")) {
            assertEquals("comp2 class wrong in list", AnotherComponent.class.getName(), compClass);
        }
        expectedCompIds.remove(compId);
    }
    assertEquals("Not all expected components were found", expectedCompIds.size(), 0);
}
Also used : NameClassPair(javax.naming.NameClassPair) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 84 with InitialContext

use of javax.naming.InitialContext in project aries by apache.

the class BlueprintURLContextTest method testListBindings.

/**
   * Test BlueprintURLContext.listBindings() 
   * This returns an enumeration of component id -> component pairs
   */
@Test
public void testListBindings() throws Exception {
    InitialContext ctx = new InitialContext();
    NamingEnumeration<Binding> bindings = ctx.listBindings("blueprint:comp");
    Set<String> expectedCompIds = new BlueprintContainerStub().getComponentIds();
    while (bindings.hasMore()) {
        Binding b = bindings.next();
        String compId = b.getName();
        Object component = b.getObject();
        if (compId.equals("comp1")) {
            SimpleComponent sc = (SimpleComponent) component;
            assertEquals("comp1 message wrong", "comp1_message", sc.getIdMessage());
        } else if (compId.equals("comp2")) {
            AnotherComponent ac = (AnotherComponent) component;
            assertEquals("comp2 message wrong", "AnotherComponent with id comp2", ac.getIdMessage());
        }
        expectedCompIds.remove(compId);
    }
    assertEquals("Not all expected components were found", expectedCompIds.size(), 0);
}
Also used : Binding(javax.naming.Binding) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 85 with InitialContext

use of javax.naming.InitialContext in project aries by apache.

the class BlueprintURLContextTest method testLookupForServiceWeNeverHad.

/**
   * Check that we get a NameNotFoundException if we lookup something not in the
   * registry.
   * 
   * @throws NamingException
   */
@Test(expected = NameNotFoundException.class)
public void testLookupForServiceWeNeverHad() throws NamingException {
    InitialContext ctx = new InitialContext();
    ctx.lookup("blueprint:comp/this.is.not.a.component");
}
Also used : InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Aggregations

InitialContext (javax.naming.InitialContext)1068 Test (org.junit.Test)325 EJBException (javax.ejb.EJBException)228 Properties (java.util.Properties)213 Context (javax.naming.Context)194 RemoteException (java.rmi.RemoteException)173 TestFailureException (org.apache.openejb.test.TestFailureException)172 NamingException (javax.naming.NamingException)168 AssertionFailedError (junit.framework.AssertionFailedError)168 JMSException (javax.jms.JMSException)167 RemoveException (javax.ejb.RemoveException)66 CreateException (javax.ejb.CreateException)57 DataSource (javax.sql.DataSource)55 Hashtable (java.util.Hashtable)54 Assembler (org.apache.openejb.assembler.classic.Assembler)47 EjbJar (org.apache.openejb.jee.EjbJar)41 NameNotFoundException (javax.naming.NameNotFoundException)40 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)38 Connection (java.sql.Connection)37 StatelessBean (org.apache.openejb.jee.StatelessBean)30