KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > bean > MBeanUnitTestCase


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.cache.bean;
23
24 import junit.framework.Test;
25 import org.jboss.test.JBossTestCase;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.rmi.PortableRemoteObject JavaDoc;
30 import javax.transaction.UserTransaction JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 /**
34  * Tests transactional access to a local TreeCache MBean service.
35  *
36  * @version $Revision: 37406 $
37  */

38 public class MBeanUnitTestCase extends JBossTestCase
39 {
40    TreeCacheMBeanTesterHome cache_home;
41    TreeCacheMBeanTester cache1 = null, cache2 = null;
42    Properties JavaDoc p_ = new Properties JavaDoc();
43
44
45    public MBeanUnitTestCase(String JavaDoc name)
46    {
47       super(name);
48    }
49
50    public void setUp() throws Exception JavaDoc
51    {
52       super.setUp();
53       mySetup();
54    }
55
56    public void tearDown() throws Exception JavaDoc
57    {
58       super.tearDown();
59       if (cache2 != null)
60          cache2.remove(); // calls stop()
61
if (cache1 != null)
62          cache1.remove();
63    }
64
65    public void mySetup() throws Exception JavaDoc
66    {
67       Object JavaDoc obj;
68
69       p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
70       p_.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
71       p_.put(Context.PROVIDER_URL, "localhost:1099");
72       obj = new InitialContext JavaDoc(p_).lookup(TreeCacheMBeanTesterHome.JNDI_NAME);
73       cache_home = (TreeCacheMBeanTesterHome) PortableRemoteObject.narrow(obj, TreeCacheMBeanTesterHome.class);
74    }
75
76
77    public void testSetup()
78    {
79       assertNotNull("TreeCacheMBeanTesterHome ", cache_home);
80    }
81
82    public void testPutTx()
83    {
84       UserTransaction JavaDoc tx = null;
85
86       try {
87          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
88          assertNotNull("UserTransaction should not be null ", tx);
89          // Note that to set tree cache properties, you can do it here
90
// or go to transient-cache-service.xml.
91
cache1 = cache_home.create();
92
93          tx.begin();
94          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
95          assertEquals(new Integer JavaDoc(38), cache1.get("/a/b/c", "age"));
96
97          cache1.put("/a/b/c", "age", new Integer JavaDoc(39));
98          tx.commit();
99
100          tx.begin();
101          assertEquals(new Integer JavaDoc(39), cache1.get("/a/b/c", "age"));
102          tx.commit();
103
104          // Need to do cleanup ...
105
tx.begin();
106          cache1.remove("/a/b/c");
107          cache1.remove("/a/b");
108          cache1.remove("/a");
109          tx.commit();
110
111       } catch (Throwable JavaDoc t) {
112          fail(t.toString());
113          try {
114             tx.rollback();
115          } catch (Throwable JavaDoc t2) {
116             ;
117          }
118          fail(t.toString());
119       }
120    }
121
122
123    public void testRollbackTx()
124    {
125       UserTransaction JavaDoc tx = null;
126
127       try {
128          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
129          // Note that to set tree cache properties, you can do it here
130
// or go to transient-cache-service.xml.
131
assertNotNull("UserTransaction should not be null ", tx);
132          cache1 = cache_home.create();
133          // cache1.setLocking(true);
134

135          tx.begin();
136          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
137          cache1.put("/a/b/c", "age", new Integer JavaDoc(39));
138          tx.rollback();
139
140          tx.begin();
141          Integer JavaDoc val = (Integer JavaDoc) cache1.get("/a/b/c", "age");
142          tx.commit();
143          assertNull(val);
144       } catch (Throwable JavaDoc t) {
145 //t.printStackTrace();
146
fail(t.toString());
147          try {
148             tx.rollback();
149          } catch (Throwable JavaDoc t2) {
150             ;
151          }
152          fail(t.toString());
153       }
154    }
155
156    public void testReplicatedPutTx()
157    {
158       UserTransaction JavaDoc tx = null;
159
160       try {
161          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
162          assertNotNull("UserTransaction should not be null ", tx);
163          // Note that to set tree cache properties, you can do it here
164
// or go to transient-cache-service.xml.
165
cache1 = cache_home.create();
166          cache2 = cache_home.create();
167
168          tx.begin();
169          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
170          assertEquals(new Integer JavaDoc(38), cache1.get("/a/b/c", "age"));
171
172          cache1.put("/a/b/c", "age", new Integer JavaDoc(39));
173          tx.commit();
174
175          tx.begin();
176          assertEquals(new Integer JavaDoc(39), cache2.get("/a/b/c", "age"));
177          tx.commit();
178
179          // Need to do cleanup ...
180
tx.begin();
181          cache1.remove("/a/b/c");
182          cache1.remove("/a/b");
183          cache1.remove("/a");
184          tx.commit();
185
186       } catch (Throwable JavaDoc t) {
187          fail(t.toString());
188          try {
189             tx.rollback();
190          } catch (Throwable JavaDoc t2) {
191             ;
192          }
193          fail(t.toString());
194       }
195    }
196
197    void log(String JavaDoc msg)
198    {
199       getLog().info("-- [" + Thread.currentThread() + "]: " + msg);
200    }
201
202
203    public static Test suite() throws Exception JavaDoc
204    {
205 // return getDeploySetup(MBeanUnitTestCase.class, "cachetest.sar");
206
// Deploy the package recursively. The jar contains ejb and the sar file contains
207
// tree cache MBean service
208
return getDeploySetup(getDeploySetup(MBeanUnitTestCase.class, "cachetest.jar"),
209             "cachetest.sar");
210 // return new TestSuite(MBeanUnitTestCase.class);
211
}
212
213    public static void main(String JavaDoc[] args) throws Exception JavaDoc
214    {
215       junit.textui.TestRunner.run(suite());
216    }
217
218
219 }
220
Popular Tags