1 22 package org.jboss.test.cmp2.relationship.manyToOneUnidirectional; 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 return (AHome) jndiContext.lookup("relation/manyToOne/unidirectional/table/A"); 48 } catch(Exception e) { 49 log.debug("failed", e); 50 fail("Exception in getTableAHome: " + e.getMessage()); 51 } 52 return null; 53 } 54 55 private BHome getTableBHome() { 56 try { 57 InitialContext jndiContext = new InitialContext (); 58 59 return (BHome) jndiContext.lookup("relation/manyToOne/unidirectional/table/B"); 60 } catch(Exception e) { 61 log.debug("failed", e); 62 fail("Exception in getTableBHome: " + e.getMessage()); 63 } 64 return null; 65 } 66 67 private AHome getFKAHome() { 68 try { 69 InitialContext jndiContext = new InitialContext (); 70 71 return (AHome) jndiContext.lookup("relation/manyToOne/unidirectional/fk/A"); 72 } catch(Exception e) { 73 log.debug("failed", e); 74 fail("Exception in getFKAHome: " + e.getMessage()); 75 } 76 return null; 77 } 78 79 private BHome getFKBHome() { 80 try { 81 InitialContext jndiContext = new InitialContext (); 82 83 return (BHome) jndiContext.lookup("relation/manyToOne/unidirectional/fk/B"); 84 } catch(Exception e) { 85 log.debug("failed", e); 86 fail("Exception in getFKBHome: " + e.getMessage()); 87 } 88 return null; 89 } 90 91 public void test_b1jSetA_b2kGetA_Table() throws Exception { 93 AHome aHome = getTableAHome(); 94 BHome bHome = getTableBHome(); 95 b1jSetA_b2kGetA(aHome, bHome); 96 } 97 98 public void test_b1jSetA_b2kGetA_FK() throws Exception { 100 AHome aHome = getFKAHome(); 101 BHome bHome = getFKBHome(); 102 b1jSetA_b2kGetA(aHome, bHome); 103 } 104 105 private void b1jSetA_b2kGetA(AHome aHome, BHome bHome) throws Exception { 107 A a1 = aHome.create(new Integer (1)); 109 A a2 = aHome.create(new Integer (2)); 110 111 B[] b1x = new B[20]; 112 B[] b2x = new B[30]; 113 114 for(int i=0; i<b1x.length; i++) { 115 b1x[i] = bHome.create(new Integer (10000 + i)); 116 b1x[i].setA(a1); 117 } 118 119 for(int i=0; i<b2x.length; i++) { 120 b2x[i] = bHome.create(new Integer (20000 + i)); 121 b2x[i].setA(a2); 122 } 123 124 for(int i=0; i<b1x.length; i++) { 126 a1.isIdentical(b1x[i].getA()); 127 } 128 129 for(int i=0; i<b2x.length; i++) { 131 a2.isIdentical(b2x[i].getA()); 132 } 133 134 136 int j = b1x.length / 3; 138 int k = b2x.length / 2; 139 b1x[j].setA(b2x[k].getA()); 140 141 143 for(int i=0; i<b1x.length; i++) { 150 if(i != j) { 151 assertTrue(a1.isIdentical(b1x[i].getA())); 152 } else { 153 assertTrue(a2.isIdentical(b1x[i].getA())); 154 } 155 } 156 157 for(int i=0; i<b2x.length; i++) { 164 assertTrue(a2.isIdentical(b2x[i].getA())); 165 } 166 } 167 168 public void setUpEJB() throws Exception { 169 AHome aHome; 170 BHome bHome; 171 172 aHome = getTableAHome(); 173 bHome = getTableBHome(); 174 deleteAllAsAndBs(aHome, bHome); 175 176 aHome = getFKAHome(); 177 bHome = getFKBHome(); 178 deleteAllAsAndBs(aHome, bHome); 179 } 180 181 public void tearDownEJB() throws Exception { 182 } 183 184 public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception { 185 Iterator currentAs = aHome.findAll().iterator(); 187 while(currentAs.hasNext()) { 188 A a = (A)currentAs.next(); 189 a.remove(); 190 } 191 192 Iterator currentBs = bHome.findAll().iterator(); 194 while(currentBs.hasNext()) { 195 B b = (B)currentBs.next(); 196 b.remove(); 197 } 198 } 199 } 200 201 202 203 | Popular Tags |