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