1 22 package org.jboss.test.cmp2.relationship.oneToManyUnidirectional; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.naming.InitialContext ; 27 import junit.framework.Test; 28 import junit.framework.TestCase; 29 import net.sourceforge.junitejb.EJBTestCase; 30 import org.jboss.test.JBossTestCase; 31 32 public class ABTest extends EJBTestCase { 33 static org.jboss.logging.Logger log = 34 org.jboss.logging.Logger.getLogger(ABTest.class); 35 36 public static Test suite() throws Exception { 37 return JBossTestCase.getDeploySetup(ABTest.class, "cmp2-relationship.jar"); 38 } 39 40 public ABTest(String name) { 41 super(name); 42 } 43 44 private AHome getTableAHome() { 45 try { 46 InitialContext jndiContext = new InitialContext (); 47 48 return (AHome) jndiContext.lookup("relation/oneToMany/unidirectional/table/A"); 49 } catch(Exception e) { 50 log.debug("failed", e); 51 fail("Exception in getTableAHome: " + e.getMessage()); 52 } 53 return null; 54 } 55 56 private BHome getTableBHome() { 57 try { 58 InitialContext jndiContext = new InitialContext (); 59 60 return (BHome) jndiContext.lookup("relation/oneToMany/unidirectional/table/B"); 61 } catch(Exception e) { 62 log.debug("failed", e); 63 fail("Exception in getTableBHome: " + e.getMessage()); 64 } 65 return null; 66 } 67 68 private AHome getFKAHome() { 69 try { 70 InitialContext jndiContext = new InitialContext (); 71 72 return (AHome) jndiContext.lookup("relation/oneToMany/unidirectional/fk/A"); 73 } catch(Exception e) { 74 log.debug("failed", e); 75 fail("Exception in getFKAHome: " + e.getMessage()); 76 } 77 return null; 78 } 79 80 private BHome getFKBHome() { 81 try { 82 InitialContext jndiContext = new InitialContext (); 83 84 return (BHome) jndiContext.lookup("relation/oneToMany/unidirectional/fk/B"); 85 } catch(Exception e) { 86 log.debug("failed", e); 87 fail("Exception in getFKBHome: " + e.getMessage()); 88 } 89 return null; 90 } 91 92 public void Xtest_a1SetB_a2GetB_Table() throws Exception { 94 AHome aHome = getTableAHome(); 95 BHome bHome = getTableBHome(); 96 a1SetB_a2GetB(aHome, bHome); 97 } 98 99 public void test_a1SetB_a2GetB_FK() throws Exception { 101 AHome aHome = getFKAHome(); 102 BHome bHome = getFKBHome(); 103 a1SetB_a2GetB(aHome, bHome); 104 } 105 106 private void a1SetB_a2GetB(AHome aHome, BHome bHome) throws Exception { 108 A a1 = aHome.create(new Integer (1)); 110 A a2 = aHome.create(new Integer (2)); 111 112 Collection b1 = a1.getB(); 113 a1.getId(); 114 assertTrue(b1.isEmpty()); 115 116 Collection b2 = a2.getB(); 117 a2.getId(); 118 assertTrue(b2.isEmpty()); 119 120 B[] b1x = new B[20]; 121 B[] b2x = new B[30]; 122 123 for(int i=0; i<b1x.length; i++) { 124 b1x[i] = bHome.create(new Integer (10000 + i)); 125 b1.add(b1x[i]); 126 } 127 128 for(int i=0; i<b2x.length; i++) { 129 b2x[i] = bHome.create(new Integer (20000 + i)); 130 b2.add(b2x[i]); 131 } 132 133 for(int i=0; i<b1x.length; i++) { 135 assertTrue(b1.contains(b1x[i])); 136 } 137 138 for(int i=0; i<b2x.length; i++) { 140 assertTrue(b2.contains(b2x[i])); 141 } 142 143 a1.setB(a2.getB()); 145 146 148 assertTrue(a2.getB().isEmpty()); 150 151 assertTrue(b2.isEmpty()); 153 154 assertTrue(b1 == a1.getB()); 156 157 assertTrue(b2 == a2.getB()); 159 160 for(int i=0; i<b2x.length; i++) { 165 assertTrue(a1.getB().contains(b2x[i])); 166 } 167 } 168 169 public void Xtest_a1GetB_addB2m_Table() throws Exception { 171 AHome aHome = getTableAHome(); 172 BHome bHome = getTableBHome(); 173 a1GetB_addB2m(aHome, bHome); 174 } 175 176 public void Xtest_a1GetB_addB2m_FK() throws Exception { 178 AHome aHome = getFKAHome(); 179 BHome bHome = getFKBHome(); 180 a1GetB_addB2m(aHome, bHome); 181 } 182 183 private void a1GetB_addB2m(AHome aHome, BHome bHome) throws Exception { 185 A a1 = aHome.create(new Integer (1)); 187 A a2 = aHome.create(new Integer (2)); 188 189 Collection b1 = a1.getB(); 190 Collection b2 = a2.getB(); 191 192 B[] b1x = new B[20]; 193 B[] b2x = new B[30]; 194 195 for(int i=0; i<b1x.length; i++) { 196 b1x[i] = bHome.create(new Integer (10000 + i)); 197 b1.add(b1x[i]); 198 } 199 200 for(int i=0; i<b2x.length; i++) { 201 b2x[i] = bHome.create(new Integer (20000 + i)); 202 b2.add(b2x[i]); 203 } 204 205 for(int i=0; i<b1x.length; i++) { 207 assertTrue(b1.contains(b1x[i])); 208 } 209 210 for(int i=0; i<b2x.length; i++) { 212 assertTrue(b2.contains(b2x[i])); 213 } 214 215 217 a1.getB().add(b2x[b2x.length-1]); 219 220 222 for(int i=0; i<b1x.length; i++) { 227 assertTrue(b1.contains(b1x[i])); 228 } 229 230 assertTrue(b1.contains(b2x[b2x.length-1])); 232 233 for(int i=0; i<b2x.length-1; i++) { 238 assertTrue(b2.contains(b2x[i])); 239 } 240 } 241 242 public void Xtest_a1GetB_removeB1n_Table() throws Exception { 244 AHome aHome = getTableAHome(); 245 BHome bHome = getTableBHome(); 246 a1GetB_removeB1n(aHome, bHome); 247 } 248 249 public void Xtest_a1GetB_removeB1n_FK() throws Exception { 251 AHome aHome = getFKAHome(); 252 BHome bHome = getFKBHome(); 253 a1GetB_removeB1n(aHome, bHome); 254 } 255 256 private void a1GetB_removeB1n(AHome aHome, BHome bHome) throws Exception { 258 A a1 = aHome.create(new Integer (1)); 260 A a2 = aHome.create(new Integer (2)); 261 262 Collection b1 = a1.getB(); 263 Collection b2 = a2.getB(); 264 265 B[] b1x = new B[20]; 266 B[] b2x = new B[30]; 267 268 for(int i=0; i<b1x.length; i++) { 269 b1x[i] = bHome.create(new Integer (10000 + i)); 270 b1.add(b1x[i]); 271 } 272 273 for(int i=0; i<b2x.length; i++) { 274 b2x[i] = bHome.create(new Integer (20000 + i)); 275 b2.add(b2x[i]); 276 } 277 278 for(int i=0; i<b1x.length; i++) { 280 assertTrue(b1.contains(b1x[i])); 281 } 282 283 for(int i=0; i<b2x.length; i++) { 285 assertTrue(b2.contains(b2x[i])); 286 } 287 288 290 a1.getB().remove(b1x[b1x.length-1]); 292 293 295 assertTrue(b1 == a1.getB()); 297 298 for(int i=0; i<b1x.length-1; i++) { 303 assertTrue(b1.contains(b1x[i])); 304 } 305 306 assertTrue(!(b1.contains(b1x[b1x.length-1]))); 308 } 309 310 public void setUpEJB() throws Exception { 311 AHome aHome; 312 BHome bHome; 313 314 aHome = getTableAHome(); 315 bHome = getTableBHome(); 316 deleteAllAsAndBs(aHome, bHome); 317 318 aHome = getFKAHome(); 319 bHome = getFKBHome(); 320 deleteAllAsAndBs(aHome, bHome); 321 } 322 323 public void tearDownEJB() throws Exception { 324 } 325 326 public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception { 327 Iterator currentAs = aHome.findAll().iterator(); 329 while(currentAs.hasNext()) { 330 A a = (A)currentAs.next(); 331 a.remove(); 332 } 333 334 Iterator currentBs = bHome.findAll().iterator(); 336 while(currentBs.hasNext()) { 337 B b = (B)currentBs.next(); 338 b.remove(); 339 } 340 } 341 } 342 343 344 345 | Popular Tags |