1 22 package org.jboss.ejb3.test.entity.unit; 23 24 import java.util.Set ; 25 import org.jboss.ejb3.test.entity.Address; 26 import org.jboss.ejb3.test.entity.Company; 27 import org.jboss.ejb3.test.entity.Customer; 28 import org.jboss.ejb3.test.entity.EntityTest; 29 import org.jboss.ejb3.test.entity.FieldAddress; 30 import org.jboss.ejb3.test.entity.FieldCompany; 31 import org.jboss.ejb3.test.entity.FieldCustomer; 32 import org.jboss.ejb3.test.entity.FieldFlight; 33 import org.jboss.ejb3.test.entity.FieldTicket; 34 import org.jboss.ejb3.test.entity.Flight; 35 import org.jboss.ejb3.test.entity.Ticket; 36 import org.jboss.test.JBossTestCase; 37 import junit.framework.Test; 38 39 45 46 public class EntityUnitTestCase 47 extends JBossTestCase 48 { 49 org.jboss.logging.Logger log = getLog(); 50 51 static boolean deployed = false; 52 static int test = 0; 53 54 public EntityUnitTestCase(String name) 55 { 56 57 super(name); 58 59 } 60 61 public void testOneToMany() throws Exception 62 { 63 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 64 Customer c = test.oneToManyCreate(); 65 assertNotNull(c); 66 assertNotNull(c.getTickets()); 67 Set <Ticket> tickets = c.getTickets(); 68 assertTrue(tickets.size() > 0); 69 70 c = test.findCustomerById(c.getId()); 72 assertNotNull(c); 73 assertNotNull(c.getTickets()); 74 tickets = c.getTickets(); 75 assertTrue(tickets.size() > 0); 76 77 Address address = c.getAddress(); 79 assertTrue(address != null); 80 assertTrue(address.getCity().equals("Boston")); 81 } 82 83 public void testManyToOne() throws Exception 84 { 85 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 86 Flight f = test.manyToOneCreate(); 87 f = test.findFlightById(f.getId()); 88 assertTrue(f.getName().equals("AF0101")); 89 assertTrue(f.getCompany().getName().equals("Air France")); 90 91 Company c = test.findCompanyById(f.getCompany().getId()); 92 assertTrue(c != null); 93 assertTrue(c.getFlights().size() == 1); 94 } 95 96 public void testManyToMany() throws Exception 97 { 98 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 99 test.manyToManyCreate(); 100 101 Flight one = test.findFlightById(new Long (1)); 102 assertTrue(one.getCompany().getName().equals("Air France")); 103 104 Flight two = test.findFlightById(new Long (2)); 105 assertTrue(two.getCompany().getName().equals("USAir")); 106 107 System.out.println("Air France customers"); 108 for (Customer c : one.getCustomers()) 109 { 110 System.out.println(c.getName()); 111 112 } 113 System.out.println("USAir customers"); 114 115 for (Customer c : two.getCustomers()) 116 { 117 System.out.println(c.getName()); 118 } 119 120 } 121 122 public void testFieldOneToMany() throws Exception 123 { 124 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 125 FieldCustomer c = test.fieldOneToManyCreate(); 126 assertNotNull(c); 127 assertNotNull(c.getTickets()); 128 Set <FieldTicket> tickets = c.getTickets(); 129 assertTrue(tickets.size() > 0); 130 131 c = test.fieldFindCustomerById(c.getId()); 133 assertNotNull(c); 134 assertNotNull(c.getTickets()); 135 tickets = c.getTickets(); 136 assertTrue(tickets.size() > 0); 137 138 FieldAddress address = c.getAddress(); 140 assertTrue(address != null); 141 assertTrue(address.getCity().equals("Boston")); 142 } 143 144 public void testFieldManyToOne() throws Exception 145 { 146 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 147 FieldFlight f = test.fieldManyToOneCreate(); 148 f = test.fieldFindFlightById(f.getId()); 149 assertTrue(f.getName().equals("AF0101")); 150 assertTrue(f.getCompany().getName().equals("Air France")); 151 152 FieldCompany c = test.fieldFindCompanyById(f.getCompany().getId()); 153 assertTrue(c != null); 154 assertTrue(c.getFlights().size() == 1); 155 } 156 157 public void testFieldManyToMany() throws Exception 158 { 159 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 160 test.fieldManyToManyCreate(); 161 162 FieldFlight one = test.fieldFindFlightById(new Long (1)); 163 assertTrue(one.getCompany().getName().equals("Air France")); 164 165 FieldFlight two = test.fieldFindFlightById(new Long (2)); 166 assertTrue(two.getCompany().getName().equals("USAir")); 167 168 System.out.println("Air France customers"); 169 for (FieldCustomer c : one.getCustomers()) 170 { 171 System.out.println(c.getName()); 172 173 } 174 System.out.println("USAir customers"); 175 176 for (FieldCustomer c : two.getCustomers()) 177 { 178 System.out.println(c.getName()); 179 } 180 181 } 182 183 public void testNamedQueries() throws Exception 184 { 185 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 186 test.testNamedQueries(); 187 } 188 189 public void testOutsideTx() throws Exception 190 { 191 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 192 test.testOutsideTransaction(); 193 194 } 195 196 public void testFlush() throws Exception 197 { 198 EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote"); 199 Customer c = test.createCustomer("Emmanuel"); 200 test.changeCustomer(c.getId(), "Bill"); 201 Customer c2 = test.loadCustomer(c.getId()); 202 assertEquals("Bill", c2.getName()); 203 } 204 205 public static Test suite() throws Exception 206 { 207 return getDeploySetup(EntityUnitTestCase.class, "entity-test.jar"); 208 } 209 210 } 211 | Popular Tags |