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.cts.test;
023:
024: import java.util.Properties;
025: import javax.naming.Context;
026: import javax.naming.InitialContext;
027: import javax.transaction.UserTransaction;
028:
029: import junit.framework.Test;
030:
031: import org.jboss.test.JBossTestCase;
032: import org.jboss.test.cts.interfaces.CtsCmpHome;
033: import org.jboss.test.cts.interfaces.CtsCmp;
034: import org.jboss.test.cts.keys.AccountPK;
035:
036: /** Tests of accessing the UserTransaction interface.
037: *
038: * @author Scott.Stark@jboss.org
039: * @version $Revision: 57211 $
040: */
041: public class UserTransactionLookupTestCase extends JBossTestCase {
042:
043: public UserTransactionLookupTestCase(String name) {
044: super (name);
045: }
046:
047: /**
048: * Test that one can obtain the UserTransaction when a non-default
049: * InitialContext is needed.
050: *
051: * See jira issue JBAS-1270
052: * @throws Exception
053: */
054: public void testWithInvalidDefaultJndiContext() throws Exception {
055: System.setProperty(Context.PROVIDER_URL, getServerHost()
056: + ":65535");
057: Properties env = new Properties(System.getProperties());
058: env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
059: "org.jboss.naming.NamingContextFactory");
060: env
061: .setProperty(Context.PROVIDER_URL, getServerHost()
062: + ":1099");
063: env.setProperty("jnp.disableDiscovery", "true");
064: InitialContext ctx = new InitialContext(env);
065: UserTransaction ut = (UserTransaction) ctx
066: .lookup("UserTransaction");
067: ut.getStatus();
068:
069: ut.begin();
070: CtsCmpHome home = (CtsCmpHome) ctx.lookup("ejbcts/CMPBean");
071: AccountPK pk = new AccountPK(
072: "testWithInvalidDefaultJndiContext");
073: CtsCmp bean = home.create(pk, "Scott");
074: bean.setPersonsAge(40);
075: ut.commit();
076:
077: assertTrue("age == 40", bean.getPersonsAge() == 40);
078:
079: ut.begin();
080: bean.setPersonsAge(100);
081: ut.rollback();
082: assertTrue("age == 40", bean.getPersonsAge() == 40);
083:
084: bean.remove();
085: ctx.close();
086: }
087:
088: public void testWithDefaultJndiContext() throws Exception {
089: log.info("+++ testWithDefaultJndiContext");
090: System.setProperty(Context.PROVIDER_URL, getServerHost()
091: + ":1099");
092: InitialContext ctx = new InitialContext();
093: UserTransaction ut = (UserTransaction) ctx
094: .lookup("UserTransaction");
095: ut.getStatus();
096:
097: ut.begin();
098: CtsCmpHome home = (CtsCmpHome) ctx.lookup("ejbcts/CMPBean");
099: AccountPK pk = new AccountPK("testWithDefaultJndiContext");
100: CtsCmp bean = home.create(pk, "Scott");
101: bean.setPersonsAge(40);
102: ut.commit();
103:
104: assertTrue("age == 40", bean.getPersonsAge() == 40);
105:
106: ut.begin();
107: bean.setPersonsAge(100);
108: ut.rollback();
109: assertTrue("age == 40", bean.getPersonsAge() == 40);
110:
111: bean.remove();
112: ctx.close();
113: }
114:
115: public static Test suite() throws Exception {
116: return getDeploySetup(UserTransactionLookupTestCase.class,
117: "cts.jar");
118: }
119:
120: }
|