Changes for component-provided names

* The component registration record may now include a "name provider" (a
function returning a string based on an instance of the component),
which will be used to determine the component's name.
* NameProvider annotation indicates a method that returns the
component's name
This commit is contained in:
Emmanuel BENOîT 2015-09-17 13:15:41 +02:00
parent c4ff9e4339
commit 5032e44182
7 changed files with 239 additions and 63 deletions
src/test/java/info/ebenoit/ebul/cmp

View file

@ -45,9 +45,8 @@ public class TestComponentState
}
}
/** Class used to test parametric component handling */
/** Class used to test name provider handling */
private static class PCmpTest
implements ParametricComponent
{
private final String componentName;
@ -59,7 +58,6 @@ public class TestComponentState
}
@Override
public String getComponentName( )
{
return this.componentName;
@ -215,23 +213,26 @@ public class TestComponentState
}
/** Test: initialising a {@link ComponentState} with {@link ParametricComponent} */
/** Test: initialising a {@link ComponentState} with a name provider */
@Test
public void testInitialiseWithParametricComponent( )
{
final Object object = new PCmpTest( "ParametricName" );
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( object );
final PCmpTest object = new PCmpTest( "ParametricName" );
final NewComponentInfo< PCmpTest > ci = new NewComponentInfo< PCmpTest >( object ) //
.setNameProvider( o -> o.getComponentName( ) );
final ComponentState cs = new ComponentState( this.reg , ci );
Assert.assertEquals( "ParametricName" , cs.getName( ) );
}
/** Test: initialising a {@link ComponentState} with {@link ParametricComponent} overrides any configured name */
/** Test: initialising a {@link ComponentState} with a name provider overrides any configured name */
@Test
public void testInitialiseWithParametricComponentAndName( )
{
final Object object = new PCmpTest( "ParametricName" );
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( object ).setName( "Test" );
final PCmpTest object = new PCmpTest( "ParametricName" );
final NewComponentInfo< PCmpTest > ci = new NewComponentInfo< PCmpTest >( object ) //
.setNameProvider( o -> o.getComponentName( ) ) //
.setName( "Test" );
final ComponentState cs = new ComponentState( this.reg , ci );
Assert.assertEquals( "ParametricName" , cs.getName( ) );
}
@ -268,10 +269,10 @@ public class TestComponentState
@Test
public void testToStringAnon( )
{
final Object object = new PCmpTest( null );
final Object object = new Object( );
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( object );
final ComponentState cs = new ComponentState( this.reg , ci );
Assert.assertEquals( "anonymous component of type " + PCmpTest.class.getCanonicalName( ) , cs.toString( ) );
Assert.assertEquals( "anonymous component of type " + Object.class.getCanonicalName( ) , cs.toString( ) );
}

View file

@ -85,13 +85,12 @@ public class TestNewComponentInfo
// EMPTY
}
/** Test anonymous component that implements {@link ParametricComponent} (invalid!) */
/** Test anonymous component that has a name provider (invalid!) */
@Anonymous
private static class TestCmpAnonPN
implements ParametricComponent
{
@Override
@NameProvider
public String getComponentName( )
{
return null;
@ -103,23 +102,15 @@ public class TestNewComponentInfo
@Component( "Fail!" )
@Anonymous
private static class TestCmpAnonNamed
implements ParametricComponent
{
@Override
public String getComponentName( )
{
return null;
}
// EMPTY
}
/** Test component with a parametric name */
/** Test component with a name provider */
private static class TestCmpPN
implements ParametricComponent
{
@Override
@NameProvider
public String getComponentName( )
{
return "TestCmpPN";
@ -127,13 +118,12 @@ public class TestNewComponentInfo
}
/** Test component with a parametric name and an annotation-specified name */
/** Test component with a name provider and an annotation-specified name (invalid!) */
@Component( "Fail" )
private static class TestCmpPNFail
implements ParametricComponent
private static class TestCmpPNFail1
{
@Override
@NameProvider
public String getComponentName( )
{
return "TestCmpPNFail";
@ -141,6 +131,61 @@ public class TestNewComponentInfo
}
/** Test component with a static name provider (invalid) */
private static class TestCmpPNFail2
{
@NameProvider
static public String getComponentName( )
{
return "TestCmpPNFail";
}
}
/** Test component with a name provider that doesn't return a string (invalid) */
private static class TestCmpPNFail3
{
@NameProvider
static public Object getComponentName( )
{
return "TestCmpPNFail";
}
}
/** Test component with a name provider that has arguments (invalid) */
private static class TestCmpPNFail4
{
@NameProvider
static public String getComponentName( String fail )
{
return "TestCmpPNFail";
}
}
/** Test component with multiple name providers (invalid) */
private static class TestCmpPNFail5
{
@NameProvider
static public String getComponentName( )
{
return "TestCmpPNFail";
}
@NameProvider
static public String getComponentNameToo( String fail )
{
return "TestCmpPNFail";
}
}
/** Test component that has lifecycle methods */
private static class TestCmp5
{
@ -497,7 +542,7 @@ public class TestNewComponentInfo
/**
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class with the {@link Anonymous} annotation that
* implements {@link ParametricComponent}
* includes a {@link NameProvider}
*/
@Test( expected = ComponentDefinitionException.class )
public void testFromClassAnonPN( )
@ -518,7 +563,7 @@ public class TestNewComponentInfo
/**
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that implements {@link ParametricComponent}
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that uses a {@link NameProvider}
*/
@Test
public void testFromClassPN1( )
@ -529,13 +574,57 @@ public class TestNewComponentInfo
/**
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that implements {@link ParametricComponent}
* but also has an annotation-specified name
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that uses a {@link NameProvider} but also
* has an annotation-specified name
*/
@Test( expected = ComponentDefinitionException.class )
public void testFromClassPNFail1( )
{
NewComponentInfo.fromClass( TestCmpPNFail.class );
NewComponentInfo.fromClass( TestCmpPNFail1.class );
}
/**
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that uses {@link NameProvider} on a static
* method
*/
@Test( expected = ComponentDefinitionException.class )
public void testFromClassPNFail2( )
{
NewComponentInfo.fromClass( TestCmpPNFail2.class );
}
/**
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that uses {@link NameProvider} on a method
* that doesn't return a string
*/
@Test( expected = ComponentDefinitionException.class )
public void testFromClassPNFail3( )
{
NewComponentInfo.fromClass( TestCmpPNFail3.class );
}
/**
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that uses {@link NameProvider} on a method
* that has arguments
*/
@Test( expected = ComponentDefinitionException.class )
public void testFromClassPNFail4( )
{
NewComponentInfo.fromClass( TestCmpPNFail4.class );
}
/**
* Test: {@link NewComponentInfo#fromClass(Class)} on a component class that uses {@link NameProvider} on more than
* one method
*/
@Test( expected = ComponentDefinitionException.class )
public void testFromClassPNFail5( )
{
NewComponentInfo.fromClass( TestCmpPNFail5.class );
}