users@glassfish.java.net

JUnit tests with Glassfish EJB's

From: <glassfish_at_javadesktop.org>
Date: Mon, 22 Mar 2010 17:22:56 PDT

Hello,

Something strange is happening in my glassfish unit tests in netbeans. It seams that the embed container persistence doesn't really persist.

I have a test like this:

[code]
public class PlayerDaoTest {

    static EJBContainer ejbCon;

    private PlayerDao dao;

    @Before
    public void initEjb() throws Exception {
        ejbCon = EJBContainer.createEJBContainer();
        Context ctx = ejbCon.getContext();
        dao = (PlayerDao) ctx.lookup("java:global/classes/PlayerDao");
    }

    @Test
    public void login() {
        Player pr = dao.registerPlayer("someone_at_some.pl", "lolo");
        Player pl = dao.login("someone_at_some.pl", "lolo");
    }
}
[/code]

This works well in my web client but it seams that in the tests the player added in the register method is forgotten when the execution proceeds to the login method. I get a NoResultException there which is expected when a Player with the corresponding email and password is not found.

Here's the rest of my code:

[code]
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class PlayerDao {
    
    @PersistenceContext(unitName="prodPU")
    private EntityManager em;

    /**
     *
     * @param email
     * @param password
     * @return registered player
     * @throws EntityExistsException - when a user with the passed email
     * already exists
     */
    public Player registerPlayer(String email, String password)
        throws EntityExistsException {
        
        Query q = em.createNamedQuery("findPlayerByEmail");
        q.setParameter("email", email);
        List r = q.getResultList();
        if(r.size() != 0) {
            throw new EntityExistsException(conf.getGlobalResourceBundle()
                .getString("register.mail.exists.error"));
        }

        Player p = new Player();
        p.setEmail(email);
        p.setPasswordHash(calcuatePasswordHash(password));
        p.setRegisterDate(Calendar.getInstance().getTime());
        p.setGoldCoins(Integer.valueOf(
            conf.getGlobalProperty("register.gold")));
        em.persist(p);

        return p;
    }

   
    /**
     *
     * @param email
     * @param password
     * @return logged in player
     * @throws NoResultException - when email and password don't match
     */
    public Player login(String email, String password)
        throws NoResultException {

        Query q = em.createNamedQuery("findPlayerByEmailAndPasswordHash");
        q.setParameter("email", email);
        q.setParameter("passwordHash", calcuatePasswordHash(password));
        Player p = (Player)q.getSingleResult();
        p.setLastSeenDate(Calendar.getInstance().getTime());
        p.setOnline(true);
        em.merge(p);
        return p;
    }
}
[/code]

[code]
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="prodPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/__default</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>
[/code]



Please give me a clue what could I be doing wrong ?
Or does this look like a bug ?
[Message sent by forum member 'walec51']

http://forums.java.net/jive/thread.jspa?messageID=393208