From: Francois B. <fb...@us...> - 2003-08-26 19:46:52
|
Hi ! I was using MockPrintWriter and had quite a bit of NullPointerExceptions. So, I corrected them using the following tests and implementation. Thanks ! Fran=E7ois PS: My log message requires a fixed-width font to make it understandable... Log Message: Made com.mockobjects.util.AssertMo more robust to null values by adding code to explicitely check for null values. assertIncludes, assertExcludes and assertStartsWith now behave according to this table: targetString null not-null expectedString |--------------------------- null | pass fail not-null | fail compare & pass/fail Patch included and attached in case mailer breaks it Index: src/core/com/mockobjects/util/AssertMo.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util/AssertM= o.java,v retrieving revision 1.3 diff -U6 -r1.3 AssertMo.java --- src/core/com/mockobjects/util/AssertMo.java 18 May 2003 20:59:40 -0000 1.3 +++ src/core/com/mockobjects/util/AssertMo.java 26 Aug 2003 19:38:42 -0000 @@ -28,12 +28,30 @@ } =20 public static void assertExcludes( String description, String excludeString, String targetString) { + if (excludeString =3D=3D null) { + if (targetString =3D=3D null) { + return; + } + + fail(description + + "\nExclude String: <null>" + + "\n Target String: " + + targetString); + } + + if (null =3D=3D targetString) { + fail(description + + "\nExclude String: " + + excludeString + + "\n Target String: <null>"); + } + assertTrue( description + "\nExclude String: " + excludeString + "\n Target String: " + targetString, @@ -41,12 +59,30 @@ } =20 public static void assertIncludes( String description, String includeString, String targetString) { + if (includeString =3D=3D null) { + if (targetString =3D=3D null) { + return; + } + + fail(description + + "\nInclude String: <null>" + + "\n Target String: " + + targetString); + } + + if (null =3D=3D targetString) { + fail(description + + "\nInclude String: " + + includeString + + "\n Target String: <null>"); + } + assertTrue( description + "\nInclude String: " + includeString + "\n Target String: " + targetString, @@ -54,12 +90,30 @@ } =20 public static void assertStartsWith( String description, String startString, String targetString) { + if (startString =3D=3D null) { + if (targetString =3D=3D null) { + return; + } + + fail(description + + "\nInclude String: <null>" + + "\n Target String: " + + targetString); + } + + if (null =3D=3D targetString) { + fail(description + + "\n Start String: " + + startString + + "\nTarget String: <null>"); + } + assertTrue( description + "\n Start String: " + startString + "\nTarget String: " + targetString, Index: src/core/test/mockobjects/TestAssertMo.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/TestAssertM= o.java,v retrieving revision 1.3 diff -U6 -r1.3 TestAssertMo.java --- src/core/test/mockobjects/TestAssertMo.java 27 Oct 2002 22:24:13 -0000 1.3 +++ src/core/test/mockobjects/TestAssertMo.java 26 Aug 2003 19:38:47 -0000 @@ -172,7 +172,78 @@ assertEquals(TEST_MESSAGE, expected.getMessage()); return; } fail("Should have thrown an exception"); } =20 + public void testStartsWithNull() { + try { + AssertMo.assertStartsWith("description", null, "blabla"); + } catch (AssertionFailedError expected) { + return; + } + + fail("Should have failed"); + } + + public void testStartsWithAndTargetNull() { + try { + AssertMo.assertStartsWith("description", "abc", null); + } catch (AssertionFailedError expected) { + return; + } + + fail("Should have failed"); + } + + public void testIncludesNull() { + try { + AssertMo.assertIncludes("description", null, "blabla"); + } catch (AssertionFailedError expected) { + return; + } + + fail("Should have failed"); + } + + public void testIncludesAndTargetNull() { + try { + AssertMo.assertIncludes("description", "abc", null); + } catch (AssertionFailedError expected) { + return; + } + + fail("Should have failed"); + } + + public void testExcludesNull() { + try { + AssertMo.assertExcludes("description", null, "blabla"); + } catch (AssertionFailedError expected) { + return; + } + + fail("Should have failed"); + } + + public void testExcludesAndTargetNull() { + try { + AssertMo.assertExcludes("description", "abc", null); + } catch (AssertionFailedError expected) { + return; + } + + fail("Should have failed"); + } + + public void testExcludesNullNullSucceeds() { + AssertMo.assertExcludes("description", null, null); + } + + public void testIncludesNullNullSucceeds() { + AssertMo.assertExcludes("description", null, null); + } + + public void teststartsWithNullNullSucceeds() { + AssertMo.assertExcludes("description", null, null); + } } Developer of Java Gui Builder http://jgb.sourceforge.net/ |