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