1 22 package org.jboss.test.cmp2.jbas1361; 23 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.transaction.Transaction ; 27 import javax.transaction.TransactionManager ; 28 import org.jboss.test.JBossTestCase; 29 import org.jboss.test.util.ejb.EJBTestCase; 30 import junit.framework.Test; 31 32 36 public class JBAS1361UnitTestCase 37 extends EJBTestCase 38 { 39 public static Test suite() throws Exception 40 { 41 return JBossTestCase.getDeploySetup(JBAS1361UnitTestCase.class, "cmp2-jbas1361.jar"); 42 } 43 44 public JBAS1361UnitTestCase(String methodName) 45 { 46 super(methodName); 47 } 48 49 protected void setUp() throws Exception 50 { 51 TransactionManager tm = getTM(); 53 Transaction oldTx = tm.getTransaction(); 54 if(oldTx != null) 55 { 56 tm.suspend(); 57 } 58 tm.begin(); 59 60 try 61 { 62 ALocal a = getALocalHome().create(new Integer (1), "a"); 64 BLocalHome bh = getBLocalHome(); 65 for(int i = 1; i <= 3; ++i) 66 { 67 bh.create(new Integer (i), "b").setA(a); 68 } 69 70 tm.commit(); 71 } 72 catch(Throwable t) 73 { 74 tm.rollback(); 75 } 76 77 if(oldTx != null) 78 { 79 tm.resume(oldTx); 80 } 81 } 82 83 protected void tearDown() throws Exception 84 { 85 getALocalHome().remove(new Integer (1)); 87 BLocalHome bh = getBLocalHome(); 88 for(int i = 1; i <= 3; ++i) 89 { 90 bh.remove(new BPK(new Integer (i), "b")); 91 } 92 } 93 94 public void testJBAS1361() throws Throwable 95 { 96 TransactionManager tm = getTM(); 97 Transaction oldTx = tm.getTransaction(); 98 if(oldTx != null) 99 { 100 tm.suspend(); 101 } 102 tm.begin(); 103 104 try 105 { 106 108 ALocal a = getALocalHome().findByPrimaryKey(new Integer (1)); 109 110 a.getB().clear(); 111 112 BLocalHome bh = getBLocalHome(); 113 114 BPK bpk = new BPK(); 115 bpk.name = "b"; 116 for(int i = 1; i <= 3; ++i) 117 { 118 bpk.id = new Integer (i); 119 BLocal b = bh.findByPrimaryKey(bpk); 120 assertTrue(a.getB().add(b)); 121 } 122 123 assertEquals(3, a.getB().size()); 124 125 tm.commit(); 126 } 127 catch(Throwable t) 128 { 129 tm.rollback(); 130 throw t; 131 } 132 finally 133 { 134 if(oldTx != null) 135 { 136 tm.resume(oldTx); 137 } 138 } 139 } 140 141 private TransactionManager getTM() 142 throws NamingException 143 { 144 return (TransactionManager )lookup("java:/TransactionManager"); 145 } 146 147 private ALocalHome getALocalHome() 148 throws NamingException 149 { 150 return (ALocalHome)lookup(ALocalHome.JNDI_NAME); 151 } 152 153 private BLocalHome getBLocalHome() 154 throws NamingException 155 { 156 return (BLocalHome)lookup(BLocalHome.JNDI_NAME); 157 } 158 159 private Object lookup(String name) throws NamingException 160 { 161 InitialContext ic = new InitialContext (); 162 return ic.lookup(name); 163 } 164 } 165 | Popular Tags |