KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > openmbean > ArrayTypeTest


1 /**
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package test.javax.management.openmbean;
9
10 /**
11  * @version $Revision: 1.7 $
12  */

13
14 import javax.management.openmbean.ArrayType JavaDoc;
15 import javax.management.openmbean.CompositeData JavaDoc;
16 import javax.management.openmbean.CompositeDataSupport JavaDoc;
17 import javax.management.openmbean.CompositeType JavaDoc;
18 import javax.management.openmbean.OpenType JavaDoc;
19 import javax.management.openmbean.SimpleType JavaDoc;
20
21 import junit.framework.TestCase;
22
23
24 public class ArrayTypeTest extends TestCase
25 {
26    public ArrayTypeTest(String JavaDoc s)
27    {
28       super(s);
29    }
30
31    protected void setUp() throws Exception JavaDoc
32    {
33       super.setUp();
34    }
35
36    protected void tearDown() throws Exception JavaDoc
37    {
38       super.tearDown();
39    }
40
41    public void testIsValue() throws Exception JavaDoc
42    {
43       ArrayType JavaDoc simple = new ArrayType JavaDoc(1, SimpleType.STRING);
44
45       String JavaDoc[] stringarray = {"do", "re", "mi"};
46       assertTrue("Expecting equality for array of strings", simple.isValue(stringarray));
47       assertFalse("Expecting inequality for array of ints", simple.isValue(new int[]{1, 2, 3, 4}));
48       assertFalse("Expecting inequality for null", simple.isValue(null));
49       assertFalse("Expecting inequality for string", simple.isValue("fa"));
50
51       String JavaDoc[] items = {"type", "winery", "vintage"};
52       String JavaDoc[] descriptions = {"Type of wine", "Wine producer", "Year produced"};
53       OpenType JavaDoc[] types = {SimpleType.STRING, SimpleType.STRING, SimpleType.INTEGER};
54       CompositeType JavaDoc wine = new CompositeType JavaDoc("wine", "nectar of the gods", items, descriptions, types);
55
56       items = new String JavaDoc[]{"type", "brewery"};
57       descriptions = new String JavaDoc[]{"Type of beer", "Beer producer"};
58       types = new OpenType JavaDoc[]{SimpleType.STRING, SimpleType.STRING};
59       CompositeType JavaDoc beer = new CompositeType JavaDoc("beer", "a meal in a glass", items, descriptions, types);
60
61       ArrayType JavaDoc composite = new ArrayType JavaDoc(1, wine);
62
63       CompositeDataSupport JavaDoc amarone =
64               new CompositeDataSupport JavaDoc(wine,
65                                        new String JavaDoc[]{"type", "winery", "vintage"},
66                                        new Object JavaDoc[]{"red", "Allegrini", new Integer JavaDoc(1996)});
67       CompositeDataSupport JavaDoc orvieto =
68               new CompositeDataSupport JavaDoc(wine,
69                                        new String JavaDoc[]{"type", "winery", "vintage"},
70                                        new Object JavaDoc[]{"white", "Ruffino", new Integer JavaDoc(2002)});
71       CompositeData JavaDoc[] winecellar = {amarone, orvieto};
72       CompositeData JavaDoc[] sparsewines = {amarone, null, orvieto};
73
74       CompositeDataSupport JavaDoc stout =
75               new CompositeDataSupport JavaDoc(beer,
76                                        new String JavaDoc[]{"type", "brewery"},
77                                        new Object JavaDoc[]{"stout", "Guiness"});
78       CompositeData JavaDoc[] beerlist = {stout};
79
80       CompositeData JavaDoc[] mixer = {amarone, stout, orvieto};
81
82       assertTrue("Expecting equality for array of wines", composite.isValue(winecellar));
83       assertTrue("Expecting equality for sparse array of wines", composite.isValue(sparsewines));
84       assertFalse("Expecting inequality for array of beer", composite.isValue(beerlist));
85       assertFalse("Expecting inequality for mixed array", composite.isValue(mixer));
86       assertFalse("Expecting inequality for single wine", composite.isValue(orvieto));
87
88       ArrayType JavaDoc winematrix = new ArrayType JavaDoc(2, wine);
89
90       CompositeData JavaDoc[][] matrix = {{amarone, orvieto}, {orvieto, amarone}};
91       assertTrue("Expecting equality for wine matrix", winematrix.isValue(matrix));
92       assertFalse("Expecting inequality for wine vector", winematrix.isValue(winecellar));
93
94       winematrix = new ArrayType JavaDoc(2, wine);
95       CompositeData JavaDoc[][] matrix2 = new CompositeData JavaDoc[1][1];
96       assertTrue("Expecting equality for wine matrix", winematrix.isValue(matrix2));
97    }
98
99    public void testEquals() throws Exception JavaDoc
100    {
101       ArrayType JavaDoc a1 = new ArrayType JavaDoc(1, SimpleType.LONG);
102       ArrayType JavaDoc a2 = new ArrayType JavaDoc(1, SimpleType.LONG);
103       ArrayType JavaDoc a3 = new ArrayType JavaDoc(2, SimpleType.LONG);
104       ArrayType JavaDoc a4 = new ArrayType JavaDoc(2, SimpleType.STRING);
105
106       assertTrue(a1.equals(a2));
107       assertTrue(!a1.equals(a3));
108       assertTrue(!a1.equals(a4));
109
110
111    }
112
113    public void testCompositeEquals() throws Exception JavaDoc
114    {
115       String JavaDoc[] items = {"type", "winery", "vintage"};
116       String JavaDoc[] descriptions = {"Type of wine", "Wine producer", "Year produced"};
117       OpenType JavaDoc[] types = {SimpleType.STRING, SimpleType.STRING, SimpleType.DATE};
118       CompositeType JavaDoc californiaWine = new CompositeType JavaDoc("California", "Vino", items, descriptions, types);
119       ArrayType JavaDoc aone = new ArrayType JavaDoc(2, californiaWine);
120       ArrayType JavaDoc atwo = new ArrayType JavaDoc(2, californiaWine);
121       assertTrue("Expecting equality for identical composite types", aone.equals(atwo));
122
123       CompositeType JavaDoc italianWine = new CompositeType JavaDoc("Italia", "Vino", items, descriptions, types);
124       atwo = new ArrayType JavaDoc(2, italianWine);
125       assertFalse("Expecting inequality for different composite types", aone.equals(atwo));
126    }
127
128    public void testClassName() throws Exception JavaDoc
129    {
130       ArrayType JavaDoc a1 = new ArrayType JavaDoc(2, SimpleType.STRING);
131       assertEquals(a1.getClassName(), "[[Ljava.lang.String;");
132       assertEquals(a1.getTypeName(), "[[Ljava.lang.String;");
133
134    }
135
136    public void testHashCode() throws Exception JavaDoc
137    {
138
139
140       ArrayType JavaDoc a1 = new ArrayType JavaDoc(2, SimpleType.STRING);
141       ArrayType JavaDoc a2 = new ArrayType JavaDoc(2, SimpleType.STRING);
142
143       assertTrue(a1.hashCode() == a2.hashCode());
144
145    }
146
147    public void testBadSize() throws Exception JavaDoc
148    {
149       try
150       {
151          ArrayType JavaDoc t = new ArrayType JavaDoc(0, SimpleType.STRING);
152       }
153       catch (IllegalArgumentException JavaDoc x)
154       {
155          assertTrue(true); // success
156
}
157    }
158 }
159
Popular Tags