From 79a5f065728a5e9e472946c9c796a8336a3dce99 Mon Sep 17 00:00:00 2001 From: freein <> Date: Tue, 23 Oct 2012 18:13:25 +0200 Subject: [PATCH] Refactoring. Creating superclasses. --- nbactions.xml | 3 - pom.xml | 14 +++-- .../java/dat076/frukostklubben/bb/CartBB.java | 4 +- .../frukostklubben/bb/ChangeInfoBB.java | 6 +- .../frukostklubben/bb/FlightController.java | 6 +- .../dat076/frukostklubben/bb/OrderBB.java | 4 +- .../frukostklubben/bb/SearchFlightBean.java | 2 +- .../dat076/frukostklubben/bb/SignupBB.java | 5 +- .../frukostklubben/bb/UserController.java | 4 +- .../frukostklubben/ejb/ShoppingCartEJB.java | 6 +- .../dat076/frukostklubben/ejb/UserEJB.java | 56 ------------------- .../model/{Order.java => FinnishedOrder.java} | 10 ++-- .../dat076/frukostklubben/model/User.java | 12 ++-- .../AbstractPersistenceEJB.java | 35 ++++++++++++ .../{ejb => persistenceEJB}/FlightEJB.java | 35 +++--------- .../persistenceEJB/OrderEJB.java | 21 +++++++ .../persistenceEJB/UserEJB.java | 30 ++++++++++ src/main/resources/META-INF/persistence.xml | 3 +- src/main/setup/glassfish-resources.xml | 6 -- .../frukostklubben/test/FlightEJBTest.java | 22 ++++---- .../test/ShoppingCartEJBTest.java | 3 +- .../frukostklubben/test/UserEJBTest.java | 46 ++++++++------- src/test/resources/META-INF/persistence.xml | 12 ---- 23 files changed, 171 insertions(+), 174 deletions(-) delete mode 100644 src/main/java/dat076/frukostklubben/ejb/UserEJB.java rename src/main/java/dat076/frukostklubben/model/{Order.java => FinnishedOrder.java} (80%) create mode 100644 src/main/java/dat076/frukostklubben/persistenceEJB/AbstractPersistenceEJB.java rename src/main/java/dat076/frukostklubben/{ejb => persistenceEJB}/FlightEJB.java (82%) create mode 100644 src/main/java/dat076/frukostklubben/persistenceEJB/OrderEJB.java create mode 100644 src/main/java/dat076/frukostklubben/persistenceEJB/UserEJB.java delete mode 100644 src/test/resources/META-INF/persistence.xml diff --git a/nbactions.xml b/nbactions.xml index 377f0c2..5d07b58 100644 --- a/nbactions.xml +++ b/nbactions.xml @@ -5,9 +5,6 @@ install - - true - run diff --git a/pom.xml b/pom.xml index 4f449e8..b7a438f 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ UTF-8 2.4.0 gfv3ee6 + C:\Program Files\glassfish-3.1.2.2\glassfish\lib\embedded\glassfish-embedded-static-shell.jar @@ -47,12 +48,6 @@ junit 4.10 test - - - org.glassfish.main.extras - glassfish-embedded-all - 3.1.2 - test org.icefaces @@ -69,6 +64,13 @@ derbyclient 10.9.1.0 + + org.glassfish.extras + glassfish-embedded-static-shell + 3.1.1 + system + ${glassfish.embedded-static-shell.jar} + javax javaee-web-api diff --git a/src/main/java/dat076/frukostklubben/bb/CartBB.java b/src/main/java/dat076/frukostklubben/bb/CartBB.java index db1917b..371f657 100644 --- a/src/main/java/dat076/frukostklubben/bb/CartBB.java +++ b/src/main/java/dat076/frukostklubben/bb/CartBB.java @@ -3,7 +3,7 @@ * and open the template in the editor. */ package dat076.frukostklubben.bb; -import dat076.frukostklubben.ejb.FlightEJB; +import dat076.frukostklubben.persistenceEJB.FlightEJB; import dat076.frukostklubben.ejb.ShoppingCartEJB; import dat076.frukostklubben.model.Flight; import java.io.Serializable; @@ -39,7 +39,7 @@ public List getFlights(){ } public String removeFlight(Long id){ - cartEJB.removeItem(flightEJB.findFlightById(id)); + cartEJB.removeItem(flightEJB.find(id)); return "cart?faces-redirect=true"; } } diff --git a/src/main/java/dat076/frukostklubben/bb/ChangeInfoBB.java b/src/main/java/dat076/frukostklubben/bb/ChangeInfoBB.java index 9fc275d..e5273c8 100644 --- a/src/main/java/dat076/frukostklubben/bb/ChangeInfoBB.java +++ b/src/main/java/dat076/frukostklubben/bb/ChangeInfoBB.java @@ -4,7 +4,7 @@ */ package dat076.frukostklubben.bb; -import dat076.frukostklubben.ejb.UserEJB; +import dat076.frukostklubben.persistenceEJB.UserEJB; import dat076.frukostklubben.model.User; import java.io.Serializable; import javax.annotation.ManagedBean; @@ -43,14 +43,14 @@ public class ChangeInfoBB implements Serializable { // = Public Methods = // ====================================== public String fetchUser() { - user = userRegistry.findByMail(currentMail); + user = userRegistry.find(currentMail); return "/users/changeInfo?faces-redirect=true"; } public void change() { if (checkPasswd.equals(user.getPasswd())) { user.setPasswd(newPasswd); - userRegistry.updateUser(user); + userRegistry.update(user); } else { checkPasswd = ""; } diff --git a/src/main/java/dat076/frukostklubben/bb/FlightController.java b/src/main/java/dat076/frukostklubben/bb/FlightController.java index 105cdf1..334ec0b 100644 --- a/src/main/java/dat076/frukostklubben/bb/FlightController.java +++ b/src/main/java/dat076/frukostklubben/bb/FlightController.java @@ -4,7 +4,7 @@ */ package dat076.frukostklubben.bb; -import dat076.frukostklubben.ejb.FlightEJB; +import dat076.frukostklubben.persistenceEJB.FlightEJB; import dat076.frukostklubben.ejb.ShoppingCartEJB; import dat076.frukostklubben.model.Flight; import java.util.ArrayList; @@ -38,7 +38,7 @@ public class FlightController { // ====================================== public String doCreateFlight() { - flight = flightEJB.createFlight(flight); + flightEJB.create(flight); return "browse?faces-redirect=true"; // bug fix, need parameter to redirect correctly } @@ -54,7 +54,7 @@ public void setFlight(Flight flight) { } public List getFlightList() { - return flightEJB.findFlights(); + return flightEJB.findAll(); } public void buy(){ diff --git a/src/main/java/dat076/frukostklubben/bb/OrderBB.java b/src/main/java/dat076/frukostklubben/bb/OrderBB.java index 9474197..3532b3f 100644 --- a/src/main/java/dat076/frukostklubben/bb/OrderBB.java +++ b/src/main/java/dat076/frukostklubben/bb/OrderBB.java @@ -5,8 +5,8 @@ package dat076.frukostklubben.bb; import dat076.frukostklubben.ejb.ShoppingCartEJB; -import dat076.frukostklubben.ejb.UserEJB; import dat076.frukostklubben.model.User; +import dat076.frukostklubben.persistenceEJB.UserEJB; import javax.ejb.EJB; import javax.faces.bean.RequestScoped; import javax.inject.Named; @@ -34,7 +34,7 @@ public void setMail(String mail) { } public String checkout(){ - User user = userEJB.findByMail(mail); + User user = userEJB.find(mail); shoppingCartEJB.checkout(user); return null; //när en order är skickad. var ska jag då? } diff --git a/src/main/java/dat076/frukostklubben/bb/SearchFlightBean.java b/src/main/java/dat076/frukostklubben/bb/SearchFlightBean.java index 6460de4..e8ec975 100644 --- a/src/main/java/dat076/frukostklubben/bb/SearchFlightBean.java +++ b/src/main/java/dat076/frukostklubben/bb/SearchFlightBean.java @@ -4,8 +4,8 @@ */ package dat076.frukostklubben.bb; -import dat076.frukostklubben.ejb.FlightEJB; import dat076.frukostklubben.model.Flight; +import dat076.frukostklubben.persistenceEJB.FlightEJB; import java.io.Serializable; import java.util.List; import javax.ejb.EJB; diff --git a/src/main/java/dat076/frukostklubben/bb/SignupBB.java b/src/main/java/dat076/frukostklubben/bb/SignupBB.java index 1746ef3..d540a58 100644 --- a/src/main/java/dat076/frukostklubben/bb/SignupBB.java +++ b/src/main/java/dat076/frukostklubben/bb/SignupBB.java @@ -1,7 +1,8 @@ package dat076.frukostklubben.bb; -import dat076.frukostklubben.ejb.UserEJB; + import dat076.frukostklubben.model.User; +import dat076.frukostklubben.persistenceEJB.UserEJB; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.ManagedBean; @@ -87,7 +88,7 @@ else if((!input1.equals(input2)) || input1 == null) public String doCreateUser() { try { - user = userRegistry.createUser(user); + userRegistry.create(user); return "login?faces-redirect=true"; // bug fix, need parameter to redirect correctly } catch (Exception e) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Bad Login name")); diff --git a/src/main/java/dat076/frukostklubben/bb/UserController.java b/src/main/java/dat076/frukostklubben/bb/UserController.java index 5eac596..3bca4cd 100644 --- a/src/main/java/dat076/frukostklubben/bb/UserController.java +++ b/src/main/java/dat076/frukostklubben/bb/UserController.java @@ -1,7 +1,7 @@ package dat076.frukostklubben.bb; -import dat076.frukostklubben.ejb.UserEJB; import dat076.frukostklubben.model.User; +import dat076.frukostklubben.persistenceEJB.UserEJB; import java.io.Serializable; import javax.ejb.EJB; import javax.enterprise.context.SessionScoped; @@ -48,7 +48,7 @@ public void setPassword(String password) { } public String signin() { - user = userEJB.findByMail(mail); + user = userEJB.find(mail); if (user != null) { if (user.getPasswd().equals(password)) { return "chat?faces-redirect=true"; diff --git a/src/main/java/dat076/frukostklubben/ejb/ShoppingCartEJB.java b/src/main/java/dat076/frukostklubben/ejb/ShoppingCartEJB.java index e4435d2..bdc71f0 100644 --- a/src/main/java/dat076/frukostklubben/ejb/ShoppingCartEJB.java +++ b/src/main/java/dat076/frukostklubben/ejb/ShoppingCartEJB.java @@ -5,7 +5,7 @@ package dat076.frukostklubben.ejb; import dat076.frukostklubben.model.Flight; -import dat076.frukostklubben.model.Order; +import dat076.frukostklubben.model.FinnishedOrder; import dat076.frukostklubben.model.User; import java.util.ArrayList; import java.util.List; @@ -99,10 +99,10 @@ public void empty() { @Remove //Detta betyder att denna böna försvinner när checkout() anropas. public void checkout(User user) { - Order order = new Order(); + FinnishedOrder order = new FinnishedOrder(); order.setFlights(cartItems); order.setUser(user); ///Hur vet jag vem??? - user.getOrderhistory().add(order); + user.getFinnishedOrders().add(order); em.persist(order); cartItems.clear(); } diff --git a/src/main/java/dat076/frukostklubben/ejb/UserEJB.java b/src/main/java/dat076/frukostklubben/ejb/UserEJB.java deleted file mode 100644 index 340db84..0000000 --- a/src/main/java/dat076/frukostklubben/ejb/UserEJB.java +++ /dev/null @@ -1,56 +0,0 @@ -package dat076.frukostklubben.ejb; - -import dat076.frukostklubben.model.User; -import java.util.List; -import javax.ejb.LocalBean; -import javax.ejb.Stateless; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.TypedQuery; - -/** - * - * @author freein Edited by: Anders, 2012-10-17 - */ -@Stateless -@LocalBean -public class UserEJB { - - @PersistenceContext(unitName = "projectPU") - private EntityManager em; - - - public List findUsers() { - TypedQuery query = em.createNamedQuery("findAllUsers", User.class); - return query.getResultList(); - } - - - public User findUserById(Long id) { - return em.find(User.class, id); - } - - - public User createUser(User user) { - em.persist(user); - return user; - } - - - public void deleteUser(User user) { - em.remove(em.merge(user)); - } - - - public User updateUser(User user) { - return em.merge(user); - } - - - public User findByMail(String mail) { - String query = "select u from UserObj u where u.mail = :mail"; - TypedQuery q = em.createQuery(query, User.class); - q.setParameter("mail", mail); - return q.getSingleResult(); - } -} diff --git a/src/main/java/dat076/frukostklubben/model/Order.java b/src/main/java/dat076/frukostklubben/model/FinnishedOrder.java similarity index 80% rename from src/main/java/dat076/frukostklubben/model/Order.java rename to src/main/java/dat076/frukostklubben/model/FinnishedOrder.java index 3096fef..daa1487 100644 --- a/src/main/java/dat076/frukostklubben/model/Order.java +++ b/src/main/java/dat076/frukostklubben/model/FinnishedOrder.java @@ -11,26 +11,26 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.ManyToOne; import javax.persistence.OneToMany; -import javax.persistence.OneToOne; /** * * @author Fredrik */ @Entity -public class Order implements Serializable { +public class FinnishedOrder implements Serializable { @Id @GeneratedValue private Long id; @ElementCollection(fetch = FetchType.LAZY) @OneToMany List flights = new ArrayList<>(); - @OneToOne //Lägg till casce. //bidirectional + + @ManyToOne User user; - public Order(){ + public FinnishedOrder(){ } public List getFlights() { diff --git a/src/main/java/dat076/frukostklubben/model/User.java b/src/main/java/dat076/frukostklubben/model/User.java index 06903f4..08cb6fa 100644 --- a/src/main/java/dat076/frukostklubben/model/User.java +++ b/src/main/java/dat076/frukostklubben/model/User.java @@ -33,8 +33,8 @@ public class User extends Subject implements Serializable { @Embedded @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) private Address address = new Address(); - @OneToMany - private List orderhistory = new ArrayList<>(); + @OneToMany(mappedBy = "user") + private List finnishedOrders; public User(String firstName, String lastName, String mail, Address address) { this.firstName = firstName; @@ -76,12 +76,12 @@ public void setAddress(Address address) { this.address = address; } - public List getOrderhistory() { - return orderhistory; + public List getFinnishedOrders() { + return finnishedOrders; } - public void setOrderhistory(List orderhistory) { - this.orderhistory = orderhistory; + public void setFinnishedOrders(List finnishedOrders) { + this.finnishedOrders = finnishedOrders; } } \ No newline at end of file diff --git a/src/main/java/dat076/frukostklubben/persistenceEJB/AbstractPersistenceEJB.java b/src/main/java/dat076/frukostklubben/persistenceEJB/AbstractPersistenceEJB.java new file mode 100644 index 0000000..35a3a81 --- /dev/null +++ b/src/main/java/dat076/frukostklubben/persistenceEJB/AbstractPersistenceEJB.java @@ -0,0 +1,35 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package dat076.frukostklubben.persistenceEJB; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +/** + * + * @author Fredrik + */ + +public abstract class AbstractPersistenceEJB{ + @PersistenceContext(unitName = "projectPU") + private EntityManager em; + + public void create(T t) { + em.persist(t); + } + + + public void delete(T t) { + em.remove(em.merge(t)); + } + + + public T update(T t) { + return em.merge(t); + } + + public T find(Class clazz,K k) { + return (T) em.find(clazz, k); + } +} diff --git a/src/main/java/dat076/frukostklubben/ejb/FlightEJB.java b/src/main/java/dat076/frukostklubben/persistenceEJB/FlightEJB.java similarity index 82% rename from src/main/java/dat076/frukostklubben/ejb/FlightEJB.java rename to src/main/java/dat076/frukostklubben/persistenceEJB/FlightEJB.java index fbb388d..2057369 100644 --- a/src/main/java/dat076/frukostklubben/ejb/FlightEJB.java +++ b/src/main/java/dat076/frukostklubben/persistenceEJB/FlightEJB.java @@ -1,7 +1,7 @@ /* * If something is added here, add extra test case in FlightEJBTest */ -package dat076.frukostklubben.ejb; +package dat076.frukostklubben.persistenceEJB; import dat076.frukostklubben.model.Flight; import java.util.List; @@ -18,41 +18,20 @@ */ @Stateless @LocalBean -public class FlightEJB { +public class FlightEJB extends AbstractPersistenceEJB { @PersistenceContext(unitName = "projectPU") private EntityManager em; + public Flight find(Long id) { + return find(Flight.class,id); + } - public List findFlights() { + public List findAll() { TypedQuery query = em.createNamedQuery("findAllFlights", Flight.class); return query.getResultList(); } - - - public Flight findFlightById(Long id) { - return em.find(Flight.class, id); - } - - - public Flight createFlight(Flight flight) { - em.persist(flight); - return flight; - } - - - public void deleteFlight(Flight flight) { - em.remove(em.merge(flight)); - } - - - public Flight updateFlight(Flight flight) { - return em.merge(flight); - } - /* - * Metoden söker i databasen efter antingen från och till eller någon - * av dem. - */ + public List searchFlights(Flight flight) { if (!flight.getFromAirport().equals("") && !flight.getToAirport().equals("") diff --git a/src/main/java/dat076/frukostklubben/persistenceEJB/OrderEJB.java b/src/main/java/dat076/frukostklubben/persistenceEJB/OrderEJB.java new file mode 100644 index 0000000..3d024ec --- /dev/null +++ b/src/main/java/dat076/frukostklubben/persistenceEJB/OrderEJB.java @@ -0,0 +1,21 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package dat076.frukostklubben.persistenceEJB; + +import dat076.frukostklubben.model.FinnishedOrder; +import javax.ejb.LocalBean; +import javax.ejb.Stateless; + +/** + * + * @author Fredrik + */ +@Stateless +@LocalBean +public class OrderEJB extends AbstractPersistenceEJB { + public FinnishedOrder find(Long id){ + return find(FinnishedOrder.class, id); + } +} diff --git a/src/main/java/dat076/frukostklubben/persistenceEJB/UserEJB.java b/src/main/java/dat076/frukostklubben/persistenceEJB/UserEJB.java new file mode 100644 index 0000000..11154bb --- /dev/null +++ b/src/main/java/dat076/frukostklubben/persistenceEJB/UserEJB.java @@ -0,0 +1,30 @@ +package dat076.frukostklubben.persistenceEJB; + +import dat076.frukostklubben.model.User; +import java.util.List; +import javax.ejb.LocalBean; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; + +/** + * + * @author freein Edited by: Anders, 2012-10-17 + */ +@Stateless +@LocalBean +public class UserEJB extends AbstractPersistenceEJB { + + @PersistenceContext(unitName = "projectPU") + private EntityManager em; + + public User find(String mail){ + return super.find(User.class, mail); + } + + public List findAll() { + TypedQuery query = em.createNamedQuery("findAllUsers", User.class); + return query.getResultList(); + } +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 10a30a7..650c5c7 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -2,9 +2,10 @@ org.eclipse.persistence.jpa.PersistenceProvider + jdbc/projectPU - + diff --git a/src/main/setup/glassfish-resources.xml b/src/main/setup/glassfish-resources.xml index 10a934b..4341ec9 100644 --- a/src/main/setup/glassfish-resources.xml +++ b/src/main/setup/glassfish-resources.xml @@ -1,12 +1,6 @@ - - - - - - diff --git a/src/test/java/dat076/frukostklubben/test/FlightEJBTest.java b/src/test/java/dat076/frukostklubben/test/FlightEJBTest.java index 453178b..18708e6 100644 --- a/src/test/java/dat076/frukostklubben/test/FlightEJBTest.java +++ b/src/test/java/dat076/frukostklubben/test/FlightEJBTest.java @@ -4,7 +4,7 @@ * and open the template in the editor. */ -import dat076.frukostklubben.ejb.FlightEJB; +import dat076.frukostklubben.persistenceEJB.FlightEJB; import dat076.frukostklubben.model.Flight; import java.util.Calendar; import java.util.Date; @@ -49,35 +49,35 @@ public static void closeContainer() throws Exception { // ====================================== // = Unit tests = // ====================================== - @Test public void testOfAllFlightEJBMethods() throws Exception { // Creates an instance of flight Flight flight = new Flight("Resa 1","Landvetter", "Frankfurt",new Date(Calendar.getInstance().getTimeInMillis())); + flight.setCost(2.3); // Persists the flight to the database - flight = flightEJB.createFlight(flight); + flightEJB.create(flight); Assert.assertNotNull("ID should not be null", flight.getId()); // Retrieves all the flights from the database - List flights = flightEJB.findFlights(); + List flights = flightEJB.findAll(); Assert.assertNotNull(flights); //Gets a flight by its id - Flight flight2 = flightEJB.findFlightById(flight.getId()); - Assert.assertTrue(flight.getId() == flight2.getId()); + Flight flight2 = flightEJB.find(flight.getId()); + Assert.assertEquals(flight.getId(), flight2.getId()); //Updating the flight flight.setName("Ny Flygtur"); - flightEJB.updateFlight(flight); - Flight flight3 = flightEJB.findFlightById(flight.getId()); - Assert.assertTrue(flight3.getName().equals("Ny Flygtur")); + flightEJB.update(flight); + Flight flight3 = flightEJB.find(flight.getId()); + Assert.assertEquals(flight3.getName(),"Ny Flygtur"); //If we delete the last flight the list of flights should på empty. - flightEJB.deleteFlight(flight); - flights = flightEJB.findFlights(); + flightEJB.delete(flight); + flights = flightEJB.findAll(); Assert.assertTrue(flights.isEmpty()); } } diff --git a/src/test/java/dat076/frukostklubben/test/ShoppingCartEJBTest.java b/src/test/java/dat076/frukostklubben/test/ShoppingCartEJBTest.java index f5bb088..4f85676 100644 --- a/src/test/java/dat076/frukostklubben/test/ShoppingCartEJBTest.java +++ b/src/test/java/dat076/frukostklubben/test/ShoppingCartEJBTest.java @@ -11,13 +11,14 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; /** * * @author freein */ -public class ShoppingCartEJBTest { +@Ignore public class ShoppingCartEJBTest { // ====================================== // = Fields = diff --git a/src/test/java/dat076/frukostklubben/test/UserEJBTest.java b/src/test/java/dat076/frukostklubben/test/UserEJBTest.java index a002428..7fd42a0 100644 --- a/src/test/java/dat076/frukostklubben/test/UserEJBTest.java +++ b/src/test/java/dat076/frukostklubben/test/UserEJBTest.java @@ -1,8 +1,9 @@ package dat076.frukostklubben.test; -import dat076.frukostklubben.ejb.UserEJB; +import dat076.frukostklubben.persistenceEJB.UserEJB; import dat076.frukostklubben.model.Address; import dat076.frukostklubben.model.User; +import dat076.frukostklubben.security.SubjectGroup; import java.util.List; import javax.ejb.embeddable.EJBContainer; import junit.framework.Assert; @@ -44,31 +45,34 @@ public static void closeContainer() throws Exception { @Test public void testUser() throws Exception{ // Creates an instance of User - User user = new User("Anders", "Nordin", "mail@mail.com", new Address()); + User user = new User(); + user.addGroup(SubjectGroup.USER); + user.setAddress(new Address()); + user.setFirstName("Fredrik"); + user.setLastName("Einarsson"); + user.setMail("Einarsson.F@gmail.com"); + user.setPasswd("fredrik"); // Persists the user to the database - user = userEJB.createUser(user); - Assert.assertNotNull(user); + userEJB.create(user); - // Retrieves all the users from the database - List users = userEJB.findUsers(); - Assert.assertNotNull(users); + //Try aquire it again + User user2 = userEJB.find(user.getMail()); + Assert.assertEquals(user2.getFirstName(), user.getFirstName()); - //Gets a user by its id - /*User user2 = userEJB.findUserById(user.getId()); - Assert.assertEquals(user2.getId(), user.getId());*/ + //Try update + user.setLastName("Andersson"); + userEJB.update(user); + User user3 = userEJB.find(user.getMail()); + Assert.assertEquals(user3.getLastName(),"Andersson"); - //Updating the user - /* user.setMail("Anders@mail.com"); - userEJB.updateUser(user); - User user3 = userEJB.findUserById(user.getId()); - Assert.assertEquals(user3.getMail(),"Anders@mail.com");*/ + //Try delete + userEJB.delete(user); + user = userEJB.find(user.getMail()); + Assert.assertNull(user); - - //If we delete the last user the list of users should på empty. - userEJB.deleteUser(user); - users = userEJB.findUsers(); - Assert.assertTrue(users.isEmpty()); - + //findall + List list = userEJB.findAll(); + Assert.assertNotNull(list); } } diff --git a/src/test/resources/META-INF/persistence.xml b/src/test/resources/META-INF/persistence.xml deleted file mode 100644 index ccb5756..0000000 --- a/src/test/resources/META-INF/persistence.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - org.eclipse.persistence.jpa.PersistenceProvider - jdbc/__default - - - - - - \ No newline at end of file