You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/01/22 22:08:01 UTC

svn commit: r1234607 - in /commons/sandbox/beanutils2/trunk/src: changes/changes.xml main/java/org/apache/commons/beanutils2/Assertions.java test/java/org/apache/commons/beanutils2/AssertionsTest.java

Author: simonetripodi
Date: Sun Jan 22 21:08:00 2012
New Revision: 1234607

URL: http://svn.apache.org/viewvc?rev=1234607&view=rev
Log:
[SANDBOX-365] Extend Assertions for checking null references in arrays - patch submitted by Benedikt Ritter

Added:
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java   (with props)
Modified:
    commons/sandbox/beanutils2/trunk/src/changes/changes.xml
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/Assertions.java

Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/changes/changes.xml?rev=1234607&r1=1234606&r2=1234607&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Sun Jan 22 21:08:00 2012
@@ -23,6 +23,9 @@
   </properties>
   <body>
   <release version="0.1" date="201?-??-??" description="First release.">
+    <action dev="simonetripodi" type="update" issue="SANDBOX-365" due-to="Benedikt Ritter">
+      Extend Assertions for checking null references in arrays
+    </action>
     <action dev="simonetripodi" type="update" issue="SANDBOX-360" due-to="Benedikt Ritter">
       Rename Converter&lt;T, S&gt; to Transformer&lt;T, S&gt;.
     </action>

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/Assertions.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/Assertions.java?rev=1234607&r1=1234606&r2=1234607&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/Assertions.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/Assertions.java Sun Jan 22 21:08:00 2012
@@ -109,4 +109,23 @@ final class Assertions
         return reference;
     }
 
+    /**
+     * Ensures that none of the references in the array passed as a parameter to the
+     * calling method is null. Note that an array of length 0 will pass the check.
+     *
+     * @param references an array of object reference. Must not be {@code null}!
+     * @return the non-null reference that was validated
+     * @throws NullPointerException
+     *             if one of the contained references is null or if {@code references} itself
+     *             is null (don't let this happen)
+     */
+    public static <T> T[] checkNoneIsNull( T[] references )
+    {
+        for ( int i = 0; i < references.length; i++ )
+        {
+            checkNotNull( references[i], "Reference at index %s/%s is null!", i, references.length );
+        }
+        return references;
+    }
+
 }

Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java?rev=1234607&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java (added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java Sun Jan 22 21:08:00 2012
@@ -0,0 +1,115 @@
+package org.apache.commons.beanutils2;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class AssertionsTest
+{
+
+    // TODO check null msgTemplate and errorArgs
+    // TODO check correct processing of error msg
+
+    @Test
+    public void checkArgument()
+    {
+        Assertions.checkArgument( true, "True has to be okay!" );
+        try
+        {
+            Assertions.checkArgument( false, "False must throw an exception!" );
+            fail( "No exception has been thrown!" );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            // do nothing here, we want this exception to be thrown!
+        }
+    }
+
+    @Test
+    public void checkState()
+    {
+        Assertions.checkState( true, "True has to be okay!" );
+        try
+        {
+            Assertions.checkState( false, "False must throw an excpetion!" );
+            fail( "No exception has been thrown!" );
+        }
+        catch ( IllegalStateException e )
+        {
+            // do nothing here, we want this exception to be thrown!
+        }
+    }
+
+    @Test
+    public void checkNotNull()
+    {
+        Object obj = new Object();
+        Object checkedObj = Assertions.checkNotNull( obj, "An object reference has to be okay!" );
+        assertSame( obj, checkedObj );
+        try
+        {
+            Assertions.checkNotNull( null, "Null must throw an exception." );
+            fail( "No exception has been thrown!" );
+        }
+        catch ( NullPointerException e )
+        {
+            // do nothing here, we want this exception to be thrown!
+        }
+    }
+
+    @Test
+    public void checkNoneIsNull()
+    {
+        Object[] objects = new Object[0];
+        Object[] checkedObjects = Assertions.checkNoneIsNull( objects );
+        assertSame( objects, checkedObjects );
+
+        objects = new Object[] {
+            new Object(), new Object(), new Object(), new Object() };
+        checkedObjects = Assertions.checkNoneIsNull( objects );
+        assertSame( objects, checkedObjects );
+        for ( int i = 0; i < objects.length; i++ )
+        {
+            assertSame( objects[i], checkedObjects[i] );
+        }
+
+        try
+        {
+            Assertions.checkNoneIsNull( null );
+        }
+        catch ( NullPointerException e )
+        {
+            // do nothing here, we want this exception to be thrown!
+        }
+
+        try
+        {
+            objects[0] = null;
+            Assertions.checkNoneIsNull( objects );
+            fail( "No exception has been thrown!" );
+        }
+        catch ( NullPointerException e )
+        {
+            // do nothing here, we want this exception to be thrown!
+        }
+    }
+
+}

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain