<% out.print(""); %> <%@ page import=" javax.naming.Context, javax.naming.InitialContext, javax.servlet.http.HttpServletRequest, javax.servlet.jsp.JspWriter, java.io.ByteArrayOutputStream, java.io.IOException, java.io.PrintStream, java.lang.reflect.Method, java.util.Properties "%> <%@ page import="java.io.PrintWriter" %> TomEE

Testing an Enterprise JavaBean

<% try{ synchronized (this) { main(request, session, out); } } catch (Exception e){ out.println("

FAIL

"); return; } %>

<%! static String invLock = "lock"; static int invCount; String OK = "OK"; String FAIL = "FAIL"; private Object getEjbObj(InitialContext ctx, ClassLoader myLoader) { try { Class[] params = new Class[0]; Class homeInterface = Class.forName("javax.management.j2ee.ManagementHome", true, myLoader); Method create = homeInterface.getDeclaredMethod("create", params); Object ejbHome = ctx.lookup("MEJB"); Object ejbObject = create.invoke(ejbHome); return ejbObject; } catch (Exception e) { return null; } } /** * The main method of this JSP */ public void main(final HttpServletRequest request, final HttpSession session, final JspWriter out) throws Exception { final ClassLoader myLoader = this.getClass().getClassLoader(); final Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory"); p.put("openejb.loader", "embed"); final InitialContext ctx = new InitialContext(p); // --------------------------------------------------- // Can I lookup a home interface from the testsuite? // --------------------------------------------------- printTest(out, "Looking up an ejb home", new TestAction() { @Override public String run() throws Exception { Object ejbHome = ctx.lookup("MEJB"); if (ejbHome instanceof java.rmi.Remote) { return OK; } else { return FAIL; } } }); // --------------------------------------------------- // Is the home interface visible? // --------------------------------------------------- printTest(out, "Checking for the home interface class definition", new TestAction() { @Override public String run() throws Exception { Class.forName("javax.management.j2ee.ManagementHome", true, myLoader); return OK; } }); // --------------------------------------------------- // Can I invoke a create method on the ejb home? // --------------------------------------------------- printTest(out, "Invoking the create method on the ejb home", new TestAction() { @Override public String run() throws Exception { Object ejbObject = getEjbObj(ctx, myLoader); if (java.rmi.Remote.class.isInstance(ejbObject)) { return OK; } else { return FAIL; } } }); // --------------------------------------------------- // Is the remote interface visible? // --------------------------------------------------- printTest(out, "Checking for the remote interface class definition", new TestAction() { @Override public String run() throws Exception { Class.forName("javax.management.j2ee.Management", true, myLoader); return OK; } }); // --------------------------------------------------- // Can I invoke a business method on the ejb object? // --------------------------------------------------- printTest(out, "Invoking a business method on the ejb object", new TestAction() { @Override public String run() throws Exception { Class remoteInterface = Class.forName("javax.management.j2ee.Management", true, myLoader); Method businessMethod = remoteInterface.getDeclaredMethod("getMBeanCount"); Object ejbObject = getEjbObj(ctx, myLoader); Object returnValue = null; if(ejbObject != null) { returnValue = businessMethod.invoke(ejbObject); } if (java.lang.Integer.class.isInstance(returnValue)) { return OK; } else { return FAIL; } } }); } private interface TestAction { String run() throws Exception; } protected void printTest(JspWriter out, String test, TestAction testAction) throws IOException { out.print(""); out.print(test); out.print(""); try { out.print(testAction.run()); } catch (Exception e) { out.print(FAIL + "
" + formatThrowable(e)); } out.print(""); } public String formatThrowable(Throwable err) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); err.printStackTrace(new PrintStream(baos)); byte[] bytes = baos.toByteArray(); StringBuffer sb = new StringBuffer(bytes.length); for (int i = 0; i < bytes.length; i++) { char c = (char) bytes[i]; switch (c) { case' ': sb.append(" "); break; case'\n': sb.append("
"); break; case'\r': break; default: sb.append(c); } } return sb.toString(); } %>