Package scanner test: add tests for JAR files

The older tests are executed from class files. Added tests that scan
org.junit as well.
This commit is contained in:
Emmanuel BENOîT 2015-09-13 11:21:05 +02:00
parent ae5a3d5d1a
commit 7a86275c1a

View file

@ -1,11 +1,10 @@
package info.ebenoit.ebul.reflection;
import static org.junit.Assert.*;
import java.io.Serializable;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Test;
@ -23,13 +22,13 @@ public class TestPackageScanner
public void testGetClassNamesNoRecursion( )
throws Exception
{
PackageScanner scanner = new PackageScanner( "test" , false );
ArrayList< String > result = scanner.getClassNames( );
final PackageScanner scanner = new PackageScanner( "test" , false );
final ArrayList< String > result = scanner.getClassNames( );
ArrayList< String > expected = new ArrayList< >( );
final ArrayList< String > expected = new ArrayList< >( );
expected.add( "test.PSTest1" );
assertEquals( expected , result );
Assert.assertEquals( expected , result );
}
@ -38,15 +37,15 @@ public class TestPackageScanner
public void testGetClassNamesRecursion( )
throws Exception
{
PackageScanner scanner = new PackageScanner( "test" , true );
ArrayList< String > result = scanner.getClassNames( );
result.sort( ( String a , String b ) -> a.compareTo( b ) );
final PackageScanner scanner = new PackageScanner( "test" , true );
final ArrayList< String > result = scanner.getClassNames( );
result.sort( ( final String a , final String b ) -> a.compareTo( b ) );
ArrayList< String > expected = new ArrayList< >( );
final ArrayList< String > expected = new ArrayList< >( );
expected.add( "test.PSTest1" );
expected.add( "test.sub.PSTest2" );
assertEquals( expected , result );
Assert.assertEquals( expected , result );
}
@ -55,15 +54,16 @@ public class TestPackageScanner
public void testLoadClasses( )
throws Exception
{
PackageScanner scanner = new PackageScanner( "test" , true );
ArrayList< Class< ? > > result = scanner.loadClasses( );
result.sort( ( Class< ? > a , Class< ? > b ) -> a.getCanonicalName( ).compareTo( b.getCanonicalName( ) ) );
final PackageScanner scanner = new PackageScanner( "test" , true );
final ArrayList< Class< ? > > result = scanner.loadClasses( );
result.sort( ( final Class< ? > a , final Class< ? > b ) -> a.getCanonicalName( )
.compareTo( b.getCanonicalName( ) ) );
ArrayList< Class< ? > > expected = new ArrayList< >( );
final ArrayList< Class< ? > > expected = new ArrayList< >( );
expected.add( test.PSTest1.class );
expected.add( test.sub.PSTest2.class );
assertEquals( expected , result );
Assert.assertEquals( expected , result );
}
@ -72,13 +72,40 @@ public class TestPackageScanner
public void testFindClasses( )
throws Exception
{
PackageScanner scanner = new PackageScanner( "test" , true );
ArrayList< Class< ? > > result = scanner.findClasses( a -> Serializable.class.isAssignableFrom( a ) );
final PackageScanner scanner = new PackageScanner( "test" , true );
final ArrayList< Class< ? > > result = scanner.findClasses( a -> Serializable.class.isAssignableFrom( a ) );
ArrayList< Class< ? > > expected = new ArrayList< >( );
final ArrayList< Class< ? > > expected = new ArrayList< >( );
expected.add( test.sub.PSTest2.class );
assertEquals( expected , result );
Assert.assertEquals( expected , result );
}
/** Test: list the class names in the org.junit package, without recursion */
@Test
public void testGetClassNamesJARNoRecursion( )
throws Exception
{
final PackageScanner scanner = new PackageScanner( "org.junit" , false );
final ArrayList< String > result = scanner.getClassNames( );
Assert.assertTrue( !result.isEmpty( ) );
Assert.assertTrue( result.stream( ).allMatch( s -> s.startsWith( "org.junit." ) ) );
Assert.assertTrue( result.stream( ).noneMatch( s -> s.startsWith( "org.junit.experimental." ) ) );
}
/** Test: list the class names in the org.junit package, with recursion */
@Test
public void testGetClassNamesJARRecursion( )
throws Exception
{
final PackageScanner scanner = new PackageScanner( "org.junit" , true );
final ArrayList< String > result = scanner.getClassNames( );
Assert.assertTrue( !result.isEmpty( ) );
Assert.assertTrue( result.stream( ).allMatch( s -> s.startsWith( "org.junit." ) ) );
Assert.assertTrue( result.stream( ).anyMatch( s -> s.startsWith( "org.junit.experimental." ) ) );
}
}