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.math.BigDecimal;
029: import java.math.BigInteger;
030: import java.util.Date;
031:
032: import javax.management.ObjectName;
033: import javax.management.openmbean.SimpleType;
034:
035: import junit.framework.TestCase;
036:
037: /**
038: * Simple type tests.<p>
039: *
040: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
041: */
042: public class SimpleTypeTestCase extends TestCase {
043: // Static --------------------------------------------------------------------
044:
045: static ObjectName objectName;
046:
047: static {
048: try {
049: objectName = new ObjectName("test:test=test");
050: } catch (Exception e) {
051: throw new RuntimeException(e.toString());
052: }
053: }
054:
055: // Attributes ----------------------------------------------------------------
056:
057: SimpleType[] types = new SimpleType[] { SimpleType.BIGDECIMAL,
058: SimpleType.BIGINTEGER, SimpleType.BOOLEAN, SimpleType.BYTE,
059: SimpleType.CHARACTER, SimpleType.DATE, SimpleType.DOUBLE,
060: SimpleType.FLOAT, SimpleType.INTEGER, SimpleType.LONG,
061: SimpleType.OBJECTNAME, SimpleType.SHORT, SimpleType.STRING,
062: SimpleType.VOID };
063:
064: Class[] classes = new Class[] { BigDecimal.class, BigInteger.class,
065: Boolean.class, Byte.class, Character.class, Date.class,
066: Double.class, Float.class, Integer.class, Long.class,
067: ObjectName.class, Short.class, String.class, Void.class };
068:
069: Object[] objects = new Object[] { new BigDecimal(1),
070: BigInteger.ONE, new Boolean(false),
071: new Byte(Byte.MAX_VALUE), new Character('a'),
072: new Date(System.currentTimeMillis()), new Double(1),
073: new Float(1), new Integer(1), new Long(1), objectName,
074: new Short(Short.MAX_VALUE), new String("hello"), null };
075:
076: // Constructor ---------------------------------------------------------------
077:
078: /**
079: * Construct the test
080: */
081: public SimpleTypeTestCase(String s) {
082: super (s);
083: }
084:
085: // Tests ---------------------------------------------------------------------
086:
087: public void testSimpleTypes() throws Exception {
088: for (int i = 0; i < types.length; i++) {
089: String className = classes[i].getName();
090: assertEquals(className, types[i].getClassName());
091: assertEquals(className, types[i].getTypeName());
092: assertEquals(className, types[i].getDescription());
093: }
094: }
095:
096: public void testEquals() throws Exception {
097: for (int i = 0; i < types.length; i++)
098: for (int j = 0; j < types.length; j++) {
099: if (i == j)
100: assertEquals(types[i], types[j]);
101: else
102: assertTrue("Simple Types should be different "
103: + classes[i], types[i] != types[j]);
104: }
105: }
106:
107: public void testIsValue() throws Exception {
108: for (int i = 0; i < types.length; i++) {
109: for (int j = 0; j < types.length; j++) {
110: // isValue makes no sense for Void
111: if (objects[i] == null)
112: continue;
113:
114: if (i == j)
115: assertTrue(classes[i]
116: + " should be a simple value of "
117: + types[j], types[j].isValue(objects[i]));
118: else
119: assertTrue(classes[i]
120: + " should NOT be a simple value of "
121: + types[j],
122: types[j].isValue(objects[i]) == false);
123: }
124:
125: assertTrue("null should NOT be a simple value of "
126: + types[i], types[i].isValue(null) == false);
127: }
128: }
129:
130: public void testHashCode() throws Exception {
131: for (int i = 0; i < types.length; i++)
132: assertEquals(classes[i].getName().hashCode(), types[i]
133: .hashCode());
134: }
135:
136: public void testToString() throws Exception {
137: for (int i = 0; i < types.length; i++) {
138: assertTrue("Simple Type " + classes[i].getName()
139: + " should contain " + SimpleType.class.getName(),
140: types[i].toString().indexOf(
141: SimpleType.class.getName()) != -1);
142: assertTrue(
143: "Simple Type " + classes[i].getName()
144: + " should contain " + classes[i].getName(),
145: types[i].toString().indexOf(classes[i].getName()) != -1);
146: }
147: }
148:
149: public void testSerialization() throws Exception {
150: for (int i = 0; i < types.length; i++) {
151: // Serialize it
152: ByteArrayOutputStream baos = new ByteArrayOutputStream();
153: ObjectOutputStream oos = new ObjectOutputStream(baos);
154: oos.writeObject(types[i]);
155:
156: // Deserialize it
157: ByteArrayInputStream bais = new ByteArrayInputStream(baos
158: .toByteArray());
159: ObjectInputStream ois = new ObjectInputStream(bais);
160: SimpleType result = (SimpleType) ois.readObject();
161:
162: assertTrue(
163: "Should resolve to same object after serialization "
164: + types[i], types[i] == result);
165: }
166: }
167: }
|