Browse Source

Tidied up test class.

3.0.x
Luke Taylor 17 years ago
parent
commit
a9d69ac4e8
  1. 351
      acl/src/test/java/org/springframework/security/acls/objectidentity/ObjectIdentityTests.java

351
acl/src/test/java/org/springframework/security/acls/objectidentity/ObjectIdentityTests.java

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

Loading…
Cancel
Save