You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ja...@apache.org on 2012/09/28 14:59:03 UTC
svn commit: r1391437 [5/5] - in /felix/trunk/useradmin: ./ filestore/
filestore/src/ filestore/src/main/ filestore/src/main/java/
filestore/src/main/java/org/ filestore/src/main/java/org/apache/
filestore/src/main/java/org/apache/felix/ filestore/src/m...
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,814 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.useradmin.impl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.framework.FilterImpl;
+import org.apache.felix.useradmin.impl.role.UserImpl;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.useradmin.Authorization;
+import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+import org.osgi.service.useradmin.UserAdminListener;
+
+/**
+ * Test cases for {@link UserAdminImpl}.
+ */
+public class UserAdminImplTest extends TestCase {
+
+ /**
+ * Provides a stub implementation for {@link EventAdmin}.
+ */
+ static class StubEventAdmin implements EventAdmin {
+
+ public void postEvent(Event event) {
+ }
+
+ public void sendEvent(Event event) {
+ }
+ }
+
+ /**
+ * Provides a stub implementation for {@link UserAdminListenerList}.
+ */
+ static class StubUserAdminListenerList implements UserAdminListenerList {
+
+ public UserAdminListener[] getListeners() {
+ return new UserAdminListener[0];
+ }
+ }
+
+ private UserAdminImpl m_userAdmin;
+ private RoleRepository m_roleRepository;
+ private EventDispatcher m_dispatcher;
+
+ /**
+ * Tests that adding a basic member to a group works.
+ */
+ public void testAddGroupMemberOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ Group group1 = (Group) m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertTrue(group1.addMember(user1));
+ assertFalse(group1.addMember(user1));
+ }
+
+ /**
+ * Tests that adding a required member to a group works.
+ */
+ public void testAddRequiredGroupMemberOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ Group group1 = (Group) m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertTrue(group1.addRequiredMember(user1));
+ assertFalse(group1.addRequiredMember(user1));
+ }
+
+ /**
+ * Tests that adding a property of an invalid type to a role does not work and yields an exception.
+ */
+ public void testAddRolePropertyOfInvalidTypeFail() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+
+ try {
+ user1.getProperties().put("key", Integer.valueOf(1));
+
+ fail("Expected IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that adding a property to a role works.
+ */
+ public void testAddRolePropertyOk() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+
+ assertNull(user1.getProperties().get("key"));
+
+ user1.getProperties().put("key", "value");
+
+ assertEquals("value", user1.getProperties().get("key"));
+ }
+
+ /**
+ * Tests that adding a credential of an invalid type to a user does not work and yields an exception.
+ */
+ public void testAddUserCredentialInvalidTypeFails() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+
+ try {
+ user1.getCredentials().put("key", Integer.valueOf(1));
+
+ fail("Expected IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that adding a String credential to a user works.
+ */
+ public void testAddUserCredentialOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+
+ assertNull(user1.getCredentials().get("key"));
+
+ user1.getCredentials().put("key", "value");
+
+ assertEquals("value", user1.getCredentials().get("key"));
+
+ user1.getCredentials().put("key2", "value2".getBytes());
+
+ assertTrue(Arrays.equals("value2".getBytes(), (byte[]) user1.getCredentials().get("key2")));
+ }
+
+ /**
+ * Tests that testing for basic group membership works.
+ */
+ public void testBasicGroupMembershipOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ User user2 = (User) m_userAdmin.createRole("user2", Role.USER);
+ User user3 = (User) m_userAdmin.createRole("user3", Role.USER);
+
+ Group reqGroup = (Group) m_userAdmin.createRole("reqGroup", Role.GROUP);
+ reqGroup.addMember(user1);
+ reqGroup.addMember(user2);
+ reqGroup.addMember(user3);
+
+ Group group = (Group) m_userAdmin.createRole("group", Role.GROUP);
+ group.addRequiredMember(reqGroup);
+ group.addMember(user1);
+ group.addMember(user2);
+
+ Authorization auth = m_userAdmin.getAuthorization(user1);
+ assertTrue(auth.hasRole("group"));
+
+ auth = m_userAdmin.getAuthorization(user2);
+ assertTrue(auth.hasRole("group"));
+
+ auth = m_userAdmin.getAuthorization(user3);
+ assertFalse(auth.hasRole("group"));
+ }
+
+ /**
+ * Tests that changing a property to an invalid type does not work and yields an exception.
+ */
+ public void testChangeRolePropertyOfInvalidTypeFail() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key", "value");
+
+ try {
+ user1.getProperties().put("key", Integer.valueOf(1));
+
+ fail("Expected IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that changing a property of a role works.
+ */
+ public void testChangeRolePropertyOk() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key", "value");
+
+ assertEquals("value", user1.getProperties().get("key"));
+
+ user1.getProperties().put("key", "changed");
+
+ assertEquals("changed", user1.getProperties().get("key"));
+ }
+
+ /**
+ * Tests that changing a credential of a user works.
+ */
+ public void testChangeUserCredentialOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ user1.getCredentials().put("key", "value");
+
+ assertEquals("value", user1.getCredentials().get("key"));
+
+ user1.getCredentials().put("key", "changed");
+
+ assertEquals("changed", user1.getCredentials().get("key"));
+ }
+
+ /**
+ * Tests that creating an existing group does not succeed and yields null.
+ */
+ public void testCreateExistingGroupFail() {
+ Role role = null;
+
+ role = m_userAdmin.createRole("group1", Role.GROUP);
+ assertNotNull(role);
+
+ role = m_userAdmin.createRole("group1", Role.GROUP);
+ assertNull(role);
+ }
+
+ /**
+ * Tests that creating an existing role does not succeed and yields null.
+ */
+ public void testCreateExistingUserFail() {
+ Role role = null;
+
+ role = m_userAdmin.createRole("user1", Role.USER);
+ assertNotNull(role);
+
+ role = m_userAdmin.createRole("user1", Role.USER);
+ assertNull(role);
+ }
+
+ /**
+ * Tests that creating a role with an invalid type does not succeed and yields an exception.
+ */
+ public void testCreateInvalidRoleTypeFail() {
+ try {
+ m_userAdmin.createRole("role1", Role.ROLE);
+
+ fail("Expected an IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that creating a role without a name does not succeed and yields an exception.
+ */
+ public void testCreateInvalidRoleNameFail() {
+ try {
+ m_userAdmin.createRole(null, Role.USER);
+
+ fail("Expected an IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that creating a role without a name does not succeed and yields an exception.
+ */
+ public void testCreateRoleWithEmptyNameFail() {
+ try {
+ m_userAdmin.createRole("", Role.USER);
+
+ fail("Expected an IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that creating a non-existing group succeeds and yields a valid role instance.
+ */
+ public void testCreateNonExistingGroupOk() {
+ Role role = null;
+
+ role = m_userAdmin.createRole("group1", Role.GROUP);
+ assertNotNull(role);
+ assertEquals("group1", role.getName());
+ }
+
+ /**
+ * Tests that creating a non-existing role succeeds and yields a valid role instance.
+ */
+ public void testCreateNonExistingUserOk() {
+ Role role = null;
+
+ role = m_userAdmin.createRole("user1", Role.USER);
+ assertNotNull(role);
+ assertEquals("user1", role.getName());
+ }
+
+ /**
+ * Tests that creating a {@link UserAdminImpl} with a null event dispatcher fails.
+ */
+ public void testCreateUserAdminImplWithNullDispatcherFail() {
+ try {
+ new UserAdminImpl(m_roleRepository, null);
+
+ fail("Expected IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that creating a {@link UserAdminImpl} with a null role repository fails.
+ */
+ public void testCreateUserAdminImplWithNullRepositoryFail() {
+ try {
+ new UserAdminImpl(null, m_dispatcher);
+
+ fail("Expected IllegalArgumentException!");
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that obtaining the authorization for a non-existing user yields null.
+ */
+ public void testGetAuthorizationForAnonymousUserOk() {
+ Authorization auth = m_userAdmin.getAuthorization(null);
+
+ assertNotNull(auth);
+
+ assertNull(auth.getRoles());
+ assertNull(auth.getName());
+ }
+
+ /**
+ * Tests that obtaining the authorization for a non-existing user yields null.
+ */
+ public void testGetAuthorizationForNonExistingUserOk() {
+ Authorization auth = m_userAdmin.getAuthorization(new UserImpl("foo"));
+
+ assertNotNull(auth);
+
+ assertNull(auth.getRoles());
+ assertNotNull(auth.getName());
+ }
+
+ /**
+ * Tests that getting a existing group with an unique key-value pair does not work and yields null.
+ */
+ public void testGetExistingGroupFail() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key1", "value1");
+ user1.getProperties().put("key2", "constant");
+ Role user2 = m_userAdmin.createRole("user2", Role.USER);
+ user2.getProperties().put("key1", "value2");
+ user1.getProperties().put("key2", "constant");
+ Role group1 = m_userAdmin.createRole("group1", Role.GROUP);
+ group1.getProperties().put("key1", "value3");
+
+ assertNull(m_userAdmin.getUser("key1", "value3"));
+ }
+
+ /**
+ * Tests that getting roles based on existing names works correctly.
+ */
+ public void testGetExistingRolesOk() {
+ m_userAdmin.createRole("user1", Role.USER);
+ m_userAdmin.createRole("user2", Role.USER);
+ m_userAdmin.createRole("user3", Role.USER);
+ m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertEquals("user1", m_userAdmin.getRole("user1").getName());
+ assertEquals("user2", m_userAdmin.getRole("user2").getName());
+ assertEquals("user3", m_userAdmin.getRole("user3").getName());
+ assertEquals("group1", m_userAdmin.getRole("group1").getName());
+ }
+
+ /**
+ * Tests that getting an existing user with a non unique key-value pair does not work and yields null.
+ */
+ public void testGetExistingUserWithNonUniqueKeyValueFail() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key1", "value1");
+ user1.getProperties().put("key2", "constant");
+ Role user2 = m_userAdmin.createRole("user2", Role.USER);
+ user2.getProperties().put("key1", "value2");
+ user2.getProperties().put("key2", "constant");
+ Role group1 = m_userAdmin.createRole("group1", Role.GROUP);
+ group1.getProperties().put("key1", "value3");
+
+ assertNull(m_userAdmin.getUser("key2", "constant"));
+ }
+
+ /**
+ * Tests that getting an existing user with an unique key-value pair works and yields the expected result.
+ */
+ public void testGetExistingUserWithUniqueKeyValueOk() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key1", "value1");
+ user1.getProperties().put("key2", "constant");
+ Role user2 = m_userAdmin.createRole("user2", Role.USER);
+ user2.getProperties().put("key1", "value2");
+ user2.getProperties().put("key2", "constant");
+ Role group1 = m_userAdmin.createRole("group1", Role.GROUP);
+ group1.getProperties().put("key1", "value3");
+
+ assertEquals(user1, m_userAdmin.getUser("key1", "value1"));
+ assertEquals(user2, m_userAdmin.getUser("key1", "value2"));
+ }
+
+ /**
+ * Tests that retrieving the basic members from a group works.
+ */
+ public void testGetGroupMemberOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+
+ Group group1 = (Group) m_userAdmin.createRole("group1", Role.GROUP);
+ assertNull(group1.getMembers());
+
+ assertTrue(group1.addMember(user1));
+
+ assertEquals(1, group1.getMembers().length);
+ }
+
+ /**
+ * Tests that getting a non existing role by its name does not work and yields null.
+ */
+ public void testGetNonExistingRoleFails() {
+ assertNull(m_userAdmin.getRole("user1"));
+ }
+
+ /**
+ * Tests that getting a non-existing user does not work and yields null.
+ */
+ public void testGetNonExistingUserFail() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key1", "value1");
+ user1.getProperties().put("key2", "constant");
+ Role user2 = m_userAdmin.createRole("user2", Role.USER);
+ user2.getProperties().put("key1", "value2");
+ user1.getProperties().put("key2", "constant");
+ Role group1 = m_userAdmin.createRole("group1", Role.GROUP);
+ group1.getProperties().put("key1", "value3");
+
+ assertNull(m_userAdmin.getUser("key1", "value4"));
+ }
+
+ /**
+ * Tests that getting roles based on a OSGi-filter that does not match any roles yields null.
+ */
+ public void testGetNonMatchingRolesOk() throws Exception {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key", "value1");
+ Role user2 = m_userAdmin.createRole("user2", Role.USER);
+ user2.getProperties().put("key", "value2");
+ Role group1 = m_userAdmin.createRole("group1", Role.GROUP);
+ group1.getProperties().put("key", "value3");
+
+ Role[] roles = m_userAdmin.getRoles("(nonExisting=value*)");
+ assertNull(roles);
+ }
+
+ /**
+ * Tests that getting predefined roles based on their names works correctly.
+ */
+ public void testGetPredefinedRolesOk() {
+ assertEquals(Role.USER_ANYONE, m_userAdmin.getRole(Role.USER_ANYONE).getName());
+ }
+
+ /**
+ * Tests that getting a removed role yields null.
+ */
+ public void testGetRemovedRoleFail() {
+ m_userAdmin.createRole("user1", Role.USER);
+ assertEquals("user1", m_userAdmin.getRole("user1").getName());
+
+ assertTrue(m_userAdmin.removeRole("user1"));
+ assertNull(m_userAdmin.getRole("user1"));
+ }
+
+ /**
+ * Tests that retrieving the required members from a group works.
+ */
+ public void testGetRequiredGroupMemberOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+
+ Group group1 = (Group) m_userAdmin.createRole("group1", Role.GROUP);
+ assertNull(group1.getRequiredMembers());
+
+ assertTrue(group1.addRequiredMember(user1));
+
+ assertEquals(1, group1.getRequiredMembers().length);
+ }
+
+ /**
+ * Tests that getting the various names of defined roles works and yields the expected result.
+ */
+ public void testGetRoleNamesOk() {
+ m_userAdmin.createRole("user1", Role.USER);
+ m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertEquals(Role.USER_ANYONE, m_userAdmin.getRole(Role.USER_ANYONE).getName());
+ assertEquals("user1", m_userAdmin.getRole("user1").getName());
+ assertEquals("group1", m_userAdmin.getRole("group1").getName());
+ }
+
+ /**
+ * Tests that getting multiple roles based on a OSGi-filter works and yields the correct result.
+ */
+ public void testGetRolesWithFilterOk() throws Exception {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key", "value1");
+ Role user2 = m_userAdmin.createRole("user2", Role.USER);
+ user2.getProperties().put("key", "value2");
+ Role group1 = m_userAdmin.createRole("group1", Role.GROUP);
+ group1.getProperties().put("key", "value3");
+ Role group2 = m_userAdmin.createRole("group2", Role.GROUP);
+ group2.getProperties().put("key", "otherValue3");
+
+ Role[] roles = m_userAdmin.getRoles("(key=value*)");
+ assertNotNull(roles);
+
+ assertEquals(3, roles.length);
+
+ List roleList = Arrays.asList(roles);
+ assertTrue(roleList.contains(user1));
+ assertTrue(roleList.contains(user2));
+ assertTrue(roleList.contains(group1));
+ }
+
+ /**
+ * Tests that getting roles based on an invalid OSGi-filter yields an exception.
+ */
+ public void testGetRolesWithInvalidFilterFails() throws Exception {
+ try {
+ m_userAdmin.getRoles("(nonExisting=value*");
+
+ fail("Expected an InvalidSyntaxException!");
+ } catch (InvalidSyntaxException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that getting multiple roles based on a OSGi-filter works and yields the correct result.
+ */
+ public void testGetRolesWithoutFilterOk() throws Exception {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key", "value1");
+ Role user2 = m_userAdmin.createRole("user2", Role.USER);
+ user2.getProperties().put("key", "value2");
+ Role group1 = m_userAdmin.createRole("group1", Role.GROUP);
+ group1.getProperties().put("key", "value3");
+ Role group2 = m_userAdmin.createRole("group2", Role.GROUP);
+ group2.getProperties().put("key", "otherValue3");
+
+ Role[] roles = m_userAdmin.getRoles(null);
+ assertNotNull(roles);
+
+ assertEquals(4, roles.length);
+
+ List roleList = Arrays.asList(roles);
+ assertTrue(roleList.contains(user1));
+ assertTrue(roleList.contains(user2));
+ assertTrue(roleList.contains(group1));
+ assertTrue(roleList.contains(group2));
+ }
+
+ /**
+ * Tests that getting the various types of defined roles works and yields the expected result.
+ */
+ public void testGetRoleTypesOk() {
+ m_userAdmin.createRole("user1", Role.USER);
+ m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertEquals(Role.ROLE, m_userAdmin.getRole(Role.USER_ANYONE).getType());
+ assertEquals(Role.USER, m_userAdmin.getRole("user1").getType());
+ assertEquals(Role.GROUP, m_userAdmin.getRole("group1").getType());
+ }
+
+ /**
+ * Tests that testing for group membership with anonymous users works.
+ */
+ public void testGroupMembershipWithAnonymousUserOk() {
+ Role user = m_userAdmin.createRole("user", Role.USER);
+
+ Group group = (Group) m_userAdmin.createRole("group", Role.GROUP);
+ group.addMember(user);
+
+ Authorization auth = m_userAdmin.getAuthorization(null);
+ assertTrue(auth.hasRole(Role.USER_ANYONE));
+ assertFalse(auth.hasRole("group"));
+ }
+
+ /**
+ * Tests that testing for group membership with "user.anyone" works.
+ */
+ public void testGroupMembershipWithUserAnyoneOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ User user2 = (User) m_userAdmin.createRole("user2", Role.USER);
+ User user3 = (User) m_userAdmin.createRole("user3", Role.USER);
+ User user4 = (User) m_userAdmin.createRole("user4", Role.USER);
+
+ Group reqGroup = (Group) m_userAdmin.createRole("reqGroup", Role.GROUP);
+ reqGroup.addMember(user1);
+ reqGroup.addMember(user2);
+
+ Group group = (Group) m_userAdmin.createRole("group", Role.GROUP);
+ group.addRequiredMember(reqGroup);
+ group.addMember(m_userAdmin.getRole(Role.USER_ANYONE));
+
+ Authorization auth = m_userAdmin.getAuthorization(user1);
+ assertTrue(auth.hasRole("group"));
+
+ auth = m_userAdmin.getAuthorization(user2);
+ assertTrue(auth.hasRole("group"));
+
+ auth = m_userAdmin.getAuthorization(user3);
+ assertFalse(auth.hasRole("group"));
+
+ auth = m_userAdmin.getAuthorization(user4);
+ assertFalse(auth.hasRole("group"));
+ }
+
+ /**
+ * Tests that testing the credentials for a user works and yields the correct result.
+ */
+ public void testHasUserCredentialOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ user1.getCredentials().put("key1", "value");
+ user1.getCredentials().put("key2", "value".getBytes());
+
+ assertTrue(user1.hasCredential("key1", "value"));
+ assertTrue(user1.hasCredential("key1", "value".getBytes()));
+ assertTrue(user1.hasCredential("key2", "value"));
+ assertTrue(user1.hasCredential("key2", "value".getBytes()));
+ assertFalse(user1.hasCredential("otherKey", "value"));
+ }
+
+ /**
+ * Tests that removing an existing role works.
+ */
+ public void testRemoveExistingRoleOk() {
+ Role role = null;
+
+ role = m_userAdmin.createRole("group1", Role.GROUP);
+ assertNotNull(role);
+
+ assertTrue(m_userAdmin.removeRole("group1"));
+ assertFalse(m_userAdmin.removeRole("group1"));
+ }
+
+ /**
+ * Tests that removing a non existing role does not work, yields a false result.
+ */
+ public void testRemoveNonExistingRoleOk() {
+ assertFalse(m_userAdmin.removeRole("group1"));
+ }
+
+ /**
+ * Tests that removing a predefined role does not work, and yields a false result.
+ */
+ public void testRemovePredefinedRoleOk() {
+ assertFalse(m_userAdmin.removeRole(Role.USER_ANYONE));
+ }
+
+ /**
+ * Tests that remove a property of a role works.
+ */
+ public void testRemoveRolePropertyOk() {
+ Role user1 = m_userAdmin.createRole("user1", Role.USER);
+ user1.getProperties().put("key", "value");
+
+ assertEquals("value", user1.getProperties().get("key"));
+
+ user1.getProperties().remove("key");
+
+ assertNull(user1.getProperties().get("key"));
+ }
+
+ /**
+ * Tests that remove a credential of a user works.
+ */
+ public void testRemoveUserCredentialOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ user1.getCredentials().put("key", "value");
+
+ assertEquals("value", user1.getCredentials().get("key"));
+
+ user1.getCredentials().remove("key");
+
+ assertNull(user1.getCredentials().get("key"));
+ }
+
+ /**
+ * Tests that removing a basic member from a group works.
+ */
+ public void testRemovingGroupMemberOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ Group group1 = (Group) m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertTrue(group1.addMember(user1));
+
+ assertTrue(group1.removeMember(user1));
+ assertFalse(group1.removeMember(user1));
+ }
+
+ /**
+ * Tests that removing a required member from a group works.
+ */
+ public void testRemovingRequiredGroupMemberOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ Group group1 = (Group) m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertTrue(group1.addRequiredMember(user1));
+
+ assertTrue(group1.removeMember(user1));
+ assertFalse(group1.removeMember(user1));
+ }
+
+ /**
+ * Tests that testing for required group membership works.
+ */
+ public void testRequiredGroupMembershipOk() {
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ User user2 = (User) m_userAdmin.createRole("user2", Role.USER);
+ User user3 = (User) m_userAdmin.createRole("user3", Role.USER);
+
+ Group reqGroup = (Group) m_userAdmin.createRole("reqGroup", Role.GROUP);
+ reqGroup.addMember(user1);
+ reqGroup.addMember(user2);
+ reqGroup.addMember(user3);
+
+ Group group = (Group) m_userAdmin.createRole("group", Role.GROUP);
+ group.addRequiredMember(reqGroup);
+
+ Authorization auth = m_userAdmin.getAuthorization(user1);
+ assertFalse(auth.hasRole("group"));
+
+ auth = m_userAdmin.getAuthorization(user2);
+ assertFalse(auth.hasRole("group"));
+
+ auth = m_userAdmin.getAuthorization(user3);
+ assertFalse(auth.hasRole("group"));
+ }
+
+ /**
+ * Tests that the list of roles in an {@link Authorization} does not contain the any-user, although it is defined as group member.
+ */
+ public void testUserAnyoneIsNotPartOfAuthorizedRolesOk() {
+ Role userAnyone = m_userAdmin.getRole(Role.USER_ANYONE);
+ User user1 = (User) m_userAdmin.createRole("user1", Role.USER);
+ Group group1 = (Group) m_userAdmin.createRole("group1", Role.GROUP);
+
+ assertTrue(group1.addRequiredMember(user1));
+ assertTrue(group1.addMember(userAnyone));
+
+ Authorization auth = m_userAdmin.getAuthorization(user1);
+ assertNotNull(auth);
+
+ assertTrue(auth.hasRole("group1"));
+
+ String[] roles = auth.getRoles();
+ assertNotNull(roles);
+
+ for (int i = 0; i < roles.length; i++) {
+ assertFalse(Role.USER_ANYONE.equals(roles[i]));
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ m_roleRepository = new RoleRepository(new MemoryRoleRepositoryStore());
+ m_dispatcher = new EventDispatcher(new StubEventAdmin(), new StubUserAdminListenerList());
+
+ m_userAdmin = new UserAdminImpl(m_roleRepository, m_dispatcher) {
+ protected Filter createFilter(String filter) throws InvalidSyntaxException {
+ if (filter == null || "".equals(filter.trim())) {
+ return null;
+ }
+ return new FilterImpl(filter);
+ }
+ };
+
+ m_roleRepository.start();
+ m_dispatcher.start();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void tearDown() throws Exception {
+ m_roleRepository.stop();
+ m_dispatcher.stop();
+
+ super.tearDown();
+ }
+
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplSecurityTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplSecurityTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplSecurityTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplSecurityTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,160 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.felix.useradmin.impl.role;
+
+import java.security.Permission;
+
+import junit.framework.TestCase;
+
+
+import org.apache.felix.useradmin.impl.role.GroupImpl;
+import org.apache.felix.useradmin.impl.role.RoleImpl;
+import org.osgi.service.useradmin.Role;
+
+/**
+ * Security-related test cases for {@link GroupImpl}.
+ */
+public class GroupImplSecurityTest extends TestCase {
+
+ private TestSecurityManager m_securityManager;
+ private GroupImpl m_group;
+
+ /**
+ * Tests that with permission, the {@link GroupImpl#addMember(Role)} method can be accessed.
+ */
+ public void testAddMemberWithPermissionsOk() throws SecurityException {
+ m_securityManager.m_allowed = true;
+
+ m_group.addMember(new RoleImpl(Role.USER_ANYONE));
+ }
+
+ /**
+ * Tests that without permission, the {@link GroupImpl#addMember(Role)} method can not be accessed.
+ */
+ public void testAddMemberWithoutPermissionsFail() throws SecurityException {
+ try {
+ m_group.addMember(new RoleImpl(Role.USER_ANYONE));
+
+ fail("Security exception expected!");
+ } catch (SecurityException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that with permission, the {@link GroupImpl#addRequiredMember(Role)} method can be accessed.
+ */
+ public void testAddRequiredMemberWithPermissionsOk() throws SecurityException {
+ m_securityManager.m_allowed = true;
+
+ m_group.addRequiredMember(new RoleImpl(Role.USER_ANYONE));
+ }
+
+ /**
+ * Tests that without permission, the {@link GroupImpl#addRequiredMember(Role)} method can not be accessed.
+ */
+ public void testAddRequiredMemberWithoutPermissionsFail() throws SecurityException {
+ try {
+ m_group.addRequiredMember(new RoleImpl(Role.USER_ANYONE));
+
+ fail("Security exception expected!");
+ } catch (SecurityException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that no special permissions are needed to access the {@link GroupImpl#getMembers()} method.
+ */
+ public void testGetMembers() throws SecurityException {
+ assertNull(m_group.getMembers());
+ }
+
+ /**
+ * Tests that no special permissions are needed to access the {@link GroupImpl#getRequiredMembers()} method.
+ */
+ public void testGetRequiredMembers() throws SecurityException {
+ assertNull(m_group.getRequiredMembers());
+ }
+
+ /**
+ * Tests that with permission, the {@link GroupImpl#removeMember(Role)} method can be accessed.
+ */
+ public void testRemoveMemberWithPermissionsOk() throws SecurityException {
+ m_securityManager.m_allowed = true;
+
+ assertFalse(m_group.removeMember(new RoleImpl(Role.USER_ANYONE)));
+ }
+
+ /**
+ * Tests that without permission, the {@link GroupImpl#removeMember(Role)} method can not be accessed.
+ */
+ public void testRemoveMemberWithoutPermissionsFail() throws SecurityException {
+ try {
+ assertFalse(m_group.removeMember(new RoleImpl(Role.USER_ANYONE)));
+
+ fail("Security exception expected!");
+ } catch (SecurityException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ m_securityManager = new TestSecurityManager();
+ System.setSecurityManager(m_securityManager);
+
+ m_group = new GroupImpl("group");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ System.setSecurityManager(null);
+ }
+
+ /**
+ * Provides a test security manager.
+ */
+ static final class TestSecurityManager extends SecurityManager {
+
+ volatile boolean m_allowed = false;
+
+ public void checkPermission(Permission perm) {
+ // Cannot use instanceof as it requires a special permission as well...
+ if ("UserAdminPermission".equals(perm.getClass().getSimpleName())) {
+ String name = perm.getName();
+ if ("admin".equals(name) && !m_allowed) {
+ throw new SecurityException("Not allowed!");
+ }
+ }
+ // Do not check for other permissions...
+ }
+
+ public void checkPermission(Permission perm, Object context) {
+ // Do not check for other permissions...
+ }
+ }
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplSecurityTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,196 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.useradmin.impl.role;
+
+
+import org.apache.felix.useradmin.impl.role.GroupImpl;
+import org.apache.felix.useradmin.impl.role.RoleImpl;
+import org.apache.felix.useradmin.impl.role.UserImpl;
+import org.osgi.service.useradmin.Role;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link GroupImpl}.
+ */
+public class GroupImplTest extends TestCase {
+
+ /**
+ * Tests that adding a role as basic member twice does not cause duplication.
+ */
+ public void testAddBasicMemberWithExistingBasicMemberOk() {
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addMember(new UserImpl("bar")));
+ assertFalse(group.addMember(new UserImpl("bar"))); // should be ignored...
+
+ assertEquals(1, group.getMembers().length);
+ assertNull(group.getRequiredMembers());
+ }
+
+ /**
+ * Tests that adding a role as required member works if it is not contained at all.
+ */
+ public void testAddBasicMemberWithExistingRequiredMemberOk() {
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addRequiredMember(new UserImpl("bar")));
+ assertFalse(group.addMember(new UserImpl("bar"))); // should be ignored...
+
+ assertNull(group.getMembers());
+ assertEquals(1, group.getRequiredMembers().length);
+ }
+
+ /**
+ * Tests that adding a role as basic member while another role with the same name exists does not cause duplication.
+ */
+ public void testAddBasicMemberWithExistingRoleOk() {
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addMember(new UserImpl("bar")));
+ assertFalse(group.addMember(new RoleImpl("bar"))); // should be ignored...
+
+ assertEquals(1, group.getMembers().length);
+ assertNull(group.getRequiredMembers());
+ }
+
+ /**
+ * Tests that adding a role as basic member works if it is not contained at all.
+ */
+ public void testAddNonExistingMemberOk() {
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addMember(new UserImpl("bar")));
+
+ assertEquals(1, group.getMembers().length);
+ assertNull(group.getRequiredMembers());
+ }
+
+ /**
+ * Tests that adding a role as basic member while it exists as required member does not cause duplication.
+ */
+ public void testAddNonExistingRequiredMemberOk() {
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addRequiredMember(new UserImpl("bar")));
+
+ assertNull(group.getMembers());
+ assertEquals(1, group.getRequiredMembers().length);
+ }
+
+ /**
+ * Tests that adding a role as required member works if it is not contained at all.
+ */
+ public void testAddRequiredMemberWithExistingBasicMemberOk() {
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addMember(new UserImpl("bar")));
+ assertFalse(group.addRequiredMember(new UserImpl("bar"))); // should be ignored...
+
+ assertEquals(1, group.getMembers().length);
+ assertNull(group.getRequiredMembers());
+ }
+
+ /**
+ * Tests that adding a role as required member while another role with the same name exists does not cause duplication.
+ */
+ public void testAddRequiredMemberWithExistingRoleOk() {
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addRequiredMember(new UserImpl("bar")));
+ assertFalse(group.addRequiredMember(new RoleImpl("bar"))); // should be ignored...
+
+ assertNull(group.getMembers());
+ assertEquals(1, group.getRequiredMembers().length);
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.RoleImpl#getType()}.
+ */
+ public void testGetType() {
+ GroupImpl group = new GroupImpl("foo");
+
+ assertEquals(Role.GROUP, group.getType());
+ }
+
+ /**
+ * Tests that {@link GroupImpl#hashCode()} yields predictable results.
+ */
+ public void testHashCodeOk() {
+ GroupImpl group1 = new GroupImpl("foo");
+ GroupImpl group2 = new GroupImpl("foo");
+ GroupImpl group3 = new GroupImpl("bar");
+
+ assertTrue(group1.hashCode() == group2.hashCode());
+ assertFalse(group1.hashCode() == group3.hashCode());
+ assertFalse(group2.hashCode() == group3.hashCode());
+ }
+
+ /**
+ * Tests that removing an basic required member works.
+ */
+ public void testRemoveExistingBasicMemberOk() {
+ UserImpl role1 = new UserImpl("bar");
+ UserImpl role2 = new UserImpl("qux");
+
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addMember(role1));
+ assertTrue(group.addRequiredMember(role2));
+
+ assertEquals(1, group.getMembers().length);
+ assertEquals(1, group.getRequiredMembers().length);
+
+ assertTrue(group.removeMember(role1));
+
+ assertNull(group.getMembers());
+ assertEquals(1, group.getRequiredMembers().length);
+ }
+
+ /**
+ * Tests that removing an existing required member works.
+ */
+ public void testRemoveExistingRequiredMemberOk() {
+ UserImpl role1 = new UserImpl("bar");
+ UserImpl role2 = new UserImpl("qux");
+
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addMember(role1));
+ assertTrue(group.addRequiredMember(role2));
+
+ assertEquals(1, group.getMembers().length);
+ assertEquals(1, group.getRequiredMembers().length);
+
+ assertTrue(group.removeMember(role2));
+
+ assertEquals(1, group.getMembers().length);
+ assertNull(group.getRequiredMembers());
+ }
+
+ /**
+ * Tests that removing an basic required member works.
+ */
+ public void testRemoveNonExistingMemberOk() {
+ UserImpl role1 = new UserImpl("bar");
+ UserImpl role2 = new UserImpl("qux");
+ UserImpl role3 = new UserImpl("quu");
+
+ GroupImpl group = new GroupImpl("foo");
+ assertTrue(group.addMember(role1));
+ assertTrue(group.addRequiredMember(role2));
+
+ assertEquals(1, group.getMembers().length);
+ assertEquals(1, group.getRequiredMembers().length);
+
+ assertFalse(group.removeMember(role3));
+
+ assertEquals(1, group.getMembers().length);
+ assertEquals(1, group.getRequiredMembers().length);
+ }
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/GroupImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionarySecurityTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionarySecurityTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionarySecurityTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionarySecurityTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,173 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.felix.useradmin.impl.role;
+
+import java.security.Permission;
+import java.util.Enumeration;
+
+
+import org.apache.felix.useradmin.impl.role.ObservableDictionary;
+import org.osgi.service.useradmin.UserAdminPermission;
+
+import junit.framework.TestCase;
+
+/**
+ * Security-related test cases for {@link ObservableDictionary}.
+ */
+public class ObservableDictionarySecurityTest extends TestCase {
+
+ private static final String GET_ACTION = UserAdminPermission.GET_CREDENTIAL;
+ private static final String CHANGE_ACTION = UserAdminPermission.CHANGE_CREDENTIAL;
+
+ private TestSecurityManager m_securityManager;
+ private ObservableDictionary m_dict;
+
+ /**
+ * Tests that no special permissions are needed to access the {@link ObservableDictionary#size()} method.
+ */
+ public void testSize() throws SecurityException {
+ assertEquals(0, m_dict.size());
+ }
+
+ /**
+ * Tests that no special permissions are needed to access the {@link ObservableDictionary#isEmpty()} method.
+ */
+ public void testIsEmpty() throws SecurityException {
+ assertTrue(m_dict.isEmpty());
+ }
+
+ /**
+ * Tests that no special permissions are needed to access the {@link ObservableDictionary#elements()} method.
+ */
+ public void testElements() throws SecurityException {
+ Enumeration elements = m_dict.elements();
+ assertNotNull(elements);
+ assertFalse(elements.hasMoreElements());
+ }
+
+ /**
+ * Tests that with permission, the {@link ObservableDictionary#get(Object)} method can be accessed.
+ */
+ public void testGetObjectWithPermissionsOk() throws SecurityException {
+ assertNull(m_dict.get("permFoo"));
+ }
+
+ /**
+ * Tests that without permission, the {@link ObservableDictionary#get(Object)} method can not be accessed.
+ */
+ public void testGetObjectWithoutPermissionsFail() throws SecurityException {
+ try {
+ assertNull(m_dict.get("bar"));
+
+ fail("Security exception expected!");
+ } catch (SecurityException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that no special permissions are needed to access the {@link ObservableDictionary#keys()} method.
+ */
+ public void testKeys() throws SecurityException {
+ Enumeration keys = m_dict.keys();
+ assertNotNull(keys);
+ assertFalse(keys.hasMoreElements());
+ }
+
+ /**
+ * Tests that with permission, the {@link ObservableDictionary#put(Object, Object)} method can be accessed.
+ */
+ public void testPutObjectWithPermissionsOk() throws SecurityException {
+ assertNull(m_dict.put("permKey", "value"));
+ }
+
+ /**
+ * Tests that without permission, the {@link ObservableDictionary#put(Object, Object)} method can not be accessed.
+ */
+ public void testPutObjectWithoutPermissionsFail() throws SecurityException {
+ try {
+ assertNull(m_dict.put("key", "value"));
+
+ fail("Security exception expected!");
+ } catch (SecurityException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that with permission, the {@link ObservableDictionary#remove(Object)} method can be accessed.
+ */
+ public void testRemoveObjectWithPermissionsOk() throws SecurityException {
+ assertNull(m_dict.remove("permKey"));
+ }
+
+ /**
+ * Tests that without permission, the {@link ObservableDictionary#remove(Object)} method can not be accessed.
+ */
+ public void testRemoveObjectWithoutPermissionsFail() throws SecurityException {
+ try {
+ assertNull(m_dict.remove("key"));
+
+ fail("Security exception expected!");
+ } catch (SecurityException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ m_securityManager = new TestSecurityManager();
+ System.setSecurityManager(m_securityManager);
+
+ m_dict = new ObservableDictionary(GET_ACTION, CHANGE_ACTION);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ System.setSecurityManager(null);
+ }
+
+ /**
+ * Provides a test security manager.
+ */
+ static final class TestSecurityManager extends SecurityManager {
+
+ public void checkPermission(Permission perm) {
+ // Cannot use instanceof as it requires a special permission as well...
+ if ("UserAdminPermission".equals(perm.getClass().getSimpleName())) {
+ String name = perm.getName();
+ if ((name != null) && !name.startsWith("perm")) {
+ throw new SecurityException("Not allowed!");
+ }
+ }
+ // Do not check for other permissions...
+ }
+
+ public void checkPermission(Permission perm, Object context) {
+ // Do not check for other permissions...
+ }
+ }
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionarySecurityTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionaryTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionaryTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionaryTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionaryTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,247 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.useradmin.impl.role;
+
+import java.util.Date;
+
+import org.apache.felix.useradmin.impl.role.ObservableDictionary;
+import org.apache.felix.useradmin.impl.role.ObservableDictionary.DictionaryChangeListener;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Test case for {@link ObservableDictionary}.
+ */
+public class ObservableDictionaryTest extends TestCase {
+
+ /**
+ * Implementation of {@link DictionaryChangeListener} that keeps track of
+ * which callback methods are being called at least once.
+ */
+ static class StateCheckingDictionaryChangeListener implements DictionaryChangeListener {
+
+ volatile boolean m_addedCalled = false;
+ volatile boolean m_removedCalled = false;
+ volatile boolean m_changedCalled = false;
+
+ public void entryAdded(Object key, Object value) {
+ m_addedCalled = true;
+ }
+
+ public void entryChanged(Object key, Object oldValue, Object newValue) {
+ m_changedCalled = true;
+ }
+
+ public void entryRemoved(Object key) {
+ m_removedCalled = true;
+ }
+ }
+
+ private ObservableDictionary m_dict;
+ private StateCheckingDictionaryChangeListener m_listener;
+
+ /**
+ * Tests that adding an entry emits a change event.
+ */
+ public void testAddEntryEmitsEvent() {
+ m_dict.setDictionaryChangeListener(m_listener);
+
+ m_dict.put("key", "value");
+
+ assertTrue(m_listener.m_addedCalled);
+ assertFalse(m_listener.m_changedCalled);
+ assertFalse(m_listener.m_removedCalled);
+ }
+
+ /**
+ * Tests that changing an entry emits a change event.
+ */
+ public void testChangeEntryEmitsEvent() {
+ m_dict.put("key", "value1");
+
+ m_dict.setDictionaryChangeListener(m_listener);
+
+ m_dict.put("key", "value2");
+
+ assertTrue(m_listener.m_changedCalled);
+ assertFalse(m_listener.m_addedCalled);
+ assertFalse(m_listener.m_removedCalled);
+ }
+
+ /**
+ * Tests that creating a new {@link ObservableDictionary} with a valid dictionary succeeds.
+ */
+ public void testCreateWithNonNullDictionaryFail() {
+ m_dict.put("foo", "bar");
+ m_dict.put("bar", "foo");
+
+ m_dict = new ObservableDictionary("foo", "bar", m_dict);
+
+ assertEquals("bar", m_dict.get("foo"));
+ assertEquals("foo", m_dict.get("bar"));
+ }
+
+ /**
+ * Tests that creating a new {@link ObservableDictionary} with a <code>null</code> dictionary fails.
+ */
+ public void testCreateWithNullDictionaryFail() {
+ try {
+ new ObservableDictionary("foo", "bar", null);
+ fail("Expected IllegalArgumentException!");
+ } catch (Exception e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that we can get any kind of object from an {@link ObservableDictionary}.
+ */
+ public void testGetAnyKindOfObject() {
+ assertNull(m_dict.get("non-existing"));
+
+ Integer key = Integer.valueOf(3);
+ Date value = new Date();
+
+ m_dict.put(key, value);
+ assertEquals(value, m_dict.get(key));
+ }
+
+ /**
+ * Tests that we cannot get a null-key form a {@link ObservableDictionary}.
+ */
+ public void testGetNullKeyFail() {
+ try {
+ m_dict.get(null);
+
+ fail();
+ } catch (Exception e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Test that we can put any kind of object in a {@link ObservableDictionary}.
+ */
+ public void testPutAnyKindOfObject() {
+ Integer key = Integer.valueOf(3);
+ Date value = new Date();
+
+ m_dict.put(key, value);
+ assertEquals(value, m_dict.get(key));
+
+ m_dict.put(key, "other-value");
+ assertEquals("other-value", m_dict.get(key));
+ }
+
+ /**
+ * Test that we cannot put a null-key into a {@link ObservableDictionary}.
+ */
+ public void testPutNullKeyFail() {
+ try {
+ m_dict.put(null, "value");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Test that we cannot put a null-value into a {@link ObservableDictionary}.
+ */
+ public void testPutNullValueFail() {
+ try {
+ m_dict.put("key", null);
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that removing a key emits a change event.
+ */
+ public void testRemoveEmitsEvent() {
+ m_dict.setDictionaryChangeListener(m_listener);
+
+ m_dict.remove("key");
+
+ assertTrue(m_listener.m_removedCalled);
+ assertFalse(m_listener.m_addedCalled);
+ assertFalse(m_listener.m_changedCalled);
+ }
+
+ /**
+ * Tests that we cannot remove a null-key from a {@link ObservableDictionary}.
+ */
+ public void testRemoveNullKeyFail() {
+ try {
+ m_dict.remove(null);
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that we can remove any kind of object from a {@link ObservableDictionary}.
+ */
+ public void testRemoveObject() {
+ Integer key = Integer.valueOf(3);
+ Date value = new Date();
+
+ m_dict.put(key, value);
+ assertEquals(value, m_dict.get(key));
+
+ m_dict.remove(key);
+ assertNull(m_dict.get(key));
+ }
+
+ /**
+ * Tests that {@link ObservableDictionary#equals(Object)} and {@link ObservableDictionary#hashCode()} work correctly.
+ */
+ public void testEqualsAndHashcode() {
+ ObservableDictionary d1 = new ObservableDictionary(null, null);
+ ObservableDictionary d2 = new ObservableDictionary(null, null);
+
+ assertTrue(d1.hashCode() == d2.hashCode());
+ assertTrue(d1.equals(d2));
+ assertTrue(d1.equals(d1));
+
+ d2.put("foo", "bar");
+
+ assertFalse(d1.hashCode() == d2.hashCode());
+ assertFalse(d1.equals(d2));
+ assertFalse(d1.equals(null));
+ assertFalse(d1.equals("bar"));
+ assertTrue(d1.equals(d1));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ m_dict = new ObservableDictionary(null, null);
+ m_listener = new StateCheckingDictionaryChangeListener();
+
+ }
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservableDictionaryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservablePropertiesTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservablePropertiesTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservablePropertiesTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservablePropertiesTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,119 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.felix.useradmin.impl.role;
+
+import org.apache.felix.useradmin.impl.role.ObservableProperties;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link ObservableProperties}.
+ */
+public class ObservablePropertiesTest extends TestCase {
+
+ private ObservableProperties m_dict;
+
+ /**
+ * Tests that calling get with a non-string value yields an exception.
+ */
+ public void testGetNonStringKeyFail() {
+ try {
+ m_dict.get(Integer.valueOf(1));
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that calling get with a string value does not yield an exception.
+ */
+ public void testGetStringKeyOk() {
+ assertNull(m_dict.get("key"));
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.ObservableProperties#put(java.lang.Object, java.lang.Object)}.
+ */
+ public void testPutByteArrayValueOk() {
+ assertNull(m_dict.put("key", "value".getBytes()));
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.ObservableProperties#put(java.lang.Object, java.lang.Object)}.
+ */
+ public void testPutNonStringKeyFail() {
+ try {
+ m_dict.put(Integer.valueOf(1), "value");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.ObservableProperties#put(java.lang.Object, java.lang.Object)}.
+ */
+ public void testPutNonStringValueFail() {
+ try {
+ m_dict.put("key", Integer.valueOf(1));
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.ObservableProperties#put(java.lang.Object, java.lang.Object)}.
+ */
+ public void testPutStringKeyValueOk() {
+ assertNull(m_dict.put("key", "value"));
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.ObservableProperties#remove(java.lang.Object)}.
+ */
+ public void testRemoveNonStringKeyFail() {
+ try {
+ m_dict.remove(Integer.valueOf(1));
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.ObservableProperties#remove(java.lang.Object)}.
+ */
+ public void testRemoveStringKeyOk() {
+ assertNull(m_dict.remove("foo"));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ m_dict = new ObservableProperties(null, null);
+ }
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/ObservablePropertiesTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/RoleImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/RoleImplTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/RoleImplTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/RoleImplTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.useradmin.impl.role;
+
+import java.util.Dictionary;
+
+import junit.framework.TestCase;
+
+
+import org.apache.felix.useradmin.impl.role.RoleImpl;
+import org.osgi.service.useradmin.Role;
+
+/**
+ * Test case for {@link RoleImpl}.
+ */
+public class RoleImplTest extends TestCase {
+
+ /**
+ * Tests that we must enter a valid name upon constructing a {@link RoleImpl}.
+ */
+ public void testCreateNullNameFail() {
+ try {
+ new RoleImpl(null);
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that we must enter a valid name upon constructing a {@link RoleImpl}.
+ */
+ public void testCreateEmptyNameFail() {
+ try {
+ new RoleImpl(" ");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ // Ok; expected
+ }
+ }
+
+ /**
+ * Tests that we can obtain the name of a {@link RoleImpl} as set in its constructor.
+ */
+ public void testGetName() {
+ RoleImpl role = new RoleImpl("foo");
+ assertEquals("foo", role.getName());
+ }
+
+ /**
+ * Tests that we can obtain the properties of a {@link RoleImpl}.
+ */
+ public void testGetProperties() {
+ RoleImpl role = new RoleImpl("foo");
+
+ Dictionary dict = role.getProperties();
+ assertNotNull(dict);
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.RoleImpl#getType()}.
+ */
+ public void testGetType() {
+ RoleImpl role = new RoleImpl("foo");
+ assertEquals(Role.ROLE, role.getType());
+ }
+
+ /**
+ * Tests that {@link RoleImpl#hashCode()} yields predictable results.
+ */
+ public void testHashCodeOk() {
+ RoleImpl role1 = new RoleImpl("foo");
+ RoleImpl role2 = new RoleImpl("foo");
+ RoleImpl role3 = new RoleImpl("bar");
+
+ assertTrue(role1.hashCode() == role2.hashCode());
+ assertFalse(role1.hashCode() == role3.hashCode());
+ assertFalse(role2.hashCode() == role3.hashCode());
+ }
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/RoleImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/UserImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/UserImplTest.java?rev=1391437&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/UserImplTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/UserImplTest.java Fri Sep 28 12:58:59 2012
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.felix.useradmin.impl.role;
+
+import java.util.Dictionary;
+
+
+import org.apache.felix.useradmin.impl.role.UserImpl;
+import org.osgi.service.useradmin.Role;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link UserImpl}.
+ */
+public class UserImplTest extends TestCase {
+
+ /**
+ * Tests that we can get the credentials for a {@link UserImpl}.
+ */
+ public void testGetCredentials() {
+ UserImpl user = new UserImpl("foo");
+
+ Dictionary dict = user.getCredentials();
+ assertNotNull(dict);
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.RoleImpl#getType()}.
+ */
+ public void testGetType() {
+ UserImpl user = new UserImpl("foo");
+
+ assertEquals(Role.USER, user.getType());
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.UserImpl#hasCredential(java.lang.String, java.lang.Object)}.
+ */
+ public void testHasExistingCredentialAlternativeValueTypeOk() {
+ UserImpl user = new UserImpl("foo");
+
+ Dictionary dict = user.getCredentials();
+ dict.put("password", "secret");
+
+ // Direct comparison...
+ assertTrue(user.hasCredential("password", "secret"));
+
+ // In case the given value is a byte[]...
+ assertTrue(user.hasCredential("password", "secret".getBytes()));
+
+ dict.put("password", "otherSecret".getBytes());
+
+ // Direct comparison...
+ assertTrue(user.hasCredential("password", "otherSecret".getBytes()));
+
+ // In case the stored value is a byte[]...
+ assertTrue(user.hasCredential("password", "otherSecret"));
+ }
+
+ /**
+ * Test method for {@link org.apache.felix.useradmin.impl.role.UserImpl#hasCredential(java.lang.String, java.lang.Object)}.
+ */
+ public void testHasExistingCredentialOk() {
+ UserImpl user = new UserImpl("foo");
+
+ Dictionary dict = user.getCredentials();
+ dict.put("password", "secret");
+
+ assertTrue(user.hasCredential("password", "secret"));
+ }
+
+ /**
+ * Tests that {@link UserImpl#hashCode()} yields predictable results.
+ */
+ public void testHashCodeOk() {
+ UserImpl user1 = new UserImpl("foo");
+ UserImpl user2 = new UserImpl("foo");
+ UserImpl user3 = new UserImpl("bar");
+
+ assertTrue(user1.hashCode() == user2.hashCode());
+ assertFalse(user1.hashCode() == user3.hashCode());
+ assertFalse(user2.hashCode() == user3.hashCode());
+ }
+}
Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/role/UserImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native