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.cache.bean;
023:
024: import junit.framework.Test;
025: import org.jboss.test.JBossTestCase;
026:
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029: import javax.rmi.PortableRemoteObject;
030: import javax.transaction.UserTransaction;
031: import java.util.Properties;
032:
033: /**
034: * Tests transactional access to a local TreeCache MBean service.
035: *
036: * @version $Revision: 57211 $
037: */
038: public class MBeanUnitTestCase extends JBossTestCase {
039: TreeCacheMBeanTesterHome cache_home;
040: TreeCacheMBeanTester cache1 = null, cache2 = null;
041: Properties p_ = new Properties();
042:
043: public MBeanUnitTestCase(String name) {
044: super (name);
045: }
046:
047: public void setUp() throws Exception {
048: super .setUp();
049: mySetup();
050: }
051:
052: public void tearDown() throws Exception {
053: super .tearDown();
054: if (cache2 != null)
055: cache2.remove(); // calls stop()
056: if (cache1 != null)
057: cache1.remove();
058: }
059:
060: public void mySetup() throws Exception {
061: Object obj;
062:
063: p_.put(Context.INITIAL_CONTEXT_FACTORY,
064: "org.jnp.interfaces.NamingContextFactory");
065: p_.put(Context.URL_PKG_PREFIXES,
066: "jboss.naming:org.jnp.interfaces");
067: p_.put(Context.PROVIDER_URL, getServerHost() + ":1099");
068: obj = new InitialContext(p_)
069: .lookup(TreeCacheMBeanTesterHome.JNDI_NAME);
070: cache_home = (TreeCacheMBeanTesterHome) PortableRemoteObject
071: .narrow(obj, TreeCacheMBeanTesterHome.class);
072: }
073:
074: public void testSetup() {
075: assertNotNull("TreeCacheMBeanTesterHome ", cache_home);
076: }
077:
078: public void testPutTx() {
079: UserTransaction tx = null;
080:
081: try {
082: tx = (UserTransaction) new InitialContext(p_)
083: .lookup("UserTransaction");
084: assertNotNull("UserTransaction should not be null ", tx);
085: // Note that to set tree cache properties, you can do it here
086: // or go to transient-cache-service.xml.
087: cache1 = cache_home.create();
088:
089: tx.begin();
090: cache1.put("/a/b/c", "age", new Integer(38));
091: assertEquals(new Integer(38), cache1.get("/a/b/c", "age"));
092:
093: cache1.put("/a/b/c", "age", new Integer(39));
094: tx.commit();
095:
096: tx.begin();
097: assertEquals(new Integer(39), cache1.get("/a/b/c", "age"));
098: tx.commit();
099:
100: // Need to do cleanup ...
101: tx.begin();
102: cache1.remove("/a/b/c");
103: cache1.remove("/a/b");
104: cache1.remove("/a");
105: tx.commit();
106:
107: } catch (Throwable t) {
108: fail(t.toString());
109: try {
110: tx.rollback();
111: } catch (Throwable t2) {
112: ;
113: }
114: fail(t.toString());
115: }
116: }
117:
118: public void testRollbackTx() {
119: UserTransaction tx = null;
120:
121: try {
122: tx = (UserTransaction) new InitialContext(p_)
123: .lookup("UserTransaction");
124: // Note that to set tree cache properties, you can do it here
125: // or go to transient-cache-service.xml.
126: assertNotNull("UserTransaction should not be null ", tx);
127: cache1 = cache_home.create();
128: // cache1.setLocking(true);
129:
130: tx.begin();
131: cache1.put("/a/b/c", "age", new Integer(38));
132: cache1.put("/a/b/c", "age", new Integer(39));
133: tx.rollback();
134:
135: tx.begin();
136: Integer val = (Integer) cache1.get("/a/b/c", "age");
137: tx.commit();
138: assertNull(val);
139: } catch (Throwable t) {
140: //t.printStackTrace();
141: fail(t.toString());
142: try {
143: tx.rollback();
144: } catch (Throwable t2) {
145: ;
146: }
147: fail(t.toString());
148: }
149: }
150:
151: public void testReplicatedPutTx() {
152: UserTransaction tx = null;
153:
154: try {
155: tx = (UserTransaction) new InitialContext(p_)
156: .lookup("UserTransaction");
157: assertNotNull("UserTransaction should not be null ", tx);
158: // Note that to set tree cache properties, you can do it here
159: // or go to transient-cache-service.xml.
160: cache1 = cache_home.create();
161: cache2 = cache_home.create();
162:
163: tx.begin();
164: cache1.put("/a/b/c", "age", new Integer(38));
165: assertEquals(new Integer(38), cache1.get("/a/b/c", "age"));
166:
167: cache1.put("/a/b/c", "age", new Integer(39));
168: tx.commit();
169:
170: tx.begin();
171: assertEquals(new Integer(39), cache2.get("/a/b/c", "age"));
172: tx.commit();
173:
174: // Need to do cleanup ...
175: tx.begin();
176: cache1.remove("/a/b/c");
177: cache1.remove("/a/b");
178: cache1.remove("/a");
179: tx.commit();
180:
181: } catch (Throwable t) {
182: fail(t.toString());
183: try {
184: tx.rollback();
185: } catch (Throwable t2) {
186: ;
187: }
188: fail(t.toString());
189: }
190: }
191:
192: void log(String msg) {
193: getLog().info("-- [" + Thread.currentThread() + "]: " + msg);
194: }
195:
196: public static Test suite() throws Exception {
197: // return getDeploySetup(MBeanUnitTestCase.class, "cachetest.sar");
198: // Deploy the package recursively. The jar contains ejb and the sar file contains
199: // tree cache MBean service
200: return getDeploySetup(getDeploySetup(MBeanUnitTestCase.class,
201: "cachetest.jar"), "cachetest.sar");
202: // return new TestSuite(MBeanUnitTestCase.class);
203: }
204:
205: public static void main(String[] args) throws Exception {
206: junit.textui.TestRunner.run(suite());
207: }
208:
209: }
|