From 93e8c9a90e078d9af6f5187d7c7ec24cf4a74370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 13 Sep 2015 15:03:43 +0200 Subject: [PATCH] Started writing tests --- pom.xml | 6 ++ .../info/ebenoit/ebul/cmp/DependencyInfo.java | 6 ++ .../ebenoit/ebul/cmp/TestDependencyInfo.java | 65 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/test/java/info/ebenoit/ebul/cmp/TestDependencyInfo.java diff --git a/pom.xml b/pom.xml index 904c797..76569eb 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,12 @@ ebul-reflection 0.0.1-SNAPSHOT + + junit + junit + 4.12 + test + diff --git a/src/main/java/info/ebenoit/ebul/cmp/DependencyInfo.java b/src/main/java/info/ebenoit/ebul/cmp/DependencyInfo.java index 4a7e89d..1521386 100644 --- a/src/main/java/info/ebenoit/ebul/cmp/DependencyInfo.java +++ b/src/main/java/info/ebenoit/ebul/cmp/DependencyInfo.java @@ -1,6 +1,10 @@ package info.ebenoit.ebul.cmp; +import java.util.Objects; + + + /** * This class carries information about a dependency's target. Dependencies may be specified by name or by type. * @@ -29,6 +33,7 @@ public class DependencyInfo */ public DependencyInfo( final String name ) { + Objects.requireNonNull( name ); this.name = name; this.klass = null; } @@ -45,6 +50,7 @@ public class DependencyInfo public DependencyInfo( final Class< ? > klass ) throws ComponentDefinitionException { + Objects.requireNonNull( klass ); this.name = null; this.klass = klass; if ( klass.isPrimitive( ) ) { diff --git a/src/test/java/info/ebenoit/ebul/cmp/TestDependencyInfo.java b/src/test/java/info/ebenoit/ebul/cmp/TestDependencyInfo.java new file mode 100644 index 0000000..b0f47c6 --- /dev/null +++ b/src/test/java/info/ebenoit/ebul/cmp/TestDependencyInfo.java @@ -0,0 +1,65 @@ +package info.ebenoit.ebul.cmp; + + +import org.junit.Test; +import static org.junit.Assert.*; + + + +/** Tests for {@link DependencyInfo} */ +public class TestDependencyInfo +{ + + /** Test: name-based constructor throws {@link NullPointerException} if name is null */ + @Test( expected = NullPointerException.class ) + public void testNewWithNullName( ) + { + new DependencyInfo( (String) null ); + } + + + /** Test: class-based constructor throws {@link NullPointerException} if class is null */ + @Test( expected = NullPointerException.class ) + public void testNewWithNullClass( ) + { + new DependencyInfo( (Class< ? >) null ); + } + + + /** Test: class-based constructor throws {@link ComponentDefinitionException} if class is a primitive type */ + @Test( expected = ComponentDefinitionException.class ) + public void testNewWithPrimitiveType( ) + { + new DependencyInfo( int.class ); + } + + + /** Test: class-based constructor throws {@link ComponentDefinitionException} if class is an array type */ + @Test( expected = ComponentDefinitionException.class ) + public void testNewWithArrayType( ) + { + new DependencyInfo( Object[].class ); + } + + + /** Test: equals() method */ + @Test + public void testEquals( ) + { + DependencyInfo[] di = new DependencyInfo[] { + new DependencyInfo( "test" ) , new DependencyInfo( "test" ) , new DependencyInfo( "test2" ) , + new DependencyInfo( String.class ) , new DependencyInfo( String.class ) , + new DependencyInfo( Object.class ) + }; + + assertTrue( di[ 0 ].equals( di[ 0 ] ) ); + assertTrue( di[ 0 ].equals( di[ 1 ] ) ); + assertFalse( di[ 0 ].equals( di[ 2 ] ) ); + assertFalse( di[ 0 ].equals( di[ 3 ] ) ); + + assertTrue( di[ 3 ].equals( di[ 3 ] ) ); + assertTrue( di[ 3 ].equals( di[ 4 ] ) ); + assertFalse( di[ 3 ].equals( di[ 5 ] ) ); + assertFalse( di[ 3 ].equals( di[ 0 ] ) ); + } +}