[Nullbuster-cvs] [CVS nullbuster] Initial commit
Brought to you by:
osdchicago,
sl4mmy
|
From: Kent R. S. <sl...@us...> - 2005-07-31 10:22:41
|
<html>
<head>
<style><!--
body {background-color:#ffffff;}
.file {border:1px solid #eeeeee;margin-top:1em;margin-bottom:1em;}
.pathname {font-family:monospace; float:right;}
.fileheader {margin-bottom:.5em;}
.diff {margin:0;}
.tasklist {padding:4px;border:1px dashed #000000;margin-top:1em;}
.tasklist ul {margin-top:0;margin-bottom:0;}
tr.alt {background-color:#eeeeee}
#added {background-color:#ddffdd;}
#addedchars {background-color:#99ff99;font-weight:bolder;}
tr.alt #added {background-color:#ccf7cc;}
#removed {background-color:#ffdddd;}
#removedchars {background-color:#ff9999;font-weight:bolder;}
tr.alt #removed {background-color:#f7cccc;}
#info {color:#888888;}
#context {background-color:#eeeeee;}
td {padding-left:.3em;padding-right:.3em;}
tr.head {border-bottom-width:1px;border-bottom-style:solid;}
tr.head td {padding:0;padding-top:.2em;}
.task {background-color:#ffff00;}
.comment {padding:4px;border:1px dashed #000000;background-color:#ffffdd}
.error {color:red;}
hr {border-width:0px;height:2px;background:black;}
--></style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" rules="cols">
<tr class="head"><td colspan="4">Commit in <b><tt>nullbuster/src/java/net/sourceforge/nullbuster</tt></b><span id="info"> on MAIN</span></td></tr>
<tr><td><tt><a href="#file1"><span id="added">NullBusterTest.java</span></a></tt></td><td align="right" id="added">+71</td><td></td><td nowrap="nowrap" align="right">added <a href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster/NullBusterTest.java?rev=1.1&content-type=text/vnd.viewcvs-markup">1.1</a></td></tr>
<tr class="alt"><td><tt><a href="#file2"><span id="added">NullBusterSample.java</span></a></tt></td><td align="right" id="added">+34</td><td></td><td nowrap="nowrap" align="right">added <a href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster/NullBusterSample.java?rev=1.1&content-type=text/vnd.viewcvs-markup">1.1</a></td></tr>
<tr><td><tt><a href="#file3"><span id="added">NullBuster.java</span></a></tt></td><td align="right" id="added">+79</td><td></td><td nowrap="nowrap" align="right">added <a href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster/NullBuster.java?rev=1.1&content-type=text/vnd.viewcvs-markup">1.1</a></td></tr>
<tr><td></td><td align="right" id="added">+184</td><td></td><td></td></tr>
</table>
<small id="info">3 added files</small><br />
<pre class="comment">
Initial commit
</pre>
<hr /><a name="file2" /><div class="file">
<span class="pathname" id="added"><a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster">nullbuster</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src">src</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java">java</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net">net</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge">sourceforge</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster">nullbuster</a></span><br />
<div class="fileheader" id="added"><big><b>NullBusterTest.java</b></big> <small id="info">added at <a href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster/NullBusterTest.java?rev=1.1&content-type=text/vnd.viewcvs-markup">1.1</a></small></div>
<pre class="diff"><small id="info">diff -N NullBusterTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ NullBusterTest.java 31 Jul 2005 10:22:32 -0000 1.1
@@ -0,0 +1,71 @@
</small></pre><pre class="diff" id="added">+package net.sourceforge.nullbuster;
+
+import junit.framework.TestCase;
+
+/**
+ * Ensures correctness of NullBuster.
+ *
+ * @author Kent R. Spillner <ksp...@ac...>
+ */
+public class NullBusterTest extends TestCase {
+ private static final String NO_SUCH_METHOD_FAILURE = "no such method";
+
+ private static final String SECURITY_FAILURE = "do not have access to method";
+
+ private NullBusterSample nullBusterSample;
+
+ private NullBuster nullBuster;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ nullBusterSample = new NullBusterSample();
+ nullBuster = new NullBuster(nullBusterSample);
+ }
+
+ public void testMethodsWithNoParameters() {
+ assertTrue(nullBuster("sampleWithNoParameters", null));
+ }
+
+ public void testMethodsWithOnlyNativeTypeParameters() {
+ assertTrue(nullBuster("sampleWithOnlyNativeTypeParameters",
+ new Class[] { Boolean.TYPE, Byte.TYPE, Character.TYPE,
+ Double.TYPE, Float.TYPE, Integer.TYPE, Long.TYPE,
+ Short.TYPE }));
+ }
+
+ public void testBrokenMethodsWithOneObjectTypeParameter() {
+ assertFalse(nullBuster("brokenSampleWithOneObjectTypeParameter",
+ new Class[] { Object.class }));
+ }
+
+ public void testWorkingMethodsWithOneObjectTypeParameter() {
+ assertTrue(nullBuster("workingSampleWithOneObjectTypeParameter",
+ new Class[] { Object.class }));
+ }
+
+ public void testBrokenMethodsWithNativeAndObjectParameters() {
+ assertFalse(nullBuster("brokenSampleWithNativeAndObjectTypeParameters",
+ new Class[] { Integer.TYPE, Object.class }));
+ }
+
+ public void testWorkingMethodsWithNativeAndObjectParameters() {
+ assertTrue(nullBuster("workingSampleWithNativeAndObjectTypeParameters",
+ new Class[] { Integer.TYPE, Object.class }));
+ }
+
+ private boolean nullBuster(String methodName, Class[] parameterTypes) {
+ try {
+ return nullBuster.bust(nullBusterSample.getClass().getMethod(
+ methodName, parameterTypes));
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ fail(SECURITY_FAILURE);
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ fail(NO_SUCH_METHOD_FAILURE);
+ }
+
+ return false;
+ }
+}
</pre></div>
<hr /><a name="file3" /><div class="file">
<span class="pathname" id="added"><a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster">nullbuster</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src">src</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java">java</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net">net</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge">sourceforge</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster">nullbuster</a></span><br />
<div class="fileheader" id="added"><big><b>NullBusterSample.java</b></big> <small id="info">added at <a href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster/NullBusterSample.java?rev=1.1&content-type=text/vnd.viewcvs-markup">1.1</a></small></div>
<pre class="diff"><small id="info">diff -N NullBusterSample.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ NullBusterSample.java 31 Jul 2005 10:22:32 -0000 1.1
@@ -0,0 +1,34 @@
</small></pre><pre class="diff" id="added">+package net.sourceforge.nullbuster;
+
+/**
+ * For testing NullBuster.
+ *
+ * @author Kent R. Spillner <ksp...@ac...>
+ */
+public class NullBusterSample {
+ public void sampleWithNoParameters() {
+ System.out.println("sampleWithNoParameters() called");
+ }
+
+ public void sampleWithOnlyNativeTypeParameters(boolean bo, byte by,
+ char ch, double d, float f, int i, long l, short s) {
+ }
+
+ public void brokenSampleWithOneObjectTypeParameter(Object obj) {
+ }
+
+ public void workingSampleWithOneObjectTypeParameter(Object obj) {
+ if (obj == null)
+ throw new IllegalArgumentException(
+ "Parameter Object obj can not be null");
+ }
+
+ public void brokenSampleWithNativeAndObjectTypeParameters(int i, Object obj) {
+ }
+
+ public void workingSampleWithNativeAndObjectTypeParameters(int i, Object obj) {
+ if (obj == null)
+ throw new IllegalArgumentException(
+ "Parameter Object obj can not be null");
+ }
+}
</pre></div>
<hr /><a name="file4" /><div class="file">
<span class="pathname" id="added"><a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster">nullbuster</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src">src</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java">java</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net">net</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge">sourceforge</a>/<a
href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster">nullbuster</a></span><br />
<div class="fileheader" id="added"><big><b>NullBuster.java</b></big> <small id="info">added at <a href="http://cvs.sourceforge.net/viewcvs.py/nullbuster/nullbuster/src/java/net/sourceforge/nullbuster/NullBuster.java?rev=1.1&content-type=text/vnd.viewcvs-markup">1.1</a></small></div>
<pre class="diff"><small id="info">diff -N NullBuster.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ NullBuster.java 31 Jul 2005 10:22:32 -0000 1.1
@@ -0,0 +1,79 @@
</small></pre><pre class="diff" id="added">+package net.sourceforge.nullbuster;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Understands proper IllegalArgumentException compliance.
+ *
+ * @author Kent R. Spillner <ksp...@ac...>
+ */
+public class NullBuster {
+ private static final Map NATIVE_TYPES_DEFAULT_VALUES = new HashMap() {
+ {
+ put(Boolean.TYPE, new Boolean(false));
+ put(Byte.TYPE, new Byte((byte) 0));
+ put(Character.TYPE, new Character('\0'));
+ put(Double.TYPE, new Double(0.0));
+ put(Float.TYPE, new Float(0.0f));
+ put(Integer.TYPE, new Integer(0));
+ put(Long.TYPE, new Long(0l));
+ put(Short.TYPE, new Short((short) 0));
+ }
+ };
+
+ private Object obj;
+
+ public NullBuster(Object obj) {
+ this.obj = obj;
+ }
+
+ public boolean bust(Method method) {
+ try {
+ Object[] arguments = createArgumentsFromMethodParameters(method
+ .getParameterTypes());
+
+ boolean nullParameters = false;
+ nullParameters = Arrays.asList(arguments).contains(null);
+
+ // Only bother testing methods with Object parameters
+ if (nullParameters)
+ method.invoke(obj, arguments);
+ else
+ return true;
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ // Methods ought to throw IAEs if they are passed any null arguments
+ if (e.getCause() instanceof IllegalArgumentException)
+ return true;
+
+ e.printStackTrace();
+ }
+
+ return false;
+ }
+
+ private Object[] createArgumentsFromMethodParameters(Class[] parameterTypes) {
+ Object[] arguments = new Object[parameterTypes.length];
+
+ for (int i = 0; i < parameterTypes.length; i++) {
+ arguments[i] = getDefaultValueForType(parameterTypes[i]);
+ }
+
+ return arguments;
+ }
+
+ private Object getDefaultValueForType(Class parameterType) {
+ if (NATIVE_TYPES_DEFAULT_VALUES.containsKey(parameterType))
+ return NATIVE_TYPES_DEFAULT_VALUES.get(parameterType);
+ else
+ return null;
+ }
+}
</pre></div>
<center><small><a href="http://www.badgers-in-foil.co.uk/projects/cvsspam/" title="commit -> email">CVSspam</a> 0.2.11</small></center>
</body></html>
|