001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestUtil.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.common;
030:
031: import java.io.File;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.lang.Class;
035: import java.lang.reflect.Method;
036: import java.net.InetAddress;
037: import java.util.ArrayList;
038: import java.util.Iterator;
039: import java.util.List;
040: import junit.framework.TestCase;
041:
042: /**
043: * test class
044: * @author Sun Microsystems, Inc.
045: */
046: public class TestUtil extends TestCase {
047:
048: private int intType;
049: private float floatType;
050:
051: private Integer integerObjType;
052: private Float floatObjType;
053:
054: /**
055: * Creates a new instance of TestMgmtMessage
056: * @param aTestName name
057: */
058: public TestUtil(String aTestName) {
059: super (aTestName);
060: }
061:
062: public void setIntType(int i) {
063: this .intType = i;
064: }
065:
066: public int getIntType() {
067: return this .intType;
068: }
069:
070: public void setFloatType(float f) {
071: this .floatType = f;
072: }
073:
074: public float getFloatType() {
075: return this .floatType;
076: }
077:
078: public void setIntegerObjType(Integer iObj) {
079: this .integerObjType = iObj;
080: }
081:
082: public Integer getIntegerObjType() {
083: return this .integerObjType;
084: }
085:
086: public void setFloatObjType(Float fObj) {
087: this .floatObjType = fObj;
088: }
089:
090: public Float getFloatObjType() {
091: return this .floatObjType;
092: }
093:
094: public static void assignValue(Object obj, String methodName,
095: Class paramType, Object value) {
096: try {
097: Method method = obj.getClass().getMethod(methodName,
098: new Class[] { paramType });
099: method.invoke(obj, new Object[] { value });
100: } catch (Exception ex) {
101: ex.printStackTrace();
102: }
103: }
104:
105: /**
106: * test sucess msg
107: * @throws Exception on error
108: */
109: public void testNewInstance() throws Exception {
110: Object testObj = this ;
111:
112: Object valueObj = Util.newInstance("int", "200");
113: assignValue(testObj, "setIntType", Integer.TYPE, valueObj);
114:
115: valueObj = Util.newInstance("java.lang.Integer", "300");
116: assignValue(testObj, "setIntegerObjType", Integer.class,
117: valueObj);
118:
119: valueObj = Util.newInstance("float", "2.20");
120: assignValue(testObj, "setFloatType", Float.TYPE, valueObj);
121:
122: valueObj = Util.newInstance("java.lang.Float", "3.30");
123: assignValue(testObj, "setFloatObjType", Float.class, valueObj);
124:
125: this .assertTrue(" int value Expected = 200", (200 == this
126: .getIntType()));
127:
128: this .assertTrue(" Integer value Expected = 300", (300 == this
129: .getIntegerObjType().intValue()));
130:
131: this .assertTrue(" float value Expected = 2.2",
132: (((float) 2.2) == this .getFloatType()));
133:
134: this .assertTrue(" Float Object value Expected = 3.3",
135: (((float) 3.3) == this .getFloatObjType().floatValue()));
136:
137: Object fileObj = Util.newInstance("java.io.File", "temp");
138: this .assertTrue("java.io.File type expected ",
139: (fileObj instanceof File));
140: this .assertTrue("File Object Value expected is temp ", fileObj
141: .toString().equals("temp"));
142:
143: }
144:
145: public void testForLocalHost() throws Exception {
146:
147: this .assertTrue("expected localhost true for : host=empty ",
148: Util.isLocalHost(""));
149:
150: this .assertTrue("expected localhost true for : host=null ",
151: Util.isLocalHost(null));
152:
153: this .assertTrue(
154: "expected localhost true for : host=localhost ", Util
155: .isLocalHost("localhost"));
156:
157: this
158: .assertTrue(
159: "expected localhost true for : host=InetAddress.getByName(localhost).getHostName()",
160: Util.isLocalHost(InetAddress.getByName(
161: "localhost").getHostName()));
162:
163: this
164: .assertTrue(
165: "expected localhost true for : host=InetAddress.getByName(localhost).getCanonicalHostName()",
166: Util.isLocalHost(InetAddress.getByName(
167: "localhost").getCanonicalHostName()));
168:
169: this
170: .assertTrue(
171: "expected localhost true for : host=InetAddress.getByName(localhost).getHostAddress()",
172: Util.isLocalHost(InetAddress.getByName(
173: "localhost").getHostAddress()));
174:
175: this
176: .assertTrue(
177: "expected localhost true for : host=InetAddress.getLocalHost().getHostName()",
178: Util.isLocalHost(InetAddress.getLocalHost()
179: .getHostName()));
180:
181: this
182: .assertTrue(
183: "expected localhost true for : host=InetAddress.getLocalHost().getCanonicalHostName()",
184: Util.isLocalHost(InetAddress.getLocalHost()
185: .getCanonicalHostName()));
186:
187: this
188: .assertTrue(
189: "expected localhost true for : host=InetAddress.getLocalHost().getHostAddress()",
190: Util.isLocalHost(InetAddress.getLocalHost()
191: .getHostAddress()));
192:
193: }
194:
195: /**
196: * main
197: * @param args the command line arguments
198: */
199: public static void main(String[] args) {
200: // TODO code application logic here
201: try {
202: new TestUtil("test").testNewInstance();
203: new TestUtil("test").testForLocalHost();
204: } catch (Exception ex) {
205: ex.printStackTrace();
206: }
207: }
208: }
|