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.openmbean;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.util.Arrays;
029: import java.util.List;
030:
031: import javax.management.openmbean.OpenDataException;
032: import javax.management.openmbean.OpenType;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * Open type tests.<p>
038: *
039: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
040: */
041: public class OpenTypeTestCase extends TestCase {
042: // Attributes ----------------------------------------------------------------
043:
044: // Constructor ---------------------------------------------------------------
045:
046: /**
047: * Construct the test
048: */
049: public OpenTypeTestCase(String s) {
050: super (s);
051: }
052:
053: // Tests ---------------------------------------------------------------------
054:
055: /**
056: * Test Allowed Classes.
057: */
058: public void testAllowedClasses() throws Exception {
059: String[] allowedClassNames = OpenType.ALLOWED_CLASSNAMES;
060: assertEquals(16, allowedClassNames.length);
061: List names = Arrays.asList(allowedClassNames);
062: checkOpenType(names, java.lang.Void.class);
063: checkOpenType(names, java.lang.Boolean.class);
064: checkOpenType(names, java.lang.Character.class);
065: checkOpenType(names, java.lang.Byte.class);
066: checkOpenType(names, java.lang.Short.class);
067: checkOpenType(names, java.lang.Integer.class);
068: checkOpenType(names, java.lang.Long.class);
069: checkOpenType(names, java.lang.Float.class);
070: checkOpenType(names, java.lang.Double.class);
071: checkOpenType(names, java.lang.String.class);
072: checkOpenType(names, java.util.Date.class);
073: checkOpenType(names, java.math.BigDecimal.class);
074: checkOpenType(names, java.math.BigInteger.class);
075: checkOpenType(names, javax.management.ObjectName.class);
076: checkOpenType(names,
077: javax.management.openmbean.CompositeData.class);
078: checkOpenType(names,
079: javax.management.openmbean.TabularData.class);
080: }
081:
082: public void testConstructorSimple() throws Exception {
083: OpenType test = new MyOpenType("java.lang.Void", "type",
084: "description");
085: assertEquals("java.lang.Void", test.getClassName());
086: assertEquals("type", test.getTypeName());
087: assertEquals("description", test.getDescription());
088: assertEquals(false, test.isArray());
089: }
090:
091: public void testConstructorArray() throws Exception {
092: OpenType test = new MyOpenType("[[Ljava.lang.Void;", "type",
093: "description");
094: assertEquals("[[Ljava.lang.Void;", test.getClassName());
095: assertEquals("type", test.getTypeName());
096: assertEquals("description", test.getDescription());
097: assertEquals(true, test.isArray());
098: }
099:
100: public void testSerializationSimple() throws Exception {
101: testSerialization("java.lang.Void", "type", "description");
102: }
103:
104: public void testSerializationArray() throws Exception {
105: testSerialization("[[Ljava.lang.Void;", "type", "description");
106: }
107:
108: /**
109: * Test Errors.
110: */
111: public void testErrors() throws Exception {
112: boolean caught = false;
113: try {
114: new MyOpenType(null, "dummy", "dummy");
115: } catch (IllegalArgumentException e) {
116: caught = true;
117: }
118: assertTrue("className cannot be null", caught);
119:
120: caught = false;
121: try {
122: new MyOpenType("", "dummy", "dummy");
123: } catch (IllegalArgumentException e) {
124: caught = true;
125: }
126: assertTrue("className cannot be empty", caught);
127:
128: caught = false;
129: try {
130: new MyOpenType("java.lang.Void", null, "dummy");
131: } catch (IllegalArgumentException e) {
132: caught = true;
133: }
134: assertTrue("typeName cannot be null", caught);
135:
136: caught = false;
137: try {
138: new MyOpenType("java.lang.Void", null, "dummy");
139: } catch (IllegalArgumentException e) {
140: caught = true;
141: }
142: assertTrue("typeName cannot be empty", caught);
143:
144: caught = false;
145: try {
146: new MyOpenType("java.lang.Void", "dummy", null);
147: } catch (IllegalArgumentException e) {
148: caught = true;
149: }
150: assertTrue("description cannot be null", caught);
151:
152: caught = false;
153: try {
154: new MyOpenType("java.lang.Void", "dummy", "");
155: } catch (IllegalArgumentException e) {
156: caught = true;
157: }
158: assertTrue("description cannot be empty", caught);
159:
160: caught = false;
161: try {
162: new MyOpenType("java.lang.Class", "dummy", "dummy");
163: } catch (OpenDataException e) {
164: caught = true;
165: }
166: assertTrue("className must be an OpenDataType", caught);
167:
168: caught = false;
169: try {
170: new MyOpenType("[Ljava.lang.Void", "dummy", "dummy");
171: } catch (OpenDataException e) {
172: caught = true;
173: }
174: assertTrue("[Ljava.lang.Void is not a valid array", caught);
175: }
176:
177: /**
178: * Test Errors that fail in the RI.
179: */
180: public void testErrors2() throws Exception {
181: boolean caught = false;
182: try {
183: new MyOpenType("[L", "dummy", "dummy");
184: } catch (StringIndexOutOfBoundsException e) {
185: fail("FAILS IN RI: [L open type should be an OpenDataException not a StringIndexOutOfBoundsException");
186: } catch (OpenDataException e) {
187: caught = true;
188: }
189: assertTrue("[L is not a valid array", caught);
190: }
191:
192: /**
193: * Test Errors that fail in the RI.
194: */
195: public void testErrors3() throws Exception {
196: boolean caught = false;
197: try {
198: new MyOpenType("[Xjava.lang.Void;", "dummy", "dummy");
199: } catch (OpenDataException e) {
200: caught = true;
201: }
202: assertTrue(
203: "FAILS IN RI: [Xjava.lang.Void; is not a valid array",
204: caught);
205: }
206:
207: // Support -------------------------------------------------------------------
208:
209: private void checkOpenType(List names, Class clazz)
210: throws Exception {
211: String name = clazz.getName();
212: assertTrue(name + " is an OpenType", names.contains(name));
213:
214: new MyOpenType(name, "dummy", "dummy");
215:
216: new MyOpenType("[L" + name + ";", "dummy", "dummy");
217:
218: new MyOpenType("[[[[[L" + name + ";", "dummy", "dummy");
219: }
220:
221: private void testSerialization(String className, String type,
222: String description) throws Exception {
223: // Create the role
224: OpenType original = new MyOpenType(className, type, description);
225:
226: // Serialize it
227: ByteArrayOutputStream baos = new ByteArrayOutputStream();
228: ObjectOutputStream oos = new ObjectOutputStream(baos);
229: oos.writeObject(original);
230:
231: // Deserialize it
232: ByteArrayInputStream bais = new ByteArrayInputStream(baos
233: .toByteArray());
234: ObjectInputStream ois = new ObjectInputStream(bais);
235: OpenType result = (OpenType) ois.readObject();
236:
237: // Did it work?
238: assertEquals(original.getClassName(), result.getClassName());
239: assertEquals(original.getTypeName(), result.getTypeName());
240: assertEquals(original.getDescription(), result.getDescription());
241: assertEquals(original.isArray(), result.isArray());
242: }
243:
244: public static class MyOpenType extends OpenType {
245: public MyOpenType(String className, String typeName,
246: String description) throws OpenDataException {
247: super (className, typeName, description);
248: }
249:
250: public boolean equals(Object other) {
251: throw new UnsupportedOperationException("irrelevent");
252: }
253:
254: public int hashCode() {
255: throw new UnsupportedOperationException("irrelevent");
256: }
257:
258: public boolean isValue(Object other) {
259: throw new UnsupportedOperationException("irrelevent");
260: }
261:
262: public String toString() {
263: throw new UnsupportedOperationException("irrelevent");
264: }
265: }
266: }
|