Sunteți pe pagina 1din 6

c c


    
 Create a new JavaEE EJB Module Project
À × your Entity an Entity Manager or D× java files in the Jource Packages irectory.
x ×fter Step 2. You shoul have a ersistence.xml file in the Configuration Files irectory.
 Set up your ersistence.xml file like the example below, so you o not have to rely on
G  an  stuff,

    

<?xml version="1.0" enco ing="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.xs ">

<persistence-unit name="FooEJBPU" transaction-type="RESURCE_LC×L">

<jta- ata-source>j bc/alertmail</jta- ata-source>

<class>FooMail.Users</class>

<exclu e-unliste -classes>true</exclu e-unliste -classes>

<properties>
<property name="javax.persistence.j bc. river" value="com.mysql.j bc.Driver"/>
<property name="javax.persistence.j bc.url"
value="j bc:mysql://localhost:3306/alertmail"/>
<property name="javax.persistence.j bc.user" value="root"/>
<property name="javax.persistence.j bc.passwor " value="root"/>
</properties>
</persistence-unit>

</persistence>

×bove the FooMail is the irectory in the Jource Packages irectory an


  is the
Entity file
Generate by NetBeans from the first     atabase we were originally given. I
chose to test the
  Entity through the
  c    
ü Next you will nee to a the c !"# library to your ‰ibraries irectory so the
c !"# $  will be available uring the test.

% *now you will nee to create a Test file in the aest Packages irectory.

A The example below is the Test file ë  I create to test persistence through EJB.
When I create the Test file I also create the irectory Fooaest un er the aest
Packages irectory where it woul resi e,

ë  &$

packageFooTest;

importFooMail.UsersEntityManagerBean;
importFooMail.Users;
importjavax.persistence.Persistence;
importjavax.persistence.EntityManager;
importjavax.persistence.EntityManagerFactory;
importjunit.framework.TestCase;
import static org.junit.×ssert.*;

public class FooTest exten s TestCase {

//IMRT×NT NTE: To Test The × min You Must First × Her In The DB
//× min To Check By Persistence If In Data Base ×lertMail
private static final String ×DMIN_EM×IL = "1@uco.e u";
private static final String ×DMIN_P×SSWRD = "root";
private static final short ×DMIN_SYSTEM_RLE = 1;

//New User To Persist To Data Base ×lertMail


private static final String USER_EM×IL = "2@uco.e u";
private static final String USER_P×SSWRD = "user";
private static final short USER_SYSTEM_RLE = 0;
privateEntityManagerFactoryemf;
privateEntityManagerem;

public voi setUp() {

System.out.println("Creating Entity Manager Factory");


emf = Persistence.createEntityManagerFactory("FooEJBPU");
if(emf.equals(null))
System.out.println("NULL EMF");
em = emf.createEntityManager();
System.out.println("Creating Entity Manager");
createTestData();
}
public voi tearDown() {
System.out.println("Tear Down");
if (em != null) {
removeTestData();
em.close();
System.out.println("Closing Entity Manager");
}
if (emf != null) {
emf.close();
}
}

private voi createTestData() {


System.out.println("Creating new user");
Users user = new Users();
user.setEmail(USER_EM×IL);
user.setPasswor (USER_P×SSWRD);
user.setSystemRole(USER_SYSTEM_RLE);
em.getTransaction().begin();
System.out.println("Persisting to DB via Entity Manager");
em.persist(user);
em.getTransaction().commit();
}

private voi removeTestData() {


System.out.println("Removing new user previously create ");
em.getTransaction().begin();
Users user = em.fin (Users.class, USER_EM×IL);
if (user != null) {
em.remove(user);
}
em.getTransaction().commit();
}
public voi testInserte UserIs×ali User() throws Exception {
UsersEntityManagerBeanuemb = new UsersEntityManagerBean();
uemb.em = em;
Users user = uemb.getUser(USER_EM×IL);
System.out.println("Using UsersEntityManagerBean to query new user previously
entere into DB");
if(!user.equals(null))
System.out.println("User was retrieve from the DB via UsersEntityManagerBean");
assertNotNull(user);
assertEquals(USER_EM×IL, user.getEmail());
assertEquals(USER_P×SSWRD, user.getPasswor ());
assertEquals(USER_SYSTEM_RLE, (short)user.getSystemRole());
}

//IMRT×NT NTE: To Test The × min You Must First × Her In The DB
public voi test× minIs×ali User() throws Exception {
UsersEntityManagerBeanuemb = new UsersEntityManagerBean();
uemb.em = em;
Users user = uemb.getUser(×DMIN_EM×IL);
System.out.println("Using UsersEntityManagerBean to query a min in DB");
if(!user.equals(null))
System.out.println("× min was retrieve from the DB via UsersEntityManagerBean");
assertNotNull(user);
assertEquals(×DMIN_EM×IL, user.getEmail());
assertEquals(×DMIN_P×SSWRD, user.getPasswor ());
assertEquals(×DMIN_SYSTEM_RLE, (short)user.getSystemRole());
}
}

- Cross your fingers an select Test by right click on project name an selecting Test.

My final test looke like this:

Creating Entity Manager Factory


Mar 8, 2011 11:13:35 PM org.hibernate.vali ator.util.ersion<clinit>
INF: Hibernate ali ator bean-vali ator-3.0-JBoss-4.0.2
Mar 8, 2011 11:13:35 PM
org.hibernate.vali ator.engine.resolver.DefaultTraversableResolver etectJP×
INF: Instantiate an instance of
org.hibernate.vali ator.engine.resolver.JP×TraversableResolver.
UEL Warning]: 2011-03-08 23:13:37.918--ServerSession(24745276)--
PersistenceUnitInfoFooEJBPU has transactionType RESURCE_LC×L an therefore
jtaDataSource will be ignore
UEL Info]: 2011-03-08 23:13:37.934--ServerSession(24745276)--EclipseLink, version:
Eclipse Persistence Services - 2.0.1.v20100213-r6600
UEL Info]: 2011-03-08 23:13:39.166--ServerSession(24745276)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/buil /classes/_FooEJBPU login
successful
Creating Entity Manager
Creating new user
Persisting to DB via Entity Manager
Using UsersEntityManagerBean to query new user previously entere into DB
User was retrieve from the DB via UsersEntityManagerBean
Tear Down
Removing new user previously create
Closing Entity Manager
UEL Info]: 2011-03-08 23:13:39.743--ServerSession(24745276)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/buil /classes/_FooEJBPU
logout successful
Creating Entity Manager Factory
UEL Warning]: 2011-03-08 23:13:39.79--ServerSession(21411547)--
PersistenceUnitInfoFooEJBPU has transactionType RESURCE_LC×L an therefore
jtaDataSource will be ignore
UEL Info]: 2011-03-08 23:13:39.79--ServerSession(21411547)--EclipseLink, version:
Eclipse Persistence Services - 2.0.1.v20100213-r6600
Mar 8, 2011 11:13:39 PM
org.hibernate.vali ator.engine.resolver.DefaultTraversableResolver etectJP×
INF: Instantiate an instance of
org.hibernate.vali ator.engine.resolver.JP×TraversableResolver.
UEL Info]: 2011-03-08 23:13:39.899--ServerSession(21411547)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/buil /classes/_FooEJBPU login
successful
Creating Entity Manager
Creating new user
Persisting to DB via Entity Manager
Using UsersEntityManagerBean to query a min in DB
× min was retrieve from the DB via UsersEntityManagerBean
Tear Down
Removing new user previously create
Closing Entity Manager
UEL Info]: 2011-03-08 23:13:39.977--ServerSession(21411547)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/buil /classes/_FooEJBPU
logout successful