1 22 package org.jboss.ejb3.test.singletableinheritance; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.ejb.Remote ; 27 import javax.ejb.Stateless ; 28 import javax.persistence.EntityManager; 29 import javax.persistence.PersistenceContext; 30 31 37 @Stateless 38 @Remote (EntityTest.class) 39 public class EntityTestBean implements EntityTest 40 { 41 private @PersistenceContext EntityManager manager; 42 43 private void assertTrue(boolean value) 44 { 45 if (!value) throw new RuntimeException ("assert failed"); 46 } 47 48 private void assertFalse(boolean value) 49 { 50 if (value) throw new RuntimeException ("assert failed"); 51 } 52 53 private void assertEquals(int x, int y) 54 { 55 if (x != y) throw new RuntimeException ("assert failed"); 56 } 57 58 private void assertEquals(String x, String y) 59 { 60 if (x == y) return; 61 if (x == null && y != null) throw new RuntimeException ("assert failed"); 62 if (x != null && y == null) throw new RuntimeException ("assert failed"); 63 if (!x.equals(y)) throw new RuntimeException ("assert failed"); 64 } 65 66 public long[] createBeans() throws Exception 67 { 68 Employee mark = new Employee(); 69 mark.setName("Mark"); 70 mark.setTitle("internal sales"); 71 mark.setSex('M'); 72 mark.setAddress("buckhead"); 73 mark.setZip("30305"); 74 mark.setCountry("USA"); 75 76 Customer joe = new Customer(); 77 joe.setName("Joe"); 78 joe.setAddress("San Francisco"); 79 joe.setZip("XXXXX"); 80 joe.setCountry("USA"); 81 joe.setComments("Very demanding"); 82 joe.setSalesperson(mark); 83 84 Person yomomma = new Person(); 85 yomomma.setName("mum"); 86 yomomma.setSex('F'); 87 88 manager.persist(mark); 89 manager.persist(joe); 90 manager.persist(yomomma); 91 long[] ids = {mark.getId(), joe.getId(), yomomma.getId()}; 92 return ids; 93 } 94 95 public void test1() throws Exception 96 { 97 assertEquals(manager.createQuery("from java.io.Serializable").getResultList().size(), 0); 98 99 assertEquals(manager.createQuery("from Person").getResultList().size(), 3); 100 assertEquals(manager.createQuery("from Person p where p.class = Customer").getResultList().size(), 1); 101 } 102 103 public void test2() throws Exception 104 { 105 List customers = manager.createQuery("from Customer c left join fetch c.salesperson").getResultList(); 106 for (Iterator iter = customers.iterator(); iter.hasNext();) 107 { 108 Customer c = (Customer) iter.next(); 109 assertEquals(c.getSalesperson().getName(), "Mark"); 110 } 111 assertEquals(customers.size(), 1); 112 } 113 114 public void test3() throws Exception 115 { 116 List customers = manager.createQuery("from Customer").getResultList(); 117 for (Iterator iter = customers.iterator(); iter.hasNext();) 118 { 119 Customer c = (Customer) iter.next(); 120 assertEquals(c.getSalesperson().getName(), "Mark"); 121 } 122 assertEquals(customers.size(), 1); 123 } 124 125 public void test4(long[] ids) throws Exception 126 { 127 Employee mark = manager.find(Employee.class, new Long (ids[0])); 128 Customer joe = (Customer) manager.find(Customer.class, new Long (ids[1])); 129 Person yomomma = manager.find(Person.class, new Long (ids[2])); 130 131 mark.setZip("30306"); 132 assertEquals(manager.createQuery("from Person p where p.zip = '30306'").getResultList().size(), 1); 133 manager.remove(mark); 134 manager.remove(joe); 135 manager.remove(yomomma); 136 assertTrue(manager.createQuery("from Person").getResultList().isEmpty()); 137 } 138 } 139 | Popular Tags |