001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.jbas1361;
023:
024: import javax.naming.InitialContext;
025: import javax.naming.NamingException;
026: import javax.transaction.Transaction;
027: import javax.transaction.TransactionManager;
028: import org.jboss.test.JBossTestCase;
029: import org.jboss.test.util.ejb.EJBTestCase;
030: import junit.framework.Test;
031:
032: /**
033: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
034: * @version <tt>$Revision: 57211 $</tt>
035: */
036: public class JBAS1361UnitTestCase extends EJBTestCase {
037: public static Test suite() throws Exception {
038: return JBossTestCase.getDeploySetup(JBAS1361UnitTestCase.class,
039: "cmp2-jbas1361.jar");
040: }
041:
042: public JBAS1361UnitTestCase(String methodName) {
043: super (methodName);
044: }
045:
046: protected void setUp() throws Exception {
047: // NOTE: setUp, tearDown and tests appear to run in THE SAME transaction!
048: TransactionManager tm = getTM();
049: Transaction oldTx = tm.getTransaction();
050: if (oldTx != null) {
051: tm.suspend();
052: }
053: tm.begin();
054:
055: try {
056: //System.out.println("setUp: " + tm.getTransaction());
057: ALocal a = getALocalHome().create(new Integer(1), "a");
058: BLocalHome bh = getBLocalHome();
059: for (int i = 1; i <= 3; ++i) {
060: bh.create(new Integer(i), "b").setA(a);
061: }
062:
063: tm.commit();
064: } catch (Throwable t) {
065: tm.rollback();
066: }
067:
068: if (oldTx != null) {
069: tm.resume(oldTx);
070: }
071: }
072:
073: protected void tearDown() throws Exception {
074: //System.out.println("tearDown: " + (getTM()).getTransaction());
075: getALocalHome().remove(new Integer(1));
076: BLocalHome bh = getBLocalHome();
077: for (int i = 1; i <= 3; ++i) {
078: bh.remove(new BPK(new Integer(i), "b"));
079: }
080: }
081:
082: public void testJBAS1361() throws Throwable {
083: TransactionManager tm = getTM();
084: Transaction oldTx = tm.getTransaction();
085: if (oldTx != null) {
086: tm.suspend();
087: }
088: tm.begin();
089:
090: try {
091: //System.out.println("test: " + (getTM()).getTransaction());
092:
093: ALocal a = getALocalHome().findByPrimaryKey(new Integer(1));
094:
095: a.getB().clear();
096:
097: BLocalHome bh = getBLocalHome();
098:
099: BPK bpk = new BPK();
100: bpk.name = "b";
101: for (int i = 1; i <= 3; ++i) {
102: bpk.id = new Integer(i);
103: BLocal b = bh.findByPrimaryKey(bpk);
104: assertTrue(a.getB().add(b));
105: }
106:
107: assertEquals(3, a.getB().size());
108:
109: tm.commit();
110: } catch (Throwable t) {
111: tm.rollback();
112: throw t;
113: } finally {
114: if (oldTx != null) {
115: tm.resume(oldTx);
116: }
117: }
118: }
119:
120: private TransactionManager getTM() throws NamingException {
121: return (TransactionManager) lookup("java:/TransactionManager");
122: }
123:
124: private ALocalHome getALocalHome() throws NamingException {
125: return (ALocalHome) lookup(ALocalHome.JNDI_NAME);
126: }
127:
128: private BLocalHome getBLocalHome() throws NamingException {
129: return (BLocalHome) lookup(BLocalHome.JNDI_NAME);
130: }
131:
132: private Object lookup(String name) throws NamingException {
133: InitialContext ic = new InitialContext();
134: return ic.lookup(name);
135: }
136: }
|