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