@ -9,10 +9,10 @@ import org.junit.Test;
@@ -9,10 +9,10 @@ import org.junit.Test;
*
* @author Andrei Stefan
* /
public class ObjectIdentityTests {
public class ObjectIdentityImpl Tests {
private static final String DOMAIN_CLASS =
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject" ;
"org.springframework.security.acls.objectidentity.ObjectIdentityImpl Tests$MockIdDomainObject" ;
//~ Methods ========================================================================================================
@ -27,7 +27,7 @@ public class ObjectIdentityTests {
@@ -27,7 +27,7 @@ public class ObjectIdentityTests {
// Check String-Serializable constructor required field
try {
new ObjectIdentityImpl ( "" , new Long ( 1 ) ) ;
new ObjectIdentityImpl ( "" , Long . valueOf ( 1 ) ) ;
fail ( "It should have thrown IllegalArgumentException" ) ;
} catch ( IllegalArgumentException expected ) {
}
@ -42,7 +42,7 @@ public class ObjectIdentityTests {
@@ -42,7 +42,7 @@ public class ObjectIdentityTests {
// The correct way of using String-Serializable constructor
try {
new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 1 ) ) ;
new ObjectIdentityImpl ( DOMAIN_CLASS , Long . valueOf ( 1 ) ) ;
}
catch ( IllegalArgumentException notExpected ) {
fail ( "It shouldn't have thrown IllegalArgumentException" ) ;
@ -54,10 +54,16 @@ public class ObjectIdentityTests {
@@ -54,10 +54,16 @@ public class ObjectIdentityTests {
fail ( "It should have thrown IllegalArgumentException" ) ;
}
catch ( IllegalArgumentException expected ) {
}
}
@Test
public void gettersReturnExpectedValues ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl ( DOMAIN_CLASS , Long . valueOf ( 1 ) ) ;
assertEquals ( Long . valueOf ( 1 ) , obj . getIdentifier ( ) ) ;
assertEquals ( MockIdDomainObject . class , obj . getJavaType ( ) ) ;
}
@Test
public void testGetIdMethodConstraints ( ) throws Exception {
// Check the getId() method is present
@ -99,53 +105,56 @@ public class ObjectIdentityTests {
@@ -99,53 +105,56 @@ public class ObjectIdentityTests {
@Test ( expected = IllegalStateException . class )
public void testConstructorInvalidClassParameter ( ) throws Exception {
new ObjectIdentityImpl ( "not.a.Class" , new Long ( 1 ) ) ;
new ObjectIdentityImpl ( "not.a.Class" , Long . valueOf ( 1 ) ) ;
}
@Test
public void testEquals ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 1 ) ) ;
ObjectIdentity obj = new ObjectIdentityImpl ( DOMAIN_CLASS , Long . valueOf ( 1 ) ) ;
MockIdDomainObject mockObj = new MockIdDomainObject ( ) ;
mockObj . setId ( new Long ( 1 ) ) ;
mockObj . setId ( Long . valueOf ( 1 ) ) ;
String string = "SOME_STRING" ;
assertNotSame ( obj , string ) ;
assertFalse ( obj . equals ( null ) ) ;
assertFalse ( obj . equals ( "DIFFERENT_OBJECT_TYPE" ) ) ;
assertFalse ( obj . equals ( new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 2 ) ) ) ) ;
assertFalse ( obj . equals ( new ObjectIdentityImpl ( DOMAIN_CLASS , Long . valueOf ( 2 ) ) ) ) ;
assertFalse ( obj . equals ( new ObjectIdentityImpl (
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockOtherIdDomainObject" ,
new Long ( 1 ) ) ) ) ;
assertEquals ( new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 1 ) ) , obj ) ;
"org.springframework.security.acls.objectidentity.ObjectIdentityImpl Tests$MockOtherIdDomainObject" ,
Long . valueOf ( 1 ) ) ) ) ;
assertEquals ( new ObjectIdentityImpl ( DOMAIN_CLASS , Long . valueOf ( 1 ) ) , obj ) ;
assertEquals ( obj , new ObjectIdentityImpl ( mockObj ) ) ;
}
@Test
public void testHashCode ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 1 ) ) ;
assertEquals ( new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 1 ) ) . hashCode ( ) , obj . hashCode ( ) ) ;
assertTrue ( new ObjectIdentityImpl (
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockOtherIdDomainObject" ,
new Long ( 1 ) ) . hashCode ( ) ! = obj . hashCode ( ) ) ;
assertTrue ( new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 2 ) ) . hashCode ( ) ! = obj . hashCode ( ) ) ;
public void hashcodeIsDifferentForDifferentJavaTypes ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl ( Object . class , Long . valueOf ( 1 ) ) ;
ObjectIdentity obj2 = new ObjectIdentityImpl ( String . class , Long . valueOf ( 1 ) ) ;
assertFalse ( obj . hashCode ( ) = = obj2 . hashCode ( ) ) ;
}
@Test
public void longAndIntegerIdsWithSameValueAreEqualAndHaveSameHashcode ( ) {
ObjectIdentity obj = new ObjectIdentityImpl ( Object . class , new Long ( 5 ) ) ;
ObjectIdentity obj2 = new ObjectIdentityImpl ( Object . class , new Integer ( 5 ) ) ;
assertEquals ( obj , obj2 ) ;
assertEquals ( obj . hashCode ( ) , obj2 . hashCode ( ) ) ;
}
/ * public void testHashCodeDifferentSerializableTypes ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl (
DOMAIN_CLASS , new Long ( 1 ) ) ;
assertEquals ( new ObjectIdentityImpl (
DOMAIN_CLASS , "1" )
. hashCode ( ) , obj . hashCode ( ) ) ;
assertEquals ( new ObjectIdentityImpl (
DOMAIN_CLASS ,
new Integer ( 1 ) ) . hashCode ( ) , obj . hashCode ( ) ) ;
} * /
@Test
public void equalStringIdsAreEqualAndHaveSameHashcode ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl ( Object . class , "1000" ) ;
ObjectIdentity obj2 = new ObjectIdentityImpl ( Object . class , "1000" ) ;
assertEquals ( obj , obj2 ) ;
assertEquals ( obj . hashCode ( ) , obj2 . hashCode ( ) ) ;
}
@Test
public void testGetters ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl ( DOMAIN_CLASS , new Long ( 1 ) ) ;
assertEquals ( new Long ( 1 ) , obj . getIdentifier ( ) ) ;
assertEquals ( MockIdDomainObject . class , obj . getJavaType ( ) ) ;
public void stringAndNumericIdsAreNotEqual ( ) throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl ( Object . class , "1000" ) ;
ObjectIdentity obj2 = new ObjectIdentityImpl ( Object . class , Long . valueOf ( 1000 ) ) ;
assertFalse ( obj . equals ( obj2 ) ) ;
}
//~ Inner Classes ==================================================================================================