001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package test.javax.management.openmbean;
008:
009: import java.util.Arrays;
010: import java.util.HashSet;
011: import java.util.List;
012: import javax.management.openmbean.CompositeType;
013: import javax.management.openmbean.OpenDataException;
014: import javax.management.openmbean.OpenType;
015: import javax.management.openmbean.SimpleType;
016: import javax.management.openmbean.TabularType;
017:
018: import test.MX4JTestCase;
019:
020: /**
021: * @version $Revision: 1.8 $
022: */
023: public class TabularTypeTest extends MX4JTestCase {
024: private String[] itemNames = null;
025: private String[] itemDescriptions = null;
026: private OpenType[] itemTypes;
027: private String[] indexNames;
028: private CompositeType tShirtType;
029: private CompositeType wine;
030: private TabularType allTShirtTypes;
031:
032: public TabularTypeTest(String s) {
033: super (s);
034: }
035:
036: protected void setUp() throws Exception {
037: itemNames = new String[] { "model", "color", "size", "price" };
038: itemDescriptions = new String[] { "TShirt's model name",
039: "TShirt's color", "TShirt's size", "TShirt's price" };
040: itemTypes = new OpenType[] { SimpleType.STRING,
041: SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT };
042: indexNames = new String[] { "model", "color", "size" };
043: tShirtType = new CompositeType("tShirt", "a TShirt", itemNames,
044: itemDescriptions, itemTypes);
045:
046: allTShirtTypes = new TabularType("tShirts",
047: "List of available TShirts", tShirtType, indexNames);
048:
049: wine = new CompositeType("Wine", "Nectar of the gods",
050: new String[] { "Type", "Winery", "Vintage" },
051: new String[] { "Red, White, Rose, etc.",
052: "The wine's producer",
053: "The year the wine was made" }, new OpenType[] {
054: SimpleType.STRING, SimpleType.STRING,
055: SimpleType.INTEGER });
056: }
057:
058: public void testEquals() throws Exception {
059: String[] itemNames2 = { "model", "color", "size", "price" };
060: String[] itemDescriptions2 = { "Manufacturer's model name",
061: "The shirt's color", "How big is it",
062: "How much does it cost" };
063: OpenType[] itemTypes2 = { SimpleType.STRING, SimpleType.STRING,
064: SimpleType.STRING, SimpleType.FLOAT };
065: String[] indexNames2 = { "model", "color", "size" };
066: CompositeType tShirtType2 = new CompositeType("tShirt",
067: "a TShirt", itemNames2, itemDescriptions2, itemTypes2);
068: TabularType allTShirtTypes2 = new TabularType("tShirts",
069: "List of available TShirts", tShirtType2, indexNames2);
070: assertTrue(allTShirtTypes.equals(allTShirtTypes2));
071: // assert reverse
072: assertTrue(allTShirtTypes2.equals(allTShirtTypes));
073: }
074:
075: public void testNotEqual() throws Exception {
076: String[] itemNames3 = { "model3", "color3", "size3", "price3" };
077: String[] itemDescriptions3 = { "TShirt's model name",
078: "TShirt's color", "TShirt's size", "TShirt's price" };
079: OpenType[] itemTypes3 = { SimpleType.STRING,
080: SimpleType.BOOLEAN, SimpleType.STRING, SimpleType.FLOAT };
081: String[] indexNames3 = { "model3", "color3", "size3" };
082: CompositeType tShirtType3 = new CompositeType("tShirt3",
083: "a TShirt", itemNames3, itemDescriptions3, itemTypes3);
084: TabularType allTShirtTypes3 = new TabularType("tShirts3",
085: "List of available TShirts", tShirtType3, indexNames3);
086: assertFalse(allTShirtTypes.equals(allTShirtTypes3));
087: }
088:
089: public void testGetRowType() {
090: CompositeType test = allTShirtTypes.getRowType();
091: assertEquals(test, tShirtType);
092: }
093:
094: public void testGetIndexNames() {
095: List temp = allTShirtTypes.getIndexNames();
096: String[] tempList = (String[]) temp.toArray(new String[temp
097: .size()]);
098:
099: assertEquals(tempList.length, indexNames.length);
100: // assert all elements are the same
101: for (int i = 0; i < tempList.length; i++) {
102: assertTrue(tempList[i] == indexNames[i]);
103: }
104: }
105:
106: public void testHashCode() throws Exception {
107: String[] itemNames2 = { "model", "color", "size", "price" };
108: String[] itemDescriptions2 = { "Manufacturer's model name",
109: "The shirt's color", "How big is it",
110: "How much does it cost" };
111: OpenType[] itemTypes2 = { SimpleType.STRING, SimpleType.STRING,
112: SimpleType.STRING, SimpleType.FLOAT };
113: String[] indexNames2 = { "model", "color", "size" };
114: CompositeType tShirtType2 = new CompositeType("tShirt",
115: "a TShirt", itemNames2, itemDescriptions2, itemTypes2);
116: TabularType allTShirtTypes2 = new TabularType("tShirts",
117: "List of available TShirts", tShirtType2, indexNames2);
118:
119: String[] itemNames3 = { "model3", "color3", "size3", "price3" };
120: String[] itemDescriptions3 = { "TShirt's model name",
121: "TShirt's color", "TShirt's size", "TShirt's price" };
122: OpenType[] itemTypes3 = { SimpleType.STRING,
123: SimpleType.BOOLEAN, SimpleType.STRING, SimpleType.FLOAT };
124: String[] indexNames3 = { "model3", "color3", "size3" };
125: CompositeType tShirtType3 = new CompositeType("tShirt3",
126: "a TShirt", itemNames3, itemDescriptions3, itemTypes3);
127: TabularType allTShirtTypes3 = new TabularType("tShirts3",
128: "List of available TShirts", tShirtType3, indexNames3);
129:
130: int code1 = allTShirtTypes.hashCode();
131: int code2 = allTShirtTypes2.hashCode();
132: assertEquals(code2, "tShirts".hashCode()
133: + tShirtType2.hashCode()
134: + (new HashSet(Arrays.asList(indexNames))).hashCode());
135: int code3 = allTShirtTypes3.hashCode();
136:
137: assertEquals(code1, code2);
138: assertTrue(code1 != code3);
139: }
140:
141: public void testCtor() throws Exception {
142: TabularType winecatalog = new TabularType("Wine Catalog",
143: "Catalog of available wines", wine, new String[] {
144: "Winery", "Vintage" });
145: assertTrue("Unexpected name", winecatalog.getTypeName()
146: .compareTo("Wine Catalog") == 0);
147: assertTrue("Unexpected CompositeType", winecatalog.getRowType()
148: .equals(wine));
149: List index = winecatalog.getIndexNames();
150: assertTrue("Incorrect index size", index.size() == 2);
151: assertTrue("Unexpected index entries", ((String) index.get(0))
152: .compareTo("Winery") == 0
153: && ((String) index.get(1)).compareTo("Vintage") == 0);
154: }
155:
156: public void testCtorNullName() throws Exception {
157: try {
158: new TabularType(null, "Catalog of available wines", wine,
159: new String[] { "Winery", "Vintage" });
160: fail("Expecting IllegalArgumentException");
161: } catch (IllegalArgumentException x) {
162: }
163: }
164:
165: public void testCtorEmptyName() throws Exception {
166: try {
167: new TabularType("", "Catalog of available wines", wine,
168: new String[] { "Winery", "Vintage" });
169: fail("Expecting IllegalArgumentException");
170: } catch (IllegalArgumentException x) {
171: }
172: }
173:
174: public void testCtorNullDescriptor() throws Exception {
175: try {
176: new TabularType("Wine Catalog", null, wine, new String[] {
177: "Winery", "Vintage" });
178: fail("Expecting IllegalArgumentException");
179: } catch (IllegalArgumentException x) {
180: }
181: }
182:
183: public void testCtorEmptyDescriptor() throws Exception {
184: try {
185: new TabularType("Wine Catalog", "", wine, new String[] {
186: "Winery", "Vintage" });
187: fail("Expecting IllegalArgumentException");
188: } catch (IllegalArgumentException x) {
189: }
190: }
191:
192: public void testCtorNullRowType() throws Exception {
193: try {
194: new TabularType("Wine Catalog",
195: "Catalog of available wines", null, new String[] {
196: "Winery", "Vintage" });
197: fail("Expecting IllegalArgumentException");
198: } catch (IllegalArgumentException x) {
199: }
200: }
201:
202: public void testCtorNullIndex() throws Exception {
203: try {
204: new TabularType("Wine Catalog",
205: "Catalog of available wines", wine, null);
206: fail("Expecting IllegalArgumentException");
207: } catch (IllegalArgumentException x) {
208: }
209: }
210:
211: public void testCtorEmptyIndex() throws Exception {
212: try {
213: new TabularType("Wine Catalog",
214: "Catalog of available wines", wine, new String[0]);
215: fail("Expecting IllegalArgumentException");
216: } catch (IllegalArgumentException x) {
217: }
218: }
219:
220: public void testCtorBogusIndex() throws Exception {
221: try {
222: new TabularType("Wine Catalog",
223: "Catalog of available wines", wine, new String[] {
224: "Region", "Vintage" });
225: fail("Expecting OpenDataException");
226: } catch (OpenDataException x) {
227: }
228: }
229: }
|