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.compliance.varia;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028:
029: import javax.management.MalformedObjectNameException;
030: import javax.management.ObjectInstance;
031: import javax.management.ObjectName;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * Object Instance tests.<p>
037: *
038: * NOTE: The tests use String literals to ensure the comparisons are
039: * not performed on object references.
040: *
041: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
042: */
043: public class ObjectInstanceTestCase extends TestCase {
044: // Attributes ----------------------------------------------------------------
045:
046: // Constructor ---------------------------------------------------------------
047:
048: /**
049: * Construct the test
050: */
051: public ObjectInstanceTestCase(String s) {
052: super (s);
053: }
054:
055: // Tests ---------------------------------------------------------------------
056:
057: /**
058: * Test String constructor.
059: */
060: public void testStringConstructor() {
061: ObjectInstance instance = null;
062:
063: try {
064: instance = new ObjectInstance("test:type=test", "ClassName");
065: } catch (Exception e) {
066: fail(e.toString());
067: }
068:
069: // Did it work?
070: assertEquals("test:type=test", instance.getObjectName()
071: .toString());
072: assertEquals("ClassName", instance.getClassName());
073: }
074:
075: /**
076: * Test ObjectName constructor.
077: */
078: public void testObjectNameConstructor() {
079: ObjectInstance instance = null;
080: ObjectName objectName = null;
081:
082: try {
083: objectName = new ObjectName(":type=test");
084: instance = new ObjectInstance(objectName, "ClassName");
085: } catch (Exception e) {
086: fail(e.toString());
087: }
088:
089: // Did it work?
090: assertEquals(objectName, instance.getObjectName());
091: assertEquals("ClassName", instance.getClassName());
092: }
093:
094: /**
095: * Test Equals.
096: */
097: public void testEquals() {
098: ObjectInstance instanceTest = null;
099: ObjectInstance instanceSame = null;
100: ObjectInstance instanceDiffName = null;
101: ObjectInstance instanceDiffClass = null;
102:
103: try {
104: instanceTest = new ObjectInstance("test:type=test",
105: "ClassName");
106: instanceSame = new ObjectInstance("test:type=test",
107: "ClassName");
108: instanceDiffName = new ObjectInstance(
109: "test:type=different", "ClassName");
110: instanceDiffClass = new ObjectInstance("test:type=test",
111: "Another");
112: } catch (Exception e) {
113: fail(e.toString());
114: }
115:
116: assertEquals(instanceTest, instanceTest);
117: assertEquals(instanceTest, instanceSame);
118: if (instanceTest.equals(instanceDiffName))
119: fail("ObjectInstance.equals broken for object name");
120: if (instanceTest.equals(instanceDiffClass))
121: fail("ObjectInstance.equals broken for class name");
122: }
123:
124: /**
125: * Test errors.
126: */
127: public void testErrors() {
128: boolean caught = false;
129: try {
130: new ObjectInstance("rubbish", "ClassName");
131: } catch (MalformedObjectNameException e) {
132: caught = true;
133: } catch (Exception e) {
134: fail(e.toString());
135: }
136: if (caught == false)
137: fail("ObjectInstance(String, String) failed to report malformed object name");
138:
139: try {
140: String NULL = null;
141: new ObjectInstance(NULL, "ClassName");
142: } catch (MalformedObjectNameException e) {
143: caught = true;
144: } catch (Exception e) {
145: fail(e.toString());
146: }
147: if (caught == false)
148: fail("ObjectInstance(String, String) failed to report null object name");
149: }
150:
151: /**
152: * Test serialization.
153: */
154: public void testSerialization() {
155: ObjectInstance original = null;
156: ObjectInstance result = null;
157: ObjectName objectName = null;
158:
159: try {
160: objectName = new ObjectName(":type=test");
161: original = new ObjectInstance(objectName, "ClassName");
162:
163: // Serialize it
164: ByteArrayOutputStream baos = new ByteArrayOutputStream();
165: ObjectOutputStream oos = new ObjectOutputStream(baos);
166: oos.writeObject(original);
167:
168: // Deserialize it
169: ByteArrayInputStream bais = new ByteArrayInputStream(baos
170: .toByteArray());
171: ObjectInputStream ois = new ObjectInputStream(bais);
172: result = (ObjectInstance) ois.readObject();
173: } catch (Exception e) {
174: fail(e.toString());
175: }
176:
177: // Did it work?
178: assertEquals(original, result);
179: assertEquals(objectName, result.getObjectName());
180: assertEquals("ClassName", result.getClassName());
181: }
182: }
|