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 junit.framework.TestCase;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030:
031: import javax.management.openmbean.ArrayType;
032: import javax.management.openmbean.CompositeDataSupport;
033: import javax.management.openmbean.CompositeType;
034: import javax.management.openmbean.OpenDataException;
035: import javax.management.openmbean.OpenType;
036: import javax.management.openmbean.SimpleType;
037: import javax.management.openmbean.TabularType;
038: import javax.management.openmbean.TabularDataSupport;
039:
040: /**
041: * Array type tests.<p>
042: *
043: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
044: */
045: public class ArrayTypeTestCase extends TestCase {
046: // Static --------------------------------------------------------------------
047:
048: // Attributes ----------------------------------------------------------------
049:
050: // Constructor ---------------------------------------------------------------
051:
052: /**
053: * Construct the test
054: */
055: public ArrayTypeTestCase(String s) {
056: super (s);
057: }
058:
059: // Tests ---------------------------------------------------------------------
060:
061: public void testArrayTypeOpenType() throws Exception {
062: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
063: assertEquals("[[[Ljava.lang.String;", arrayType.getClassName());
064: assertEquals("3-dimension array of java.lang.String", arrayType
065: .getDescription());
066: assertEquals("[[[Ljava.lang.String;", arrayType.getTypeName());
067: assertTrue("Composite type should be an array", arrayType
068: .isArray());
069: }
070:
071: public void testGetDimension() throws Exception {
072: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
073: assertTrue("Dimension should be 3",
074: arrayType.getDimension() == 3);
075: }
076:
077: public void testElementOpenType() throws Exception {
078: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
079: assertTrue("Element OpenType should be " + SimpleType.STRING,
080: arrayType.getElementOpenType()
081: .equals(SimpleType.STRING));
082: }
083:
084: public void testIsValue() throws Exception {
085: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
086:
087: assertTrue("null is not a value of array type", arrayType
088: .isValue(null) == false);
089: assertTrue("object is not a value of array type", arrayType
090: .isValue(new Object()) == false);
091:
092: String[][][] data = new String[1][2][3];
093: assertTrue("data should be a value of array type", arrayType
094: .isValue(data));
095:
096: String[][] data2 = new String[1][2];
097: assertTrue(
098: "data should not be a value of array type, wrong number of dimensions",
099: arrayType.isValue(data2) == false);
100:
101: Object[][][] data3 = new Object[1][2][3];
102: assertTrue(
103: "data should not be a value of array type, wrong element type",
104: arrayType.isValue(data3) == false);
105:
106: String[] itemNames = new String[] { "name1", "name2" };
107: String[] itemDescriptions = new String[] { "desc1", "desc2" };
108: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
109: SimpleType.INTEGER };
110: CompositeType compositeType = new CompositeType("typeName",
111: "description", itemNames, itemDescriptions, itemTypes);
112: Object[] itemValues = new Object[] { "string", new Integer(1) };
113: CompositeDataSupport cds = new CompositeDataSupport(
114: compositeType, itemNames, itemValues);
115: CompositeDataSupport[][] compData1 = new CompositeDataSupport[][] {
116: { cds, null }, { cds, cds } };
117:
118: ArrayType compArrayType1 = new ArrayType(2, compositeType);
119: assertTrue("compData1 should be a value of array type",
120: compArrayType1.isValue(compData1));
121:
122: ArrayType compArrayType2 = new ArrayType(1, compositeType);
123: assertTrue(
124: "compData1 should not be a value of array type, wrong dimension",
125: compArrayType2.isValue(compData1) == false);
126:
127: CompositeType compositeType2 = new CompositeType("typeName2",
128: "description", itemNames, itemDescriptions, itemTypes);
129: ArrayType compArrayType3 = new ArrayType(2, compositeType2);
130: assertTrue(
131: "compData1 should not be a value of array type, wrong element type",
132: compArrayType3.isValue(compData1) == false);
133:
134: TabularType tabularType = new TabularType("typeName",
135: "description", compositeType, new String[] { "name1" });
136: TabularDataSupport tds = new TabularDataSupport(tabularType);
137: TabularDataSupport[][] tabData1 = new TabularDataSupport[][] {
138: { tds, null }, { tds, tds } };
139:
140: ArrayType tabArrayType1 = new ArrayType(2, tabularType);
141: assertTrue("tabData1 should be a value of array type",
142: tabArrayType1.isValue(tabData1));
143:
144: ArrayType tabArrayType2 = new ArrayType(1, tabularType);
145: assertTrue(
146: "tabData1 should not be a value of array type, wrong number of dimensions",
147: tabArrayType2.isValue(tabData1) == false);
148:
149: TabularType tabularType2 = new TabularType("typeName2",
150: "description", compositeType, new String[] { "name1" });
151: ArrayType tabArrayType3 = new ArrayType(2, tabularType2);
152: assertTrue(
153: "tabData1 should not be a value of array type, wrong element type",
154: tabArrayType3.isValue(tabData1) == false);
155: }
156:
157: public void testEquals() throws Exception {
158: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
159: assertTrue("null is not an array type",
160: arrayType.equals(null) == false);
161: assertTrue("object is not an array type", arrayType
162: .equals(new Object()) == false);
163:
164: assertTrue("should be equal", arrayType.equals(arrayType));
165:
166: ArrayType arrayType2 = new ArrayType(3, SimpleType.STRING);
167: assertTrue("should be equal, even though different instances",
168: arrayType.equals(arrayType2));
169: assertTrue("should be equal, even though different instances",
170: arrayType2.equals(arrayType));
171:
172: arrayType2 = new ArrayType(2, SimpleType.STRING);
173: assertTrue("should not be equal, wrong number of dimensions",
174: arrayType.equals(arrayType2) == false);
175: assertTrue("should not be equal, wrong number of dimensions",
176: arrayType2.equals(arrayType) == false);
177:
178: arrayType2 = new ArrayType(3, SimpleType.INTEGER);
179: assertTrue("should not be equal, wrong element type", arrayType
180: .equals(arrayType2) == false);
181: assertTrue("should not be equal, wrong element type",
182: arrayType2.equals(arrayType) == false);
183: }
184:
185: public void testHashCode() throws Exception {
186: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
187:
188: int myHashCode = 3 + SimpleType.STRING.hashCode();
189: assertTrue("Wrong hash code generated", myHashCode == arrayType
190: .hashCode());
191: }
192:
193: public void testToString() throws Exception {
194: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
195:
196: String toString = arrayType.toString();
197:
198: assertTrue(
199: "toString() should contain the array type class name",
200: toString.indexOf(ArrayType.class.getName()) != -1);
201: assertTrue("toString() should contain the dimension", toString
202: .indexOf("3") != -1);
203: assertTrue("toString() should contain the element type",
204: toString.indexOf(SimpleType.STRING.toString()) != -1);
205: }
206:
207: public void testSerialization() throws Exception {
208: ArrayType arrayType = new ArrayType(3, SimpleType.STRING);
209:
210: // Serialize it
211: ByteArrayOutputStream baos = new ByteArrayOutputStream();
212: ObjectOutputStream oos = new ObjectOutputStream(baos);
213: oos.writeObject(arrayType);
214:
215: // Deserialize it
216: ByteArrayInputStream bais = new ByteArrayInputStream(baos
217: .toByteArray());
218: ObjectInputStream ois = new ObjectInputStream(bais);
219: Object result = ois.readObject();
220:
221: assertEquals(arrayType, result);
222: }
223:
224: public void testErrors() throws Exception {
225: boolean caught = false;
226: try {
227: new ArrayType(-1, SimpleType.STRING);
228: } catch (IllegalArgumentException e) {
229: caught = true;
230: }
231: if (caught == false)
232: fail("Excepted IllegalArgumentException for negative dimension");
233:
234: caught = false;
235: try {
236: new ArrayType(1, new ArrayType(2, SimpleType.STRING));
237: } catch (OpenDataException e) {
238: caught = true;
239: }
240: if (caught == false)
241: fail("Excepted OpenDataException for ArrayType element type");
242: }
243:
244: public void testErrors2() throws Exception {
245: boolean caught = false;
246: try {
247: new ArrayType(1, null);
248: } catch (NullPointerException e) {
249: fail("FAILS IN RI: expected IllegalArgumentException for null element type");
250: } catch (IllegalArgumentException e) {
251: caught = true;
252: }
253: if (caught == false)
254: fail("Excepted IllegalArgumentException for null element type");
255: }
256: }
|