users@glassfish.java.net

Question about changing PUs used by EJB with embedded Glassfish for unit testing

From: NBW <emailnbw_at_gmail.com>
Date: Mon, 21 Mar 2011 14:51:04 -0400

I'm using embedded glassfish to test out some EJBs which include JPA code
and inject their entity manager's like so:

@Stateless
public class MyBean {

@PersistenceContext(unitname = "FOO")
private EntityManager em;

...
}


My persistence.xml defines PU FOO and it defines PU FOO-TEST. FOO-TEST uses
derby embedded and I want to select that PU when the EJB is run in a test.
My tests are running in embedded GF like so:

Map<String, Object> props = new HashMap<String, Object>();
props.put(EJBContainer.APP_NAME, myApp);
props.put(EJBContainer.MODULES, new File("out/production/MYAPP"));
props.put("org.glassfish.ejb.embedded.glassfish.installation.root",
"C:/glassfish");

EJBContainer ejbC = EJBContainer.createEJBContainer(props);
Context ctx = ejbC.getContext();
MyBean myBean = (MyBean) ctx.lookup("java:global/myApp/MYAPP/MyBean");
Assert.assertNotNull(myBean);
ejbC.close();

So the unit name in the bean is set. Is there a way to override this for my
tests so that they can use the FOO-TEST pu? I thought about using ANT to
alter the persistence.xml file to set the unit name's prior to packaging
based on the context (production or test) but I don't like that solution.
I'm hoping there's a cleaner way of doing it.

Thanks,

-Noah