Search in sources :

Example 76 with InitialContext

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

the class ServiceRegistryContextTest method testLookupWithPause.

@Test
public void testLookupWithPause() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(JNDIConstants.REBIND_TIMEOUT, 1000);
    InitialContext ctx = new InitialContext(env);
    Context ctx2 = (Context) ctx.lookup("osgi:service");
    Runnable r1 = (Runnable) ctx2.lookup("java.lang.Runnable");
    reg.unregister();
    long startTime = System.currentTimeMillis();
    try {
        r1.run();
        fail("Should have received an exception");
    } catch (ServiceException e) {
        long endTime = System.currentTimeMillis();
        long diff = endTime - startTime;
        assertTrue("The run method did not fail in the expected time (1s): " + diff, diff >= 1000);
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) ServiceException(org.osgi.framework.ServiceException) Hashtable(java.util.Hashtable) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 77 with InitialContext

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

the class ServiceRegistryContextTest method jndiLookupServiceNameTest.

@Test
public void jndiLookupServiceNameTest() throws NamingException, SQLException {
    InitialContext ctx = new InitialContext(new Hashtable<Object, Object>());
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    DataSource first = Skeleton.newMock(DataSource.class);
    DataSource second = Skeleton.newMock(DataSource.class);
    Hashtable<String, String> properties = new Hashtable<String, String>();
    properties.put("osgi.jndi.service.name", "jdbc/myDataSource");
    bc.registerService(DataSource.class.getName(), first, properties);
    properties = new Hashtable<String, String>();
    properties.put("osgi.jndi.service.name", "jdbc/myDataSource2");
    bc.registerService(DataSource.class.getName(), second, properties);
    DataSource s = (DataSource) ctx.lookup("osgi:service/jdbc/myDataSource");
    assertNotNull(s);
    s = (DataSource) ctx.lookup("osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/myDataSource2)");
    assertNotNull(s);
    // don't care about the method, just need to call something.
    s.isWrapperFor(DataSource.class);
    Skeleton.getSkeleton(second).assertCalled(new MethodCall(DataSource.class, "isWrapperFor", Class.class));
}
Also used : Hashtable(java.util.Hashtable) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 78 with InitialContext

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

the class ServiceRegistryContextTest method listRepositoryContents.

/**
   * This test checks that we can list the contents of the repository using the
   * list method
   * 
   * @throws NamingException
   */
public void listRepositoryContents() throws NamingException {
    InitialContext ctx = new InitialContext();
    NamingEnumeration<NameClassPair> serviceList = ctx.list("osgi:service/java.lang.Runnable/(rubbish=smelly)");
    checkThreadRetrievedViaListMethod(serviceList);
    assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
    //Now add a second service
    registerService(new Thread());
    serviceList = ctx.list("osgi:service/java.lang.Runnable/(rubbish=smelly)");
    checkThreadRetrievedViaListMethod(serviceList);
    checkThreadRetrievedViaListMethod(serviceList);
    assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
}
Also used : NameClassPair(javax.naming.NameClassPair) InitialContext(javax.naming.InitialContext)

Example 79 with InitialContext

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

the class ServiceRegistryContextTest method checkServiceListListBindings.

@Test
public void checkServiceListListBindings() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    String className = Runnable.class.getName();
    MethodCall run = new MethodCall(Runnable.class, "run");
    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);
    ServiceRegistration reg2 = bc.registerService(className, t2, null);
    NamingEnumeration<Binding> ne = ctx.listBindings("osgi:servicelist/" + className);
    assertTrue(ne.hasMoreElements());
    Binding bnd = ne.nextElement();
    assertEquals(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID)), bnd.getName());
    assertTrue("Class name not correct. Was: " + bnd.getClassName(), bnd.getClassName().contains("Proxy") || bnd.getClassName().contains("EnhancerByCGLIB"));
    Runnable r = (Runnable) bnd.getObject();
    assertNotNull(r);
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(run, 1);
    Skeleton.getSkeleton(t2).assertNotCalled(run);
    assertTrue(ne.hasMoreElements());
    bnd = ne.nextElement();
    assertEquals(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID)), bnd.getName());
    assertTrue("Class name not correct. Was: " + bnd.getClassName(), bnd.getClassName().contains("Proxy") || bnd.getClassName().contains("EnhancerByCGLIB"));
    r = (Runnable) bnd.getObject();
    assertNotNull(r);
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(run, 1);
    Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(run, 1);
    assertFalse(ne.hasMoreElements());
}
Also used : Binding(javax.naming.Binding) 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 80 with InitialContext

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

the class ServiceRegistryContextTest 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 {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    ctx.lookup("osgi:service/java.lang.Integer");
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) 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