ebul-reflection/src/test/java/info/ebenoit/ebul/reflection/TestPackageScanner.java

128 lines
3.8 KiB
Java

package info.ebenoit.ebul.reflection;
import java.io.Serializable;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests for the {@link PackageScanner} class
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
public class TestPackageScanner
{
/** Test: list the class names in the test package, without recursion */
@Test
public void testGetClassNamesNoRecursion( )
throws Exception
{
final PackageScanner scanner = new PackageScanner( "test" , false );
final ArrayList< String > result = scanner.getClassNames( );
final ArrayList< String > expected = new ArrayList< >( );
expected.add( "test.PSTest1" );
Assert.assertEquals( expected , result );
}
/** Test: list the class names in the test package, with recursion into test.sub */
@Test
public void testGetClassNamesRecursion( )
throws Exception
{
final PackageScanner scanner = new PackageScanner( "test" , true );
final ArrayList< String > result = scanner.getClassNames( );
result.sort( ( final String a , final String b ) -> a.compareTo( b ) );
final ArrayList< String > expected = new ArrayList< >( );
expected.add( "test.PSTest1" );
expected.add( "test.sub.PSTest2" );
Assert.assertEquals( expected , result );
}
/** Test: load all classes in test and test.sub */
@Test
public void testLoadClasses( )
throws Exception
{
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( ) ) );
final ArrayList< Class< ? > > expected = new ArrayList< >( );
expected.add( test.PSTest1.class );
expected.add( test.sub.PSTest2.class );
Assert.assertEquals( expected , result );
}
/** Test: find classes in test and test.sub that implement {@link Serializable} */
@Test
public void testFindClasses( )
throws Exception
{
final PackageScanner scanner = new PackageScanner( "test" , true );
final ArrayList< Class< ? > > result = scanner.findClasses( a -> Serializable.class.isAssignableFrom( a ) );
final ArrayList< Class< ? > > expected = new ArrayList< >( );
expected.add( test.sub.PSTest2.class );
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." ) ) );
}
/**
* Test: list the class names in the org package, with recursion; both JUnit's classes and the PSTest3 class should
* be in there
*/
@Test
public void testGetClassNamesMixed( )
throws Exception
{
final PackageScanner scanner = new PackageScanner( "org" , true );
final ArrayList< String > result = scanner.getClassNames( );
Assert.assertTrue( !result.isEmpty( ) );
Assert.assertTrue( result.stream( ).anyMatch( s -> s.startsWith( "org.junit." ) ) );
Assert.assertTrue( result.contains( "org.PSTest3" ) );
}
}