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 test.implementation.registry;
023:
024: import java.util.Date;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import javax.management.MBeanServer;
029: import javax.management.MBeanServerFactory;
030: import javax.management.ObjectName;
031:
032: import junit.framework.TestCase;
033:
034: import org.jboss.mx.server.ServerConstants;
035:
036: import test.implementation.registry.support.Trivial;
037:
038: /**
039: * Tests the value map processing in the managed mbean registry
040: *
041: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
042: */
043: public class ValuesTestCase extends TestCase implements ServerConstants {
044: // Attributes ----------------------------------------------------------------
045:
046: // Constructor ---------------------------------------------------------------
047:
048: /**
049: * Construct the test
050: */
051: public ValuesTestCase(String s) {
052: super (s);
053: }
054:
055: // Tests ---------------------------------------------------------------------
056:
057: /**
058: * Test classloader
059: */
060: public void testClassLoader() throws Exception {
061: // Get the previous registry value
062: String previous = System
063: .getProperty(MBEAN_REGISTRY_CLASS_PROPERTY);
064: if (previous == null)
065: previous = DEFAULT_MBEAN_REGISTRY_CLASS;
066: //System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY,
067: // "org.jboss.mx.server.registry.ManagedMBeanRegistry");
068: MBeanServer server = MBeanServerFactory.createMBeanServer();
069: try {
070: HashMap valuesMap = new HashMap();
071: ClassLoader cl = Thread.currentThread()
072: .getContextClassLoader();
073: valuesMap.put(CLASSLOADER, Thread.currentThread()
074: .getContextClassLoader());
075: ObjectName mbean = new ObjectName("test:test=test");
076: server.invoke(new ObjectName(MBEAN_REGISTRY),
077: "registerMBean", new Object[] { new Trivial(),
078: mbean, valuesMap }, new String[] {
079: Object.class.getName(),
080: ObjectName.class.getName(),
081: Map.class.getName() });
082: Object result = server.invoke(
083: new ObjectName(MBEAN_REGISTRY), "getValue",
084: new Object[] { mbean, CLASSLOADER }, new String[] {
085: ObjectName.class.getName(),
086: String.class.getName() });
087: assertEquals(cl, result);
088: } finally {
089: MBeanServerFactory.releaseMBeanServer(server);
090: System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY, previous);
091: }
092: }
093:
094: /**
095: * Test value
096: */
097: public void testValue() throws Exception {
098: // Get the previous registry value
099: String previous = System
100: .getProperty(MBEAN_REGISTRY_CLASS_PROPERTY);
101: if (previous == null)
102: previous = DEFAULT_MBEAN_REGISTRY_CLASS;
103: //System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY,
104: // "org.jboss.mx.server.registry.ManagedMBeanRegistry");
105: MBeanServer server = MBeanServerFactory.createMBeanServer();
106: try {
107: HashMap valuesMap = new HashMap();
108: Date date = new Date(System.currentTimeMillis());
109: valuesMap.put("date", date);
110: ObjectName mbean = new ObjectName("test:test=test");
111: server.invoke(new ObjectName(MBEAN_REGISTRY),
112: "registerMBean", new Object[] { new Trivial(),
113: mbean, valuesMap }, new String[] {
114: Object.class.getName(),
115: ObjectName.class.getName(),
116: Map.class.getName() });
117: Object result = server.invoke(
118: new ObjectName(MBEAN_REGISTRY), "getValue",
119: new Object[] { mbean, "date" }, new String[] {
120: ObjectName.class.getName(),
121: String.class.getName() });
122: assertEquals(date, result);
123: } finally {
124: MBeanServerFactory.releaseMBeanServer(server);
125: System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY, previous);
126: }
127: }
128:
129: /**
130: * Test value registered
131: */
132: public void testValueRegistered() throws Exception {
133: // Get the previous registry value
134: String previous = System
135: .getProperty(MBEAN_REGISTRY_CLASS_PROPERTY);
136: if (previous == null)
137: previous = DEFAULT_MBEAN_REGISTRY_CLASS;
138: //System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY,
139: // "org.jboss.mx.server.registry.ManagedMBeanRegistry");
140: MBeanServer server = MBeanServerFactory.createMBeanServer();
141: try {
142: HashMap valuesMap = new HashMap();
143: Date date = new Date(System.currentTimeMillis());
144: valuesMap.put("date", date);
145: ObjectName mbean = new ObjectName("test:test=test");
146: server.invoke(new ObjectName(MBEAN_REGISTRY),
147: "registerMBean", new Object[] { new Trivial(),
148: mbean, valuesMap }, new String[] {
149: Object.class.getName(),
150: ObjectName.class.getName(),
151: Map.class.getName() });
152: Object result = server.invoke(
153: new ObjectName(MBEAN_REGISTRY), "getValue",
154: new Object[] { mbean, "date" }, new String[] {
155: ObjectName.class.getName(),
156: String.class.getName() });
157: assertEquals(date, result);
158:
159: // Now remove it, reregister it and make sure it returns the new value
160: server.unregisterMBean(mbean);
161:
162: Thread.sleep(2);
163: date = new Date(System.currentTimeMillis());
164: HashMap valueMap2 = new HashMap();
165: valueMap2.put("date", date);
166: server.invoke(new ObjectName(MBEAN_REGISTRY),
167: "registerMBean", new Object[] { new Trivial(),
168: mbean, valueMap2 }, new String[] {
169: Object.class.getName(),
170: ObjectName.class.getName(),
171: Map.class.getName() });
172: result = server.invoke(new ObjectName(MBEAN_REGISTRY),
173: "getValue", new Object[] { mbean, "date" },
174: new String[] { ObjectName.class.getName(),
175: String.class.getName() });
176: assertEquals(date, result);
177: } finally {
178: MBeanServerFactory.releaseMBeanServer(server);
179: System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY, previous);
180: }
181: }
182: }
|