From 955c46a7ebcfaab40bc7e73e455eb7221fbde969 Mon Sep 17 00:00:00 2001 From: dchandler Date: Sat, 28 Sep 2002 14:38:59 +0000 Subject: [PATCH] Added an unchecked exception that I find useful for marking code that the developer thinks will not cause an exception. Added JUnit unit tests for this new exception. --- source/org/thdl/util/AllTests.java | 42 ++++++ source/org/thdl/util/ThdlLazyException.java | 94 ++++++++++++++ .../org/thdl/util/ThdlLazyExceptionTest.java | 120 ++++++++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 source/org/thdl/util/AllTests.java create mode 100644 source/org/thdl/util/ThdlLazyException.java create mode 100644 source/org/thdl/util/ThdlLazyExceptionTest.java diff --git a/source/org/thdl/util/AllTests.java b/source/org/thdl/util/AllTests.java new file mode 100644 index 0000000..0d58a0f --- /dev/null +++ b/source/org/thdl/util/AllTests.java @@ -0,0 +1,42 @@ +/* +The contents of this file are subject to the THDL Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the THDL web site +(http://www.thdl.org/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is the Tibetan and Himalayan Digital +Library (THDL). Portions created by the THDL are Copyright 2001 THDL. +All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.util; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @author David Chandler + * + * Runs all JUnit test cases in this package. + */ +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(AllTests.class); + } + + public static Test suite() { + TestSuite suite = new TestSuite("Test for org.thdl.util"); + //$JUnit-BEGIN$ + suite.addTest(new TestSuite(ThdlLazyExceptionTest.class)); + //$JUnit-END$ + return suite; + } +} diff --git a/source/org/thdl/util/ThdlLazyException.java b/source/org/thdl/util/ThdlLazyException.java new file mode 100644 index 0000000..d173a90 --- /dev/null +++ b/source/org/thdl/util/ThdlLazyException.java @@ -0,0 +1,94 @@ +/* +The contents of this file are subject to the THDL Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the THDL web site +(http://www.thdl.org/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is the Tibetan and Himalayan Digital +Library (THDL). Portions created by the THDL are Copyright 2001 THDL. +All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.util; + +/** + * @author David Chandler + * + *

A ThdlLazyException wraps a "real" java.lang.Exception. + * ThdlLazyException, however, is unchecked. Thus, if you want + * to be a lazy Java programmer, you can use this idiom:

+ *
+ * // Note that bleh does not throw any checked exceptions: 
+ * public static void bleh() {
+ *     try {
+ *         Foo.bar();
+ *     } catch (Throwable t) {
+ *         // Nah, this'll never happen!
+ *         throw new ThdlLazyException(e);
+ *     }
+ * }
+ * 
+ * + *

Now your code appears to be well written, and no one but the user + * will ever know the difference.

+ * + *

In Java 1.4 (1.3?), Sun introduced constructors for java.lang.Error that + * allow much the same thing. This code is Java 1 compatible, though, and + * is also a red flag, equivalent to a // FIXME comment.

+ */ +public final class ThdlLazyException extends Error { + + /** + * the wrapped exception + */ + private Throwable wrappedException = null; + + /** + * Constructor for ThdlLazyException. + */ + public ThdlLazyException() { + super(); + } + + /** + * Constructor for ThdlLazyException. + * @param descrip description + */ + public ThdlLazyException(String descrip) { + super(descrip); + } + + /** + * Constructor for ThdlLazyException. + * @param descrip description + * @param realException the exception the user should actually care about + */ + public ThdlLazyException(String descrip, Throwable realException) { + super(descrip); + wrappedException = realException; + } + + /** + * Constructor for ThdlLazyException. + * @param realException the exception the user should actually care about + */ + public ThdlLazyException(Throwable realException) { + super(); + wrappedException = realException; + } + + /** + * Returns the wrapped exception, the one about which you should actually + * be concerned. + */ + public Throwable getRealException() { + return wrappedException; + } +} diff --git a/source/org/thdl/util/ThdlLazyExceptionTest.java b/source/org/thdl/util/ThdlLazyExceptionTest.java new file mode 100644 index 0000000..4a2f663 --- /dev/null +++ b/source/org/thdl/util/ThdlLazyExceptionTest.java @@ -0,0 +1,120 @@ +/* +The contents of this file are subject to the THDL Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the THDL web site +(http://www.thdl.org/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is the Tibetan and Himalayan Digital +Library (THDL). Portions created by the THDL are Copyright 2001 THDL. +All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.util; + +import junit.framework.TestCase; + +import java.io.IOException; /* a checked exception */ + +/** + * @author David Chandler + * + * Tests the ThdlLazyException class. + */ +public class ThdlLazyExceptionTest extends TestCase { + + /** + * A helper class that has methods that throw both + * checked and unchecked exceptions. + */ + private class Helper { + Helper() { } + void throwChecked() throws IOException { + throw new java.io.IOException("sure"); + } + void saysItThrowsNothing() { + try { + throwChecked(); + } catch (IOException e) { + throw new ThdlLazyException(e); + } + } + } + + static Helper helper; + protected void setUp() throws Exception { + helper = new Helper(); + } + protected void tearDown() throws Exception { + helper = null; + } + + /** + * Constructor for ThdlLazyExceptionTest. + * @param arg0 + */ + public ThdlLazyExceptionTest(String arg0) { + super(arg0); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(ThdlLazyExceptionTest.class); + } + + /* + * Test for void ThdlLazyException() + */ + public void testThdlLazyException() { + Helper x = new Helper(); + try { + x.saysItThrowsNothing(); + fail("No exception was thrown."); + } catch (ThdlLazyException jle) { + /* good. */ + assertTrue(jle.getRealException() instanceof IOException); + } /* don't catch anything else. */ + } + + /* + * Test for void ThdlLazyException(String) + */ + public void testThdlLazyExceptionString() { + String msg = "foo"; + ThdlLazyException e = new ThdlLazyException(msg); + assertTrue(msg.equals(e.getMessage())); + assertTrue(null == e.getRealException()); + } + + /* + * Test for void ThdlLazyException(String, Throwable) + */ + public void testThdlLazyExceptionStringThrowable() { + String msg = "foo"; + IOException ioe = new IOException("bah"); + ThdlLazyException e = new ThdlLazyException(msg, ioe); + assertTrue(msg.equals(e.getMessage())); + assertTrue(ioe.equals(e.getRealException())); + assertTrue("bah".equals(e.getRealException().getMessage())); + } + + /* + * Test for void ThdlLazyException(Throwable) + */ + public void testThdlLazyExceptionThrowable() { + IOException ioe = new IOException("bah"); + ThdlLazyException e = new ThdlLazyException(ioe); + assertTrue(ioe.equals(e.getRealException())); + assertTrue("bah".equals(e.getRealException().getMessage())); + } + + public void testGetRealException() { + testThdlLazyExceptionThrowable(); + } + +}