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.Set;
011: import javax.management.openmbean.OpenMBeanConstructorInfo;
012: import javax.management.openmbean.OpenMBeanConstructorInfoSupport;
013: import javax.management.openmbean.OpenMBeanParameterInfo;
014: import javax.management.openmbean.OpenMBeanParameterInfoSupport;
015: import javax.management.openmbean.OpenType;
016: import javax.management.openmbean.SimpleType;
017:
018: import junit.framework.TestCase;
019: import junit.textui.TestRunner;
020:
021: /**
022: */
023: public class OpenMBeanConstructorInfoSupportTest extends TestCase {
024: private static class MyMBeanParameterInfo implements
025: OpenMBeanParameterInfo {
026: public boolean equals(Object o) {
027: return false;
028: }
029:
030: public Object getDefaultValue() {
031: return null;
032: }
033:
034: public String getDescription() {
035: return null;
036: }
037:
038: public Set getLegalValues() {
039: return null;
040: }
041:
042: public Comparable getMaxValue() {
043: return null;
044: }
045:
046: public Comparable getMinValue() {
047: return null;
048: }
049:
050: public String getName() {
051: return null;
052: }
053:
054: public OpenType getOpenType() {
055: return null;
056: }
057:
058: public boolean hasDefaultValue() {
059: return false;
060: }
061:
062: public boolean hasLegalValues() {
063: return false;
064: }
065:
066: public boolean hasMinValue() {
067: return false;
068: }
069:
070: public boolean hasMaxValue() {
071: return false;
072: }
073:
074: public boolean isValue(Object o) {
075: return false;
076: }
077:
078: public String toString() {
079: return null;
080: }
081: }
082:
083: private OpenMBeanParameterInfo[] signature;
084:
085: public static void main(String[] args) {
086: TestRunner.run(OpenMBeanConstructorInfoSupportTest.class);
087: }
088:
089: public void testCtor() throws Exception {
090: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
091: "wine", "Non-default constructor", signature);
092: assertTrue("Null info constructed", info != null);
093: assertTrue("Unexpected name",
094: info.getName().compareTo("wine") == 0);
095: assertTrue("Unexpected description", info.getDescription()
096: .compareTo("Non-default constructor") == 0);
097: assertTrue("Unexpected signature", Arrays.equals(info
098: .getSignature(), signature));
099:
100: info = new OpenMBeanConstructorInfoSupport("wine",
101: "Non-default constructor", null);
102: assertTrue("Null info constructed", info != null);
103: assertTrue("Unexpected name",
104: info.getName().compareTo("wine") == 0);
105: assertTrue("Unexpected description", info.getDescription()
106: .compareTo("Non-default constructor") == 0);
107: assertTrue("Unexpected signature", info.getSignature() == null
108: || info.getSignature().length == 0);
109:
110: info = new OpenMBeanConstructorInfoSupport("wine",
111: "Non-default constructor",
112: new OpenMBeanParameterInfoSupport[0]);
113: assertTrue("Null info constructed", info != null);
114: assertTrue("Unexpected name",
115: info.getName().compareTo("wine") == 0);
116: assertTrue("Unexpected description", info.getDescription()
117: .compareTo("Non-default constructor") == 0);
118: assertTrue("Unexpected signature",
119: info.getSignature().length == 0);
120: }
121:
122: public void testCtorNullName() throws Exception {
123: try {
124: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
125: null, "Non-default constructor", signature);
126: fail("Expecting IllegalArgumentException");
127: } catch (IllegalArgumentException x) {
128: assertTrue(true);
129: }
130: }
131:
132: public void testCtorEmptyName() throws Exception {
133: try {
134: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
135: "", "Non-default constructor", signature);
136: fail("Expecting IllegalArgumentException");
137: } catch (IllegalArgumentException x) {
138: assertTrue(true);
139: }
140: }
141:
142: public void testCtorNullDescription() throws Exception {
143: try {
144: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
145: "wine", null, signature);
146: fail("Expecting IllegalArgumentException");
147: } catch (IllegalArgumentException x) {
148: assertTrue(true);
149: }
150: }
151:
152: public void testCtorEmptyDescription() throws Exception {
153: try {
154: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
155: "wine", "", signature);
156: fail("Expecting IllegalArgumentException");
157: } catch (IllegalArgumentException x) {
158: assertTrue(true);
159: }
160: }
161:
162: public void testCtorBogusSignature() throws Exception {
163: try {
164: MyMBeanParameterInfo[] bogusig = {
165: new MyMBeanParameterInfo(),
166: new MyMBeanParameterInfo(),
167: new MyMBeanParameterInfo() };
168:
169: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
170: "wine", "Non-default constructor", bogusig);
171: fail("Expecting ArrayStoreException");
172: } catch (ArrayStoreException x) {
173: assertTrue(true);
174: }
175: }
176:
177: public void testEquals() throws Exception {
178: OpenMBeanConstructorInfo infoone = new OpenMBeanConstructorInfoSupport(
179: "wine", "Vino", signature);
180: assertTrue("Null infoone constructed", infoone != null);
181:
182: OpenMBeanConstructorInfo infotwo = new OpenMBeanConstructorInfoSupport(
183: "wine", "Nectar of the gods", signature);
184: assertTrue("Null infotwo constructed", infotwo != null);
185:
186: assertTrue("Expected equality", infoone.equals(infotwo));
187:
188: OpenMBeanConstructorInfo infothree = new OpenMBeanConstructorInfoSupport(
189: "Vino", "Vino", signature);
190: assertTrue("Null infothree constructed", infothree != null);
191:
192: assertFalse("Expected inequality based on name", infothree
193: .equals(infoone));
194:
195: OpenMBeanConstructorInfo infofour = new OpenMBeanConstructorInfoSupport(
196: "wine", "Vino", new OpenMBeanParameterInfoSupport[0]);
197: assertTrue("Null infofour constructed", infofour != null);
198:
199: assertFalse("Expected inequality based on signature", infofour
200: .equals(infoone));
201: }
202:
203: public void testHashCode() throws Exception {
204: OpenMBeanConstructorInfo infoone = new OpenMBeanConstructorInfoSupport(
205: "wine", "Vino", signature);
206: assertTrue("Null infoone constructed", infoone != null);
207:
208: assertTrue("Unexpected hash code",
209: infoone.hashCode() == hashCode(infoone));
210:
211: OpenMBeanConstructorInfo infotwo = new OpenMBeanConstructorInfoSupport(
212: "wine", "Nectar of the gods", signature);
213: assertTrue("Null infotwo constructed", infotwo != null);
214:
215: assertTrue("Expecting equal hash codes",
216: infoone.hashCode() == infotwo.hashCode());
217: }
218:
219: protected void setUp() {
220: try {
221: signature = new OpenMBeanParameterInfoSupport[] {
222: new OpenMBeanParameterInfoSupport("type",
223: "type of wine", SimpleType.STRING, "Red",
224: new String[] { "Red", "White", "Rose" }),
225: new OpenMBeanParameterInfoSupport("winery",
226: "who produced the wine", SimpleType.STRING),
227: new OpenMBeanParameterInfoSupport("vintage",
228: "when the wine was produced",
229: SimpleType.INTEGER, null,
230: new Integer(1900), new Integer(2000)) };
231: } catch (Exception x) {
232: fail(x.toString());
233: }
234: }
235:
236: protected void tearDown() {
237: }
238:
239: private int hashCode(OpenMBeanConstructorInfo info) {
240: int result = info.getName().hashCode();
241: result += Arrays.asList(info.getSignature()).hashCode();
242: return result;
243: }
244: }
|