1 22 package org.jboss.test.cmp2.relationship.oneToOneUnidirectional; 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/unidirectional/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/unidirectional/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/unidirectional/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/unidirectional/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 144 public void setUpEJB() throws Exception { 145 AHome aHome; 146 BHome bHome; 147 148 aHome = getTableAHome(); 149 bHome = getTableBHome(); 150 deleteAllAsAndBs(aHome, bHome); 151 152 aHome = getFKAHome(); 153 bHome = getFKBHome(); 154 deleteAllAsAndBs(aHome, bHome); 155 } 156 157 public void tearDownEJB() throws Exception { 158 } 159 160 public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception { 161 Iterator currentAs = aHome.findAll().iterator(); 163 while(currentAs.hasNext()) { 164 A a = (A)currentAs.next(); 165 a.remove(); 166 } 167 168 Iterator currentBs = bHome.findAll().iterator(); 170 while(currentBs.hasNext()) { 171 B b = (B)currentBs.next(); 172 b.remove(); 173 } 174 } 175 } 176 177 178 179 | Popular Tags |