001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.lang;
007:
008: import java.util.Locale;
009:
010: import junit.framework.TestCase;
011:
012: /**
013: * The <code>ThrowableHelper_Test</code> class tests
014: * <code>ThrowableHelper</code> class.
015: *
016: * @version $Revision: 34826 $
017: *
018: * @author <a href="mailto:jnielsen@sct.com">Jan Nielsen</a>
019: **/
020: public class ThrowableHelper_Test extends TestCase {
021: /** Class version identifier. */
022: public static final String RCS_ID = "@(#) $Header$";
023:
024: /**
025: * Run all the test cases defined in the class.
026: *
027: * @param args not used
028: **/
029: public static void main(String[] args) {
030: junit.textui.TestRunner.run(suite());
031: }
032:
033: /**
034: * Build a test suite using reflection.
035: *
036: * @return test suite for the class
037: **/
038: public static junit.framework.TestSuite suite() {
039: return new junit.framework.TestSuite(ThrowableHelper_Test.class);
040: }
041:
042: /**
043: * Setup for each test method.
044: **/
045: public void setUp() {
046: }
047:
048: /**
049: * Tear down for each test method.
050: **/
051: public void tearDown() {
052: }
053:
054: public ThrowableHelper_Test(String name) {
055: super (name);
056: }
057:
058: public void testGetDefaultHandler() {
059: ThrowableHandler defaultHandler = ThrowableHelper
060: .getDefaultHandler(null);
061:
062: String defaultHandlerClass = defaultHandler.getClass()
063: .getName();
064:
065: String handlerClass = "";
066:
067: verifyEquals(defaultHandlerClass, "");
068:
069: verifyEquals(defaultHandlerClass, "someUnknownClass");
070:
071: // Trigger a InstantiationException
072: verifyEquals(defaultHandlerClass,
073: "java.lang.reflect.Constructor");
074:
075: // Trigger an IllegalAccessException
076: verifyEquals(defaultHandlerClass,
077: "java.lang.reflect.ReflectAccess");
078:
079: // Trigger an ExceptionInInitializerError...
080: }
081:
082: private void verifyEquals(String correctClassName, String className) {
083: ThrowableHandler handler = ThrowableHelper
084: .getDefaultHandler(className);
085:
086: TestCase.assertEquals(correctClassName, handler.getClass()
087: .getName());
088: }
089:
090: public void testGetInternationalizedMessage() {
091: try {
092: ThrowableHelper.getInternationalizedMessage(null, null,
093: null);
094:
095: TestCase.fail("Expected a NullPointerException");
096: } catch (NullPointerException x) {
097: ;//correct
098: }
099:
100: try {
101: ThrowableHelper.getInternationalizedMessage(
102: ThrowableHelper_Test.class, null, null);
103:
104: TestCase.fail("Expected a NullPointerException");
105: } catch (NullPointerException x) {
106: ;//correct
107: }
108:
109: String i18nMessage = ThrowableHelper
110: .getInternationalizedMessage(
111: ThrowableHelper_Test.class,
112: "some.error.message", null);
113:
114: i18nMessage = ThrowableHelper.getInternationalizedMessage(
115: ThrowableHelper_Test.class, "some.error.message",
116: new String[] { null });
117:
118: i18nMessage = ThrowableHelper.getInternationalizedMessage(
119: ThrowableHelper_Test.class, "some.error.message",
120: new String[] { "one", "two" });
121: }
122:
123: public void testGetLocalizedMessage() {
124: Throwable throwable = new Exception();
125:
126: String l10nMessage = ThrowableHelper
127: .getLocalizedMessage(throwable);
128:
129: l10nMessage = ThrowableHelper.getLocalizedMessage(throwable,
130: java.util.Locale.getDefault());
131:
132: String i18nMessage = ThrowableHelper
133: .getInternationalizedMessage(
134: ThrowableHelper_Test.class, "some.error",
135: new String[] { "one", "two" });
136:
137: l10nMessage = ThrowableHelper.getLocalizedMessage(i18nMessage,
138: Locale.getDefault());
139:
140: l10nMessage = ThrowableHelper.getLocalizedMessage(
141: "not_a_well_formed_i18n_Message", Locale.getDefault());
142: }
143:
144: public void testInitCause() {
145: try {
146: ThrowableHelper.initCause(null, null);
147:
148: TestCase.fail("Expected NullPointerException");
149: } catch (NullPointerException x) {
150: ;//correct
151: }
152:
153: ThrowableHelper.initCause(new Throwable(), new Exception());
154: }
155:
156: public void testCreate() {
157: ThrowableHelper.create(IllegalAccessException.class,
158: ThrowableHelper_Test.class, "some.property",
159: new String[] { "one", "two" }, new Error(
160: "my random cause "));
161: }
162:
163: public void testHandle() {
164: ThrowableHelper.handle(new Throwable("something"));
165:
166: ThrowableHelper.handle(ThrowableHelper_Test.class,
167: "some.error", new String[] { "one", "two" }, new Error(
168: "my cause"));
169: }
170: }
171:
172: /**
173: * The <code>ExceptionInInitializerHandler</code> class tests
174: * <code>ThrowableHelper</code> class.
175: *
176: * @version $Revision: 34826 $
177: *
178: * @author <a href="mailto:jnielsen@sct.com">Jan Nielsen</a>
179: **/
180: class ExceptionInInitializerHandler implements ThrowableHandler {
181: /*
182: static
183: {
184: String somethingNull = null;
185:
186: // Trigger a NullPointerException during static initialization.
187: somethingNull.length();
188: }
189: */
190:
191: /**
192: * Handles the condition specified in the parameters. The handler
193: * can use the client class to resolve the property name of the
194: * error message, and generate a localized message from the
195: * optional objects.
196: *
197: * @param client client calling the handle method
198: *
199: * @param name property name associated with error message
200: *
201: * @param objects objects associated with the error message, or
202: * <code>null</code>
203: *
204: * @param cause throwable condition which caused the error, or
205: * <code>null</code>
206: *
207: * @throws NullPointerException if client or property is
208: * <code>null</code>
209: **/
210: public void handle(Class client, String name, String[] objects,
211: Throwable cause) {
212: ;//do nothing
213: }
214: }
215:
216: /**
217: * The <code>IllegalAccessHandler</code> class tests
218: * <code>ThrowableHelper</code> class.
219: *
220: * @version $Revision: 34826 $
221: *
222: * @author <a href="mailto:jnielsen@sct.com">Jan Nielsen</a>
223: **/
224: class IllegalAccessHandler implements ThrowableHandler {
225: protected IllegalAccessHandler(String something) {
226: }
227:
228: /**
229: * Handles the condition specified in the parameters. The handler
230: * can use the client class to resolve the property name of the
231: * error message, and generate a localized message from the
232: * optional objects.
233: *
234: * @param client client calling the handle method
235: *
236: * @param name property name associated with error message
237: *
238: * @param objects objects associated with the error message, or
239: * <code>null</code>
240: *
241: * @param cause throwable condition which caused the error, or
242: * <code>null</code>
243: *
244: * @throws NullPointerException if client or property is
245: * <code>null</code>
246: **/
247: public void handle(Class client, String name, String[] objects,
248: Throwable cause) {
249: ;//do nothing
250: }
251: }
252:
253: /**
254: * The <code>InstantiationExceptionHandler</code> class tests
255: * <code>ThrowableHelper</code> class.
256: *
257: * @version $Revision: 34826 $
258: *
259: * @author <a href="mailto:jnielsen@sct.com">Jan Nielsen</a>
260: **/
261: class InstantiationExceptionHandler implements ThrowableHandler {
262: /**
263: * Hide the constructor to trigger the instantiation exception.
264: **/
265: private InstantiationExceptionHandler() {
266: }
267:
268: /**
269: * Handles the condition specified in the parameters. The handler
270: * can use the client class to resolve the property name of the
271: * error message, and generate a localized message from the
272: * optional objects.
273: *
274: * @param client client calling the handle method
275: *
276: * @param name property name associated with error message
277: *
278: * @param objects objects associated with the error message, or
279: * <code>null</code>
280: *
281: * @param cause throwable condition which caused the error, or
282: * <code>null</code>
283: *
284: * @throws NullPointerException if client or property is
285: * <code>null</code>
286: **/
287: public void handle(Class client, String name, String[] objects,
288: Throwable cause) {
289: ;//do nothing
290: }
291: }
|