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/27 11:13:53 UTC
svn commit: r1236622 -
/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java
Author: simonetripodi
Date: Fri Jan 27 10:13:53 2012
New Revision: 1236622
URL: http://svn.apache.org/viewvc?rev=1236622&view=rev
Log:
AssertionsTest refactoring - patch provided by Benedikt Ritter (part of [SANDBOX-362])
Modified:
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java
Modified: 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=1236622&r1=1236621&r2=1236622&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java (original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/AssertionsTest.java Fri Jan 27 10:13:53 2012
@@ -17,99 +17,210 @@ package org.apache.commons.beanutils2;
* limitations under the License.
*/
+import static java.util.Arrays.fill;
+
+import static org.apache.commons.beanutils2.Assertions.checkArgument;
+import static org.apache.commons.beanutils2.Assertions.checkNoneIsNull;
+import static org.apache.commons.beanutils2.Assertions.checkNotNull;
+import static org.apache.commons.beanutils2.Assertions.checkState;
import static org.junit.Assert.assertSame;
-import static org.junit.Assert.fail;
+import java.util.Arrays;
+
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
public class AssertionsTest
{
- // TODO check null msgTemplate and errorArgs
- // TODO check correct processing of error msg
+ private static final int INT_VALUE = 15;
+
+ private static final String STRING_VALUE = "doing something awesome";
+
+ private static final String PLACEHOLDER = "%s";
+
+ private static final String ERROR_PART1 = "Something went wrong while ";
+
+ private static final String ERROR_PART2 = " because we got a value of ";
+
+ private static final String ERROR_MSG = ERROR_PART1 + PLACEHOLDER + ERROR_PART2 + PLACEHOLDER;
+
+ @Rule
+ public final ExpectedException thrown = ExpectedException.none();
@Test
- public void checkArgument()
+ public void checkArgumentTrueString()
{
- 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!
- }
+ checkArgument( true, "True has to be okay!" );
}
@Test
- public void checkState()
+ public void checkArgumentFalseString()
{
- 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!
- }
+ thrown.expect( IllegalArgumentException.class );
+ thrown.expectMessage( ERROR_PART1 );
+ checkArgument( false, ERROR_PART1 );
}
@Test
- public void checkNotNull()
+ public void checkArgumentFalseStringObjectObject()
+ {
+ thrown.expect( IllegalArgumentException.class );
+ thrown.expectMessage( ERROR_PART1 );
+ thrown.expectMessage( ERROR_PART2 );
+ thrown.expectMessage( String.valueOf( INT_VALUE ) );
+ thrown.expectMessage( STRING_VALUE );
+ checkArgument( false, ERROR_MSG, STRING_VALUE, INT_VALUE );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkArgumentFalseNull()
+ {
+ checkArgument( false, null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkArgumentFalseNullObjects()
+ {
+ checkArgument( false, null, STRING_VALUE, INT_VALUE );
+ }
+
+ @Test
+ public void checkStateTrueString()
+ {
+ checkState( true, "True has to be okay!" );
+ }
+
+ @Test
+ public void checkStateFalseString()
+ {
+ thrown.expect( IllegalStateException.class );
+ thrown.expectMessage( ERROR_PART1 );
+ checkState( false, ERROR_PART1 );
+ }
+
+ @Test
+ public void checkStateFalseStringObjects()
+ {
+ thrown.expect( IllegalStateException.class );
+ thrown.expectMessage( ERROR_PART1 );
+ thrown.expectMessage( ERROR_PART2 );
+ thrown.expectMessage( STRING_VALUE );
+ thrown.expectMessage( String.valueOf( INT_VALUE ) );
+ checkState( false, ERROR_MSG, STRING_VALUE, INT_VALUE );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkStateFalseNull()
+ {
+ checkState( false, null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkStateFalseNullObjects()
+ {
+ checkState( false, null, STRING_VALUE, INT_VALUE );
+ }
+
+ @Test
+ public void checkNotNullObjectString()
{
Object obj = new Object();
- Object checkedObj = Assertions.checkNotNull( obj, "An object reference has to be okay!" );
+ Object checkedObj = 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()
+ public void checkNotNullNullString()
{
- 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] );
- }
+ thrown.expect( NullPointerException.class );
+ thrown.expectMessage( ERROR_PART1 );
+ checkNotNull( null, ERROR_PART1 );
+ }
- try
- {
- Assertions.checkNoneIsNull( null );
- }
- catch ( NullPointerException e )
- {
- // do nothing here, we want this exception to be thrown!
- }
+ @Test
+ public void checkNotNullNullStringObjects()
+ {
+ thrown.expect( NullPointerException.class );
+ thrown.expectMessage( ERROR_PART1 );
+ thrown.expectMessage( ERROR_PART2 );
+ thrown.expectMessage( STRING_VALUE );
+ thrown.expectMessage( String.valueOf( INT_VALUE ) );
+ checkNotNull( null, ERROR_MSG, STRING_VALUE, INT_VALUE );
+ }
- try
- {
- objects[0] = null;
- Assertions.checkNoneIsNull( objects );
- fail( "No exception has been thrown!" );
- }
- catch ( NullPointerException e )
+ // this is a bit ambiguous, we do not distinguish if NPE is thrown because reference is null or if NPE is
+ // thrown because errorTemplate or errorMessageArgs are null
+
+ @Test( expected = NullPointerException.class )
+ public void checkNotNullNullNull()
+ {
+ checkNotNull( null, null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkNotNullNullObjects()
+ {
+ checkNotNull( null, null, STRING_VALUE, INT_VALUE );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkNotNullNullStringNull()
+ {
+ checkNotNull( null, ERROR_MSG, (Object[]) null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkNotNullNullNullNull()
+ {
+ checkNotNull( null, null, (Object[]) null );
+ }
+
+ @Test
+ public void checkNoneIsNullEmptyObjectArray()
+ {
+ Object[] refs = new Object[0];
+ Object[] checkedRefs = checkNoneIsNull( refs );
+ assertSame( refs, checkedRefs );
+ }
+
+ @Test
+ public void checkNoneIsNullObjectArray()
+ {
+ Object[] refs = getObjectArray( 10 );
+ Object[] checkedRefs = checkNoneIsNull( refs );
+ assertSame( refs, checkedRefs );
+ for ( int i = 0; i < refs.length; i++ )
{
- // do nothing here, we want this exception to be thrown!
+ assertSame( refs[i], checkedRefs[i] );
}
}
+ @Test
+ public void checkNoneIsNullObjectArrayWithNullRefs()
+ {
+
+ Object[] refs = getObjectArray( 10 );
+ refs[5] = null;
+ thrown.expect( NullPointerException.class );
+ thrown.expectMessage( "5/10" );
+ thrown.expectMessage( "Reference at index" );
+ thrown.expectMessage( "is null!" );
+ checkNoneIsNull( refs );
+ }
+
+ private Object[] getObjectArray( int size )
+ {
+ Object[] array = new Object[size];
+ fill( array, new Object() );
+ return array;
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void checkNoneIsNullNull()
+ {
+ checkNoneIsNull( null );
+ }
+
}