KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > test > UserTransactionLookupTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cts.test;
23
24 import java.util.Properties JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import junit.framework.Test;
30
31 import org.jboss.test.JBossTestCase;
32 import org.jboss.test.cts.interfaces.CtsCmpHome;
33 import org.jboss.test.cts.interfaces.CtsCmp;
34 import org.jboss.test.cts.keys.AccountPK;
35
36 /** Tests of accessing the UserTransaction interface.
37  *
38  * @author Scott.Stark@jboss.org
39  * @version $Revision: 37406 $
40  */

41 public class UserTransactionLookupTestCase extends JBossTestCase
42 {
43
44    public UserTransactionLookupTestCase(String JavaDoc name)
45    {
46       super(name);
47    }
48
49    /**
50     * Test that one can obtain the UserTransaction when a non-default
51     * InitialContext is needed.
52     *
53     * See jira issue JBAS-1270
54     * @throws Exception
55     */

56    public void testWithInvalidDefaultJndiContext() throws Exception JavaDoc
57    {
58       System.setProperty(Context.PROVIDER_URL, "localhost:65535");
59       Properties JavaDoc env = new Properties JavaDoc(System.getProperties());
60       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
61       env.setProperty(Context.PROVIDER_URL, "localhost:1099");
62       env.setProperty("jnp.disableDiscovery", "true");
63       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
64       UserTransaction JavaDoc ut = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
65       ut.getStatus();
66
67       ut.begin();
68       CtsCmpHome home = (CtsCmpHome) ctx.lookup("ejbcts/CMPBean");
69       AccountPK pk = new AccountPK("testWithInvalidDefaultJndiContext");
70       CtsCmp bean = home.create(pk, "Scott");
71       bean.setPersonsAge(40);
72       ut.commit();
73
74       assertTrue("age == 40", bean.getPersonsAge() == 40);
75
76       ut.begin();
77       bean.setPersonsAge(100);
78       ut.rollback();
79       assertTrue("age == 40", bean.getPersonsAge() == 40);
80
81       bean.remove();
82       ctx.close();
83    }
84
85    public void testWithDefaultJndiContext() throws Exception JavaDoc
86    {
87       log.info("+++ testWithDefaultJndiContext");
88       System.setProperty(Context.PROVIDER_URL, "localhost:1099");
89       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
90       UserTransaction JavaDoc ut = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
91       ut.getStatus();
92
93       ut.begin();
94       CtsCmpHome home = (CtsCmpHome) ctx.lookup("ejbcts/CMPBean");
95       AccountPK pk = new AccountPK("testWithDefaultJndiContext");
96       CtsCmp bean = home.create(pk, "Scott");
97       bean.setPersonsAge(40);
98       ut.commit();
99
100       assertTrue("age == 40", bean.getPersonsAge() == 40);
101
102       ut.begin();
103       bean.setPersonsAge(100);
104       ut.rollback();
105       assertTrue("age == 40", bean.getPersonsAge() == 40);
106
107       bean.remove();
108       ctx.close();
109    }
110
111     public static Test suite() throws Exception JavaDoc
112    {
113         return getDeploySetup(UserTransactionLookupTestCase.class, "cts.jar");
114    }
115
116 }
117
Popular Tags