KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 package test.javax.management.openmbean;
10
11 import java.util.Arrays JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.management.openmbean.CompositeType JavaDoc;
15 import javax.management.openmbean.OpenDataException JavaDoc;
16 import javax.management.openmbean.OpenType JavaDoc;
17 import javax.management.openmbean.SimpleType JavaDoc;
18 import javax.management.openmbean.TabularType JavaDoc;
19
20 import test.MX4JTestCase;
21
22 /**
23  * @version $Revision: 1.8 $
24  */

25 public class TabularTypeTest extends MX4JTestCase
26 {
27    private String JavaDoc[] itemNames = null;
28    private String JavaDoc[] itemDescriptions = null;
29    private OpenType JavaDoc[] itemTypes;
30    private String JavaDoc[] indexNames;
31    private CompositeType JavaDoc tShirtType;
32    private CompositeType JavaDoc wine;
33    private TabularType JavaDoc allTShirtTypes;
34
35    public TabularTypeTest(String JavaDoc s)
36    {
37       super(s);
38    }
39
40    protected void setUp() throws Exception JavaDoc
41    {
42       itemNames = new String JavaDoc[]{"model", "color", "size", "price"};
43       itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
44       itemTypes = new OpenType JavaDoc[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT};
45       indexNames = new String JavaDoc[]{"model", "color", "size"};
46       tShirtType = new CompositeType JavaDoc("tShirt", "a TShirt", itemNames, itemDescriptions, itemTypes);
47
48       allTShirtTypes = new TabularType JavaDoc("tShirts", "List of available TShirts", tShirtType, indexNames);
49
50       wine =
51       new CompositeType JavaDoc("Wine",
52                         "Nectar of the gods",
53                         new String JavaDoc[]{"Type", "Winery", "Vintage"},
54                         new String JavaDoc[]{
55                            "Red, White, Rose, etc.",
56                            "The wine's producer",
57                            "The year the wine was made"},
58                         new OpenType JavaDoc[]{
59                            SimpleType.STRING,
60                            SimpleType.STRING,
61                            SimpleType.INTEGER});
62    }
63
64    public void testEquals() throws Exception JavaDoc
65    {
66       String JavaDoc[] itemNames2 = {"model", "color", "size", "price"};
67       String JavaDoc[] itemDescriptions2 = {"Manufacturer's model name", "The shirt's color", "How big is it", "How much does it cost"};
68       OpenType JavaDoc[] itemTypes2 = {SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT};
69       String JavaDoc[] indexNames2 = {"model", "color", "size"};
70       CompositeType JavaDoc tShirtType2 = new CompositeType JavaDoc("tShirt", "a TShirt", itemNames2, itemDescriptions2, itemTypes2);
71       TabularType JavaDoc allTShirtTypes2 = new TabularType JavaDoc("tShirts", "List of available TShirts", tShirtType2, indexNames2);
72       assertTrue(allTShirtTypes.equals(allTShirtTypes2));
73       // assert reverse
74
assertTrue(allTShirtTypes2.equals(allTShirtTypes));
75    }
76
77    public void testNotEqual() throws Exception JavaDoc
78    {
79       String JavaDoc[] itemNames3 = {"model3", "color3", "size3", "price3"};
80       String JavaDoc[] itemDescriptions3 = {"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
81       OpenType JavaDoc[] itemTypes3 = {SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.STRING, SimpleType.FLOAT};
82       String JavaDoc[] indexNames3 = {"model3", "color3", "size3"};
83       CompositeType JavaDoc tShirtType3 = new CompositeType JavaDoc("tShirt3", "a TShirt", itemNames3, itemDescriptions3, itemTypes3);
84       TabularType JavaDoc allTShirtTypes3 = new TabularType JavaDoc("tShirts3", "List of available TShirts", tShirtType3, indexNames3);
85       assertFalse(allTShirtTypes.equals(allTShirtTypes3));
86    }
87
88    public void testGetRowType()
89    {
90       CompositeType JavaDoc test = allTShirtTypes.getRowType();
91       assertEquals(test, tShirtType);
92    }
93
94    public void testGetIndexNames()
95    {
96       List JavaDoc temp = allTShirtTypes.getIndexNames();
97       String JavaDoc[] tempList = (String JavaDoc[])temp.toArray(new String JavaDoc[temp.size()]);
98
99       assertEquals(tempList.length, indexNames.length);
100       // assert all elements are the same
101
for (int i = 0; i < tempList.length; i++)
102       {
103          assertTrue(tempList[i] == indexNames[i]);
104       }
105    }
106
107    public void testHashCode() throws Exception JavaDoc
108    {
109       String JavaDoc[] itemNames2 = {"model", "color", "size", "price"};
110       String JavaDoc[] itemDescriptions2 = {"Manufacturer's model name", "The shirt's color", "How big is it", "How much does it cost"};
111       OpenType JavaDoc[] itemTypes2 = {SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT};
112       String JavaDoc[] indexNames2 = {"model", "color", "size"};
113       CompositeType JavaDoc tShirtType2 = new CompositeType JavaDoc("tShirt", "a TShirt", itemNames2, itemDescriptions2, itemTypes2);
114       TabularType JavaDoc allTShirtTypes2 = new TabularType JavaDoc("tShirts", "List of available TShirts", tShirtType2, indexNames2);
115
116       String JavaDoc[] itemNames3 = {"model3", "color3", "size3", "price3"};
117       String JavaDoc[] itemDescriptions3 = {"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
118       OpenType JavaDoc[] itemTypes3 = {SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.STRING, SimpleType.FLOAT};
119       String JavaDoc[] indexNames3 = {"model3", "color3", "size3"};
120       CompositeType JavaDoc tShirtType3 = new CompositeType JavaDoc("tShirt3", "a TShirt", itemNames3, itemDescriptions3, itemTypes3);
121       TabularType JavaDoc allTShirtTypes3 = new TabularType JavaDoc("tShirts3", "List of available TShirts", tShirtType3, indexNames3);
122
123       int code1 = allTShirtTypes.hashCode();
124       int code2 = allTShirtTypes2.hashCode();
125       assertEquals(code2, "tShirts".hashCode() + tShirtType2.hashCode() + (new HashSet JavaDoc(Arrays.asList(indexNames))).hashCode());
126       int code3 = allTShirtTypes3.hashCode();
127
128       assertEquals(code1, code2);
129       assertTrue(code1 != code3);
130    }
131
132    public void testCtor() throws Exception JavaDoc
133    {
134       TabularType JavaDoc winecatalog =
135               new TabularType JavaDoc("Wine Catalog",
136                               "Catalog of available wines",
137                               wine,
138                               new String JavaDoc[]{"Winery", "Vintage"});
139       assertTrue("Unexpected name",
140                  winecatalog.getTypeName().compareTo("Wine Catalog") == 0);
141       assertTrue("Unexpected CompositeType",
142                  winecatalog.getRowType().equals(wine));
143       List JavaDoc index = winecatalog.getIndexNames();
144       assertTrue("Incorrect index size", index.size() == 2);
145       assertTrue("Unexpected index entries",
146                  ((String JavaDoc)index.get(0)).compareTo("Winery") == 0
147                  && ((String JavaDoc)index.get(1)).compareTo("Vintage") == 0);
148    }
149
150    public void testCtorNullName() throws Exception JavaDoc
151    {
152       try
153       {
154          new TabularType JavaDoc(null,
155                          "Catalog of available wines",
156                          wine,
157                          new String JavaDoc[]{"Winery", "Vintage"});
158          fail("Expecting IllegalArgumentException");
159       }
160       catch (IllegalArgumentException JavaDoc x)
161       {
162       }
163    }
164
165    public void testCtorEmptyName() throws Exception JavaDoc
166    {
167       try
168       {
169          new TabularType JavaDoc("",
170                          "Catalog of available wines",
171                          wine,
172                          new String JavaDoc[]{"Winery", "Vintage"});
173          fail("Expecting IllegalArgumentException");
174       }
175       catch (IllegalArgumentException JavaDoc x)
176       {
177       }
178    }
179
180    public void testCtorNullDescriptor() throws Exception JavaDoc
181    {
182       try
183       {
184          new TabularType JavaDoc("Wine Catalog",
185                          null,
186                          wine,
187                          new String JavaDoc[]{"Winery", "Vintage"});
188          fail("Expecting IllegalArgumentException");
189       }
190       catch (IllegalArgumentException JavaDoc x)
191       {
192       }
193    }
194
195    public void testCtorEmptyDescriptor() throws Exception JavaDoc
196    {
197       try
198       {
199          new TabularType JavaDoc("Wine Catalog",
200                          "",
201                          wine,
202                          new String JavaDoc[]{"Winery", "Vintage"});
203          fail("Expecting IllegalArgumentException");
204       }
205       catch (IllegalArgumentException JavaDoc x)
206       {
207       }
208    }
209
210    public void testCtorNullRowType() throws Exception JavaDoc
211    {
212       try
213       {
214          new TabularType JavaDoc("Wine Catalog",
215                          "Catalog of available wines",
216                          null,
217                          new String JavaDoc[]{"Winery", "Vintage"});
218          fail("Expecting IllegalArgumentException");
219       }
220       catch (IllegalArgumentException JavaDoc x)
221       {
222       }
223    }
224
225    public void testCtorNullIndex() throws Exception JavaDoc
226    {
227       try
228       {
229          new TabularType JavaDoc("Wine Catalog",
230                          "Catalog of available wines",
231                          wine,
232                          null);
233          fail("Expecting IllegalArgumentException");
234       }
235       catch (IllegalArgumentException JavaDoc x)
236       {
237       }
238    }
239
240    public void testCtorEmptyIndex() throws Exception JavaDoc
241    {
242       try
243       {
244          new TabularType JavaDoc("Wine Catalog",
245                          "Catalog of available wines",
246                          wine,
247                          new String JavaDoc[0]);
248          fail("Expecting IllegalArgumentException");
249       }
250       catch (IllegalArgumentException JavaDoc x)
251       {
252       }
253    }
254
255    public void testCtorBogusIndex() throws Exception JavaDoc
256    {
257       try
258       {
259          new TabularType JavaDoc("Wine Catalog",
260                          "Catalog of available wines",
261                          wine,
262                          new String JavaDoc[]{"Region", "Vintage"});
263          fail("Expecting OpenDataException");
264       }
265       catch (OpenDataException JavaDoc x)
266       {
267       }
268    }
269 }
270
271
Popular Tags