Browse Source

test classes

Andrei Stefan 17 years ago
parent
commit
83ecb3e9e0

+ 193 - 0
core/src/test/java/org/springframework/security/acls/objectidentity/ObjectIdentityTests.java

@@ -0,0 +1,193 @@
+package org.springframework.security.acls.objectidentity;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.springframework.security.acls.IdentityUnavailableException;
+
+/**
+ * Tests for {@link ObjectIdentityImpl}.
+ * 
+ * @author Andrei Stefan
+ */
+public class ObjectIdentityTests extends TestCase {
+	
+	//~ Methods ========================================================================================================
+
+	public void testConstructorsRequiredFields() {
+		// 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
+		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
+		try {
+			ObjectIdentity obj = new ObjectIdentityImpl(
+					"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject", null);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		// The correct way of using String-Serializable constructor
+		try {
+			ObjectIdentity obj = new ObjectIdentityImpl(
+					"org.springframework.security.acls.objectidentity.ObjectIdentityTests$MockIdDomainObject",
+					new Long(1));
+			Assert.assertTrue(true);
+		}
+		catch (IllegalArgumentException notExpected) {
+			Assert.fail("It shouldn't have thrown IllegalArgumentException");
+		}
+
+		// Check the Class-Serializable constructor
+		try {
+			ObjectIdentity obj = new ObjectIdentityImpl(MockIdDomainObject.class, null);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+	}
+
+	public void testGetIdMethodConstraints() {
+		// Check the getId() method is present
+		try {
+			ObjectIdentity obj = new ObjectIdentityImpl("A_STRING_OBJECT");
+			Assert.fail("It should have thrown IdentityUnavailableException");
+		}
+		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
+		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
+		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() {
+		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() {
+		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";
+		Assert.assertNotSame(obj, string);
+		Assert.assertTrue(!obj.equals(null));
+		Assert.assertTrue(!obj.equals("DIFFERENT_OBJECT_TYPE"));
+		Assert.assertTrue(!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",
+				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() {
+		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 testGetters() {
+		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 ==================================================================================================
+	
+	private class MockIdDomainObject {
+		private Object id;
+
+		public Object getId() {
+			return id;
+		}
+
+		public void setId(Object id) {
+			this.id = id;
+		}
+	}
+
+	private class MockOtherIdDomainObject {
+		private Object id;
+
+		public Object getId() {
+			return id;
+		}
+
+		public void setId(Object id) {
+			this.id = id;
+		}
+	}
+}

+ 10 - 0
core/src/test/java/org/springframework/security/acls/sid/SidRetrievalStrategyTests.java

@@ -0,0 +1,10 @@
+package org.springframework.security.acls.sid;
+
+import junit.framework.TestCase;
+
+public class SidRetrievalStrategyTests extends TestCase {
+	
+	public void testSidsRetrieval() {
+		
+	}
+}

+ 192 - 0
core/src/test/java/org/springframework/security/acls/sid/SidTests.java

@@ -0,0 +1,192 @@
+package org.springframework.security.acls.sid;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.springframework.security.Authentication;
+import org.springframework.security.GrantedAuthority;
+import org.springframework.security.GrantedAuthorityImpl;
+import org.springframework.security.acls.sid.GrantedAuthoritySid;
+import org.springframework.security.acls.sid.PrincipalSid;
+import org.springframework.security.acls.sid.Sid;
+import org.springframework.security.providers.TestingAuthenticationToken;
+
+public class SidTests extends TestCase {
+
+	//~ Methods ========================================================================================================
+	
+	public void testPrincipalSidConstructorsRequiredFields() {
+		// Check one String-argument constructor
+		try {
+			String string = null;
+			Sid principalSid = new PrincipalSid(string);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			Sid principalSid = new PrincipalSid("");
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			Sid principalSid = new PrincipalSid("johndoe");
+			Assert.assertTrue(true);
+		}
+		catch (IllegalArgumentException notExpected) {
+			Assert.fail("It shouldn't have thrown IllegalArgumentException");
+		}
+
+		// Check one Authentication-argument constructor
+		try {
+			Authentication authentication = null;
+			Sid principalSid = new PrincipalSid(authentication);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			Authentication authentication = new TestingAuthenticationToken(null, "password", null);
+			Sid principalSid = new PrincipalSid(authentication);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			Authentication authentication = new TestingAuthenticationToken("johndoe", "password", null);
+			Sid principalSid = new PrincipalSid(authentication);
+			Assert.assertTrue(true);
+		}
+		catch (IllegalArgumentException notExpected) {
+			Assert.fail("It shouldn't have thrown IllegalArgumentException");
+		}
+	}
+
+	public void testGrantedAuthoritySidConstructorsRequiredFields() {
+		// Check one String-argument constructor
+		try {
+			String string = null;
+			Sid gaSid = new GrantedAuthoritySid(string);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			Sid gaSid = new GrantedAuthoritySid("");
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			Sid gaSid = new GrantedAuthoritySid("ROLE_TEST");
+			Assert.assertTrue(true);
+		}
+		catch (IllegalArgumentException notExpected) {
+			Assert.fail("It shouldn't have thrown IllegalArgumentException");
+		}
+
+		// Check one GrantedAuthority-argument constructor
+		try {
+			GrantedAuthority ga = null;
+			Sid gaSid = new GrantedAuthoritySid(ga);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			GrantedAuthority ga = new GrantedAuthorityImpl(null);
+			Sid gaSid = new GrantedAuthoritySid(ga);
+			Assert.fail("It should have thrown IllegalArgumentException");
+		}
+		catch (IllegalArgumentException expected) {
+			Assert.assertTrue(true);
+		}
+
+		try {
+			GrantedAuthority ga = new GrantedAuthorityImpl("ROLE_TEST");
+			Sid gaSid = new GrantedAuthoritySid(ga);
+			Assert.assertTrue(true);
+		}
+		catch (IllegalArgumentException notExpected) {
+			Assert.fail("It shouldn't have thrown IllegalArgumentException");
+		}
+	}
+
+	public void testPrincipalSidEquals() {
+		Authentication authentication = new TestingAuthenticationToken("johndoe", "password", null);
+		Sid principalSid = new PrincipalSid(authentication);
+
+		Assert.assertFalse(principalSid.equals(null));
+		Assert.assertFalse(principalSid.equals("DIFFERENT_TYPE_OBJECT"));
+		Assert.assertTrue(principalSid.equals(principalSid));
+		Assert.assertTrue(principalSid.equals(new PrincipalSid(authentication)));
+		Assert.assertTrue(principalSid.equals(new PrincipalSid(new TestingAuthenticationToken("johndoe", null, null))));
+		Assert.assertFalse(principalSid.equals(new PrincipalSid(new TestingAuthenticationToken("scott", null, null))));
+		Assert.assertTrue(principalSid.equals(new PrincipalSid("johndoe")));
+		Assert.assertFalse(principalSid.equals(new PrincipalSid("scott")));
+	}
+
+	public void testGrantedAuthoritySidEquals() {
+		GrantedAuthority ga = new GrantedAuthorityImpl("ROLE_TEST");
+		Sid gaSid = new GrantedAuthoritySid(ga);
+
+		Assert.assertFalse(gaSid.equals(null));
+		Assert.assertFalse(gaSid.equals("DIFFERENT_TYPE_OBJECT"));
+		Assert.assertTrue(gaSid.equals(gaSid));
+		Assert.assertTrue(gaSid.equals(new GrantedAuthoritySid(ga)));
+		Assert.assertTrue(gaSid.equals(new GrantedAuthoritySid(new GrantedAuthorityImpl("ROLE_TEST"))));
+		Assert.assertFalse(gaSid.equals(new GrantedAuthoritySid(new GrantedAuthorityImpl("ROLE_NOT_EQUAL"))));
+		Assert.assertTrue(gaSid.equals(new GrantedAuthoritySid("ROLE_TEST")));
+		Assert.assertFalse(gaSid.equals(new GrantedAuthoritySid("ROLE_NOT_EQUAL")));
+	}
+
+	public void testPrincipalSidHashCode() {
+		Authentication authentication = new TestingAuthenticationToken("johndoe", "password", null);
+		Sid principalSid = new PrincipalSid(authentication);
+
+		Assert.assertTrue(principalSid.hashCode() == new String("johndoe").hashCode());
+		Assert.assertTrue(principalSid.hashCode() == new PrincipalSid("johndoe").hashCode());
+		Assert.assertTrue(principalSid.hashCode() != new PrincipalSid("scott").hashCode());
+		Assert.assertTrue(principalSid.hashCode() != new PrincipalSid(new TestingAuthenticationToken("scott",
+				"password", null)).hashCode());
+	}
+
+	public void testGrantedAuthoritySidHashCode() {
+		GrantedAuthority ga = new GrantedAuthorityImpl("ROLE_TEST");
+		Sid gaSid = new GrantedAuthoritySid(ga);
+
+		Assert.assertTrue(gaSid.hashCode() == new String("ROLE_TEST").hashCode());
+		Assert.assertTrue(gaSid.hashCode() == new GrantedAuthoritySid("ROLE_TEST").hashCode());
+		Assert.assertTrue(gaSid.hashCode() != new GrantedAuthoritySid("ROLE_TEST_2").hashCode());
+		Assert.assertTrue(gaSid.hashCode() != new GrantedAuthoritySid(new GrantedAuthorityImpl("ROLE_TEST_2"))
+				.hashCode());
+	}
+	
+	public void testGetters() {
+		Authentication authentication = new TestingAuthenticationToken("johndoe", "password", null);
+		PrincipalSid principalSid = new PrincipalSid(authentication);
+		GrantedAuthority ga = new GrantedAuthorityImpl("ROLE_TEST");
+		GrantedAuthoritySid gaSid = new GrantedAuthoritySid(ga);
+		
+		Assert.assertTrue("johndoe".equals(principalSid.getPrincipal()));
+		Assert.assertFalse("scott".equals(principalSid.getPrincipal()));
+		
+		Assert.assertTrue("ROLE_TEST".equals(gaSid.getGrantedAuthority()));
+		Assert.assertFalse("ROLE_TEST2".equals(gaSid.getGrantedAuthority()));
+	}
+}