KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collection JavaDoc;
11 import java.util.Set JavaDoc;
12 import javax.management.openmbean.CompositeData JavaDoc;
13 import javax.management.openmbean.CompositeDataSupport JavaDoc;
14 import javax.management.openmbean.CompositeType JavaDoc;
15 import javax.management.openmbean.OpenType JavaDoc;
16 import javax.management.openmbean.SimpleType JavaDoc;
17 import javax.management.openmbean.TabularData JavaDoc;
18 import javax.management.openmbean.TabularDataSupport JavaDoc;
19 import javax.management.openmbean.TabularType JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * @version $Revision: 1.10 $
25  */

26 public class TabularDataSupportTest extends TestCase
27 {
28    private String JavaDoc[] itemNames = null;
29    private String JavaDoc[] itemDescriptions = null;
30    private OpenType JavaDoc[] itemTypes;
31    private String JavaDoc[] indexNames;
32    private CompositeType JavaDoc tShirtType;
33    private TabularType JavaDoc allTShirtTypes;
34    private TabularDataSupport JavaDoc tabularSupport;
35    private CompositeData JavaDoc compositeData;
36
37    public TabularDataSupportTest(String JavaDoc s)
38    {
39       super(s);
40    }
41
42    protected void setUp() throws Exception JavaDoc
43    {
44       super.setUp();
45       itemNames = new String JavaDoc[]{"model", "color", "size", "price"};
46       itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
47       itemTypes = new OpenType JavaDoc[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT};
48       indexNames = new String JavaDoc[]{"model", "color", "size"};
49       tShirtType = new CompositeType JavaDoc("tShirt",
50                                      "a TShirt",
51                                      itemNames,
52                                      itemDescriptions,
53                                      itemTypes);
54
55       allTShirtTypes = new TabularType JavaDoc("tShirts",
56                                        "List of available TShirts",
57                                        tShirtType, // row type
58
indexNames);
59
60       Object JavaDoc[] itemValues = new Object JavaDoc[]{"MX4J", "red", "L", new Float JavaDoc(15.0f)};
61
62       compositeData = new CompositeDataSupport JavaDoc(tShirtType, itemNames, itemValues);
63       // takes tabular type
64
tabularSupport = new TabularDataSupport JavaDoc(allTShirtTypes);
65    }
66
67    protected void tearDown() throws Exception JavaDoc
68    {
69       super.tearDown();
70    }
71
72    public void testCreation()
73    {
74       TabularDataSupport JavaDoc tabularData2 = new TabularDataSupport JavaDoc(allTShirtTypes);
75       assertTrue(tabularData2 != null);
76    }
77
78    public void testPut()
79    {
80       try
81       {
82          tabularSupport.put(compositeData);
83          assertTrue("tabularSupport doesn't contain compositeData", tabularSupport.containsValue(compositeData));
84       }
85       catch (Exception JavaDoc e)
86       {
87          e.printStackTrace();
88       }
89    }
90
91    public void testPutAll() throws Exception JavaDoc
92    {
93       CompositeData JavaDoc snmpshirt =
94               new CompositeDataSupport JavaDoc(tShirtType,
95                                        itemNames,
96                                        new Object JavaDoc[]{"SNMP", "green", null, new Float JavaDoc(15.0f)});
97       CompositeData JavaDoc[] shirts = {compositeData, snmpshirt};
98       tabularSupport.putAll(shirts);
99       assertTrue("tabularSupport doesn't contain compositeData", tabularSupport.containsValue(compositeData));
100       assertTrue("tabularSupport doesn't contain snmpshirt", tabularSupport.containsValue(snmpshirt));
101    }
102
103    public void testRemove() throws Exception JavaDoc
104    {
105       CompositeData JavaDoc snmpshirt =
106               new CompositeDataSupport JavaDoc(tShirtType,
107                                        itemNames,
108                                        new Object JavaDoc[]{"SNMP", "green", null, new Float JavaDoc(15.0f)});
109       CompositeData JavaDoc[] shirts = {compositeData, snmpshirt};
110       tabularSupport.putAll(shirts);
111       CompositeData JavaDoc oldshirt = tabularSupport.remove(new Object JavaDoc[]{"SNMP", "green", null});
112       assertFalse("oldshirt is null", oldshirt == null);
113       assertTrue("Expecting oldshirt equals snmpshirt", snmpshirt.equals(oldshirt));
114    }
115
116    public void testGetTabularType()
117    {
118       TabularType JavaDoc toVerify = tabularSupport.getTabularType();
119       assertEquals(toVerify, allTShirtTypes);
120    }
121
122
123    public void testCalculateIndex()
124    {
125       // returns an array of the indexNames as represented by the simpleTypes(itemTypes) ie returns the values for the index names which are the simpleTypes
126
Object JavaDoc[] object = tabularSupport.calculateIndex(compositeData);
127       assertTrue(object.length == indexNames.length);
128    }
129
130    public void testContainsValue()
131    {
132       OpenType JavaDoc[] keyTypes = new OpenType JavaDoc[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING};
133       tabularSupport.put(compositeData);
134       boolean expected = tabularSupport.containsValue(compositeData);
135       assertTrue(expected);
136    }
137
138    public void testContainsValueMissingValue() throws Exception JavaDoc
139    {
140       CompositeData JavaDoc snmpshirt =
141               new CompositeDataSupport JavaDoc(tShirtType,
142                                        itemNames,
143                                        new Object JavaDoc[]{"SNMP", "green", "M", new Float JavaDoc(15.0f)});
144       tabularSupport.put(compositeData);
145       tabularSupport.put(snmpshirt);
146       assertTrue("Should contain MX4J and SNMP shirts",
147                  tabularSupport.containsValue(compositeData)
148                  && tabularSupport.containsValue(snmpshirt));
149       CompositeData JavaDoc jmxshirt =
150               new CompositeDataSupport JavaDoc(tShirtType,
151                                        itemNames,
152                                        new Object JavaDoc[]{"JMX", "blue", "M", new Float JavaDoc(15.0f)});
153       assertFalse("Contains JMX shirt",
154                   tabularSupport.containsValue(jmxshirt));
155       CompositeData JavaDoc bogusshirt =
156               new CompositeDataSupport JavaDoc(tShirtType,
157                                        itemNames,
158                                        new Object JavaDoc[]{"Bogus", null, "M", new Float JavaDoc(15.0f)});
159       assertFalse("Contains Bogus shirt",
160                   tabularSupport.containsValue(bogusshirt));
161    }
162
163    public void testContainsValueNullValue() throws Exception JavaDoc
164    {
165       assertFalse("Contains 'null'", tabularSupport.containsValue(null));
166    }
167
168    public void testKeySet()
169    {
170       tabularSupport.put(compositeData);
171       Set JavaDoc set = tabularSupport.keySet();
172       assertTrue(set.size() == 1);
173    }
174
175
176    public void testValues()
177    {
178       tabularSupport.put(compositeData);
179       Collection JavaDoc values = tabularSupport.values();
180       assertTrue(values.contains(compositeData));
181    }
182
183    public void testEquals() throws Exception JavaDoc
184    {
185       assertFalse("Equal to 'null'", tabularSupport.equals(null));
186       assertFalse("Equal to Non-TabularData",
187                   tabularSupport.equals(new Integer JavaDoc(42)));
188
189       String JavaDoc[] items = {"model", "color", "size", "price"};
190
191       String JavaDoc[] usdescriptions =
192               {
193                  "Manufacturer's model name",
194                  "The shirt's color",
195                  "How big is it",
196                  "How much does it cost (dollars)"};
197       String JavaDoc[] eurodescriptions =
198               {
199                  "Designer's model name",
200                  "The hue of the garment",
201                  "Garment size",
202                  "How much does it cost (euros)"};
203
204       OpenType JavaDoc[] types =
205               {
206                  SimpleType.STRING,
207                  SimpleType.STRING,
208                  SimpleType.STRING,
209                  SimpleType.FLOAT};
210       String JavaDoc[] indices = {"model", "color", "size"};
211
212       CompositeType JavaDoc usst =
213               new CompositeType JavaDoc("tShirt",
214                                 "US Shirt",
215                                 items,
216                                 usdescriptions,
217                                 types);
218       CompositeType JavaDoc eurost =
219               new CompositeType JavaDoc("tShirt",
220                                 "European Shirt",
221                                 items,
222                                 usdescriptions,
223                                 types);
224
225       TabularType JavaDoc usstt =
226               new TabularType JavaDoc("tShirts",
227                               "List of available US Shirts",
228                               usst,
229                               indices);
230       TabularType JavaDoc eurostt =
231               new TabularType JavaDoc("tShirts",
232                               "List of available European Shirts",
233                               eurost,
234                               indices);
235
236       TabularData JavaDoc ussdata = new TabularDataSupport JavaDoc(usstt);
237       TabularData JavaDoc eurosdata = new TabularDataSupport JavaDoc(eurostt);
238       assertTrue("Expecting equality for tabular shirt data",
239                  ussdata.equals(eurosdata));
240       assertTrue("Expecting equal hash codes for equal data",
241                  eurosdata.hashCode() == ussdata.hashCode());
242
243       OpenType JavaDoc[] txtypes =
244               {
245                  SimpleType.STRING,
246                  SimpleType.STRING,
247                  SimpleType.STRING,
248                  SimpleType.DOUBLE};
249       CompositeType JavaDoc txst =
250               new CompositeType JavaDoc("tShirt",
251                                 "Texas Shirt",
252                                 items,
253                                 usdescriptions,
254                                 txtypes);
255       TabularType JavaDoc txstt =
256               new TabularType JavaDoc("tShirts",
257                               "List of available Texas Shirts",
258                               txst,
259                               indices);
260       TabularData JavaDoc txsdata = new TabularDataSupport JavaDoc(txstt);
261       assertFalse("Texas shirt equals US shirt", ussdata.equals(txsdata));
262
263       CompositeData JavaDoc ussnmpshirt =
264               new CompositeDataSupport JavaDoc(usst,
265                                        items,
266                                        new Object JavaDoc[]{"SNMP", "green", "M", new Float JavaDoc(15.0f)});
267       CompositeData JavaDoc usjmxshirt =
268               new CompositeDataSupport JavaDoc(usst,
269                                        items,
270                                        new Object JavaDoc[]{"JMX", "blue", "M", new Float JavaDoc(15.0f)});
271       ussdata.put(ussnmpshirt);
272       ussdata.put(usjmxshirt);
273
274       CompositeData JavaDoc eurosnmpshirt =
275               new CompositeDataSupport JavaDoc(usst,
276                                        items,
277                                        new Object JavaDoc[]{"SNMP", "green", "M", new Float JavaDoc(15.0f)});
278       CompositeData JavaDoc eurojmxshirt =
279               new CompositeDataSupport JavaDoc(usst,
280                                        items,
281                                        new Object JavaDoc[]{"JMX", "blue", "M", new Float JavaDoc(15.0f)});
282       eurosdata.put(eurosnmpshirt);
283       eurosdata.put(eurojmxshirt);
284
285       assertTrue("Expecting US and Euro shirt data to be equal",
286                  ussdata.equals(eurosdata));
287       assertTrue("Expecting US and Euro shirt data hash codes to be equal",
288                  eurosdata.hashCode() == ussdata.hashCode());
289       int ushashcode = ussdata.getTabularType().hashCode();
290       ushashcode += ussnmpshirt.hashCode();
291       ushashcode += usjmxshirt.hashCode();
292       assertTrue("Unexpected hash code computation", ussdata.hashCode() == ushashcode);
293    }
294 }
295
Popular Tags