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: import java.util.Iterator;
031: import java.util.List;
032:
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.TabularData;
038: import javax.management.openmbean.TabularDataSupport;
039: import javax.management.openmbean.TabularType;
040:
041: /**
042: * Tabular type tests.<p>
043: *
044: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
045: */
046: public class TabularTypeTestCase extends TestCase {
047: // Static --------------------------------------------------------------------
048:
049: // Attributes ----------------------------------------------------------------
050:
051: // Constructor ---------------------------------------------------------------
052:
053: /**
054: * Construct the test
055: */
056: public TabularTypeTestCase(String s) {
057: super (s);
058: }
059:
060: // Tests ---------------------------------------------------------------------
061:
062: public void testTabularTypeOpenType() throws Exception {
063: String[] itemNames = new String[] { "name1", "name2" };
064: String[] itemDescriptions = new String[] { "desc1", "desc2" };
065: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
066: SimpleType.INTEGER };
067: CompositeType rowType = new CompositeType("rowTypeName",
068: "rowDescription", itemNames, itemDescriptions,
069: itemTypes);
070:
071: String[] indexNames = new String[] { "name1", "name2" };
072: TabularType tabularType = new TabularType("typeName",
073: "description", rowType, indexNames);
074:
075: assertEquals(TabularData.class.getName(), tabularType
076: .getClassName());
077: assertEquals("description", tabularType.getDescription());
078: assertEquals("typeName", tabularType.getTypeName());
079: assertTrue("Tabular type should not be an array", tabularType
080: .isArray() == false);
081: }
082:
083: public void testGetRowType() throws Exception {
084: String[] itemNames = new String[] { "name1", "name2" };
085: String[] itemDescriptions = new String[] { "desc1", "desc2" };
086: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
087: SimpleType.INTEGER };
088: CompositeType rowType = new CompositeType("rowTypeName",
089: "rowDescription", itemNames, itemDescriptions,
090: itemTypes);
091:
092: String[] indexNames = new String[] { "name1", "name2" };
093: TabularType tabularType = new TabularType("typeName",
094: "description", rowType, indexNames);
095:
096: assertEquals(rowType, tabularType.getRowType());
097: }
098:
099: public void testIndexNames() throws Exception {
100: String[] itemNames = new String[] { "name1", "name2" };
101: String[] itemDescriptions = new String[] { "desc1", "desc2" };
102: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
103: SimpleType.INTEGER };
104: CompositeType rowType = new CompositeType("rowTypeName",
105: "rowDescription", itemNames, itemDescriptions,
106: itemTypes);
107:
108: String[] indexNames = new String[] { "name1", "name2" };
109: TabularType tabularType = new TabularType("typeName",
110: "description", rowType, indexNames);
111:
112: List indexList = tabularType.getIndexNames();
113: assertTrue("wrong number of index names", indexList.size() == 2);
114: assertTrue("index list should contain name1", indexList
115: .contains("name1"));
116: assertTrue("index list should contain name2", indexList
117: .contains("name2"));
118: Iterator i = indexList.iterator();
119: assertTrue("first index is name1", i.next().equals("name1"));
120: assertTrue("second index is name2", i.next().equals("name2"));
121: }
122:
123: public void testIsValue() throws Exception {
124: String[] itemNames = new String[] { "name1", "name2" };
125: String[] itemDescriptions = new String[] { "desc1", "desc2" };
126: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
127: SimpleType.INTEGER };
128: CompositeType rowType = new CompositeType("rowTypeName",
129: "rowDescription", itemNames, itemDescriptions,
130: itemTypes);
131:
132: String[] indexNames = new String[] { "name1", "name2" };
133: TabularType tabularType = new TabularType("typeName",
134: "description", rowType, indexNames);
135:
136: assertTrue("null is not a value of tabular type", tabularType
137: .isValue(null) == false);
138: assertTrue("object is not a value of tabular type", tabularType
139: .isValue(new Object()) == false);
140:
141: TabularDataSupport data = new TabularDataSupport(tabularType);
142: assertTrue("data should is a value", tabularType.isValue(data));
143:
144: TabularType tabularType2 = new TabularType("typeName",
145: "description", rowType, indexNames);
146: data = new TabularDataSupport(tabularType2);
147: assertTrue(
148: "data is a value, even though the tabular type is a different instance",
149: tabularType.isValue(data));
150:
151: tabularType2 = new TabularType("typeName2", "description",
152: rowType, indexNames);
153: data = new TabularDataSupport(tabularType2);
154: assertTrue(
155: "data should not be a value, they have different type names",
156: tabularType.isValue(data) == false);
157:
158: CompositeType rowType2 = new CompositeType("rowTypeName2",
159: "rowDescription", itemNames, itemDescriptions,
160: itemTypes);
161: tabularType2 = new TabularType("typeName", "description",
162: rowType2, indexNames);
163: data = new TabularDataSupport(tabularType2);
164: assertTrue(
165: "data should not be a value, they have different row types",
166: tabularType.isValue(data) == false);
167:
168: String[] indexNames2 = new String[] { "name2", "name1" };
169: tabularType2 = new TabularType("typeName", "description",
170: rowType, indexNames2);
171: data = new TabularDataSupport(tabularType2);
172: assertTrue(
173: "data should not be a value, they have different index names",
174: tabularType.isValue(data) == false);
175: }
176:
177: public void testEquals() throws Exception {
178: String[] itemNames = new String[] { "name1", "name2" };
179: String[] itemDescriptions = new String[] { "desc1", "desc2" };
180: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
181: SimpleType.INTEGER };
182: CompositeType rowType = new CompositeType("rowTypeName",
183: "rowDescription", itemNames, itemDescriptions,
184: itemTypes);
185:
186: String[] indexNames = new String[] { "name1", "name2" };
187: TabularType tabularType = new TabularType("typeName",
188: "description", rowType, indexNames);
189:
190: assertTrue("null is not equal to tabular type", tabularType
191: .equals(null) == false);
192: assertTrue("object is not a equal to tabular type", tabularType
193: .equals(new Object()) == false);
194:
195: TabularType tabularType2 = new TabularType("typeName",
196: "description", rowType, indexNames);
197: assertTrue(
198: "Should be equal, even though the tabular type is a different instance",
199: tabularType.equals(tabularType2));
200: assertTrue(
201: "Should be equal, even though the tabular type is a different instance",
202: tabularType2.equals(tabularType));
203:
204: tabularType2 = new TabularType("typeName2", "description",
205: rowType, indexNames);
206: assertTrue(
207: "should not be equal, they have different type names",
208: tabularType.equals(tabularType2) == false);
209: assertTrue(
210: "should not be equal, they have different type names",
211: tabularType2.equals(tabularType) == false);
212:
213: CompositeType rowType2 = new CompositeType("rowTypeName2",
214: "rowDescription", itemNames, itemDescriptions,
215: itemTypes);
216: tabularType2 = new TabularType("typeName", "description",
217: rowType2, indexNames);
218: assertTrue(
219: "should not be a equal, they have different row types",
220: tabularType.equals(tabularType2) == false);
221: assertTrue(
222: "should not be a equal, they have different row types",
223: tabularType2.equals(tabularType) == false);
224:
225: String[] indexNames2 = new String[] { "name2", "name1" };
226: tabularType2 = new TabularType("typeName", "description",
227: rowType, indexNames2);
228: assertTrue(
229: "should not be equal, they have different index names",
230: tabularType.equals(tabularType2) == false);
231: assertTrue(
232: "should not be equal, they have different index names",
233: tabularType2.equals(tabularType) == false);
234: }
235:
236: public void testHashCode() throws Exception {
237: String[] itemNames = new String[] { "name1", "name2" };
238: String[] itemDescriptions = new String[] { "desc1", "desc2" };
239: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
240: SimpleType.INTEGER };
241: CompositeType rowType = new CompositeType("rowTypeName",
242: "rowDescription", itemNames, itemDescriptions,
243: itemTypes);
244:
245: String[] indexNames = new String[] { "name1", "name2" };
246: TabularType tabularType = new TabularType("typeName",
247: "description", rowType, indexNames);
248:
249: int myHashCode = "typeName".hashCode() + rowType.hashCode()
250: + "name1".hashCode() + "name2".hashCode();
251: assertTrue("Wrong hash code generated",
252: myHashCode == tabularType.hashCode());
253: }
254:
255: public void testToString() throws Exception {
256: String[] itemNames = new String[] { "name1", "name2" };
257: String[] itemDescriptions = new String[] { "desc1", "desc2" };
258: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
259: SimpleType.INTEGER };
260: CompositeType rowType = new CompositeType("rowTypeName",
261: "rowDescription", itemNames, itemDescriptions,
262: itemTypes);
263:
264: String[] indexNames = new String[] { "name1", "name2" };
265: TabularType tabularType = new TabularType("typeName",
266: "description", rowType, indexNames);
267:
268: String toString = tabularType.toString();
269:
270: assertTrue(
271: "toString() should contain the tabular type class name",
272: toString.indexOf(TabularType.class.getName()) != -1);
273: assertTrue("toString() should contain the type name", toString
274: .indexOf("typeName") != -1);
275: assertTrue("toString() should contain the row type " + rowType,
276: toString.indexOf(rowType.toString()) != -1);
277: assertTrue("toString() should contain the index name1",
278: toString.indexOf("name1") != -1);
279: assertTrue("toString() should contain the index name2",
280: toString.indexOf("name2") != -1);
281: }
282:
283: public void testSerialization() throws Exception {
284: String[] itemNames = new String[] { "name1", "name2" };
285: String[] itemDescriptions = new String[] { "desc1", "desc2" };
286: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
287: SimpleType.INTEGER };
288: CompositeType rowType = new CompositeType("rowTypeName",
289: "rowDescription", itemNames, itemDescriptions,
290: itemTypes);
291:
292: String[] indexNames = new String[] { "name1", "name2" };
293: TabularType tabularType = new TabularType("typeName",
294: "description", rowType, indexNames);
295:
296: // Serialize it
297: ByteArrayOutputStream baos = new ByteArrayOutputStream();
298: ObjectOutputStream oos = new ObjectOutputStream(baos);
299: oos.writeObject(tabularType);
300:
301: // Deserialize it
302: ByteArrayInputStream bais = new ByteArrayInputStream(baos
303: .toByteArray());
304: ObjectInputStream ois = new ObjectInputStream(bais);
305: Object result = ois.readObject();
306:
307: assertEquals(tabularType, result);
308: }
309:
310: public void testErrors() throws Exception {
311: String[] itemNames = new String[] { "name1", "name2" };
312: String[] itemDescriptions = new String[] { "desc1", "desc2" };
313: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
314: SimpleType.INTEGER };
315: CompositeType rowType = new CompositeType("rowTypeName",
316: "rowDescription", itemNames, itemDescriptions,
317: itemTypes);
318:
319: String[] indexNames = new String[] { "name1", "name2" };
320:
321: boolean caught = false;
322: try {
323: TabularType tabularType = new TabularType(null,
324: "description", rowType, indexNames);
325: } catch (IllegalArgumentException e) {
326: caught = true;
327: }
328: if (caught == false)
329: fail("Expected IllegalArgumentException for null type name");
330:
331: caught = false;
332: try {
333: TabularType tabularType = new TabularType("",
334: "description", rowType, indexNames);
335: } catch (IllegalArgumentException e) {
336: caught = true;
337: }
338: if (caught == false)
339: fail("Expected IllegalArgumentException for empty type name");
340:
341: caught = false;
342: try {
343: TabularType tabularType = new TabularType("typeName", null,
344: rowType, indexNames);
345: } catch (IllegalArgumentException e) {
346: caught = true;
347: }
348: if (caught == false)
349: fail("Expected IllegalArgumentException for null description");
350:
351: caught = false;
352: try {
353: TabularType tabularType = new TabularType("typeName", "",
354: rowType, indexNames);
355: } catch (IllegalArgumentException e) {
356: caught = true;
357: }
358: if (caught == false)
359: fail("Expected IllegalArgumentException for empty description");
360:
361: caught = false;
362: try {
363: TabularType tabularType = new TabularType("typeName",
364: "description", null, indexNames);
365: } catch (IllegalArgumentException e) {
366: caught = true;
367: }
368: if (caught == false)
369: fail("Expected IllegalArgumentException for null row type");
370:
371: caught = false;
372: try {
373: TabularType tabularType = new TabularType("typeName",
374: "description", rowType, null);
375: } catch (IllegalArgumentException e) {
376: caught = true;
377: }
378: if (caught == false)
379: fail("Expected IllegalArgumentException for null index names");
380:
381: caught = false;
382: try {
383: TabularType tabularType = new TabularType("typeName",
384: "description", rowType, new String[0]);
385: } catch (IllegalArgumentException e) {
386: caught = true;
387: }
388: if (caught == false)
389: fail("Expected IllegalArgumentException for empty index names");
390:
391: caught = false;
392: try {
393: TabularType tabularType = new TabularType("typeName",
394: "description", rowType, new String[] { "name1",
395: null });
396: } catch (IllegalArgumentException e) {
397: caught = true;
398: }
399: if (caught == false)
400: fail("Expected IllegalArgumentException for null index name element");
401:
402: caught = false;
403: try {
404: TabularType tabularType = new TabularType("typeName",
405: "description", rowType,
406: new String[] { "name1", "" });
407: } catch (IllegalArgumentException e) {
408: caught = true;
409: }
410: if (caught == false)
411: fail("Expected IllegalArgumentException for empty index name element");
412:
413: caught = false;
414: try {
415: TabularType tabularType = new TabularType("typeName",
416: "description", rowType, new String[] { "name1",
417: "nameX" });
418: } catch (OpenDataException e) {
419: caught = true;
420: }
421: if (caught == false)
422: fail("Expected OpenDataException for invalid index name");
423: }
424:
425: // Support -------------------------------------------------------------------
426: }
|