1 22 package org.jboss.test.cmp2.commerce; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.naming.InitialContext ; 27 import junit.framework.TestCase; 28 import net.sourceforge.junitejb.EJBTestCase; 29 30 public class ManyToManyBiTest extends EJBTestCase { 31 32 public ManyToManyBiTest(String name) { 33 super(name); 34 } 35 36 private ProductHome getProductHome() { 37 try { 38 InitialContext jndiContext = new InitialContext (); 39 40 return (ProductHome) jndiContext.lookup("commerce/Product"); 41 } catch(Exception e) { 42 e.printStackTrace(); 43 fail("Exception in getProduct: " + e.getMessage()); 44 } 45 return null; 46 } 47 48 private ProductCategoryHome getProductCategoryHome() { 49 try { 50 InitialContext jndiContext = new InitialContext (); 51 52 return (ProductCategoryHome) 53 jndiContext.lookup("commerce/ProductCategory"); 54 } catch(Exception e) { 55 e.printStackTrace(); 56 fail("Exception in getProductCategory: " + e.getMessage()); 57 } 58 return null; 59 } 60 61 private Product a1; 62 private Product a2; 63 private Product a3; 64 private Product a4; 65 private Product a5; 66 67 private ProductCategory b1; 68 private ProductCategory b2; 69 private ProductCategory b3; 70 private ProductCategory b4; 71 private ProductCategory b5; 72 73 public void setUpEJB() throws Exception { 74 ProductHome productHome = getProductHome(); 75 ProductCategoryHome productCategoryHome = getProductCategoryHome(); 76 77 deleteAllProducts(productHome); 79 deleteAllProductCategories(productCategoryHome); 80 81 beforeChange(productHome, productCategoryHome); 83 } 84 85 private void beforeChange( 86 ProductHome productHome, 87 ProductCategoryHome productCategoryHome) throws Exception { 88 89 a1 = productHome.create(); 91 a2 = productHome.create(); 92 a3 = productHome.create(); 93 a4 = productHome.create(); 94 a5 = productHome.create(); 95 96 b1 = productCategoryHome.create(); 97 b2 = productCategoryHome.create(); 98 b3 = productCategoryHome.create(); 99 b4 = productCategoryHome.create(); 100 b5 = productCategoryHome.create(); 101 102 a1.getProductCategories().add(b1); 103 a1.getProductCategories().add(b2); 104 a2.getProductCategories().add(b1); 105 a2.getProductCategories().add(b2); 106 a2.getProductCategories().add(b3); 107 a3.getProductCategories().add(b2); 108 a3.getProductCategories().add(b3); 109 a3.getProductCategories().add(b4); 110 a4.getProductCategories().add(b3); 111 a4.getProductCategories().add(b4); 112 a4.getProductCategories().add(b5); 113 a5.getProductCategories().add(b4); 114 a5.getProductCategories().add(b5); 115 116 assertTrue(a1.getProductCategories().contains(b1)); 117 assertTrue(a1.getProductCategories().contains(b2)); 118 assertTrue(a2.getProductCategories().contains(b1)); 119 assertTrue(a2.getProductCategories().contains(b2)); 120 assertTrue(a2.getProductCategories().contains(b3)); 121 assertTrue(a3.getProductCategories().contains(b2)); 122 assertTrue(a3.getProductCategories().contains(b3)); 123 assertTrue(a3.getProductCategories().contains(b4)); 124 assertTrue(a4.getProductCategories().contains(b3)); 125 assertTrue(a4.getProductCategories().contains(b4)); 126 assertTrue(a4.getProductCategories().contains(b5)); 127 assertTrue(a5.getProductCategories().contains(b4)); 128 assertTrue(a5.getProductCategories().contains(b5)); 129 130 assertTrue(b1.getProducts().contains(a1)); 131 assertTrue(b1.getProducts().contains(a2)); 132 assertTrue(b2.getProducts().contains(a1)); 133 assertTrue(b2.getProducts().contains(a2)); 134 assertTrue(b2.getProducts().contains(a3)); 135 assertTrue(b3.getProducts().contains(a2)); 136 assertTrue(b3.getProducts().contains(a3)); 137 assertTrue(b3.getProducts().contains(a4)); 138 assertTrue(b4.getProducts().contains(a3)); 139 assertTrue(b4.getProducts().contains(a4)); 140 assertTrue(b4.getProducts().contains(a5)); 141 assertTrue(b5.getProducts().contains(a4)); 142 assertTrue(b5.getProducts().contains(a5)); 143 } 144 145 146 public void test_a1SetB_a3GetB() { 148 a1.setProductCategories(a3.getProductCategories()); 150 151 assertTrue(!a1.getProductCategories().contains(b1)); 153 assertTrue(a1.getProductCategories().contains(b2)); 154 assertTrue(a1.getProductCategories().contains(b3)); 155 assertTrue(a1.getProductCategories().contains(b4)); 156 157 assertTrue(a2.getProductCategories().contains(b1)); 158 assertTrue(a2.getProductCategories().contains(b2)); 159 assertTrue(a2.getProductCategories().contains(b3)); 160 161 assertTrue(a3.getProductCategories().contains(b2)); 162 assertTrue(a3.getProductCategories().contains(b3)); 163 assertTrue(a3.getProductCategories().contains(b4)); 164 165 assertTrue(a4.getProductCategories().contains(b3)); 166 assertTrue(a4.getProductCategories().contains(b4)); 167 assertTrue(a4.getProductCategories().contains(b5)); 168 169 assertTrue(a5.getProductCategories().contains(b4)); 170 assertTrue(a5.getProductCategories().contains(b5)); 171 172 173 assertTrue(!b1.getProducts().contains(a1)); 174 assertTrue(b1.getProducts().contains(a2)); 175 176 assertTrue(b2.getProducts().contains(a1)); 177 assertTrue(b2.getProducts().contains(a2)); 178 assertTrue(b2.getProducts().contains(a3)); 179 180 assertTrue(b3.getProducts().contains(a1)); 181 assertTrue(b3.getProducts().contains(a2)); 182 assertTrue(b3.getProducts().contains(a3)); 183 assertTrue(b3.getProducts().contains(a4)); 184 185 assertTrue(b4.getProducts().contains(a1)); 186 assertTrue(b4.getProducts().contains(a3)); 187 assertTrue(b4.getProducts().contains(a4)); 188 assertTrue(b4.getProducts().contains(a5)); 189 190 assertTrue(b5.getProducts().contains(a4)); 191 assertTrue(b5.getProducts().contains(a5)); 192 } 193 194 public void test_a1GetB_addB3() { 196 a1.getProductCategories().add(b3); 198 199 assertTrue(a1.getProductCategories().contains(b1)); 201 assertTrue(a1.getProductCategories().contains(b2)); 202 assertTrue(a1.getProductCategories().contains(b3)); 203 204 assertTrue(a2.getProductCategories().contains(b1)); 205 assertTrue(a2.getProductCategories().contains(b2)); 206 assertTrue(a2.getProductCategories().contains(b3)); 207 208 assertTrue(a3.getProductCategories().contains(b2)); 209 assertTrue(a3.getProductCategories().contains(b3)); 210 assertTrue(a3.getProductCategories().contains(b4)); 211 212 assertTrue(a4.getProductCategories().contains(b3)); 213 assertTrue(a4.getProductCategories().contains(b4)); 214 assertTrue(a4.getProductCategories().contains(b5)); 215 216 assertTrue(a5.getProductCategories().contains(b4)); 217 assertTrue(a5.getProductCategories().contains(b5)); 218 219 220 assertTrue(b1.getProducts().contains(a1)); 221 assertTrue(b1.getProducts().contains(a2)); 222 223 assertTrue(b2.getProducts().contains(a1)); 224 assertTrue(b2.getProducts().contains(a2)); 225 assertTrue(b2.getProducts().contains(a3)); 226 227 assertTrue(b3.getProducts().contains(a1)); 228 assertTrue(b3.getProducts().contains(a2)); 229 assertTrue(b3.getProducts().contains(a3)); 230 assertTrue(b3.getProducts().contains(a4)); 231 232 assertTrue(b4.getProducts().contains(a3)); 233 assertTrue(b4.getProducts().contains(a4)); 234 assertTrue(b4.getProducts().contains(a5)); 235 236 assertTrue(b5.getProducts().contains(a4)); 237 assertTrue(b5.getProducts().contains(a5)); 238 } 239 240 public void test_a2GetB_removeB2() { 242 a2.getProductCategories().remove(b2); 244 245 assertTrue(a1.getProductCategories().contains(b1)); 247 assertTrue(a1.getProductCategories().contains(b2)); 248 249 assertTrue(a2.getProductCategories().contains(b1)); 250 assertTrue(!a2.getProductCategories().contains(b2)); 251 assertTrue(a2.getProductCategories().contains(b3)); 252 253 assertTrue(a3.getProductCategories().contains(b2)); 254 assertTrue(a3.getProductCategories().contains(b3)); 255 assertTrue(a3.getProductCategories().contains(b4)); 256 257 assertTrue(a4.getProductCategories().contains(b3)); 258 assertTrue(a4.getProductCategories().contains(b4)); 259 assertTrue(a4.getProductCategories().contains(b5)); 260 261 assertTrue(a5.getProductCategories().contains(b4)); 262 assertTrue(a5.getProductCategories().contains(b5)); 263 264 265 assertTrue(b1.getProducts().contains(a1)); 266 assertTrue(b1.getProducts().contains(a2)); 267 268 assertTrue(b2.getProducts().contains(a1)); 269 assertTrue(!b2.getProducts().contains(a2)); 270 assertTrue(b2.getProducts().contains(a3)); 271 272 assertTrue(b3.getProducts().contains(a2)); 273 assertTrue(b3.getProducts().contains(a3)); 274 assertTrue(b3.getProducts().contains(a4)); 275 276 assertTrue(b4.getProducts().contains(a3)); 277 assertTrue(b4.getProducts().contains(a4)); 278 assertTrue(b4.getProducts().contains(a5)); 279 280 assertTrue(b5.getProducts().contains(a4)); 281 assertTrue(b5.getProducts().contains(a5)); 282 } 283 284 public void tearDownEJB() throws Exception { 285 } 286 287 public void deleteAllProducts(ProductHome productHome) throws Exception { 288 Iterator currentProducts = productHome.findAll().iterator(); 290 while(currentProducts.hasNext()) { 291 Product p = (Product)currentProducts.next(); 292 p.remove(); 293 } 294 } 295 296 public void deleteAllProductCategories(ProductCategoryHome productCategoryHome) throws Exception { 297 Iterator currentProductCategories = productCategoryHome.findAll().iterator(); 299 while(currentProductCategories.hasNext()) { 300 ProductCategory pc = (ProductCategory)currentProductCategories.next(); 301 pc.remove(); 302 } 303 } 304 305 306 } 307 308 309 310 | Popular Tags |