Tidied up test class.

This commit is contained in:
Luke Taylor 2008-11-15 10:54:34 +00:00
parent e259fe43a9
commit a9d69ac4e8

View File

@ -1,8 +1,8 @@
package org.springframework.security.acls.objectidentity; package org.springframework.security.acls.objectidentity;
import junit.framework.Assert; import static org.junit.Assert.*;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.security.acls.IdentityUnavailableException; import org.springframework.security.acls.IdentityUnavailableException;
/** /**
@ -10,170 +10,143 @@ import org.springframework.security.acls.IdentityUnavailableException;
* *
* @author Andrei Stefan * @author Andrei Stefan
*/ */
public class ObjectIdentityTests extends TestCase { public class ObjectIdentityTests {
private static final String DOMAIN_CLASS =
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject";
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================
public void testConstructorsRequiredFields() throws Exception { @Test
public void constructorsRespectRequiredFields() throws Exception {
// Check one-argument constructor required field // Check one-argument constructor required field
try { try {
ObjectIdentity obj = new ObjectIdentityImpl(null); new ObjectIdentityImpl(null);
Assert.fail("It should have thrown IllegalArgumentException"); fail("It should have thrown IllegalArgumentException");
} } catch (IllegalArgumentException expected) {
catch (IllegalArgumentException expected) {
Assert.assertTrue(true);
} }
// Check String-Serializable constructor required field // Check String-Serializable constructor required field
try { try {
ObjectIdentity obj = new ObjectIdentityImpl("", new Long(1)); new ObjectIdentityImpl("", new Long(1));
Assert.fail("It should have thrown IllegalArgumentException"); fail("It should have thrown IllegalArgumentException");
} } catch (IllegalArgumentException expected) {
catch (IllegalArgumentException expected) {
Assert.assertTrue(true);
} }
// Check Serializable parameter is not null // Check Serializable parameter is not null
try { try {
ObjectIdentity obj = new ObjectIdentityImpl( new ObjectIdentityImpl(DOMAIN_CLASS, null);
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", null); fail("It should have thrown IllegalArgumentException");
Assert.fail("It should have thrown IllegalArgumentException");
} }
catch (IllegalArgumentException expected) { catch (IllegalArgumentException expected) {
Assert.assertTrue(true);
} }
// The correct way of using String-Serializable constructor // The correct way of using String-Serializable constructor
try { try {
ObjectIdentity obj = new ObjectIdentityImpl( new ObjectIdentityImpl(DOMAIN_CLASS, new Long(1));
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject",
new Long(1));
Assert.assertTrue(true);
} }
catch (IllegalArgumentException notExpected) { catch (IllegalArgumentException notExpected) {
Assert.fail("It shouldn't have thrown IllegalArgumentException"); fail("It shouldn't have thrown IllegalArgumentException");
} }
// Check the Class-Serializable constructor // Check the Class-Serializable constructor
try { try {
ObjectIdentity obj = new ObjectIdentityImpl(MockIdDomainObject.class, null); new ObjectIdentityImpl(MockIdDomainObject.class, null);
Assert.fail("It should have thrown IllegalArgumentException"); fail("It should have thrown IllegalArgumentException");
} }
catch (IllegalArgumentException expected) { catch (IllegalArgumentException expected) {
Assert.assertTrue(true);
} }
} }
@Test
public void testGetIdMethodConstraints() throws Exception { public void testGetIdMethodConstraints() throws Exception {
// Check the getId() method is present // Check the getId() method is present
try { try {
ObjectIdentity obj = new ObjectIdentityImpl("A_STRING_OBJECT"); new ObjectIdentityImpl("A_STRING_OBJECT");
Assert.fail("It should have thrown IdentityUnavailableException"); fail("It should have thrown IdentityUnavailableException");
} }
catch (IdentityUnavailableException expected) { catch (IdentityUnavailableException expected) {
Assert.assertTrue(true);
} }
// getId() should return a non-null value // getId() should return a non-null value
MockIdDomainObject mockId = new MockIdDomainObject(); MockIdDomainObject mockId = new MockIdDomainObject();
try { try {
ObjectIdentity obj = new ObjectIdentityImpl(mockId); new ObjectIdentityImpl(mockId);
Assert.fail("It should have thrown IllegalArgumentException"); fail("It should have thrown IllegalArgumentException");
} }
catch (IllegalArgumentException expected) { catch (IllegalArgumentException expected) {
Assert.assertTrue(true);
} }
// getId() should return a Serializable object // getId() should return a Serializable object
mockId.setId(new MockIdDomainObject()); mockId.setId(new MockIdDomainObject());
try { try {
ObjectIdentity obj = new ObjectIdentityImpl(mockId); new ObjectIdentityImpl(mockId);
Assert.fail("It should have thrown IllegalArgumentException"); fail("It should have thrown IllegalArgumentException");
} }
catch (IllegalArgumentException expected) { catch (IllegalArgumentException expected) {
Assert.assertTrue(true);
} }
// getId() should return a Serializable object // getId() should return a Serializable object
mockId.setId(new Long(100)); mockId.setId(new Long(100));
try { try {
ObjectIdentity obj = new ObjectIdentityImpl(mockId); new ObjectIdentityImpl(mockId);
Assert.assertTrue(true);
} }
catch (IllegalArgumentException expected) { catch (IllegalArgumentException expected) {
Assert.fail("It shouldn't have thrown IllegalArgumentException");
} }
} }
@Test(expected=IllegalStateException.class)
public void testConstructorInvalidClassParameter() throws Exception { public void testConstructorInvalidClassParameter() throws Exception {
try { new ObjectIdentityImpl("not.a.Class", new Long(1));
ObjectIdentity obj = new ObjectIdentityImpl("not.a.Class", new Long(1));
}
catch (IllegalStateException expected) {
return;
}
Assert.fail("It should have thrown IllegalStateException");
} }
@Test
public void testEquals() throws Exception { public void testEquals() throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl( ObjectIdentity obj = new ObjectIdentityImpl(DOMAIN_CLASS, new Long(1));
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(1));
MockIdDomainObject mockObj = new MockIdDomainObject(); MockIdDomainObject mockObj = new MockIdDomainObject();
mockObj.setId(new Long(1)); mockObj.setId(new Long(1));
String string = "SOME_STRING"; String string = "SOME_STRING";
Assert.assertNotSame(obj, string); assertNotSame(obj, string);
Assert.assertTrue(!obj.equals(null)); assertFalse(obj.equals(null));
Assert.assertTrue(!obj.equals("DIFFERENT_OBJECT_TYPE")); assertFalse(obj.equals("DIFFERENT_OBJECT_TYPE"));
Assert.assertTrue(!obj assertFalse(obj.equals(new ObjectIdentityImpl(DOMAIN_CLASS,new Long(2))));
.equals(new ObjectIdentityImpl( assertFalse(obj.equals(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject",
new Long(2))));
Assert.assertTrue(!obj.equals(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockOtherIdDomainObject", "org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockOtherIdDomainObject",
new Long(1)))); new Long(1))));
Assert.assertEquals( assertEquals(new ObjectIdentityImpl(DOMAIN_CLASS,new Long(1)), obj);
new ObjectIdentityImpl( assertEquals(obj, new ObjectIdentityImpl(mockObj));
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject",
new Long(1)), obj);
Assert.assertTrue(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(1))
.equals(obj));
Assert.assertTrue(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(1))
.equals(new ObjectIdentityImpl(mockObj)));
} }
@Test
public void testHashCode() throws Exception { public void testHashCode() throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl( ObjectIdentity obj = new ObjectIdentityImpl(DOMAIN_CLASS, new Long(1));
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(1)); assertEquals(new ObjectIdentityImpl(DOMAIN_CLASS, new Long(1)).hashCode(), obj.hashCode());
Assert.assertEquals(new ObjectIdentityImpl( assertTrue(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(1))
.hashCode(), obj.hashCode());
Assert.assertTrue(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockOtherIdDomainObject", "org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockOtherIdDomainObject",
new Long(1)).hashCode() != obj.hashCode()); new Long(1)).hashCode() != obj.hashCode());
Assert.assertTrue(new ObjectIdentityImpl( assertTrue(new ObjectIdentityImpl(DOMAIN_CLASS, new Long(2)).hashCode() != obj.hashCode());
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(2))
.hashCode() != obj.hashCode());
} }
/* public void testHashCodeDifferentSerializableTypes() throws Exception { /* public void testHashCodeDifferentSerializableTypes() throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl( ObjectIdentity obj = new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(1)); DOMAIN_CLASS, new Long(1));
Assert.assertEquals(new ObjectIdentityImpl( assertEquals(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", "1") DOMAIN_CLASS, "1")
.hashCode(), obj.hashCode()); .hashCode(), obj.hashCode());
Assert.assertEquals(new ObjectIdentityImpl( assertEquals(new ObjectIdentityImpl(
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", DOMAIN_CLASS,
new Integer(1)).hashCode(), obj.hashCode()); new Integer(1)).hashCode(), obj.hashCode());
}*/ }*/
@Test
public void testGetters() throws Exception { public void testGetters() throws Exception {
ObjectIdentity obj = new ObjectIdentityImpl( ObjectIdentity obj = new ObjectIdentityImpl(DOMAIN_CLASS, new Long(1));
"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", new Long(1)); assertEquals(new Long(1), obj.getIdentifier());
Assert.assertEquals(new Long(1), obj.getIdentifier()); assertEquals(MockIdDomainObject.class, obj.getJavaType());
Assert.assertEquals(MockIdDomainObject.class, obj.getJavaType());
} }
//~ Inner Classes ================================================================================================== //~ Inner Classes ==================================================================================================