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.server;
023:
024: import junit.framework.TestCase;
025:
026: import test.implementation.server.support.Trivial;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.IOException;
031: import java.io.ObjectInputStream;
032: import java.io.ObjectOutputStream;
033:
034: import javax.management.MBeanServer;
035: import javax.management.MBeanServerFactory;
036: import javax.management.ObjectName;
037: import javax.management.ObjectInstance;
038:
039: import org.jboss.mx.server.ServerObjectInstance;
040:
041: /**
042: * Tests the ObjectInstance handling which is a bit brain-dead in the RI.<p>
043: *
044: * Maybe one-day these will be part of the compliance testsuite.
045: *
046: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
047: */
048: public class ObjectInstanceTestCase extends TestCase {
049: // Attributes ----------------------------------------------------------------
050:
051: // Constructor ---------------------------------------------------------------
052:
053: /**
054: * Construct the test
055: */
056: public ObjectInstanceTestCase(String s) {
057: super (s);
058: }
059:
060: // Tests that should work in the RI ------------------------------------------
061:
062: /**
063: * Test default domain
064: */
065: public void testDefaultDomain() {
066: MBeanServer server = null;
067: ObjectName unqualifiedName = null;
068: ObjectName qualifiedName = null;
069: ObjectInstance instance1 = null;
070: ObjectInstance instance2 = null;
071: try {
072: server = MBeanServerFactory.createMBeanServer();
073: unqualifiedName = new ObjectName(":property=1");
074: qualifiedName = new ObjectName("DefaultDomain:property=1");
075: instance1 = server.registerMBean(new Trivial(),
076: qualifiedName);
077: instance2 = server.getObjectInstance(unqualifiedName);
078: } catch (Exception e) {
079: fail(e.toString());
080: }
081:
082: assertEquals(instance1.getObjectName(), qualifiedName);
083: assertEquals(instance1, instance2);
084:
085: if (server != null)
086: MBeanServerFactory.releaseMBeanServer(server);
087: }
088:
089: /**
090: * Test different servers
091: */
092: public void testDifferentServers() {
093: MBeanServer server = null;
094: ObjectName name = null;
095: ObjectInstance instance1 = null;
096: ObjectInstance instance2 = null;
097: try {
098: server = MBeanServerFactory.createMBeanServer();
099: name = new ObjectName(":property=1");
100: instance1 = server.registerMBean(new Trivial(), name);
101: MBeanServerFactory.releaseMBeanServer(server);
102: server = MBeanServerFactory.createMBeanServer();
103: instance2 = server.registerMBean(new Trivial(), name);
104: } catch (Exception e) {
105: fail(e.toString());
106: }
107:
108: if (instance1.equals(instance2) == true)
109: fail("Instances in different servers are the same");
110:
111: if (server != null)
112: MBeanServerFactory.releaseMBeanServer(server);
113: }
114:
115: // Tests that need to work in JBossMX because of the extra agent id --------
116:
117: /**
118: * Test ObjectInstance/ServerObjectInstance Equals
119: */
120: public void testEquals() {
121: // Create the object instances
122: ObjectInstance oi = null;
123: ServerObjectInstance soi = null;
124: ObjectName name = null;
125: String className = "org.jboss.AClass";
126: try {
127: name = new ObjectName(":a=a");
128: oi = new ObjectInstance(name, className);
129: soi = new ServerObjectInstance(name, className, "agentid");
130: } catch (Exception e) {
131: fail(e.toString());
132: }
133: assertEquals(oi, soi);
134: }
135:
136: /**
137: * Test serialization. For moving between implementations, this HAS
138: * to produce an ObjectInstance.
139: */
140: public void testSerialization() {
141: // Create the new object Instance
142: ServerObjectInstance original = null;
143: ObjectInstance result = null;
144: ObjectName name = null;
145: String className = "org.jboss.AClassName";
146: try {
147: name = new ObjectName(":a=a");
148: original = new ServerObjectInstance(name, className,
149: "agentid");
150: } catch (Exception e) {
151: fail(e.toString());
152: }
153:
154: try {
155: // Serialize it
156: ByteArrayOutputStream baos = new ByteArrayOutputStream();
157: ObjectOutputStream oos = new ObjectOutputStream(baos);
158: oos.writeObject(original);
159:
160: // Deserialize it
161: ByteArrayInputStream bais = new ByteArrayInputStream(baos
162: .toByteArray());
163: ObjectInputStream ois = new ObjectInputStream(bais);
164: result = (ObjectInstance) ois.readObject();
165: } catch (IOException ioe) {
166: fail(ioe.toString());
167: } catch (ClassNotFoundException cnfe) {
168: fail(cnfe.toString());
169: }
170:
171: // Did it work?
172: assertEquals("javax.management.ObjectInstance", result
173: .getClass().getName());
174: assertEquals(name, result.getObjectName());
175: assertEquals(className, result.getClassName());
176: }
177: }
|