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.5 $
011: */
012:
013: import java.util.Arrays;
014: import javax.management.MBeanOperationInfo;
015: import javax.management.openmbean.OpenMBeanOperationInfo;
016: import javax.management.openmbean.OpenMBeanOperationInfoSupport;
017: import javax.management.openmbean.OpenMBeanParameterInfo;
018: import javax.management.openmbean.OpenMBeanParameterInfoSupport;
019: import javax.management.openmbean.SimpleType;
020:
021: import junit.framework.TestCase;
022:
023: public class OpenMBeanOperationInfoSupportTest extends TestCase {
024: OpenMBeanParameterInfo[] signature;
025: OpenMBeanParameterInfo[] params = null;
026:
027: public OpenMBeanOperationInfoSupportTest(String s) {
028: super (s);
029: }
030:
031: protected void setUp() throws Exception {
032: super .setUp();
033:
034: params = new OpenMBeanParameterInfo[] {
035:
036: new OpenMBeanParameterInfoSupport("name", "The name",
037: SimpleType.STRING) };
038:
039: signature = new OpenMBeanParameterInfo[] {
040: new OpenMBeanParameterInfoSupport("from",
041: "currency to convert from", SimpleType.STRING),
042: new OpenMBeanParameterInfoSupport("to",
043: "currency to convert to", SimpleType.STRING) };
044: }
045:
046: protected void tearDown() throws Exception {
047: super .tearDown();
048: }
049:
050: public void testCtor() throws Exception {
051: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
052: "exchangeRate",
053: "compute the exchange rate for two currencies",
054: signature, SimpleType.FLOAT, MBeanOperationInfo.INFO);
055: assertTrue("Null info constructed", info != null);
056: assertTrue("Unexpected name", info.getName().compareTo(
057: "exchangeRate") == 0);
058: assertTrue(
059: "Unexpected description",
060: info.getDescription().compareTo(
061: "compute the exchange rate for two currencies") == 0);
062: assertTrue("Unexpected return open type", info
063: .getReturnOpenType().equals(SimpleType.FLOAT));
064: assertTrue("Unexpected signature", Arrays.equals(info
065: .getSignature(), signature));
066: assertTrue("Unexpected impact",
067: info.getImpact() == MBeanOperationInfo.INFO);
068: }
069:
070: public void testCtorNullName() throws Exception {
071: try {
072: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
073: null,
074: "compute the exchange rate for two currencies",
075: signature, SimpleType.FLOAT,
076: MBeanOperationInfo.INFO);
077: fail("Expecting IllegalArgumentException");
078: } catch (IllegalArgumentException x) {
079: assertTrue(true);
080: }
081: }
082:
083: public void testCtorEmptyName() throws Exception {
084: try {
085: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
086: "", "compute the exchange rate for two currencies",
087: signature, SimpleType.FLOAT,
088: MBeanOperationInfo.INFO);
089: fail("Expecting IllegalArgumentException");
090: } catch (IllegalArgumentException x) {
091: assertTrue(true);
092: }
093: }
094:
095: public void testCtorNullDescription() throws Exception {
096: try {
097: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
098: "exchangeRate", null, signature, SimpleType.FLOAT,
099: MBeanOperationInfo.INFO);
100: fail("Expecting IllegalArgumentException");
101: } catch (IllegalArgumentException x) {
102: assertTrue(true);
103: }
104: }
105:
106: public void testCtorEmptyDescription() throws Exception {
107: try {
108: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
109: "exchangeRate", "", signature, SimpleType.FLOAT,
110: MBeanOperationInfo.INFO);
111: fail("Expecting IllegalArgumentException");
112: } catch (IllegalArgumentException x) {
113: assertTrue(true);
114: }
115: }
116:
117: public void testCtorNullSignature() throws Exception {
118: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
119: "exchangeRate",
120: "compute the exchange rate for two currencies", null,
121: SimpleType.FLOAT, MBeanOperationInfo.INFO);
122: assertTrue("Null info constructed", info != null);
123: assertTrue("Unexpected name", info.getName().compareTo(
124: "exchangeRate") == 0);
125: assertTrue(
126: "Unexpected description",
127: info.getDescription().compareTo(
128: "compute the exchange rate for two currencies") == 0);
129: assertTrue("Unexpected return open type", info
130: .getReturnOpenType().equals(SimpleType.FLOAT));
131: assertTrue("Unexpected signature", Arrays.equals(info
132: .getSignature(), new OpenMBeanParameterInfo[0]));
133: assertTrue("Unexpected impact",
134: info.getImpact() == MBeanOperationInfo.INFO);
135: }
136:
137: public void testCtorEmptySignature() throws Exception {
138: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
139: "exchangeRate",
140: "compute the exchange rate for two currencies",
141: new OpenMBeanParameterInfo[0], SimpleType.FLOAT,
142: MBeanOperationInfo.INFO);
143: assertTrue("Null info constructed", info != null);
144: assertTrue("Unexpected name", info.getName().compareTo(
145: "exchangeRate") == 0);
146: assertTrue(
147: "Unexpected description",
148: info.getDescription().compareTo(
149: "compute the exchange rate for two currencies") == 0);
150: assertTrue("Unexpected return open type", info
151: .getReturnOpenType().equals(SimpleType.FLOAT));
152: assertTrue("Unexpected signature", Arrays.equals(info
153: .getSignature(), new OpenMBeanParameterInfo[0]));
154: assertTrue("Unexpected impact",
155: info.getImpact() == MBeanOperationInfo.INFO);
156: }
157:
158: public void testCtorNullReturnOpenType() throws Exception {
159: try {
160: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
161: "exchangeRate",
162: "compute the exchange rate for two currencies",
163: signature, null, MBeanOperationInfo.INFO);
164: fail("Expecting IllegalArgumentException");
165: } catch (IllegalArgumentException x) {
166: assertTrue(true);
167: }
168: }
169:
170: public void testCtorBogusImpact() throws Exception {
171: try {
172: OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport(
173: "exchangeRate",
174: "compute the exchange rate for two currencies",
175: signature, SimpleType.FLOAT, 42);
176: fail("Expecting IllegalArgumentException");
177: } catch (IllegalArgumentException x) {
178: assertTrue(true);
179: }
180: }
181:
182: public void testEquals() throws Exception {
183:
184: OpenMBeanOperationInfoSupport o1 = new OpenMBeanOperationInfoSupport(
185: "hello", "Say Hello", params, SimpleType.STRING, 1);
186: OpenMBeanOperationInfoSupport o2 = new OpenMBeanOperationInfoSupport(
187: "hello", "Say Hello", params, SimpleType.STRING, 1);
188: OpenMBeanOperationInfoSupport o3 = new OpenMBeanOperationInfoSupport(
189: "hello", "Say Hello", params, SimpleType.STRING, 0);
190: OpenMBeanOperationInfoSupport o4 = new OpenMBeanOperationInfoSupport(
191: "goAway", "Go Away", params, SimpleType.STRING, 1);
192: OpenMBeanOperationInfoSupport o5 = new OpenMBeanOperationInfoSupport(
193: "goAway", "Hey There", params, SimpleType.STRING, 1);
194: OpenMBeanOperationInfoSupport o6 = new OpenMBeanOperationInfoSupport(
195: "goAway", "Hey There", params, SimpleType.INTEGER, 1);
196:
197: //test
198: assertTrue(!o1.equals(null));
199: assertTrue(o1.equals(o2));
200: assertEquals(o1.hashCode(), o2.hashCode());
201: assertTrue(!o1.equals(o3));
202: assertTrue(o4.equals(o5));
203: assertTrue(!o5.equals(o6));
204:
205: }
206:
207: public void testHashCode() throws Exception {
208: OpenMBeanOperationInfoSupport infoone = new OpenMBeanOperationInfoSupport(
209: "convertPrice",
210: "converts the price from one currency to another",
211: signature, SimpleType.FLOAT, MBeanOperationInfo.ACTION);
212: assertTrue("Unexpected hash code",
213: infoone.hashCode() == hashCode(infoone));
214:
215: OpenMBeanOperationInfoSupport infotwo = new OpenMBeanOperationInfoSupport(
216: "convertPrice",
217: "multiply Currency by exchange rate to get TargetCurrency price",
218: signature, SimpleType.FLOAT, MBeanOperationInfo.ACTION);
219: assertTrue("Unexpected hash code",
220: infotwo.hashCode() == hashCode(infoone));
221: }
222:
223: private int hashCode(OpenMBeanOperationInfo info) {
224: int result = info.getName().hashCode();
225: result += info.getReturnOpenType().hashCode();
226: result += info.getImpact();
227: result += java.util.Arrays.asList(info.getSignature())
228: .hashCode();
229: return result;
230: }
231:
232: }
|