Author: dblevins
Date: Thu Jun 9 03:19:32 2011
New Revision: 1133643
URL: http://svn.apache.org/viewvc?rev=1133643&view=rev
Log:
Tiny little Application Client test
Added:
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/server/ejbd/AppClientTest.java
Added:
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/server/ejbd/AppClientTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/server/ejbd/AppClientTest.java?rev=1133643&view=auto
==============================================================================
---
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/server/ejbd/AppClientTest.java
(added)
+++
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/server/ejbd/AppClientTest.java
Thu Jun 9 03:19:32 2011
@@ -0,0 +1,138 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.server.ejbd;
+
+import junit.framework.TestCase;
+import org.apache.openejb.OpenEJB;
+import org.apache.openejb.assembler.classic.Assembler;
+import org.apache.openejb.config.AppModule;
+import org.apache.openejb.config.ClientModule;
+import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.config.EjbModule;
+import org.apache.openejb.core.ServerFederation;
+import org.apache.openejb.jee.ApplicationClient;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.StatelessBean;
+import org.apache.openejb.jee.oejb3.EjbDeployment;
+import org.apache.openejb.jee.oejb3.OpenejbJar;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.server.ServiceDaemon;
+import org.apache.openejb.server.ServicePool;
+
+import javax.ejb.CreateException;
+import javax.ejb.EJB;
+import javax.ejb.EJBHome;
+import javax.ejb.EJBObject;
+import javax.ejb.Remote;
+import javax.ejb.RemoteHome;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.rmi.RemoteException;
+import java.util.Properties;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class AppClientTest extends TestCase {
+
+
+ public void test() throws Exception {
+
+ final EjbServer ejbServer = new EjbServer();
+
+ final Properties initProps = new Properties();
+ initProps.setProperty("openejb.deployments.classpath.include", "");
+
initProps.setProperty("openejb.deployments.classpath.filter.descriptors",
"true");
+ OpenEJB.init(initProps, new ServerFederation());
+ ejbServer.init(new Properties());
+
+ final ServicePool pool = new ServicePool(ejbServer, "ejbd", 10);
+ final ServiceDaemon serviceDaemon = new ServiceDaemon(pool, 0,
"localhost");
+ serviceDaemon.start();
+
+ int port = serviceDaemon.getPort();
+
+ final Assembler assembler =
SystemInstance.get().getComponent(Assembler.class);
+ final ConfigurationFactory config = new ConfigurationFactory();
+
+ final EjbModule ejbModule = new EjbModule(new EjbJar(), new
OpenejbJar());
+ final EjbJar ejbJar = ejbModule.getEjbJar();
+ ejbJar.addEnterpriseBean(new StatelessBean(Orange.class));
+
+ final ClassLoader loader = this.getClass().getClassLoader();
+
+ final ClientModule clientModule = new ClientModule(new
ApplicationClient(), loader, "orange-client", OrangeAppClient.class.getName(),
"orange-client");
+
+ final AppModule appModule = new AppModule(loader, "test");
+
+ appModule.getClientModules().add(clientModule);
+ appModule.getEjbModules().add(ejbModule);
+
+ assembler.createApplication(config.configureApplication(appModule));
+
+ final Properties props = new Properties();
+ props.put("java.naming.factory.initial",
"org.apache.openejb.client.RemoteInitialContextFactory");
+ props.put("java.naming.provider.url", "ejbd://127.0.0.1:" + port);
+ props.put("openejb.client.moduleId", "orange-client");
+
+ final Context context = new InitialContext(props);
+
+ final Object home = context.lookup("comp/env/home");
+ assertTrue(home instanceof OrangeHome);
+
+ OrangeHome orangeHome = (OrangeHome) home;
+ final OrangeRemote orangeRemote = orangeHome.create();
+ assertEquals("bat", orangeRemote.echo("tab"));
+
+ final Object business = context.lookup("comp/env/business");
+ assertTrue(business instanceof OrangeBusinessRemote);
+ OrangeBusinessRemote orangeBusinessRemote = (OrangeBusinessRemote)
business;
+ assertEquals("nap", orangeBusinessRemote.echo("pan"));
+ }
+
+ public static interface OrangeHome extends EJBHome {
+ OrangeRemote create() throws RemoteException, CreateException;
+ }
+
+ public static interface OrangeRemote extends EJBObject {
+ public String echo(String string) throws RemoteException;
+ }
+
+ public static interface OrangeBusinessRemote {
+ public String echo(String string);
+
+ }
+
+ @RemoteHome(OrangeHome.class)
+ @Remote(OrangeBusinessRemote.class)
+ public static class Orange implements OrangeBusinessRemote {
+ @Override
+ public String echo(String string) {
+ return new StringBuilder(string).reverse().toString();
+ }
+ }
+
+ public static class OrangeAppClient {
+
+ @EJB(name = "home")
+ public static OrangeHome orangeHome;
+
+ @EJB(name = "business")
+ public static OrangeBusinessRemote orangeBusinessRemote;
+
+ }
+}