[Plexus-svn] SF.net SVN: plexus:[900] trunk/plexus-graph/src/test/java/com/phoenixst
Status: Alpha
Brought to you by:
rconner
|
From: <rc...@us...> - 2010-09-30 17:16:00
|
Revision: 900
http://plexus.svn.sourceforge.net/plexus/?rev=900&view=rev
Author: rconner
Date: 2010-09-30 17:15:54 +0000 (Thu, 30 Sep 2010)
Log Message:
-----------
Before I forget, adding some custom hamcrest matchers I wrote. While a bit more generic, the real use case is being able to assert that doing something with every element of a collection throws an expected exception. The JUnit @Test(expected=...) will only assert that a method throws, not that something throws 100 times for 100 tested items.
Added Paths:
-----------
trunk/plexus-graph/src/test/java/com/phoenixst/test/
trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchers.java
trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchersTest.java
Added: trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchers.java
===================================================================
--- trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchers.java (rev 0)
+++ trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchers.java 2010-09-30 17:15:54 UTC (rev 900)
@@ -0,0 +1,111 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 1994-2010 by Phoenix Software Technologists,
+ * Inc. and others. All rights reserved.
+ *
+ * THIS PROGRAM AND DOCUMENTATION IS PROVIDED UNDER THE TERMS OF THE
+ * COMMON PUBLIC LICENSE ("AGREEMENT") WHICH ACCOMPANIES IT. ANY
+ * USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
+ * RECIPIENT'S ACCEPTANCE OF THE AGREEMENT.
+ *
+ * The license text can also be found at
+ * http://opensource.org/licenses/cpl.php
+ */
+
+package com.phoenixst.test;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+
+
+/**
+ * Custom hamcrest matchers.
+ *
+ * @author rconner
+ */
+public final class TaskMatchers
+{
+
+ static final ThreadLocal< String > FAILURE_STRING = new ThreadLocal< String >();
+
+
+ private TaskMatchers()
+ {
+ // prevent instantiation
+ }
+
+
+ @Factory
+ public static < T > Matcher< T > can( final Task task )
+ {
+ return new BaseMatcher< T >()
+ {
+ public boolean matches( final Object obj )
+ {
+ FAILURE_STRING.remove();
+ try {
+ task.execute( obj );
+ return true;
+ } catch( Throwable t ) {
+ FAILURE_STRING.set( "item <" + obj + "> threw <" + t + ">" );
+ return false;
+ }
+ }
+
+ public void describeTo( final Description description )
+ {
+ description.appendText( "can execute " )
+ .appendValue( task )
+ .appendText( "; " )
+ .appendText( FAILURE_STRING.get() );
+ }
+ };
+ }
+
+ @Factory
+ public static < T > Matcher< T > cannot( final Task task, final Class< ? extends Throwable > ... exceptionTypes )
+ {
+ return new BaseMatcher< T >()
+ {
+ public boolean matches( final Object obj )
+ {
+ FAILURE_STRING.remove();
+ try {
+ task.execute( obj );
+ FAILURE_STRING.set( "item <" + obj + "> did not throw" );
+ return false;
+ } catch( Throwable t ) {
+ if( exceptionTypes.length == 0 ) {
+ return true;
+ }
+ for( Class< ? extends Throwable > type : exceptionTypes ) {
+ if( type.isInstance( t ) ) {
+ return true;
+ }
+ }
+ FAILURE_STRING.set( "item <" + obj + "> threw <" + t + ">" );
+ return false;
+ }
+ }
+
+ public void describeTo( final Description description )
+ {
+ description.appendText( "throws one of " )
+ .appendValue( exceptionTypes )
+ .appendText( " when executing " )
+ .appendValue( task )
+ .appendText( "; " )
+ .appendText( FAILURE_STRING.get() );
+ }
+ };
+ }
+
+ public static interface Task
+ {
+ public void execute( Object object ) throws Throwable;
+ }
+
+}
Property changes on: trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchers.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Revision Id
Added: svn:eol-style
+ native
Added: trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchersTest.java
===================================================================
--- trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchersTest.java (rev 0)
+++ trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchersTest.java 2010-09-30 17:15:54 UTC (rev 900)
@@ -0,0 +1,117 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 1994-2010 by Phoenix Software Technologists,
+ * Inc. and others. All rights reserved.
+ *
+ * THIS PROGRAM AND DOCUMENTATION IS PROVIDED UNDER THE TERMS OF THE
+ * COMMON PUBLIC LICENSE ("AGREEMENT") WHICH ACCOMPANIES IT. ANY
+ * USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
+ * RECIPIENT'S ACCEPTANCE OF THE AGREEMENT.
+ *
+ * The license text can also be found at
+ * http://opensource.org/licenses/cpl.php
+ */
+
+
+package com.phoenixst.test;
+
+import static com.phoenixst.test.TaskMatchers.can;
+import static com.phoenixst.test.TaskMatchers.cannot;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.everyItem;
+import static org.junit.matchers.JUnitMatchers.hasItem;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.phoenixst.test.TaskMatchers.Task;
+
+
+/**
+ * Tests for TaskMatchers.
+ *
+ * @author rconner
+ */
+@SuppressWarnings( { "boxing", "unchecked" } )
+public class TaskMatchersTest
+{
+
+ private static final Task castToString = new Task()
+ {
+ public void execute( Object input )
+ {
+ ( (String) input ).toString();
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Cast to String";
+ }
+ };
+
+ public TaskMatchersTest()
+ {
+ // do nothing
+ }
+
+ @Test
+ public void allStrings()
+ {
+ List< Object > strings = Arrays.< Object >asList( "abc", "xyz", "foo" );
+ assertThat( strings, everyItem( can( castToString ) ) );
+ assertThat( strings, not( hasItem( cannot( castToString ) ) ) );
+ }
+
+ @Test
+ public void someStrings()
+ {
+ List< Object > mixed = Arrays.< Object >asList( "abc", 5, "foo" );
+ assertThat( mixed, hasItem( can( castToString ) ) );
+ assertThat( mixed, hasItem( not( can( castToString ) ) ) );
+ assertThat( mixed, hasItem( cannot( castToString, ClassCastException.class ) ) );
+ assertThat( mixed, hasItem( not( cannot( castToString ) ) ) );
+ }
+
+ @Test
+ public void noStrings()
+ {
+ List< Object > none = Arrays.< Object >asList( 1, 3, 24 );
+ assertThat( none, not( hasItem( can( castToString ) ) ) );
+ assertThat( none, everyItem( cannot( castToString, ClassCastException.class ) ) );
+ }
+
+ @Test
+ public void singleItems()
+ {
+ assertThat( "abc", can( castToString ) );
+ assertThat( 5, cannot( castToString, ClassCastException.class ) );
+ assertThat( null, cannot( castToString, NullPointerException.class ) );
+ }
+
+ // These methods are not part of the unit test, since they actually fail.
+ // They are here only to see what the assertion failure text looks like.
+
+ public void failsToThrow()
+ {
+ List< Object > array = Arrays.< Object >asList( "abc", 5 );
+ assertThat( array, everyItem( cannot( castToString, ClassCastException.class ) ) );
+ }
+
+ public void throwsWrongException()
+ {
+ List< Object > array = Arrays.< Object >asList( null, "abc" );
+ assertThat( array, everyItem( cannot( castToString, ClassCastException.class ) ) );
+ }
+
+ public void throwsException()
+ {
+ List< Object > array = Arrays.< Object >asList( "abc", 5 );
+ assertThat( array, everyItem( can( castToString ) ) );
+ }
+
+}
Property changes on: trunk/plexus-graph/src/test/java/com/phoenixst/test/TaskMatchersTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Revision Id
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|