KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.openmbean.CompositeDataSupport JavaDoc;
12 import javax.management.openmbean.CompositeType JavaDoc;
13 import javax.management.openmbean.OpenDataException JavaDoc;
14 import javax.management.openmbean.OpenType JavaDoc;
15 import javax.management.openmbean.SimpleType JavaDoc;
16 import javax.management.openmbean.TabularDataSupport JavaDoc;
17 import javax.management.openmbean.TabularType JavaDoc;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @version $Revision: 1.7 $
23  */

24 public class CompositeDataSupportTest extends TestCase
25 {
26    private String JavaDoc[] itemNames = null;
27    private String JavaDoc[] itemDescriptions = null;
28    private OpenType JavaDoc[] itemTypes;
29    private CompositeType JavaDoc tShirtType;
30
31    private String JavaDoc[] indexNames;
32    private TabularType JavaDoc allTShirtTypes;
33    private TabularDataSupport JavaDoc tabularSupport;
34
35    private CompositeDataSupport JavaDoc compositeData;
36
37    public CompositeDataSupportTest(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 testConstructor()
73    {
74       try
75       {
76          Object JavaDoc[] itemValues = new Object JavaDoc[]{"MX4J", "red", "L", new Float JavaDoc(15.0f)};
77          CompositeDataSupport JavaDoc support = new CompositeDataSupport JavaDoc(tShirtType, itemNames, itemValues);
78          assertTrue(support != null);
79       }
80       catch (OpenDataException JavaDoc e)
81       {
82          e.printStackTrace();
83       }
84
85       // test for bug #769086 including a null value
86
try
87       {
88          Object JavaDoc[] itemValues = new Object JavaDoc[]{"MX4J", "red", null, new Float JavaDoc(15.0f)};
89          CompositeDataSupport JavaDoc support = new CompositeDataSupport JavaDoc(tShirtType, itemNames, itemValues);
90          assertTrue(support != null);
91       }
92       catch (OpenDataException JavaDoc e)
93       {
94          e.printStackTrace();
95       }
96    }
97
98    public void testGet()
99    {
100       String JavaDoc expected = "MX4J";
101       String JavaDoc obj = (String JavaDoc)compositeData.get("model");
102       assertTrue("expected was stored as the value against model", expected.equals(obj));
103
104    }
105
106    public void testGetAll()
107    {
108       int expectedLength = 4;
109       Object JavaDoc[] obj = compositeData.getAll(itemNames);
110       assertEquals(expectedLength, obj.length);
111    }
112
113    public void testValues()
114    {
115       int expected = 4;
116       Collection JavaDoc result = compositeData.values();
117       assertEquals(expected, result.size());
118    }
119
120    public void testSparseValues() throws Exception JavaDoc
121    {
122       Object JavaDoc[] sparsevalues = new Object JavaDoc[]{"MX4J", null, "L", new Float JavaDoc(15.0f)};
123       CompositeDataSupport JavaDoc sparsedata = new CompositeDataSupport JavaDoc(tShirtType, itemNames, sparsevalues);
124       assertTrue("Null instance", sparsedata != null);
125    }
126
127    public void testEquals() throws Exception JavaDoc
128    {
129       Object JavaDoc[] testvalues = {"MX4J", "White", "XL", new Float JavaDoc(15.0f)};
130       CompositeDataSupport JavaDoc cdone =
131               new CompositeDataSupport JavaDoc(tShirtType, itemNames, testvalues);
132
133       assertFalse("cdone equals 'null'", cdone.equals(null));
134       assertFalse("cdone equals Integer value",
135                   cdone.equals(new Integer JavaDoc(42)));
136
137       String JavaDoc[] items = new String JavaDoc[]{"model", "color", "size", "price"};
138       String JavaDoc[] descriptions =
139               new String JavaDoc[]{
140                  "TShirt's model name",
141                  "TShirt's color",
142                  "TShirt's size",
143                  "TShirt's price"};
144       OpenType JavaDoc[] types =
145               new OpenType JavaDoc[]{
146                  SimpleType.STRING,
147                  SimpleType.STRING,
148                  SimpleType.STRING,
149                  SimpleType.FLOAT};
150       CompositeType JavaDoc shirt =
151               new CompositeType JavaDoc("tShirt",
152                                 "A 'Tee' Shirt",
153                                 items,
154                                 descriptions,
155                                 types);
156       CompositeDataSupport JavaDoc cdtwo =
157               new CompositeDataSupport JavaDoc(shirt, items, testvalues);
158       assertTrue("cdtwo not equal to cdone", cdtwo.equals(cdone));
159
160       cdone =
161       new CompositeDataSupport JavaDoc(tShirtType,
162                                items,
163                                new Object JavaDoc[]{"GAP", null, "S", new Float JavaDoc(30.0f)});
164       cdtwo =
165       new CompositeDataSupport JavaDoc(shirt,
166                                items,
167                                new Object JavaDoc[]{"GAP", null, "S", new Float JavaDoc(30.0f)});
168       assertTrue("sparse cdtwo not equal to sparse cdone",
169                  cdtwo.equals(cdone));
170
171    }
172 }
173
Popular Tags