[Plexus-svn] SF.net SVN: plexus:[905] trunk/plexus-graph/src/test/java/com/phoenixst/ plexus/Abstr
Status: Alpha
Brought to you by:
rconner
From: <rc...@us...> - 2010-11-19 23:45:48
|
Revision: 905 http://plexus.svn.sourceforge.net/plexus/?rev=905&view=rev Author: rconner Date: 2010-11-19 23:45:41 +0000 (Fri, 19 Nov 2010) Log Message: ----------- refactor failure testing to use new matchers Modified Paths: -------------- trunk/plexus-graph/src/test/java/com/phoenixst/plexus/AbstractGraphTest.java Modified: trunk/plexus-graph/src/test/java/com/phoenixst/plexus/AbstractGraphTest.java =================================================================== --- trunk/plexus-graph/src/test/java/com/phoenixst/plexus/AbstractGraphTest.java 2010-11-19 23:42:32 UTC (rev 904) +++ trunk/plexus-graph/src/test/java/com/phoenixst/plexus/AbstractGraphTest.java 2010-11-19 23:45:41 UTC (rev 905) @@ -15,29 +15,32 @@ package com.phoenixst.plexus; +import static com.phoenixst.test.TaskMatchers.cannot; +import static com.phoenixst.test.TaskMatchers.isDone; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.everyItem; import static org.junit.matchers.JUnitMatchers.hasItem; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; -import java.util.NoSuchElementException; import java.util.Set; -import org.apache.commons.collections.Closure; import org.apache.commons.collections.Predicate; import org.junit.Test; import com.phoenixst.collections.OrderedPair; import com.phoenixst.plexus.Graph.Edge; +import com.phoenixst.test.TaskMatchers.AbstractTask; +import com.phoenixst.test.TaskMatchers.Task; /** @@ -49,12 +52,6 @@ public abstract class AbstractGraphTest { - private static final String SHOULD_THROW_MESSAGE = "Should throw an Exception."; - - private static final String SHOULD_THROW_NO_SUCH_ELEMENT_MESSAGE = "Should throw a NoSuchElementException."; - - private static final String SHOULD_THROW_ILLEGAL_ARGUMENT_MESSAGE = "Should throw IllegalArgumentException."; - /** * Everything but directed in. */ @@ -308,6 +305,7 @@ return ( tail == null ) ? head == null : tail.equals( head ); } + @SuppressWarnings( "unchecked" ) private void validateEdge( final Edge edge, final Object tail, final Object head ) { Object edgeTail = edge.getTail(); @@ -340,37 +338,29 @@ assertThat( edge.getOtherEndpoint( head ), is( tail ) ); assertThat( edge.getOtherEndpoint( tail ), is( head ) ); - for( Object node : notPresentNodes ) { - try { - edge.getOtherEndpoint( node ); - fail( SHOULD_THROW_ILLEGAL_ARGUMENT_MESSAGE ); - } catch( IllegalArgumentException e ) { - // Do nothing + validateNotPresentNodes( new AbstractTask( "edge.getOtherEndpoint(node)" ) + { + public void execute( Object object ) + throws Throwable + { + edge.getOtherEndpoint( object ); } - } + }, IllegalArgumentException.class ); } - private void validateNotPresentNodes( final Closure closure ) + private void validateNotPresentNodes( final Task task, final Class< ? extends Throwable > ... exceptionTypes ) { - // Check that using nodes not present in the graph throws an - // exception. - for( Object node : notPresentNodes ) { - try { - closure.execute( node ); - fail( SHOULD_THROW_MESSAGE ); - } catch( ClassCastException e ) { - // Do nothing - } catch( IllegalArgumentException e ) { - // Do nothing - } catch( NoSuchNodeException e ) { - // Do nothing - } catch( NullPointerException e ) { - // Do nothing - } - } + assertThat( Arrays.asList( notPresentNodes ), + everyItem( cannot( task, exceptionTypes ) ) ); } + @SuppressWarnings( "unchecked" ) + private void validateNotPresentNodes( final Task task ) + { + validateNotPresentNodes( task, IllegalArgumentException.class, NoSuchNodeException.class, ClassCastException.class ); + } + //////////////////////////////////////// // Public test methods //////////////////////////////////////// @@ -405,9 +395,10 @@ assertThat( g.degree( node ), is( count ) ); } - validateNotPresentNodes( new Closure() + validateNotPresentNodes( new AbstractTask( "degree(node)" ) { - public void execute( final Object object ) + public void execute( Object object ) + throws Throwable { getGraph().degree( object ); } @@ -428,9 +419,10 @@ assertThat( g.degree( node, GraphUtils.OUT_TRAVERSER_PREDICATE ), is( count ) ); } - validateNotPresentNodes( new Closure() + validateNotPresentNodes( new AbstractTask( "degree(node,GraphUtils.OUT_TRAVERSER_PREDICATE)" ) { - public void execute( final Object object ) + public void execute( Object object ) + throws Throwable { getGraph().degree( object, GraphUtils.OUT_TRAVERSER_PREDICATE ); } @@ -451,9 +443,10 @@ assertThat( g.degree( node, GraphUtils.IN_TRAVERSER_PREDICATE ), is( count ) ); } - validateNotPresentNodes( new Closure() + validateNotPresentNodes( new AbstractTask( "degree(node,GraphUtils.IN_TRAVERSER_PREDICATE)" ) { - public void execute( final Object object ) + public void execute( Object object ) + throws Throwable { getGraph().degree( object, GraphUtils.IN_TRAVERSER_PREDICATE ); } @@ -529,13 +522,8 @@ for( count = 0; nodeIter.hasNext(); count++ ) { nodeIter.next(); } - try { - nodeIter.next(); - fail( SHOULD_THROW_NO_SUCH_ELEMENT_MESSAGE ); - } catch( NoSuchElementException e ) { - // Do nothing - } + assertThat( nodeIter, isDone() ); assertThat( nodes.size(), is( count ) ); } @@ -555,13 +543,8 @@ Edge edge = (Edge) edgeIter.next(); validateEdge( edge, edge.getTail(), edge.getHead() ); } - try { - edgeIter.next(); - fail( SHOULD_THROW_NO_SUCH_ELEMENT_MESSAGE ); - } catch( NoSuchElementException e ) { - // Do nothing - } + assertThat( edgeIter, isDone() ); assertThat( realEdges.size(), is( count ) ); } @@ -592,12 +575,8 @@ totalCount++; } } - try { - edgeIter.next(); - fail( SHOULD_THROW_NO_SUCH_ELEMENT_MESSAGE ); - } catch( NoSuchElementException e ) { - // Do nothing - } + + assertThat( edgeIter, isDone() ); } } @@ -648,22 +627,19 @@ validateEdge( edge, adjNode, node ); } } - try { - t.next(); - fail( SHOULD_THROW_NO_SUCH_ELEMENT_MESSAGE ); - } catch( NoSuchElementException e ) { - // Do nothing - } + assertThat( t, isDone() ); + // Check that each edge accessed by the traverser is unique. assertThat( edges.size(), is( count ) ); } assertThat( totalCount + selfCount, is( 2 * realEdges.size() ) ); - validateNotPresentNodes( new Closure() + validateNotPresentNodes( new AbstractTask( "traverser(node,null)" ) { - public void execute( final Object object ) + public void execute( Object object ) + throws Throwable { getGraph().traverser( object, null ); } @@ -691,20 +667,17 @@ edges.add( edge ); validateEdge( edge, node, adjNode ); } - try { - t.next(); - fail( SHOULD_THROW_NO_SUCH_ELEMENT_MESSAGE ); - } catch( NoSuchElementException e ) { - // Do nothing - } + assertThat( t, isDone() ); + // Check that each edge accessed by the traverser is unique. assertThat( edges.size(), is( count ) ); } - validateNotPresentNodes( new Closure() + validateNotPresentNodes( new AbstractTask( "traverser(node,OUT_TRAVERSER_PREDICATE)" ) { - public void execute( final Object object ) + public void execute( Object object ) + throws Throwable { getGraph().traverser( object, GraphUtils.OUT_TRAVERSER_PREDICATE ); } @@ -732,20 +705,17 @@ edges.add( edge ); validateEdge( edge, adjNode, node ); } - try { - t.next(); - fail( SHOULD_THROW_NO_SUCH_ELEMENT_MESSAGE ); - } catch( NoSuchElementException e ) { - // Do nothing - } + assertThat( t, isDone() ); + // Check that each edge accessed by the traverser is unique. assertThat( edges.size(), is( count ) ); } - validateNotPresentNodes( new Closure() + validateNotPresentNodes( new AbstractTask( "traverser(node,IN_TRAVERSER_PREDICATE)" ) { - public void execute( final Object object ) + public void execute( Object object ) + throws Throwable { getGraph().traverser( object, GraphUtils.IN_TRAVERSER_PREDICATE ); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |