Skip to content

Commit

Permalink
Add EmptyCatchBlock checkstyle rule
Browse files Browse the repository at this point in the history
This rule checks for empty catch blocks.
  • Loading branch information
lutovich committed May 9, 2016
1 parent 20ef116 commit 8611ef2
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 118 deletions.
3 changes: 3 additions & 0 deletions build/checkstyle.xml
Expand Up @@ -48,6 +48,9 @@
<module name="EmptyStatement"/> <module name="EmptyStatement"/>
<module name="SuperFinalize"/> <module name="SuperFinalize"/>
<module name="EqualsHashCode"/> <module name="EqualsHashCode"/>
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="ignore|ignored"/>
</module>


</module> </module>


Expand Down
Expand Up @@ -21,56 +21,82 @@


import org.junit.Test; import org.junit.Test;


import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;


import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


/** /**
* @author mh * @author mh
* @since 19.04.12 * @since 19.04.12
*/ */
public class FirstItemIterableTest { public class FirstItemIterableTest
{
@Test @Test
public void testEmptyIterator() throws Exception { public void testEmptyIterator() throws Exception
final FirstItemIterable firstItemIterable = new FirstItemIterable(Collections.emptyList()); {
final Iterator empty = firstItemIterable.iterator(); FirstItemIterable<?> firstItemIterable = new FirstItemIterable<>( Collections.emptyList() );
assertEquals(false, empty.hasNext()); Iterator<?> empty = firstItemIterable.iterator();
try { assertEquals( false, empty.hasNext() );
empty.next(); fail(); try
} catch(NoSuchElementException nse) {} {
assertEquals(null, firstItemIterable.getFirst()); empty.next();
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( NoSuchElementException.class ) );
}
assertEquals( null, firstItemIterable.getFirst() );
} }

@Test @Test
public void testSingleIterator() throws Exception { public void testSingleIterator() throws Exception
final FirstItemIterable firstItemIterable = new FirstItemIterable(Collections.singleton(Boolean.TRUE)); {
final Iterator empty = firstItemIterable.iterator(); FirstItemIterable<Boolean> firstItemIterable = new FirstItemIterable<>( Collections.singleton( Boolean.TRUE ) );
assertEquals(true, empty.hasNext()); Iterator<Boolean> empty = firstItemIterable.iterator();
assertEquals(Boolean.TRUE, empty.next()); assertEquals( true, empty.hasNext() );
assertEquals(Boolean.TRUE, firstItemIterable.getFirst()); assertEquals( Boolean.TRUE, empty.next() );
assertEquals(false, empty.hasNext()); assertEquals( Boolean.TRUE, firstItemIterable.getFirst() );
try { assertEquals( false, empty.hasNext() );
empty.next(); fail(); try
} catch(NoSuchElementException nse) {} {
assertEquals(Boolean.TRUE, firstItemIterable.getFirst()); empty.next();
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( NoSuchElementException.class ) );
}
assertEquals( Boolean.TRUE, firstItemIterable.getFirst() );
} }

@Test @Test
public void testMultiIterator() throws Exception { public void testMultiIterator() throws Exception
final FirstItemIterable firstItemIterable = new FirstItemIterable(Arrays.asList(Boolean.TRUE,Boolean.FALSE)); {
final Iterator empty = firstItemIterable.iterator(); FirstItemIterable<Boolean> firstItemIterable = new FirstItemIterable<>( asList( Boolean.TRUE, Boolean.FALSE ) );
assertEquals(true, empty.hasNext()); Iterator<Boolean> empty = firstItemIterable.iterator();
assertEquals(Boolean.TRUE, empty.next()); assertEquals( true, empty.hasNext() );
assertEquals(Boolean.TRUE, firstItemIterable.getFirst()); assertEquals( Boolean.TRUE, empty.next() );
assertEquals(true, empty.hasNext()); assertEquals( Boolean.TRUE, firstItemIterable.getFirst() );
assertEquals(Boolean.FALSE, empty.next()); assertEquals( true, empty.hasNext() );
assertEquals(Boolean.TRUE, firstItemIterable.getFirst()); assertEquals( Boolean.FALSE, empty.next() );
assertEquals(false, empty.hasNext()); assertEquals( Boolean.TRUE, firstItemIterable.getFirst() );
try { assertEquals( false, empty.hasNext() );
empty.next(); fail(); try
} catch(NoSuchElementException nse) {} {
assertEquals(Boolean.TRUE, firstItemIterable.getFirst()); empty.next();
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( NoSuchElementException.class ) );
}
assertEquals( Boolean.TRUE, firstItemIterable.getFirst() );
} }
} }
Expand Up @@ -375,8 +375,8 @@ public void testIllegalPropertyType()
fail( "Shouldn't validate" ); fail( "Shouldn't validate" );
} }
catch ( Exception e ) catch ( Exception e )
{ { // good
} // good }
setTransaction( getGraphDb().beginTx() ); setTransaction( getGraphDb().beginTx() );
try try
{ {
Expand Down
Expand Up @@ -96,7 +96,7 @@ public void testNodeCreateAndDelete()
getGraphDb().getNodeById( nodeId ); getGraphDb().getNodeById( nodeId );
fail( "Node[" + nodeId + "] should be deleted." ); fail( "Node[" + nodeId + "] should be deleted." );
} }
catch ( NotFoundException e ) catch ( NotFoundException ignored )
{ {
} }
} }
Expand Down Expand Up @@ -127,7 +127,7 @@ public void testNodeAddProperty()
node1.setProperty( null, null ); node1.setProperty( null, null );
fail( "Null argument should result in exception." ); fail( "Null argument should result in exception." );
} }
catch ( IllegalArgumentException e ) catch ( IllegalArgumentException ignored )
{ {
} }
String key1 = "key1"; String key1 = "key1";
Expand Down Expand Up @@ -177,15 +177,15 @@ public void testNodeRemoveProperty()
fail( "Remove of non existing property should return null" ); fail( "Remove of non existing property should return null" );
} }
} }
catch ( NotFoundException e ) catch ( NotFoundException ignored )
{ {
} }
try try
{ {
node1.removeProperty( null ); node1.removeProperty( null );
fail( "Remove null property should throw exception." ); fail( "Remove null property should throw exception." );
} }
catch ( IllegalArgumentException e ) catch ( IllegalArgumentException ignored )
{ {
} }


Expand All @@ -198,7 +198,7 @@ public void testNodeRemoveProperty()
node1.removeProperty( null ); node1.removeProperty( null );
fail( "Null argument should result in exception." ); fail( "Null argument should result in exception." );
} }
catch ( IllegalArgumentException e ) catch ( IllegalArgumentException ignored )
{ {
} }


Expand Down Expand Up @@ -245,7 +245,7 @@ public void testNodeChangeProperty()
node1.setProperty( null, null ); node1.setProperty( null, null );
fail( "Null argument should result in exception." ); fail( "Null argument should result in exception." );
} }
catch ( IllegalArgumentException e ) catch ( IllegalArgumentException ignored )
{ {
} }
catch ( NotFoundException e ) catch ( NotFoundException e )
Expand Down Expand Up @@ -306,15 +306,15 @@ public void testNodeGetProperties()
node1.getProperty( key1 ); node1.getProperty( key1 );
fail( "get non existing property din't throw exception" ); fail( "get non existing property din't throw exception" );
} }
catch ( NotFoundException e ) catch ( NotFoundException ignored )
{ {
} }
try try
{ {
node1.getProperty( null ); node1.getProperty( null );
fail( "get of null key din't throw exception" ); fail( "get of null key din't throw exception" );
} }
catch ( IllegalArgumentException e ) catch ( IllegalArgumentException ignored )
{ {
} }
assertTrue( !node1.hasProperty( key1 ) ); assertTrue( !node1.hasProperty( key1 ) );
Expand Down
Expand Up @@ -269,9 +269,8 @@ public void run()
} }
} }
} }
catch ( DeadlockDetectedException e ) catch ( DeadlockDetectedException ignored )
{ {

} }
finally finally
{ {
Expand Down
Expand Up @@ -76,7 +76,7 @@ public void releaseReleaseManually() throws Exception
worker.setProperty( node, key, "ksjd" ); worker.setProperty( node, key, "ksjd" );
fail( "Shouldn't be able to grab it" ); fail( "Shouldn't be able to grab it" );
} }
catch ( Exception e ) catch ( Exception ignored )
{ {
} }
nodeLock.release(); nodeLock.release();
Expand Down
Expand Up @@ -107,7 +107,7 @@ public void testMultipleCreate() throws InterruptedException
ecs.take().get(); ecs.take().get();
++numSucceeded; ++numSucceeded;
} }
catch ( ExecutionException e ) catch ( ExecutionException ignored )
{ {
} }
} }
Expand Down
59 changes: 0 additions & 59 deletions community/server/src/test/java/org/neo4j/server/WebTestUtils.java

This file was deleted.

Expand Up @@ -134,7 +134,7 @@ public void testPathWriting() throws IOException
{ {
jsonCodec.writeValue( jsonGenerator, path ); jsonCodec.writeValue( jsonGenerator, path );
} }
catch ( Exception e ) catch ( Exception ignored )
{ {


} }
Expand All @@ -155,7 +155,7 @@ public void testIteratorWriting() throws IOException
{ {
jsonCodec.writeValue( jsonGenerator, Arrays.asList( propertyContainer ) ); jsonCodec.writeValue( jsonGenerator, Arrays.asList( propertyContainer ) );
} }
catch ( Exception e ) catch ( Exception ignored )
{ {


} }
Expand All @@ -176,7 +176,7 @@ public void testByteArrayWriting() throws IOException
{ {
jsonCodec.writeValue( jsonGenerator, byteArray ); jsonCodec.writeValue( jsonGenerator, byteArray );
} }
catch ( Exception e ) catch ( Exception ignored )
{ {


} }
Expand All @@ -196,7 +196,7 @@ public void testMapWriting() throws IOException
{ {
jsonCodec.writeValue( jsonGenerator, new HashMap<String, String>() ); jsonCodec.writeValue( jsonGenerator, new HashMap<String, String>() );
} }
catch ( Exception e ) catch ( Exception ignored )
{ {


} }
Expand Down
Expand Up @@ -483,7 +483,7 @@ public void shouldBeAbleToDeleteARelationship() throws Exception
graphdbHelper.getRelationship( relationshipId ); graphdbHelper.getRelationship( relationshipId );
fail(); fail();
} }
catch ( NotFoundException e ) catch ( NotFoundException ignored )
{ {
} }
} }
Expand Down
Expand Up @@ -64,14 +64,14 @@ public Response interpretLine( Serializable clientId, String line, Output out )
{ {
out.println( message ); out.println( message );
} }
catch ( RemoteException e ) catch ( RemoteException ignored )
{ {
} }
return new Response( "", Continuation.INPUT_COMPLETE ); return new Response( "", Continuation.INPUT_COMPLETE );
} }
}; };
} }
catch ( RemoteException e ) catch ( RemoteException ignored )
{ {
} }
return server; return server;
Expand Down Expand Up @@ -119,15 +119,15 @@ public Response interpretLine( Serializable clientId, String line, Output out )
{ {
out.println( message ); out.println( message );
} }
catch ( RemoteException e ) catch ( RemoteException ignored )
{ {
} }
return new Response( prompt, line.endsWith( ";" ) ? Continuation.EXCEPTION_CAUGHT return new Response( prompt, line.endsWith( ";" ) ? Continuation.EXCEPTION_CAUGHT
: Continuation.INPUT_INCOMPLETE ); : Continuation.INPUT_INCOMPLETE );
} }
}; };
} }
catch ( RemoteException e ) catch ( RemoteException ignored )
{ {
} }
return server; return server;
Expand Down

0 comments on commit 8611ef2

Please sign in to comment.