users@glassfish.java.net

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

From: Mitesh Meswani <mitesh.meswani_at_oracle.com>
Date: Mon, 21 Mar 2011 12:02:42 -0700

I would suggest you to map the data source as needed in your environment
instead of maintaining two different PUs. Lets say you are referring to
"jdbc/mydb" from pu "FOO" in persistence.xml. Configure your embedded GF
instance to point
"jdbc/mydb" to embedded derby and configure your normal GlassFish to
point it to the non embedded db.

On 3/21/2011 11:51 AM, NBW wrote:
> 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