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: /**
010: * @version $Revision: 1.7 $
011: */
012:
013: import java.util.Iterator;
014: import java.util.Set;
015: import javax.management.openmbean.ArrayType;
016: import javax.management.openmbean.OpenDataException;
017: import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
018: import javax.management.openmbean.SimpleType;
019:
020: import junit.framework.TestCase;
021:
022: public class OpenMBeanAttributeInfoSupportTest extends TestCase {
023:
024: public OpenMBeanAttributeInfoSupportTest(String s) {
025: super (s);
026: }
027:
028: protected void setUp() throws Exception {
029: super .setUp();
030:
031: }
032:
033: protected void tearDown() throws Exception {
034: super .tearDown();
035: }
036:
037: public void testEquals() throws Exception {
038: try {
039:
040: OpenMBeanAttributeInfoSupport o1 = new OpenMBeanAttributeInfoSupport(
041: "name", "The name", SimpleType.STRING, false, true,
042: false);
043: OpenMBeanAttributeInfoSupport o2 = new OpenMBeanAttributeInfoSupport(
044: "name", "The name", SimpleType.STRING, false, true,
045: false);
046: OpenMBeanAttributeInfoSupport o3 = new OpenMBeanAttributeInfoSupport(
047: "name", "The name", SimpleType.STRING, true, false,
048: false);
049:
050: assertTrue(!o1.equals(null));
051: assertTrue(o1.equals(o2));
052: assertEquals(o1.hashCode(), o2.hashCode());
053: assertTrue(!o1.equals(o3));
054:
055: } catch (Exception e) {
056: e.printStackTrace();
057: }
058:
059: }
060:
061: public void testSixParamCtor() throws Exception {
062: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
063: "price", "how much it costs", SimpleType.FLOAT, true,
064: false, false);
065: assertTrue("Null info constructed", info != null);
066: assertTrue("OpenType should be FLOAT", info.getOpenType()
067: .equals(SimpleType.FLOAT));
068: assertTrue("Attribute should be readable", info.isReadable());
069: assertFalse("Attribute should not be writeable", info
070: .isWritable());
071: assertFalse("Attribute is not 'is", info.isIs());
072:
073: try {
074: info = new OpenMBeanAttributeInfoSupport(null,
075: "how much it costs", SimpleType.FLOAT, true, false,
076: false);
077: fail("Expecting exception for null name");
078: } catch (IllegalArgumentException x) {
079: assertTrue(true);
080: }
081:
082: try {
083: info = new OpenMBeanAttributeInfoSupport("",
084: "how much it costs", SimpleType.FLOAT, true, false,
085: false);
086: fail("Expecting exception for empty name");
087: } catch (IllegalArgumentException x) {
088: assertTrue(true);
089: }
090:
091: try {
092: info = new OpenMBeanAttributeInfoSupport("price", null,
093: SimpleType.FLOAT, true, false, false);
094: fail("Expecting exception for null description");
095: } catch (IllegalArgumentException x) {
096: assertTrue(true);
097: }
098:
099: try {
100: info = new OpenMBeanAttributeInfoSupport("price", "",
101: SimpleType.FLOAT, true, false, false);
102: fail("Expecting exception for empty description");
103: } catch (IllegalArgumentException x) {
104: assertTrue(true);
105: }
106:
107: try {
108: info = new OpenMBeanAttributeInfoSupport("price",
109: "how much it costs", null, true, false, false);
110: fail("Expecting exception for null type");
111: } catch (IllegalArgumentException x) {
112: assertTrue(true);
113: }
114: }
115:
116: public void testSevenParamCtor() throws Exception {
117: Float defaultvalue = new Float(1.00);
118: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
119: "price", "how much it costs", SimpleType.FLOAT, true,
120: false, false, defaultvalue);
121: assertTrue("Null info constructed", info != null);
122: assertTrue("Expecting default value of 1.00", defaultvalue
123: .equals((Float) info.getDefaultValue()));
124:
125: info = new OpenMBeanAttributeInfoSupport("price",
126: "how much it costs", SimpleType.FLOAT, true, false,
127: false, null);
128: assertTrue("Null info constructed", info != null);
129: assertFalse("There should be no default value", info
130: .hasDefaultValue());
131: }
132:
133: public void testEightParamCtor() throws Exception {
134: Float[] legalvalues = { new Float(0.75), new Float(1.00),
135: new Float(1.50) };
136: Float defaultvalue = new Float(1.00);
137: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
138: "price", "how much it costs", SimpleType.FLOAT, true,
139: false, false, defaultvalue, legalvalues);
140: assertTrue("Null info constructed", info != null);
141: Set legalset = info.getLegalValues();
142: assertTrue("Legal set is the wrong size",
143: legalset.size() == legalvalues.length);
144: assertTrue("0.75 not in legal set", legalset
145: .contains(new Float(0.75)));
146: assertTrue("1.00 not in legal set", legalset
147: .contains(new Float(1.00)));
148: assertTrue("1.50 not in legal set", legalset
149: .contains(new Float(1.50)));
150:
151: info = new OpenMBeanAttributeInfoSupport("price",
152: "how much it costs", SimpleType.FLOAT, true, false,
153: false, defaultvalue, null);
154: assertTrue("Null info constructed", info != null);
155: assertFalse("There should be no legal value set for null", info
156: .hasLegalValues());
157:
158: info = new OpenMBeanAttributeInfoSupport("price",
159: "how much it costs", SimpleType.FLOAT, true, false,
160: false, defaultvalue, new Float[0]);
161: assertTrue("Null info constructed", info != null);
162: assertFalse("There should be no legal value set for Float[0]",
163: info.hasLegalValues());
164:
165: info = new OpenMBeanAttributeInfoSupport("price",
166: "how much it costs", SimpleType.FLOAT, true, false,
167: false, null, legalvalues);
168: assertTrue("Null info constructed", info != null);
169: assertFalse("Has a default value", info.hasDefaultValue());
170:
171: try {
172: info = new OpenMBeanAttributeInfoSupport("price",
173: "how much it costs", SimpleType.FLOAT, true, false,
174: false, "Invalid Default Value", new Float[0]);
175: fail("Expecting exception for invalid default value");
176: } catch (OpenDataException x) {
177: assertTrue(true);
178: }
179:
180: try {
181: info = new OpenMBeanAttributeInfoSupport("price",
182: "how much it costs", SimpleType.FLOAT, true, false,
183: false, defaultvalue, new Object[] {
184: new Float(0.75), "$1.50" });
185: fail("Expecting exception for invalid legal value");
186: } catch (OpenDataException x) {
187: assertTrue(true);
188: }
189:
190: try {
191: info = new OpenMBeanAttributeInfoSupport("price",
192: "how much it costs", new ArrayType(1,
193: SimpleType.FLOAT), true, false, false,
194: defaultvalue, null);
195: fail("Expecting exception for non null default w/ArrayType attribute");
196: } catch (OpenDataException x) {
197: assertTrue(true);
198: }
199:
200: try {
201: info = new OpenMBeanAttributeInfoSupport("price",
202: "how much it costs", new ArrayType(1,
203: SimpleType.FLOAT), true, false, false,
204: null, new Float[] { new Float(0.75),
205: new Float(1.50) });
206: fail("Expecting exception for non null legal set w/ArrayType attribute");
207: } catch (OpenDataException x) {
208: assertTrue(true);
209: }
210:
211: try {
212: info = new OpenMBeanAttributeInfoSupport("price",
213: "how much it costs", new ArrayType(1,
214: SimpleType.FLOAT), true, false, false,
215: new Float(0.25), legalvalues);
216: fail("Expecting exception for default not in legal set");
217: } catch (OpenDataException x) {
218: assertTrue(true);
219: }
220:
221: try {
222: info = new OpenMBeanAttributeInfoSupport("price",
223: "how much it costs", SimpleType.INTEGER, true,
224: false, false, new Integer(1), new Integer[] {
225: new Integer(0), new Integer(2) });
226: fail("Expecting exception for default not in legal set");
227: } catch (OpenDataException x) {
228: assertTrue(true);
229: }
230: }
231:
232: public void testNineParameCtor() throws Exception {
233: Float defaultvalue = new Float(1.00);
234: Float minvalue = new Float(0.75);
235: Float maxvalue = new Float(1.50);
236: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
237: "price", "how much it costs", SimpleType.FLOAT, true,
238: false, false, defaultvalue, minvalue, maxvalue);
239: assertTrue("Null info constructed", info != null);
240: assertTrue("Expecting min value of 0.75", info.hasMinValue()
241: && minvalue.equals((Float) info.getMinValue()));
242: assertTrue("Expecting max value of 1.50", info.hasMaxValue()
243: && maxvalue.equals((Float) info.getMaxValue()));
244:
245: info = new OpenMBeanAttributeInfoSupport("price",
246: "how much it costs", SimpleType.FLOAT, true, false,
247: false, defaultvalue, null, maxvalue);
248: assertTrue("Null info constructed", info != null);
249: assertFalse("Not expecting a min value", info.hasMinValue());
250: assertTrue("Expecting max value of 1.50", info.hasMaxValue()
251: && maxvalue.equals((Float) info.getMaxValue()));
252:
253: info = new OpenMBeanAttributeInfoSupport("price",
254: "how much it costs", SimpleType.FLOAT, true, false,
255: false, defaultvalue, minvalue, null);
256: assertTrue("Null info constructed", info != null);
257: assertTrue("Expecting min value of 0.75", info.hasMinValue()
258: && minvalue.equals((Float) info.getMinValue()));
259: assertFalse("Not expecting a max value", info.hasMaxValue());
260:
261: try {
262: info = new OpenMBeanAttributeInfoSupport("price",
263: "how much it costs", SimpleType.FLOAT, true, false,
264: false, "1.00", minvalue, maxvalue);
265: fail("Expecting exception for bad default value type");
266: } catch (OpenDataException x) {
267: assertTrue(true);
268: }
269:
270: try {
271: info = new OpenMBeanAttributeInfoSupport("price",
272: "how much it costs", SimpleType.FLOAT, true, false,
273: false, defaultvalue, "0.75", maxvalue);
274: fail("Expecting exception for bad min value type");
275: } catch (OpenDataException x) {
276: assertTrue(true);
277: }
278:
279: try {
280: info = new OpenMBeanAttributeInfoSupport("price",
281: "how much it costs", SimpleType.FLOAT, true, false,
282: false, defaultvalue, minvalue, "1.50");
283: fail("Expecting exception for bad min value type");
284: } catch (OpenDataException x) {
285: assertTrue(true);
286: }
287:
288: try {
289: info = new OpenMBeanAttributeInfoSupport("price",
290: "how much it costs", new ArrayType(1,
291: SimpleType.FLOAT), true, false, false,
292: defaultvalue, minvalue, maxvalue);
293: fail("Expecting exception for non-null default value w/ArrayType attribute");
294: } catch (OpenDataException x) {
295: assertTrue(true);
296: }
297:
298: try {
299: info = new OpenMBeanAttributeInfoSupport("price",
300: "how much it costs", SimpleType.FLOAT, true, false,
301: false, defaultvalue, maxvalue, minvalue);
302: fail("Expecting exception for min > max");
303: } catch (OpenDataException x) {
304: assertTrue(true);
305: }
306:
307: try {
308: info = new OpenMBeanAttributeInfoSupport("price",
309: "how much it costs", SimpleType.FLOAT, true, false,
310: false, minvalue, defaultvalue, maxvalue);
311: fail("Expecting exception for default < min");
312: } catch (OpenDataException x) {
313: assertTrue(true);
314: }
315: }
316:
317: public void testSimpleInfoHashCode() {
318: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
319: "price", "how much it costs", SimpleType.FLOAT, true,
320: false, false);
321: assertTrue("Unexpected hash code for simple info", info
322: .hashCode() == hashCode(info));
323: }
324:
325: public void testDefaultValueHashCode() throws Exception {
326: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
327: "price", "how much it costs", SimpleType.FLOAT, true,
328: false, false, new Float(1.00));
329: assertTrue("Unexpected hash code for info w/default value",
330: info.hashCode() == hashCode(info));
331: }
332:
333: public void testNullDefaultValueHashCode() throws Exception {
334: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
335: "price", "how much it costs", SimpleType.FLOAT, true,
336: false, false, null);
337: assertTrue(
338: "Unexpected hash code for info w/null default value",
339: info.hashCode() == hashCode(info));
340: }
341:
342: public void testLegalValueHashCode() throws Exception {
343: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
344: "price", "how much it costs", SimpleType.FLOAT, true,
345: false, false, new Float(1.00), new Float[] {
346: new Float(0.75), new Float(1.00),
347: new Float(1.50) });
348: assertTrue("Unexpected hash code for info w/legal values", info
349: .hashCode() == hashCode(info));
350: }
351:
352: public void testEmptyLegalValueHashCode() throws Exception {
353: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
354: "price", "how much it costs", SimpleType.FLOAT, true,
355: false, false, new Float(1.00), new Float[0]);
356: assertTrue(
357: "Unexpected hash code for info w/empty legal values",
358: info.hashCode() == hashCode(info));
359: }
360:
361: public void testMinMaxValueHashCode() throws Exception {
362: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
363: "price", "how much it costs", SimpleType.FLOAT, true,
364: false, false, new Float(1.00), new Float(0.75),
365: new Float(1.50));
366: assertTrue("Unexpected hash code for info w/minmax values",
367: info.hashCode() == hashCode(info));
368: }
369:
370: public void testNullMinValueHashCode() throws Exception {
371: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
372: "price", "how much it costs", SimpleType.FLOAT, true,
373: false, false, new Float(1.00), null, new Float(1.50));
374: assertTrue("Unexpected hash code for info w/null min values",
375: info.hashCode() == hashCode(info));
376: }
377:
378: public void testNullMaxValueHashCode() throws Exception {
379: OpenMBeanAttributeInfoSupport info = new OpenMBeanAttributeInfoSupport(
380: "price", "how much it costs", SimpleType.FLOAT, true,
381: false, false, new Float(1.00), new Float(0.75), null);
382: assertTrue(
383: "Unexpected hash code for info w/empty legal values",
384: info.hashCode() == hashCode(info));
385: }
386:
387: private int hashCode(OpenMBeanAttributeInfoSupport info) {
388: int result = info.getName().hashCode();
389: result += info.getOpenType().hashCode();
390: result += (info.hasDefaultValue() == false) ? 0 : info
391: .getDefaultValue().hashCode();
392: result += (info.hasLegalValues() == false) ? 0 : hashCode(info
393: .getLegalValues());
394: result += (info.hasMinValue() == false) ? 0 : info
395: .getMinValue().hashCode();
396: result += (info.hasMaxValue() == false) ? 0 : info
397: .getMaxValue().hashCode();
398: return result;
399: }
400:
401: private int hashCode(Set legalvalues) {
402: int result = 0;
403: Iterator i = legalvalues.iterator();
404: while (i.hasNext()) {
405: Object v = i.next();
406: result += v.hashCode();
407: }
408: return result;
409: }
410: }
|