Skip to main content

Posts

EJB3 unit testing - part two - replacing the initialcontext

Some of your code might use EJB session beans through JNDI lookup. To make this code find your session beans in your unit tests, you can replace the initial context quite easily. This is an example of how it can be done. public class ReplaceInitialContext extends InitialContext implements InitialContextFactory { static ReplaceInitialContext instance = null; public ReplaceInitialContext() throws NamingException { super(false); } public static ReplaceInitialContext getInstance() throws NamingException { if(instance == null) { System.setProperty("java.naming.factory.initial",ReplaceInitialContext.class.getName()); instance = new ReplaceInitialContext(); } return instance; } @Override protected void init(Hashtable environment) throws NamingException { } @Override public Object lookup(String name) throws NamingException { return "Hello"; } ...

EJB3 unit testing - part one - dependency injection

You might want to use an embeddable container for this - but why really - cause it's really simple. EJB3 session beans are POJO's and should be tested like POJO's. However there are some issues in order to get EJB3 unit tests working. One of them are dependency injection. How does dependency injection work? Take a look at this method using reflection: /** * @param targetBean - the bean to inject into * @param annotationClass - the annotation class representing the injection * @param objectToInject - the object to inject into targetBean */ private void inject(Object targetBean, Class annotationClass, Object objectToInject) { // Scan all (private and public fields) of the bean class for(Field fld : targetBean.getClass().getDeclaredFields()) { // See if the specified annotation is present for the field if(fld.isAnnotationPresent(annotationClass)) { // See if the field type is appropriate according to the object that is to be injected if(fld....

Accessing ejb3 session beans from remote java application

LMAppletserver is a great tool for communicating from java-applet to server, but it is also a great tool for remote calls over http to ejb3 session beans in general. Here's an example of using stateful session beans with lmappletserver: https://lmappletserver.svn.sourceforge.net/svnroot/lmappletserver/lmappletserver_core/src_tests/com/lightminds/appletserver/core/tests/SFSBTest.java This is a unit test class, which also contains result verification, but is also a good example of how to use stateful session beans. You can check out the entire project in netbeans from: https://lmappletserver.svn.sourceforge.net/svnroot/lmappletserver/lmappletserver_core In NetBeans 6.5, just right click on the project, and select "Test" to run the tests (including the Stateful session bean test). The test uses Apache OpenEJB and Jetty as http servlet container (running the rpc servlet).

Seam s:convertEntity + glassfish + toplink

.... doesn't go very well together, since Seam wants to access your entityManager, and would want it to be a Hibernate instance... This is a simple workaround, by replacing s:convertEntity with a custom tag that gives the same result. By remembering the entities displayed to the s:selectItems, using the value of entity.toString() (which is className + hashCode() (which should be unique).). You might want to tune it maybe not to use hashcode (in case of non uniqueness), and maybe access your entitymanager (toplink) - instead of storing entities in memory.. But in any case this is a good starting point: package com.petersalomonsen.jsf.persistence.EntityConverter import java.util.HashMap; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; public class EntityConverter implements Converter { static HashMap<String,Object> entitiesToRemember = new HashMap<String,Object>(); public Object getAsObject(Fa...

entityManager.createQuery("Select e from "+MyEntity.class.getName()+" e") in glassfish

I've been using EJB3 with jboss for a long time - and I've got use to this way of writing my ejb3 persistence queries: entityManager.createQuery("Select e from "+MyEntity.class.getName()+" e") which is also convenient considering refactoring compared to "Select e from MyEntity e". But the EJB3 persistence spec says this: "Entities are designated in query strings by their entity names. The entity name is defined by the name element of the Entity annotation (or the entity-name XML descriptor element), and defaults to the unqualified name of the entity class. Entity names are scoped within the persistence unit and must be unique within the persistence unit." And glassfish complains about the fully qualified classnames - it just want the unqualified classname (without package information). According to the spec - glassfish seems more correct to me - but the hibernate/jboss way of using fqn seems better to me - so I wonder, which one is really...

LMAppletserver in Glassfish - invoking EJB3 session bean methods from your applet

The previous blog entry showed how to use LMAppletserver in a web archive (WAR) without any enterprise beans or enterprise app. It could also have been deployed to e.g. Tomcat where you don't have EJB3 session beans. In this example I'm showing how to call EJB3 session beans from your java applet using LMAppletserver. Download and start netbeans 6.0 (http://netbeans.org) Versioning -> Subversion -> Checkout Checkout the following URL: http://lmappletserver.svn.sourceforge.net/svnroot/lmappletserver/LMAppletServerEJB3 (no username or password) Right click on the newly opened LMAppletServerEJB3 project and select "Undeploy and Deploy" to deploy the project. When the server has started and everything is deployed then open this URL in your browser: http://localhost:8080/LMAppletServerEJB3-war/ The applet shows a message that is obtained by calling an EJB3 session bean method method on the server, using the RPC features of LMAppletserver. This is what happens in the ...

LMAppletserver in Glassfish

LMAppletserver is a toolkit for automatic deployment of java classes (or jars) to your browser, and an rpc library for easily calling methods on the server. The intention is to enable effective code reuse between the client and the server, and not have to repackage the classes for the client. Also a way of calling server methods - that is to give the feeling of coding "directly on the server". Download the example project from sourceforge.net: http://sourceforge.net/project/downloading.php?group_id=142974&use_mirror=osdn&filename=SimplestLMAppletserverProject.zip&2731151 unzip SimplestLMAppletserverProject.zip cd SimplestLMAppletserverProject ant To deploy the application to Glassfish: asadmin deploydir simplelmapp.war Open in your browser: http://localhost:8080/simplelmapp/ And see the hello world applet... The applet shows a message that is obtained by calling a business method on the server, using the RPC features of LMAppletserver. This is what happens in the ...