1 22 package org.jboss.test.cmp2.relationship.oneToOneBidirectional; 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) 49 jndiContext.lookup("relation/oneToOne/bidirectional/table/A"); 50 } catch(Exception e) { 51 log.debug("failed", e); 52 fail("Exception in getTableAHome: " + e.getMessage()); 53 } 54 return null; 55 } 56 57 private BHome getTableBHome() { 58 try { 59 InitialContext jndiContext = new InitialContext (); 60 61 return (BHome) 62 jndiContext.lookup("relation/oneToOne/bidirectional/table/B"); 63 } catch(Exception e) { 64 log.debug("failed", e); 65 fail("Exception in getTableBHome: " + e.getMessage()); 66 } 67 return null; 68 } 69 70 private AHome getFKAHome() { 71 try { 72 InitialContext jndiContext = new InitialContext (); 73 74 return (AHome) 75 jndiContext.lookup("relation/oneToOne/bidirectional/fk/A"); 76 } catch(Exception e) { 77 log.debug("failed", e); 78 fail("Exception in getFKAHome: " + e.getMessage()); 79 } 80 return null; 81 } 82 83 private BHome getFKBHome() { 84 try { 85 InitialContext jndiContext = new InitialContext (); 86 87 return (BHome) 88 jndiContext.lookup("relation/oneToOne/bidirectional/fk/B"); 89 } catch(Exception e) { 90 log.debug("failed", e); 91 fail("Exception in getFKBHome: " + e.getMessage()); 92 } 93 return null; 94 } 95 96 private A a1; 97 private A a2; 98 private B b1; 99 private B b2; 100 101 protected void beforeChange(AHome aHome, BHome bHome) throws Exception { 102 a1 = aHome.create(new Integer (1)); 103 a2 = aHome.create(new Integer (2)); 104 b1 = bHome.create(new Integer (10)); 105 b2 = bHome.create(new Integer (20)); 106 a1.setB(b1); 107 a2.setB(b2); 108 109 assertTrue(b1.isIdentical(a1.getB())); 110 assertTrue(b2.isIdentical(a2.getB())); 111 } 112 113 public void test_a1SetB_a2GetB_Table() throws Exception { 115 AHome aHome = getTableAHome(); 116 BHome bHome = getTableBHome(); 117 118 beforeChange(aHome, bHome); 119 a1SetB_a2GetB(aHome, bHome); 120 } 121 122 public void test_a1SetB_a2GetB_FK() throws Exception { 124 AHome aHome = getFKAHome(); 125 BHome bHome = getFKBHome(); 126 beforeChange(aHome, bHome); 127 a1SetB_a2GetB(aHome, bHome); 128 } 129 130 protected void a1SetB_a2GetB(AHome aHome, BHome bHome) throws Exception { 132 a1.setB(a2.getB()); 134 135 137 assertTrue(b2.isIdentical(a1.getB())); 139 140 assertNull(a2.getB()); 142 143 assertNull(b1.getA()); 145 146 assertTrue(a1.isIdentical(b2.getA())); 148 } 149 150 public void setUpEJB() throws Exception { 151 AHome aHome; 152 BHome bHome; 153 154 aHome = getTableAHome(); 155 bHome = getTableBHome(); 156 deleteAllAsAndBs(aHome, bHome); 157 158 aHome = getFKAHome(); 159 bHome = getFKBHome(); 160 deleteAllAsAndBs(aHome, bHome); 161 } 162 163 public void tearDownEJB() throws Exception { 164 } 165 166 public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception { 167 Iterator currentAs = aHome.findAll().iterator(); 169 while(currentAs.hasNext()) { 170 A a = (A)currentAs.next(); 171 a.remove(); 172 } 173 174 Iterator currentBs = bHome.findAll().iterator(); 176 while(currentBs.hasNext()) { 177 B b = (B)currentBs.next(); 178 b.remove(); 179 } 180 } 181 } 182 183 184 185 | Popular Tags |