1 22 package org.jboss.ejb3.test.relationships.unit; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import org.jboss.ejb3.test.relationships.Customer; 27 import org.jboss.ejb3.test.relationships.LineItem; 28 import org.jboss.ejb3.test.relationships.Order; 29 import org.jboss.ejb3.test.relationships.SessionRemote; 30 import org.jboss.test.JBossTestCase; 31 import junit.framework.Test; 32 33 40 41 public class RelationshipUnitTestCase 42 extends JBossTestCase 43 { 44 org.jboss.logging.Logger log = getLog(); 45 46 static boolean deployed = false; 47 static int test = 0; 48 49 public RelationshipUnitTestCase(String name) 50 { 51 52 super(name); 53 54 } 55 56 public void testUniOneToMany() throws Exception 57 { 58 SessionRemote remote = (SessionRemote) getInitialContext().lookup("SessionBean/remote"); 59 remote.createCategory(); 60 remote.createItem(); 61 int num = remote.getNumItems(); 62 assertEquals(1, num); 63 } 64 65 public void testOneToOne() throws Exception 66 { 67 SessionRemote remote = (SessionRemote) getInitialContext().lookup("SessionBean/remote"); 68 long id = remote.createCustomer(); 69 Customer cust = remote.getCustomer(id); 70 long record = cust.getCustomerRecord().getId(); 71 remote.testInverse(record); 72 cust = remote.getCustomer(id); 73 record = cust.getCustomerRecord().getId(); 74 75 } 76 77 public void testMappedByOneToMany() throws Exception 78 { 79 SessionRemote remote = (SessionRemote) getInitialContext().lookup("SessionBean/remote"); 80 long id1 = remote.createCustomer(); 81 Customer cust1 = remote.getCustomer(id1); 82 long orderId = remote.creatOrder(); 83 Order order = remote.getOrder(orderId); 84 order.setCustomer(cust1); 85 remote.merge(order); 86 orderId = remote.creatOrder(); 87 order = remote.getOrder(orderId); 88 order.setCustomer(cust1); 89 remote.merge(order); 90 cust1 = remote.getCustomer(id1); 91 assertEquals(cust1.getOrders().size(), 2); 92 for (Order orderIn : cust1.getOrders()) 93 { 94 orderIn.setCustomer(null); 95 remote.merge(orderIn); 96 break; 97 } 98 cust1 = remote.getCustomer(id1); 99 assertEquals(cust1.getOrders().size(), 1); 100 101 102 103 } 104 105 public void testRemove() throws Exception 106 { 107 108 SessionRemote remote = (SessionRemote) getInitialContext().lookup("SessionBean/remote"); 109 long id = remote.creatOrder(); 110 Order order = remote.getOrder(id); 111 assertEquals(order.getItems().size(), 2); 112 remote.deleteFromCollection(id); 113 order = remote.getOrder(id); 114 assertEquals(order.getItems().size(), 1); 115 for (LineItem item : order.getItems()) 116 { 117 remote.deleteOne(item.getId()); 118 break; 119 } 120 order = remote.getOrder(id); 121 assertEquals(order.getItems().size(), 0); 122 } 123 124 public void testMergeRemove() throws Exception 125 { 126 127 SessionRemote remote = (SessionRemote) getInitialContext().lookup("SessionBean/remote"); 128 long id = remote.creatOrder(); 129 Order order = remote.getOrder(id); 130 Collection <LineItem> items = order.getItems(); 131 Iterator <LineItem> it = items.iterator(); 132 while (it.hasNext()) 133 { 134 LineItem item = it.next(); 135 item.setOrder(null); 136 break; 137 } 138 remote.merge(order); 139 order = remote.getOrder(id); 140 assertEquals(order.getItems().size(), 1); 141 } 142 143 public static Test suite() throws Exception 144 { 145 return getDeploySetup(RelationshipUnitTestCase.class, "relationships-test.jar"); 146 } 147 148 } 149 | Popular Tags |