1 22 package org.jboss.test.cmp2.relationship.manyToManyUnidirectional; 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 getAHome() { 45 try { 46 InitialContext jndiContext = new InitialContext (); 47 48 return (AHome) jndiContext.lookup("relation/manyToMany/unidirectional/A"); 49 } catch(Exception e) { 50 log.debug("failed", e); 51 fail("Exception in getAHome: " + e.getMessage()); 52 } 53 return null; 54 } 55 56 private BHome getBHome() { 57 try { 58 InitialContext jndiContext = new InitialContext (); 59 60 return (BHome) jndiContext.lookup("relation/manyToMany/unidirectional/B"); 61 } catch(Exception e) { 62 log.debug("failed", e); 63 fail("Exception in getBHome: " + e.getMessage()); 64 } 65 return null; 66 } 67 68 public void test_a1SetB_a3GetB() throws Exception { 70 AHome aHome = getAHome(); 71 BHome bHome = getBHome(); 72 73 A a1 = aHome.create(new Integer (1)); 75 A a2 = aHome.create(new Integer (2)); 76 A a3 = aHome.create(new Integer (3)); 77 A a4 = aHome.create(new Integer (4)); 78 A a5 = aHome.create(new Integer (5)); 79 80 B b1 = bHome.create(new Integer (-1)); 81 B b2 = bHome.create(new Integer (-2)); 82 B b3 = bHome.create(new Integer (-3)); 83 B b4 = bHome.create(new Integer (-4)); 84 B b5 = bHome.create(new Integer (-5)); 85 86 a1.getB().add(b1); 87 a1.getB().add(b2); 88 a2.getB().add(b1); 89 a2.getB().add(b2); 90 a2.getB().add(b3); 91 a3.getB().add(b2); 92 a3.getB().add(b3); 93 a3.getB().add(b4); 94 a4.getB().add(b3); 95 a4.getB().add(b4); 96 a4.getB().add(b5); 97 a5.getB().add(b4); 98 a5.getB().add(b5); 99 100 assertTrue(a1.getB().contains(b1)); 101 assertTrue(a1.getB().contains(b2)); 102 assertTrue(a2.getB().contains(b1)); 103 assertTrue(a2.getB().contains(b2)); 104 assertTrue(a2.getB().contains(b3)); 105 assertTrue(a3.getB().contains(b2)); 106 assertTrue(a3.getB().contains(b3)); 107 assertTrue(a3.getB().contains(b4)); 108 assertTrue(a4.getB().contains(b3)); 109 assertTrue(a4.getB().contains(b4)); 110 assertTrue(a4.getB().contains(b5)); 111 assertTrue(a5.getB().contains(b4)); 112 assertTrue(a5.getB().contains(b5)); 113 114 a1.setB(a3.getB()); 116 117 assertTrue(!a1.getB().contains(b1)); 119 assertTrue(a1.getB().contains(b2)); 120 assertTrue(a1.getB().contains(b3)); 121 assertTrue(a1.getB().contains(b4)); 122 123 assertTrue(a2.getB().contains(b1)); 124 assertTrue(a2.getB().contains(b2)); 125 assertTrue(a2.getB().contains(b3)); 126 127 assertTrue(a3.getB().contains(b2)); 128 assertTrue(a3.getB().contains(b3)); 129 assertTrue(a3.getB().contains(b4)); 130 131 assertTrue(a4.getB().contains(b3)); 132 assertTrue(a4.getB().contains(b4)); 133 assertTrue(a4.getB().contains(b5)); 134 135 assertTrue(a5.getB().contains(b4)); 136 assertTrue(a5.getB().contains(b5)); 137 } 138 139 public void test_a1GetB_addB3() throws Exception { 141 AHome aHome = getAHome(); 142 BHome bHome = getBHome(); 143 144 A a1 = aHome.create(new Integer (1)); 146 A a2 = aHome.create(new Integer (2)); 147 A a3 = aHome.create(new Integer (3)); 148 A a4 = aHome.create(new Integer (4)); 149 A a5 = aHome.create(new Integer (5)); 150 151 B b1 = bHome.create(new Integer (-1)); 152 B b2 = bHome.create(new Integer (-2)); 153 B b3 = bHome.create(new Integer (-3)); 154 B b4 = bHome.create(new Integer (-4)); 155 B b5 = bHome.create(new Integer (-5)); 156 157 a1.getB().add(b1); 158 a1.getB().add(b2); 159 a2.getB().add(b1); 160 a2.getB().add(b2); 161 a2.getB().add(b3); 162 a3.getB().add(b2); 163 a3.getB().add(b3); 164 a3.getB().add(b4); 165 a4.getB().add(b3); 166 a4.getB().add(b4); 167 a4.getB().add(b5); 168 a5.getB().add(b4); 169 a5.getB().add(b5); 170 171 assertTrue(a1.getB().contains(b1)); 172 assertTrue(a1.getB().contains(b2)); 173 assertTrue(a2.getB().contains(b1)); 174 assertTrue(a2.getB().contains(b2)); 175 assertTrue(a2.getB().contains(b3)); 176 assertTrue(a3.getB().contains(b2)); 177 assertTrue(a3.getB().contains(b3)); 178 assertTrue(a3.getB().contains(b4)); 179 assertTrue(a4.getB().contains(b3)); 180 assertTrue(a4.getB().contains(b4)); 181 assertTrue(a4.getB().contains(b5)); 182 assertTrue(a5.getB().contains(b4)); 183 assertTrue(a5.getB().contains(b5)); 184 185 a1.getB().add(b3); 187 188 assertTrue(a1.getB().contains(b1)); 190 assertTrue(a1.getB().contains(b2)); 191 assertTrue(a1.getB().contains(b3)); 192 193 assertTrue(a2.getB().contains(b1)); 194 assertTrue(a2.getB().contains(b2)); 195 assertTrue(a2.getB().contains(b3)); 196 197 assertTrue(a3.getB().contains(b2)); 198 assertTrue(a3.getB().contains(b3)); 199 assertTrue(a3.getB().contains(b4)); 200 201 assertTrue(a4.getB().contains(b3)); 202 assertTrue(a4.getB().contains(b4)); 203 assertTrue(a4.getB().contains(b5)); 204 205 assertTrue(a5.getB().contains(b4)); 206 assertTrue(a5.getB().contains(b5)); 207 } 208 209 public void test_a2GetB_removeB2() throws Exception { 211 AHome aHome = getAHome(); 212 BHome bHome = getBHome(); 213 214 A a1 = aHome.create(new Integer (1)); 216 A a2 = aHome.create(new Integer (2)); 217 A a3 = aHome.create(new Integer (3)); 218 A a4 = aHome.create(new Integer (4)); 219 A a5 = aHome.create(new Integer (5)); 220 221 B b1 = bHome.create(new Integer (-1)); 222 B b2 = bHome.create(new Integer (-2)); 223 B b3 = bHome.create(new Integer (-3)); 224 B b4 = bHome.create(new Integer (-4)); 225 B b5 = bHome.create(new Integer (-5)); 226 227 a1.getB().add(b1); 228 a1.getB().add(b2); 229 a2.getB().add(b1); 230 a2.getB().add(b2); 231 a2.getB().add(b3); 232 a3.getB().add(b2); 233 a3.getB().add(b3); 234 a3.getB().add(b4); 235 a4.getB().add(b3); 236 a4.getB().add(b4); 237 a4.getB().add(b5); 238 a5.getB().add(b4); 239 a5.getB().add(b5); 240 241 assertTrue(a1.getB().contains(b1)); 242 assertTrue(a1.getB().contains(b2)); 243 assertTrue(a2.getB().contains(b1)); 244 assertTrue(a2.getB().contains(b2)); 245 assertTrue(a2.getB().contains(b3)); 246 assertTrue(a3.getB().contains(b2)); 247 assertTrue(a3.getB().contains(b3)); 248 assertTrue(a3.getB().contains(b4)); 249 assertTrue(a4.getB().contains(b3)); 250 assertTrue(a4.getB().contains(b4)); 251 assertTrue(a4.getB().contains(b5)); 252 assertTrue(a5.getB().contains(b4)); 253 assertTrue(a5.getB().contains(b5)); 254 255 a2.getB().remove(b2); 257 258 assertTrue(a1.getB().contains(b1)); 260 assertTrue(a1.getB().contains(b2)); 261 262 assertTrue(a2.getB().contains(b1)); 263 assertTrue(!a2.getB().contains(b2)); 264 assertTrue(a2.getB().contains(b3)); 265 266 assertTrue(a3.getB().contains(b2)); 267 assertTrue(a3.getB().contains(b3)); 268 assertTrue(a3.getB().contains(b4)); 269 270 assertTrue(a4.getB().contains(b3)); 271 assertTrue(a4.getB().contains(b4)); 272 assertTrue(a4.getB().contains(b5)); 273 274 assertTrue(a5.getB().contains(b4)); 275 assertTrue(a5.getB().contains(b5)); 276 } 277 278 public void setUpEJB() throws Exception { 279 deleteAllAsAndBs(getAHome(), getBHome()); 280 } 281 282 public void tearDownEJB() throws Exception { 283 } 284 285 public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception { 286 Iterator currentAs = aHome.findAll().iterator(); 288 while(currentAs.hasNext()) { 289 A a = (A)currentAs.next(); 290 a.remove(); 291 } 292 293 Iterator currentBs = bHome.findAll().iterator(); 295 while(currentBs.hasNext()) { 296 B b = (B)currentBs.next(); 297 b.remove(); 298 } 299 } 300 } 301 302 303 304 | Popular Tags |