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.Iterator;
010: import java.util.Set;
011: import javax.management.openmbean.ArrayType;
012: import javax.management.openmbean.OpenDataException;
013: import javax.management.openmbean.OpenMBeanParameterInfo;
014: import javax.management.openmbean.OpenMBeanParameterInfoSupport;
015: import javax.management.openmbean.SimpleType;
016:
017: import junit.framework.TestCase;
018: import test.javax.management.compliance.serialization.support.Serializer;
019:
020: /**
021: * @version $Revision: 1.6 $
022: */
023:
024: public class OpenMBeanParameterInfoSupportTest extends TestCase {
025: private String[] legalModels;
026: private String[] legalColors;
027: private static String[] legalSizes;
028: private float minPrice;
029: private float maxPrice;
030: private OpenMBeanParameterInfoSupport priceParamInfo;
031:
032: public OpenMBeanParameterInfoSupportTest(String s) {
033: super (s);
034: }
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: legalModels = new String[] { "JDMK", "JMX", "JAVA" };
039: legalColors = new String[] { "black", "white", "red", "green",
040: "blue" };
041: legalSizes = new String[] { "S", "M", "L", "XL", "XXL" };
042: minPrice = 9.00f;
043: maxPrice = 19.99f;
044: priceParamInfo = new OpenMBeanParameterInfoSupport("price",
045: "Valid product price", SimpleType.FLOAT, new Float(
046: 10.00), // default price
047: new Float(minPrice), // Min legal value for price
048: new Float(maxPrice)); // Max legal value for price
049: }
050:
051: protected void tearDown() throws Exception {
052: super .tearDown();
053: }
054:
055: public void testEquals() {
056: try {
057: OpenMBeanParameterInfoSupport infoSupport = new OpenMBeanParameterInfoSupport(
058: "test", "hello world", SimpleType.STRING, "black",
059: legalColors);
060: OpenMBeanParameterInfoSupport equalInfoSupport = new OpenMBeanParameterInfoSupport(
061: "test", "hello world", SimpleType.STRING, "black",
062: legalColors);
063: OpenMBeanParameterInfoSupport info2Support = new OpenMBeanParameterInfoSupport(
064: "test2", "hello world2", SimpleType.STRING);
065:
066: OpenMBeanParameterInfoSupport priceParamInfo2 = new OpenMBeanParameterInfoSupport(
067: "price", "Valid product price", SimpleType.FLOAT,
068: new Float(10.00), // default price
069: new Float(minPrice), // Min legal value for price
070: new Float(maxPrice)); // Max legal value for price
071: // test we can equal null values
072: assertTrue(!(infoSupport.equals(info2Support)));
073:
074: assertTrue(infoSupport.equals(equalInfoSupport));
075: assertTrue(equalInfoSupport.equals(infoSupport));
076: assertTrue(priceParamInfo.equals(priceParamInfo2));
077:
078: OpenMBeanParameterInfo rebootcmd = new OpenMBeanParameterInfoSupport(
079: "reboot", "Reboot the server", SimpleType.INTEGER);
080: OpenMBeanParameterInfo rebootquery = new OpenMBeanParameterInfoSupport(
081: "reboot", "Reboot the server", SimpleType.BOOLEAN);
082: assertFalse("activeclients.equals(reboot)", rebootcmd
083: .equals(rebootquery));
084: } catch (OpenDataException e) {
085: e.printStackTrace();
086: }
087:
088: }
089:
090: public void testDefaultSerialization() {
091: int expectedHash = priceParamInfo.hashCode();
092: Serializer serializer = new Serializer();
093:
094: try {
095: byte[] data = serializer.serialize(priceParamInfo);
096:
097: Object obj = serializer.deserialize(data);
098: // assert instanceof
099: assertTrue(obj instanceof OpenMBeanParameterInfo);
100:
101: // if instanceof passes continue otherwise we will not get to the rest
102: OpenMBeanParameterInfo type = (OpenMBeanParameterInfo) obj;
103: // assert hashcodes are equal
104: assertEquals(type.hashCode(), expectedHash);
105: assertTrue(type.equals(priceParamInfo));
106: assertTrue(priceParamInfo.equals(type));
107: } catch (Exception e) {
108: e.printStackTrace();
109: }
110: }
111:
112: public void testBasicCtor() {
113: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
114: "currency", "monetary currency", SimpleType.STRING);
115: assertTrue("Null info constructed", info != null);
116: assertTrue("Unexpected name", info.getName().compareTo(
117: "currency") == 0);
118: assertTrue("Unexpected description", info.getDescription()
119: .compareTo("monetary currency") == 0);
120: assertTrue("Unexpected open type", info.getOpenType().equals(
121: SimpleType.STRING));
122: assertFalse("Shouldn't have default value", info
123: .hasDefaultValue());
124: assertFalse("Shouldn't have legal values", info
125: .hasLegalValues());
126: assertFalse("Shouldn't have a min value", info.hasMinValue());
127: assertFalse("Shouldn't have a max value", info.hasMaxValue());
128: }
129:
130: public void testBasicCtorNullName() {
131: try {
132: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
133: null, "monetary currency", SimpleType.STRING);
134: fail("Expecting IllegalArgumentException");
135: } catch (IllegalArgumentException x) {
136: assertTrue(true);
137: }
138: }
139:
140: public void testBasicCtorEmptyName() {
141: try {
142: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
143: "", "monetary currency", SimpleType.STRING);
144: fail("Expecting IllegalArgumentException");
145: } catch (IllegalArgumentException x) {
146: assertTrue(true);
147: }
148: }
149:
150: public void testBasicCtorNullDescription() {
151: try {
152: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
153: "currency", null, SimpleType.STRING);
154: fail("Expecting IllegalArgumentException");
155: } catch (IllegalArgumentException x) {
156: assertTrue(true);
157: }
158: }
159:
160: public void testBasicCtorEmptyDescription() {
161: try {
162: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
163: "currency", "", SimpleType.STRING);
164: fail("Expecting IllegalArgumentException");
165: } catch (IllegalArgumentException x) {
166: assertTrue(true);
167: }
168: }
169:
170: public void testBasicCtorNullOpenType() {
171: try {
172: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
173: "currency", "monetary currency", null);
174: fail("Expecting IllegalArgumentException");
175: } catch (IllegalArgumentException x) {
176: assertTrue(true);
177: }
178: }
179:
180: public void testDefaultValueCtor() throws Exception {
181: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
182: "currency", "monetary currency", SimpleType.STRING,
183: "Euro");
184: assertTrue("Null info constructed", info != null);
185: assertTrue("Unexpected name", info.getName().compareTo(
186: "currency") == 0);
187: assertTrue("Unexpected description", info.getDescription()
188: .compareTo("monetary currency") == 0);
189: assertTrue("Unexpected open type", info.getOpenType().equals(
190: SimpleType.STRING));
191: assertTrue("Should have default value", info.hasDefaultValue());
192: assertTrue("Unexpected default value", ((String) info
193: .getDefaultValue()).compareTo("Euro") == 0);
194: assertFalse("Shouldn't have legal values", info
195: .hasLegalValues());
196: assertFalse("Shouldn't have a min value", info.hasMinValue());
197: assertFalse("Shouldn't have a max value", info.hasMaxValue());
198: }
199:
200: public void testDefaultValueCtorInvalidType() throws Exception {
201: try {
202: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
203: "currency", "monetary currency", SimpleType.STRING,
204: new Float(0.42));
205: fail("Expecting OpenDataException");
206: } catch (OpenDataException x) {
207: assertTrue(true);
208: }
209: }
210:
211: public void testDefaultValueCtorArrayType() throws Exception {
212: try {
213: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
214: "currency", "monetary currency", new ArrayType(1,
215: SimpleType.STRING), null);
216: assertTrue("Null info constructed", info != null);
217:
218: info = new OpenMBeanParameterInfoSupport("currency",
219: "monetary currency", new ArrayType(1,
220: SimpleType.STRING), "Euro");
221: fail("Expecting OpenDataException");
222: } catch (OpenDataException x) {
223: assertTrue(true);
224: }
225: }
226:
227: public void testLegalValueCtor() throws Exception {
228: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
229: "currency", "monetary currency", SimpleType.STRING,
230: "Euro", new String[] { "Dollar", "Euro", "Yen" });
231: assertTrue("Null info constructed", info != null);
232: assertTrue("Unexpected name", info.getName().compareTo(
233: "currency") == 0);
234: assertTrue("Unexpected description", info.getDescription()
235: .compareTo("monetary currency") == 0);
236: assertTrue("Unexpected open type", info.getOpenType().equals(
237: SimpleType.STRING));
238: assertTrue("Should have default value", info.hasDefaultValue());
239: assertTrue("Unexpected default value", ((String) info
240: .getDefaultValue()).compareTo("Euro") == 0);
241: assertTrue("Should have legal values",
242: info.getLegalValues() != null
243: && info.getLegalValues().size() == 3);
244: assertFalse("Shouldn't have a min value", info.hasMinValue());
245: assertFalse("Shouldn't have a max value", info.hasMaxValue());
246: }
247:
248: public void testLegalValueCtorInvalidType() throws Exception {
249: try {
250: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
251: "currency", "monetary currency", SimpleType.STRING,
252: "Euro", new Object[] { "Dollar", "Euro",
253: new Float(0.88) });
254: fail("Expecting OpenDataException");
255: } catch (OpenDataException x) {
256: assertTrue(true);
257: }
258: }
259:
260: public void testLegalValueCtorArrayType() throws Exception {
261: try {
262: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
263: "currency", "monetary currency", new ArrayType(1,
264: SimpleType.STRING), null, new String[] {
265: "Dollar", "Euro", "Yen" });
266: fail("Expecting OpenDataException");
267: } catch (OpenDataException x) {
268: assertTrue(true);
269: }
270: }
271:
272: public void testLegalValueCtorBogusDefaultValue() throws Exception {
273: try {
274: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
275: "currency", "monetary currency", SimpleType.STRING,
276: "Pound", new String[] { "Dollar", "Euro", "Yen" });
277: fail("Expecting OpenDataException");
278: } catch (OpenDataException x) {
279: assertTrue(true);
280: }
281: }
282:
283: public void testMinMaxValueCtor() throws Exception {
284: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
285: "price", "how much it costs", SimpleType.FLOAT,
286: new Float(1.00), new Float(0.75), new Float(1.50));
287: assertTrue("Null info constructed", info != null);
288: assertTrue("Unexpected name",
289: info.getName().compareTo("price") == 0);
290: assertTrue("Unexpected description", info.getDescription()
291: .compareTo("how much it costs") == 0);
292: assertTrue("Unexpected open type", info.getOpenType().equals(
293: SimpleType.FLOAT));
294: assertTrue("Should have default value", info.hasDefaultValue());
295: assertTrue("Unexpected default value", ((Float) info
296: .getDefaultValue()).equals(new Float(1.00)));
297: assertFalse("Shouldn't have legal values", info
298: .hasLegalValues());
299: assertTrue("Should have a min value of 0.75", info
300: .hasMinValue()
301: && ((Float) info.getMinValue()).equals(new Float(0.75)));
302: assertTrue("Should have a max value of 1.50", info
303: .hasMaxValue()
304: && ((Float) info.getMaxValue()).equals(new Float(1.50)));
305: }
306:
307: public void testMinMaxValueCtorInvalidMinType() throws Exception {
308: try {
309: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
310: "price", "how much it costs", SimpleType.FLOAT,
311: new Float(1.00), "0.75", new Float(1.50));
312: fail("Expecting OpenDataException");
313: } catch (OpenDataException x) {
314: assertTrue(true);
315: }
316: }
317:
318: public void testMinMaxValueCtorInvalidMaxType() throws Exception {
319: try {
320: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
321: "price", "how much it costs", SimpleType.FLOAT,
322: new Float(1.00), new Float(0.75), "1.50");
323: fail("Expecting OpenDataException");
324: } catch (OpenDataException x) {
325: assertTrue(true);
326: }
327: }
328:
329: public void testMinMaxValueCtorMinGTMax() throws Exception {
330: try {
331: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
332: "price", "how much it costs", SimpleType.FLOAT,
333: new Float(1.00), new Float(1.50), new Float(0.75));
334: fail("Expecting OpenDataException");
335: } catch (OpenDataException x) {
336: assertTrue(true);
337: }
338: }
339:
340: public void testMinMaxValueCtorDefaultOutOfRange() throws Exception {
341: try {
342: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
343: "price", "how much it costs", SimpleType.FLOAT,
344: new Float(0.75), new Float(1.00), new Float(1.50));
345: fail("Expecting OpenDataException default < min");
346: } catch (OpenDataException x) {
347: assertTrue(true);
348: }
349:
350: try {
351: OpenMBeanParameterInfoSupport info = new OpenMBeanParameterInfoSupport(
352: "price", "how much it costs", SimpleType.FLOAT,
353: new Float(1.50), new Float(0.75), new Float(1.00));
354: fail("Expecting OpenDataException default > max");
355: } catch (OpenDataException x) {
356: assertTrue(true);
357: }
358: }
359:
360: public void testBasicHashCode() throws Exception {
361: OpenMBeanParameterInfoSupport infoone = new OpenMBeanParameterInfoSupport(
362: "currency", "monetary currency", SimpleType.STRING);
363: assertTrue("Unexpected basic hash code",
364: infoone.hashCode() == hashCode(infoone));
365:
366: OpenMBeanParameterInfoSupport infotwo = new OpenMBeanParameterInfoSupport(
367: "currency", "legal tender", SimpleType.STRING);
368: assertTrue("Expecting hash codes to be equal", infotwo
369: .hashCode() == infoone.hashCode());
370: }
371:
372: public void testDefaultValueHashCode() throws Exception {
373: OpenMBeanParameterInfoSupport infoone = new OpenMBeanParameterInfoSupport(
374: "currency", "monetary currency", SimpleType.STRING,
375: "Euro");
376: assertTrue("Unexpected default value hash code", infoone
377: .hashCode() == hashCode(infoone));
378:
379: OpenMBeanParameterInfoSupport infotwo = new OpenMBeanParameterInfoSupport(
380: "currency", "legal tender", SimpleType.STRING, "Euro");
381: assertTrue("Unexpected default value hash code", infotwo
382: .hashCode() == infoone.hashCode());
383: }
384:
385: public void testLegalValueHashCode() throws Exception {
386: OpenMBeanParameterInfoSupport infoone = new OpenMBeanParameterInfoSupport(
387: "currency", "monetary currency", SimpleType.STRING,
388: "Euro", new String[] { "Dollar", "Euro", "Yen" });
389: assertTrue("Unexpected legal value hash code", infoone
390: .hashCode() == hashCode(infoone));
391:
392: OpenMBeanParameterInfoSupport infotwo = new OpenMBeanParameterInfoSupport(
393: "currency", "monetary currency", SimpleType.STRING,
394: "Euro", new String[] { "Dollar", "Euro", "Yen" });
395: assertTrue("Unexpected legal value hash code", infoone
396: .hashCode() == hashCode(infotwo));
397: }
398:
399: public void testMinMaxHashCode() throws Exception {
400: OpenMBeanParameterInfoSupport infoone = new OpenMBeanParameterInfoSupport(
401: "price", "how much it costs", SimpleType.FLOAT,
402: new Float(1.00), new Float(0.75), new Float(1.50));
403: assertTrue("Unexpected minmax hash code",
404: infoone.hashCode() == hashCode(infoone));
405:
406: OpenMBeanParameterInfoSupport infotwo = new OpenMBeanParameterInfoSupport(
407: "price", "retail", SimpleType.FLOAT, new Float(1.00),
408: new Float(0.75), new Float(1.50));
409: assertTrue("Unexpected minmax hash code",
410: infotwo.hashCode() == infoone.hashCode());
411: }
412:
413: private int hashCode(OpenMBeanParameterInfo info) {
414: int result = info.getName().hashCode();
415: result += info.getOpenType().hashCode();
416: result += (info.hasDefaultValue() == false) ? 0 : info
417: .getDefaultValue().hashCode();
418: result += (info.hasLegalValues() == false) ? 0 : hashCode(info
419: .getLegalValues());
420: result += (info.hasMinValue() == false) ? 0 : info
421: .getMinValue().hashCode();
422: result += (info.hasMaxValue() == false) ? 0 : info
423: .getMaxValue().hashCode();
424: return result;
425: }
426:
427: private int hashCode(Set legalvalues) {
428: int result = 0;
429: Iterator i = legalvalues.iterator();
430: while (i.hasNext()) {
431: Object v = i.next();
432: result += v.hashCode();
433: }
434: return result;
435: }
436: }
|