felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1412574 [2/2] - in /felix/trunk/useradmin: filestore/src/main/java/org/apache/felix/useradmin/filestore/ filestore/src/main/java/org/apache/felix/useradmin/filestore/osgi/ filestore/src/test/java/org/apache/felix/useradmin/filestore/ itest...
Date Thu, 22 Nov 2012 14:57:58 GMT
Added: felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/ObservableUser.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/ObservableUser.java?rev=1412574&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/ObservableUser.java (added)
+++ felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/ObservableUser.java Thu Nov 22 14:57:51 2012
@@ -0,0 +1,67 @@
+/**
+ * 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.osgi.service.useradmin.User;
+import org.osgi.service.useradmin.UserAdminPermission;
+
+/**
+ * Provides an adapter for all {@link User}s, allowing its changes to be 
+ * externally observed.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class ObservableUser extends ObservableRole implements User {
+    
+    private static final long serialVersionUID = 344574927109701877L;
+    
+    private final ObservableProperties m_credentials;
+    
+    /**
+     * Creates a new {@link ObservableUser} instance.
+     * 
+     * @param user the user-role to observe for changes, cannot be <code>null</code>.
+     * @throws IllegalArgumentException in case the given user-role was <code>null</code>.
+     */
+    public ObservableUser(User user) {
+        super(user);
+        
+        m_credentials = new ObservableProperties(UserAdminPermission.GET_CREDENTIAL, UserAdminPermission.CHANGE_CREDENTIAL, user.getCredentials());
+        m_credentials.setDictionaryChangeListener(this);
+    }
+
+    public Dictionary getCredentials() {
+        return m_credentials;
+    }
+
+    public boolean hasCredential(String key, Object value) {
+        // Will throw a SecurityException if we're not allowed to do this!
+        m_credentials.get(key);
+        // We're allowed to do this; let the original implementation figure out
+        // whether or not the test holds...
+        return ((User) m_delegate).hasCredential(key, value);
+    }
+    
+    public String toString()
+    {
+        return m_delegate.toString();
+    }
+}

Propchange: felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/ObservableUser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/RoleImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/RoleImpl.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/RoleImpl.java (original)
+++ felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/RoleImpl.java Thu Nov 22 14:57:51 2012
@@ -18,25 +18,25 @@ package org.apache.felix.useradmin.impl.
 
 import java.io.Serializable;
 import java.util.Dictionary;
+import java.util.Properties;
 
-
-import org.apache.felix.useradmin.impl.RoleChangeListener;
-import org.apache.felix.useradmin.impl.role.ObservableDictionary.DictionaryChangeListener;
 import org.osgi.service.useradmin.Role;
-import org.osgi.service.useradmin.UserAdminPermission;
 
 /**
- * Provides an implementation of {@link Role}.
+ * Provides a default implementation of {@link Role} that is not security-aware 
+ * and does not keep track of changes to its properties.
+ * <p>
+ * This implementation should be wrapped in an {@link ObservableRole} when 
+ * returned from the UserAdmin implementation. 
+ * </p>
  */
-public class RoleImpl implements Serializable, Role, DictionaryChangeListener {
-	
-    private static final long serialVersionUID = -6292833161748591485L;
+public class RoleImpl implements Serializable, Role {
 
-	private final ObservableProperties m_properties;
+	private static final long serialVersionUID = 6403795608776837916L;
+	
+    private final Properties m_properties;
     private final String m_name;
     private final int m_type;
-    
-    private volatile RoleChangeListener m_listener;
 
     /**
      * Creates a new {@link RoleImpl} instance of type {@link Role#ROLE} and a given name.
@@ -59,38 +59,7 @@ public class RoleImpl implements Seriali
         }
         m_type = type;
         m_name = name;
-        m_properties = new ObservableProperties(null, UserAdminPermission.CHANGE_PROPERTY);
-        m_properties.setDictionaryChangeListener(this);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public final void entryAdded(Object key, Object value) {
-        RoleChangeListener listener = m_listener;
-        if (listener != null) {
-            listener.propertyAdded(this, key, value);
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public final void entryRemoved(Object key) {
-        RoleChangeListener listener = m_listener;
-        if (listener != null) {
-            listener.propertyRemoved(this, key);
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public final void entryChanged(Object key, Object oldValue, Object newValue) {
-        RoleChangeListener listener = m_listener;
-        if (listener != null) {
-            listener.propertyChanged(this, key, oldValue, newValue);
-        }
+        m_properties = new Properties();
     }
     
     /**
@@ -152,15 +121,6 @@ public class RoleImpl implements Seriali
         result = prime * result + m_type;
         return result;
     }
-
-    /**
-     * Sets the {@link RoleChangeListener} for this role implementation.
-     * 
-     * @param listener the listener to set, may be <code>null</code> to stop listening.
-     */
-    public void setRoleChangeListener(RoleChangeListener listener) {
-        m_listener = listener;
-    }
     
     /**
      * {@inheritDoc}

Modified: felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/UserImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/UserImpl.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/UserImpl.java (original)
+++ felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/role/UserImpl.java Thu Nov 22 14:57:51 2012
@@ -18,19 +18,24 @@ package org.apache.felix.useradmin.impl.
 
 import java.util.Arrays;
 import java.util.Dictionary;
+import java.util.Properties;
 
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
-import org.osgi.service.useradmin.UserAdminPermission;
 
 /**
- * Provides an implementation of {@link User}.
+ * Provides a default implementation of {@link User} that is not security-aware 
+ * and does not keep track of changes to its properties.
+ * <p>
+ * This implementation should be wrapped in an {@link ObservableUser} when 
+ * returned from the UserAdmin implementation. 
+ * </p>
  */
 public class UserImpl extends RoleImpl implements User {
 
-    private static final long serialVersionUID = 8639414204247841034L;
-    
-	private final ObservableProperties m_credentials;
+	private static final long serialVersionUID = 7332249440557443008L;
+	
+    private final Properties m_credentials;
 
     /**
      * Creates a new {@link UserImpl} instance with type {@link Role#USER}.
@@ -50,8 +55,7 @@ public class UserImpl extends RoleImpl i
     protected UserImpl(int type, String name) {
         super(type, name);
 
-        m_credentials = new ObservableProperties(UserAdminPermission.GET_CREDENTIAL, UserAdminPermission.CHANGE_CREDENTIAL);
-        m_credentials.setDictionaryChangeListener(this);
+        m_credentials = new Properties();
     }
 
     /**
@@ -65,7 +69,6 @@ public class UserImpl extends RoleImpl i
      * {@inheritDoc}
      */
     public boolean hasCredential(String key, Object value) {
-        // Will throw a SecurityException if we're not allowed to do this!
         Object result = m_credentials.get(key);
 
         // Be a bit more lenient with the various results we can get...

Modified: felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/RoleRepositoryStoreHelper.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/RoleRepositoryStoreHelper.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/RoleRepositoryStoreHelper.java (original)
+++ felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/RoleRepositoryStoreHelper.java Thu Nov 22 14:57:51 2012
@@ -16,12 +16,9 @@
  */
 package org.apache.felix.useradmin.osgi;
 
-import java.io.IOException;
-import java.util.concurrent.atomic.AtomicBoolean;
-
 import org.apache.felix.useradmin.RoleRepositoryStore;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Filter;
 import org.osgi.service.useradmin.Role;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -34,8 +31,6 @@ import org.osgi.util.tracker.ServiceTrac
  */
 class RoleRepositoryStoreHelper extends ServiceTracker implements RoleRepositoryStore {
 	
-	private final AtomicBoolean m_initialized = new AtomicBoolean(false);
-    
     /**
      * Creates a new {@link RoleRepositoryStoreHelper} instance.
      * 
@@ -45,50 +40,25 @@ class RoleRepositoryStoreHelper extends 
         super(context, RoleRepositoryStore.class.getName(), null /* customizer */);
     }
 
-    public Object addingService(ServiceReference reference) {
-    	// FELIX-3735: store can also become available *after* this bundle is started...
-    	RoleRepositoryStore store = (RoleRepositoryStore) super.addingService(reference);
-    	try {
-    		initializeStore(store);
-		} catch (IOException e) {
-            // Ignore; nothing we can do about this here...
-		}
-    	return store;
-    }
-    
-    public boolean addRole(Role role) throws IOException {
+    public Role addRole(String roleName, int type) throws Exception {
         RoleRepositoryStore store = getStore();
         if (store != null) {
-            return store.addRole(role);
+            return store.addRole(roleName, type);
         }
 
-        return false;
-    }
-    
-    public synchronized void close() {
-        try {
-            RoleRepositoryStore store = getStore();
-            if (store != null) {
-                closeStore(store);
-            }
-        }
-        catch (IOException e) {
-            // Ignore; nothing we can do about this here...
-        } finally {
-            super.close();
-        }
+        return null;
     }
 
-    public Role[] getAllRoles() throws IOException {
+    public Role[] getRoles(Filter filter) throws Exception {
         RoleRepositoryStore store = getStore();
         if (store != null) {
-            return store.getAllRoles();
+            return store.getRoles(filter);
         }
 
         return new Role[0];
     }
 
-    public Role getRoleByName(String roleName) throws IOException {
+    public Role getRoleByName(String roleName) throws Exception {
         RoleRepositoryStore store = getStore();
         if (store != null) {
             return store.getRoleByName(roleName);
@@ -96,33 +66,15 @@ class RoleRepositoryStoreHelper extends 
 
         return null;
     }
-    
-    public void initialize() throws IOException {
-        RoleRepositoryStore store = getStore();
-        if (store != null) {
-        	initializeStore(store);
-        }
-    }
-
-    public void removedService(ServiceReference reference, Object service) {
-        RoleRepositoryStore removedStore = (RoleRepositoryStore) service;
-        try {
-        	closeStore(removedStore);
-        }
-        catch (IOException e) {
-            // Ignore; nothing we can do about this here...
-        }
-        super.removedService(reference, service);
-    }
 
-    public boolean removeRole(Role role) throws IOException {
+    public Role removeRole(String roleName) throws Exception {
         // and possibly also from our tracked store...
         RoleRepositoryStore store = getStore();
         if (store != null) {
-            return store.removeRole(role);
+            return store.removeRole(roleName);
         }
 
-        return false;
+        return null;
     }
 
     /**
@@ -133,41 +85,4 @@ class RoleRepositoryStoreHelper extends 
     private RoleRepositoryStore getStore() {
         return (RoleRepositoryStore) getService();
     }
-
-	/**
-	 * Closes the given store.
-	 * 
-	 * @param store the store to close, cannot be <code>null</code>.
-	 * @throws IOException in case initialization failed.
-	 */
-	private void closeStore(RoleRepositoryStore store) throws IOException {
-		// Only close the store if its initialized...
-		boolean initialized = m_initialized.get();
-		if (initialized) {
-			store.close();
-
-			do {
-				initialized = m_initialized.get();
-			} while (!m_initialized.compareAndSet(initialized, false));
-		}
-	}
-
-	/**
-	 * Initializes the given store.
-	 * 
-	 * @param store the store to initialize, cannot be <code>null</code>.
-	 * @throws IOException in case initialization failed.
-	 */
-	private void initializeStore(RoleRepositoryStore store) throws IOException {
-		// FELIX-3735: store can also become available *after* this bundle is started; 
-		// hence we need to ensure we do not initialize the store twice...
-		boolean initialized = m_initialized.get();
-		if (!initialized) {
-			store.initialize();
-
-			do {
-				initialized = m_initialized.get();
-			} while (!m_initialized.compareAndSet(initialized, true));
-		}
-	}
 }

Modified: felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/ServiceContext.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/ServiceContext.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/ServiceContext.java (original)
+++ felix/trunk/useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/ServiceContext.java Thu Nov 22 14:57:51 2012
@@ -51,7 +51,6 @@ final class ServiceContext {
         m_eventAdmin.open();
         m_listenerList.open(true /* trackAllServices */);
         m_eventDispatcher.start();
-        m_roleRepository.start();
         m_store.open();
     }
     
@@ -59,7 +58,6 @@ final class ServiceContext {
      * Stops/closes all helpers.
      */
     public void stop() {
-        m_roleRepository.stop();
         m_eventDispatcher.stop();
         m_listenerList.close();
         m_eventAdmin.close();

Modified: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/AuthorizationImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/AuthorizationImplTest.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/AuthorizationImplTest.java (original)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/AuthorizationImplTest.java Thu Nov 22 14:57:51 2012
@@ -22,16 +22,18 @@ import java.util.List;
 
 import junit.framework.TestCase;
 
-import org.apache.felix.useradmin.impl.role.GroupImpl;
-import org.apache.felix.useradmin.impl.role.UserImpl;
+import org.apache.felix.useradmin.RoleFactory;
 import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
 
 /**
  * Test cases for {@link AuthorizationImpl}.
  */
 public class AuthorizationImplTest extends TestCase {
 
-    private RoleRepository m_roleManager;
+    private RoleRepository m_roleRepository;
+    private Role m_anyone;
 
     /**
      * {@inheritDoc}
@@ -39,7 +41,9 @@ public class AuthorizationImplTest exten
     protected void setUp() throws Exception {
         super.setUp();
         
-        m_roleManager = new RoleRepository(new MemoryRoleRepositoryStore());
+        m_roleRepository = new RoleRepository(new MemoryRoleRepositoryStore());
+        
+        m_anyone = m_roleRepository.getRoleByName(Role.USER_ANYONE);
     }
     
     /**
@@ -62,26 +66,26 @@ public class AuthorizationImplTest exten
         Group residents = createGroup("Residents");
         
         // Users
-        UserImpl elmer = new UserImpl("Elmer");
-        UserImpl fudd = new UserImpl("Fudd");
-        UserImpl marvin = new UserImpl("Marvin");
-        UserImpl pepe = new UserImpl("Pepe");
-        UserImpl daffy = new UserImpl("Daffy");
-        UserImpl foghorn = new UserImpl("Foghorn");
+        User elmer = RoleFactory.createUser("Elmer");
+        User fudd = RoleFactory.createUser("Fudd");
+        User marvin = RoleFactory.createUser("Marvin");
+        User pepe = RoleFactory.createUser("Pepe");
+        User daffy =RoleFactory.createUser("Daffy");
+        User foghorn = RoleFactory.createUser("Foghorn");
         
         // Not explicitly mentioned; but needed to comply with the semantics
-        alarmSystemControl.addRequiredMember(RoleRepository.USER_ANYONE);
-        internetAccess.addRequiredMember(RoleRepository.USER_ANYONE);
-        temperatureControl.addRequiredMember(RoleRepository.USER_ANYONE);
-        photoAlbumEdit.addRequiredMember(RoleRepository.USER_ANYONE);
-        photoAlbumView.addRequiredMember(RoleRepository.USER_ANYONE);
-        portForwarding.addRequiredMember(RoleRepository.USER_ANYONE);
-
-        administrators.addRequiredMember(RoleRepository.USER_ANYONE);
-        buddies.addRequiredMember(RoleRepository.USER_ANYONE);
-        children.addRequiredMember(RoleRepository.USER_ANYONE);
-        adults.addRequiredMember(RoleRepository.USER_ANYONE);
-        residents.addRequiredMember(RoleRepository.USER_ANYONE);
+        alarmSystemControl.addRequiredMember(m_anyone);
+        internetAccess.addRequiredMember(m_anyone);
+        temperatureControl.addRequiredMember(m_anyone);
+        photoAlbumEdit.addRequiredMember(m_anyone);
+        photoAlbumView.addRequiredMember(m_anyone);
+        portForwarding.addRequiredMember(m_anyone);
+
+        administrators.addRequiredMember(m_anyone);
+        buddies.addRequiredMember(m_anyone);
+        children.addRequiredMember(m_anyone);
+        adults.addRequiredMember(m_anyone);
+        residents.addRequiredMember(m_anyone);
 
         // Table 107.1
         residents.addMember(elmer);
@@ -121,7 +125,7 @@ public class AuthorizationImplTest exten
         portForwarding.addRequiredMember(administrators);
 
         // Test with the user "foghorn"...
-        AuthorizationImpl auth = new AuthorizationImpl(foghorn, m_roleManager);
+        AuthorizationImpl auth = new AuthorizationImpl(foghorn, m_roleRepository);
 
         assertFalse(auth.hasRole(alarmSystemControl.getName()));
         assertFalse(auth.hasRole(internetAccess.getName()));
@@ -131,7 +135,7 @@ public class AuthorizationImplTest exten
         assertFalse(auth.hasRole(portForwarding.getName()));
 
         // Test with the user "fudd"...
-        auth = new AuthorizationImpl(fudd, m_roleManager);
+        auth = new AuthorizationImpl(fudd, m_roleRepository);
 
         assertFalse(auth.hasRole(alarmSystemControl.getName()));
         assertTrue(auth.hasRole(internetAccess.getName()));
@@ -141,7 +145,7 @@ public class AuthorizationImplTest exten
         assertFalse(auth.hasRole(portForwarding.getName()));
 
         // Test with the user "elmer"...
-        auth = new AuthorizationImpl(elmer, m_roleManager);
+        auth = new AuthorizationImpl(elmer, m_roleRepository);
 
         assertTrue(auth.hasRole(alarmSystemControl.getName()));
         assertTrue(auth.hasRole(internetAccess.getName()));
@@ -155,54 +159,54 @@ public class AuthorizationImplTest exten
      * Test that the tests for membership work correctly. 
      */
     public void testHasRoleOk() {
-        GroupImpl citizens = createGroup("citizen");
-        citizens.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group citizens = createGroup("citizen");
+        citizens.addRequiredMember(m_anyone);
         
-        GroupImpl adults = createGroup("adult");
-        adults.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group adults = createGroup("adult");
+        adults.addRequiredMember(m_anyone);
         
-        GroupImpl voters = createGroup("voter");
+        Group voters = createGroup("voter");
         voters.addRequiredMember(citizens);
         voters.addRequiredMember(adults);
-        voters.addMember(RoleRepository.USER_ANYONE);
+        voters.addMember(m_anyone);
         
         // Elmer belongs to the citizens and adults...
-        UserImpl elmer = createUser("elmer");
+        User elmer = createUser("elmer");
         citizens.addMember(elmer);
         adults.addMember(elmer);
         
         // Pepe belongs to the citizens, but is not an adult...
-        UserImpl pepe = createUser("pepe");
+        User pepe = createUser("pepe");
         citizens.addMember(pepe);
         
         // Bugs is an adult, but is not a citizen...
-        UserImpl bugs = createUser("bugs");
+        User bugs = createUser("bugs");
         adults.addMember(bugs);
         
         // Daffy is not an adult, neither a citizen...
-        UserImpl daffy = createUser("daffy");
+        User daffy = createUser("daffy");
         
         AuthorizationImpl auth;
 
-        auth = new AuthorizationImpl(elmer, m_roleManager);
+        auth = new AuthorizationImpl(elmer, m_roleRepository);
         assertTrue(auth.hasRole("adult"));
         assertTrue(auth.hasRole("citizen"));
         assertTrue(auth.hasRole("voter"));
         assertFalse(auth.hasRole("non-existing-role"));
 
-        auth = new AuthorizationImpl(pepe, m_roleManager);
+        auth = new AuthorizationImpl(pepe, m_roleRepository);
         assertFalse(auth.hasRole("adult"));
         assertTrue(auth.hasRole("citizen"));
         assertFalse(auth.hasRole("voter"));
         assertFalse(auth.hasRole("non-existing-role"));
 
-        auth = new AuthorizationImpl(bugs, m_roleManager);
+        auth = new AuthorizationImpl(bugs, m_roleRepository);
         assertTrue(auth.hasRole("adult"));
         assertFalse(auth.hasRole("citizen"));
         assertFalse(auth.hasRole("voter"));
         assertFalse(auth.hasRole("non-existing-role"));
 
-        auth = new AuthorizationImpl(daffy, m_roleManager);
+        auth = new AuthorizationImpl(daffy, m_roleRepository);
         assertFalse(auth.hasRole("adult"));
         assertFalse(auth.hasRole("citizen"));
         assertFalse(auth.hasRole("voter"));
@@ -213,51 +217,51 @@ public class AuthorizationImplTest exten
      * Test that the tests for membership work correctly. 
      */
     public void testGetRolesOk() {
-        GroupImpl citizens = createGroup("citizen");
-        citizens.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group citizens = createGroup("citizen");
+        citizens.addRequiredMember(m_anyone);
         
-        GroupImpl adults = createGroup("adult");
-        adults.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group adults = createGroup("adult");
+        adults.addRequiredMember(m_anyone);
         
-        GroupImpl voters = createGroup("voter");
+        Group voters = createGroup("voter");
         voters.addRequiredMember(citizens);
         voters.addRequiredMember(adults);
-        voters.addMember(RoleRepository.USER_ANYONE);
+        voters.addMember(m_anyone);
         
         // Elmer belongs to the citizens and adults...
-        UserImpl elmer = createUser("elmer");
+        User elmer = createUser("elmer");
         citizens.addMember(elmer);
         adults.addMember(elmer);
         
         // Pepe belongs to the citizens, but is not an adult...
-        UserImpl pepe = createUser("pepe");
+        User pepe = createUser("pepe");
         citizens.addMember(pepe);
         
         // Bugs is an adult, but is not a citizen...
-        UserImpl bugs = createUser("bugs");
+        User bugs = createUser("bugs");
         adults.addMember(bugs);
 
         // Daffy is not an adult, neither a citizen...
-        UserImpl daffy = createUser("daffy");
+        User daffy = createUser("daffy");
 
-        // Daffy is not an adult, neither a citizen...
-        UserImpl donald = new UserImpl("donald");
+        // Donald is not an adult, neither a citizen...
+        User donald = RoleFactory.createUser("donald");
         
         AuthorizationImpl auth;
 
-        auth = new AuthorizationImpl(elmer, m_roleManager);
+        auth = new AuthorizationImpl(elmer, m_roleRepository);
         assertSameRoles(new String[]{ "elmer", "adult", "citizen", "voter" }, auth.getRoles());
 
-        auth = new AuthorizationImpl(pepe, m_roleManager);
+        auth = new AuthorizationImpl(pepe, m_roleRepository);
         assertSameRoles(new String[]{ "pepe", "citizen" }, auth.getRoles());
 
-        auth = new AuthorizationImpl(bugs, m_roleManager);
+        auth = new AuthorizationImpl(bugs, m_roleRepository);
         assertSameRoles(new String[]{ "bugs", "adult" }, auth.getRoles());
 
-        auth = new AuthorizationImpl(daffy, m_roleManager);
+        auth = new AuthorizationImpl(daffy, m_roleRepository);
         assertSameRoles(new String[]{ "daffy" }, auth.getRoles());
 
-        auth = new AuthorizationImpl(donald, m_roleManager);
+        auth = new AuthorizationImpl(donald, m_roleRepository);
         assertNull(auth.getRoles());
     }
 
@@ -271,15 +275,11 @@ public class AuthorizationImplTest exten
         assertTrue("Not seen: " + e, e.isEmpty());
     }
 
-    private GroupImpl createGroup(String name) {
-        GroupImpl result = new GroupImpl(name);
-        m_roleManager.addRole(result);
-        return result;
+    private Group createGroup(String name) {
+        return (Group) m_roleRepository.addRole(name, Role.GROUP);
     }
 
-    private UserImpl createUser(String name) {
-        UserImpl result = new UserImpl(name);
-        m_roleManager.addRole(result);
-        return result;
+    private User createUser(String name) {
+        return (User) m_roleRepository.addRole(name, Role.USER);
     }
 }

Added: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/CustomRoleImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/CustomRoleImplTest.java?rev=1412574&view=auto
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/CustomRoleImplTest.java (added)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/CustomRoleImplTest.java Thu Nov 22 14:57:51 2012
@@ -0,0 +1,486 @@
+/**
+ *  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.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.useradmin.RoleRepositoryStore;
+import org.osgi.framework.Filter;
+import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+
+/**
+ * Tests whether using custom roles in the backend work as expected and yield the proper events.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class CustomRoleImplTest extends TestCase
+{
+    static class MyGroupImpl extends MyUserImpl implements Group {
+        private final Map m_members;
+        private final Map m_requiredMembers;
+        
+        public MyGroupImpl(String name) {
+            super(name, GROUP);
+            m_members = new HashMap();
+            m_requiredMembers = new HashMap();
+        }
+
+        public boolean addMember(Role role) {
+            String name = role.getName();
+            if (m_requiredMembers.containsKey(name) || m_members.containsKey(name)) {
+                return false;
+            }
+            return m_members.put(name, role) == null;
+        }
+
+        public boolean addRequiredMember(Role role) {
+            String name = role.getName();
+            if (m_requiredMembers.containsKey(name) || m_members.containsKey(name)) {
+                return false;
+            }
+            return m_requiredMembers.put(name, role) == null;
+        }
+
+        public Role[] getMembers() {
+            Role[] result = new Role[m_members.size()];
+            return (Role[]) m_members.values().toArray(result);
+        }
+
+        public Role[] getRequiredMembers() {
+            Role[] result = new Role[m_requiredMembers.size()];
+            return (Role[]) m_requiredMembers.values().toArray(result);
+        }
+
+        public boolean removeMember(Role role) {
+            String name = role.getName();
+            if (m_requiredMembers.remove(name) != null) {
+                return true;
+            } else if (m_members.remove(name) != null) {
+                return true;
+            }
+            return false;
+        }
+    }
+    
+    static class MyRoleImpl implements Role {
+        private final String m_name;
+        private final int m_type;
+        private final Hashtable m_props;
+        
+        public MyRoleImpl(String name, int type) {
+            m_name = name;
+            m_type = type;
+            m_props = new Hashtable();
+        }
+
+        public String getName() {
+            return m_name;
+        }
+
+        public Dictionary getProperties() {
+            return m_props;
+        }
+
+        public int getType() {
+            return m_type;
+        }
+    }
+    
+    static class MyRoleRepositoryStore implements RoleRepositoryStore {
+        private final ConcurrentMap m_entries = new ConcurrentHashMap();
+
+        public Role addRole(String roleName, int type) throws IOException {
+            if (roleName == null) {
+                throw new IllegalArgumentException("Name cannot be null!");
+            }
+
+            Role role = createRole(roleName, type);
+
+            Object result = m_entries.putIfAbsent(roleName, role);
+            return (result == null) ? role : null;
+        }
+
+        public void close() throws IOException {
+            // Nop
+        }
+
+        public Role getRoleByName(String roleName) throws IOException {
+            if (roleName == null) {
+                throw new IllegalArgumentException("Role name cannot be null!");
+            }
+            return (Role) m_entries.get(roleName);
+        }
+
+        public Role[] getRoles(Filter filter) throws IOException {
+            Collection roles = m_entries.values();
+            
+            List matchingRoles = new ArrayList();
+            Iterator rolesIter = roles.iterator();
+            while (rolesIter.hasNext()) {
+                Role role = (Role) rolesIter.next();
+                if ((filter == null) || filter.match(role.getProperties())) {
+                    matchingRoles.add(role);
+                }
+            }
+
+            Role[] result = new Role[matchingRoles.size()];
+            return (Role[]) matchingRoles.toArray(result);
+        }
+        
+        public void initialize() throws IOException {
+            // Nop
+        }
+
+        public Role removeRole(String roleName) throws IOException {
+            if (roleName == null) {
+                throw new IllegalArgumentException("Name cannot be null!");
+            }
+            Role role = getRoleByName(roleName);
+            boolean result = m_entries.remove(roleName, role);
+            return result ? role : null;
+        }
+
+        private Role createRole(String roleName, int type) {
+            Role role;
+            if (type == Role.USER) {
+                role = new MyUserImpl(roleName);
+            } else if (type == Role.GROUP) {
+                role = new MyGroupImpl(roleName);
+            } else {
+                throw new IllegalArgumentException("Invalid role type!");
+            }
+            return role;
+        }
+    }
+    
+    static class MyUserImpl extends MyRoleImpl implements User {
+        private final Hashtable m_creds;
+        
+        public MyUserImpl(String name) {
+            this(name, USER);
+        }
+        
+        protected MyUserImpl(String name, int type) {
+            super(name, type);
+            m_creds = new Hashtable();
+        }
+
+        public Dictionary getCredentials() {
+            return m_creds;
+        }
+
+        public boolean hasCredential(String key, Object value) {
+            if (m_creds.containsKey(key)) {
+                Object v = m_creds.get(key);
+                return ((v == value) || ((v != null) && v.equals(value)));
+            }
+            return false;
+        }
+    }
+    
+    private CountDownLatch m_latch;
+    private RoleRepository m_repository;
+
+    /**
+     * Tests that adding a basic member to a group-role yields an event. 
+     */
+    public void testAddBasicGroupMemberYieldsEvent() throws Exception {
+        final Group role = (Group) m_repository.addRole("testGroup", Role.GROUP);
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                Role anyone = m_repository.getRoleByName(Role.USER_ANYONE);
+                assertTrue(role.addMember(anyone));
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that adding an existing role does not yield an event. 
+     */
+    public void testAddExistingRoleDoesNotYieldEvent() throws Exception {
+        assertNotNull(m_repository.addRole("testUser", Role.USER));
+
+        m_latch = new CountDownLatch(1);
+
+        new Thread(new Runnable() {
+            public void run() {
+                assertNull(m_repository.addRole("testUser", Role.USER));
+            };
+        }).start();
+
+        assertFalse(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that adding a new role yields an event. 
+     */
+    public void testAddNewRoleYieldsEvent() throws Exception {
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                assertNotNull(m_repository.addRole("testUser", Role.USER));
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that adding a required member to a group-role yields an event. 
+     */
+    public void testAddRequiredGroupMemberYieldsEvent() throws Exception {
+        final Group role = (Group) m_repository.addRole("testGroup", Role.GROUP);
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                Role anyone = m_repository.getRoleByName(Role.USER_ANYONE);
+                assertTrue(role.addRequiredMember(anyone));
+            };
+        }).start();
+
+        assertTrue(m_latch.await(100, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that changing the properties of a user-role yields an event. 
+     */
+    public void testAddRolePropertiesYieldsEvent() throws Exception {
+        final Role role = m_repository.addRole("testUser", Role.USER);
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                role.getProperties().put("key", "value");
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that changing the credentials of a user-role yields an event. 
+     */
+    public void testAddUserCredentialsYieldsEvent() throws Exception {
+        final User role = (User) m_repository.addRole("testUser", Role.USER);
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                role.getCredentials().put("key", "value");
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that changing the credentials of a user-role yields an event. 
+     */
+    public void testChangePropertiesYieldsEvent() throws Exception {
+        final Role role = m_repository.addRole("testUser", Role.USER);
+        role.getProperties().put("key", "value1");
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                role.getProperties().put("key", "value2");
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that changing the credentials of a user-role yields an event. 
+     */
+    public void testChangeUserCredentialsYieldsEvent() throws Exception {
+        final User role = (User) m_repository.addRole("testUser", Role.USER);
+        role.getCredentials().put("key", "value1");
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                role.getCredentials().put("key", "value2");
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that removing an existing role does yield an event. 
+     */
+    public void testRemoveExistingRoleYieldsEvent() throws Exception {
+        assertNotNull(m_repository.addRole("testUser", Role.USER));
+
+        m_latch = new CountDownLatch(1);
+
+        new Thread(new Runnable() {
+            public void run() {
+                assertTrue(m_repository.removeRole("testUser"));
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that removing a non-existing role does not yield an event. 
+     */
+    public void testRemoveNonExistingRoleDoesNotYieldEvent() throws Exception {
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                assertFalse(m_repository.removeRole("testUser"));
+            };
+        }).start();
+
+        assertFalse(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that changing the properties of a user-role yields an event. 
+     */
+    public void testRemoveRolePropertiesYieldsEvent() throws Exception {
+        final Role role = m_repository.addRole("testUser", Role.USER);
+        role.getProperties().put("key", "value");
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                role.getProperties().remove("key");
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that changing the credentials of a user-role yields an event. 
+     */
+    public void testRemoveUserCredentialsYieldsEvent() throws Exception {
+        final User role = (User) m_repository.addRole("testUser", Role.USER);
+        role.getCredentials().put("key", "value");
+        assertNotNull(role);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                role.getCredentials().remove("key");
+            };
+        }).start();
+
+        assertTrue(m_latch.await(1, TimeUnit.SECONDS));
+    }
+
+    /**
+     * Tests that removing a member from a group-role yields an event. 
+     */
+    public void testRemovingGroupMemberYieldsEvent() throws Exception {
+        final Role anyone = m_repository.getRoleByName(Role.USER_ANYONE);
+        final Group role = (Group) m_repository.addRole("testGroup", Role.GROUP);
+        assertNotNull(role);
+        role.addRequiredMember(anyone);
+        
+        m_latch = new CountDownLatch(1);
+        
+        new Thread(new Runnable() {
+            public void run() {
+                assertTrue(role.removeMember(anyone));
+            };
+        }).start();
+
+        assertTrue(m_latch.await(100, TimeUnit.SECONDS));
+    }
+    
+    /**
+     * Set up for all test cases.
+     */
+    protected void setUp() throws Exception
+    {
+        m_repository = new RoleRepository(new MyRoleRepositoryStore());
+        m_repository.addRoleChangeListener(new RoleChangeListener() {
+            public void propertyAdded(Role role, Object key, Object value) {
+                if (m_latch != null) {
+                    m_latch.countDown();
+                }
+            }
+
+            public void propertyChanged(Role role, Object key, Object oldValue, Object newValue) {
+                if (m_latch != null) {
+                    m_latch.countDown();
+                }
+            }
+            
+            public void propertyRemoved(Role role, Object key) {
+                if (m_latch != null) {
+                    m_latch.countDown();
+                }
+            }
+
+            public void roleAdded(Role role) {
+                if (m_latch != null) {
+                    m_latch.countDown();
+                }
+            }
+
+            public void roleRemoved(Role role) {
+                if (m_latch != null) {
+                    m_latch.countDown();
+                }
+            }
+        });
+    }
+}

Propchange: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/CustomRoleImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/EventDispatcherTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/EventDispatcherTest.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/EventDispatcherTest.java (original)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/EventDispatcherTest.java Thu Nov 22 14:57:51 2012
@@ -23,9 +23,9 @@ import java.util.concurrent.TimeUnit;
 import junit.framework.TestCase;
 
 
+import org.apache.felix.useradmin.RoleFactory;
 import org.apache.felix.useradmin.impl.EventDispatcher;
 import org.apache.felix.useradmin.impl.UserAdminListenerList;
-import org.apache.felix.useradmin.impl.role.UserImpl;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.event.Event;
@@ -99,6 +99,10 @@ public class EventDispatcherTest extends
                 m_props.put(properties[i], properties[i + 1]);
             }
         }
+        
+        public int compareTo(Object reference) {
+            return 0;
+        }
 
         public Bundle getBundle() {
             return null;
@@ -289,7 +293,7 @@ public class EventDispatcherTest extends
      * @return
      */
     private UserAdminEvent createMockEvent(int type) {
-        Role user = new UserImpl("user-" + System.currentTimeMillis());
+        Role user = RoleFactory.createUser("user-" + System.currentTimeMillis());
         ServiceReference ref = new FakeServiceReference();
         return new UserAdminEvent(ref, type, user);
     }

Modified: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/MemoryRoleRepositoryStore.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/MemoryRoleRepositoryStore.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/MemoryRoleRepositoryStore.java (original)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/MemoryRoleRepositoryStore.java Thu Nov 22 14:57:51 2012
@@ -17,11 +17,16 @@
 package org.apache.felix.useradmin.impl;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
+import org.apache.felix.useradmin.RoleFactory;
 import org.apache.felix.useradmin.RoleRepositoryStore;
+import org.osgi.framework.Filter;
 import org.osgi.service.useradmin.Role;
 
 
@@ -30,28 +35,35 @@ import org.osgi.service.useradmin.Role;
  */
 public class MemoryRoleRepositoryStore implements RoleRepositoryStore {
     
-    protected final ConcurrentMap m_entries = new ConcurrentHashMap();
+    private final ConcurrentMap m_entries = new ConcurrentHashMap();
 
-    public boolean addRole(Role role) throws IOException {
-        if (role == null) {
-            throw new IllegalArgumentException("Role cannot be null!");
+    public Role addRole(String roleName, int type) throws IOException {
+        if (roleName == null) {
+            throw new IllegalArgumentException("Name cannot be null!");
         }
-        Object result = m_entries.putIfAbsent(role.getName(), role);
-        return result == null;
+        Role role = RoleFactory.createRole(type, roleName);
+        Object result = m_entries.putIfAbsent(roleName, role);
+        return (result == null) ? role : null;
     }
 
     public void close() throws IOException {
         // Nop
     }
 
-    public void flush() throws IOException {
-        // Nop
-    }
-
-    public Role[] getAllRoles() throws IOException {
+    public Role[] getRoles(Filter filter) throws IOException {
         Collection roles = m_entries.values();
-        Role[] result = new Role[roles.size()];
-        return (Role[]) roles.toArray(result);
+        
+        List matchingRoles = new ArrayList();
+        Iterator rolesIter = roles.iterator();
+        while (rolesIter.hasNext()) {
+            Role role = (Role) rolesIter.next();
+            if ((filter == null) || filter.match(role.getProperties())) {
+                matchingRoles.add(role);
+            }
+        }
+
+        Role[] result = new Role[matchingRoles.size()];
+        return (Role[]) matchingRoles.toArray(result);
     }
 
     public Role getRoleByName(String roleName) throws IOException {
@@ -65,10 +77,12 @@ public class MemoryRoleRepositoryStore i
         // Nop
     }
 
-    public boolean removeRole(Role role) throws IOException {
-        if (role == null) {
-            throw new IllegalArgumentException("Role cannot be null!");
+    public Role removeRole(String roleName) throws IOException {
+        if (roleName == null) {
+            throw new IllegalArgumentException("Name cannot be null!");
         }
-        return m_entries.remove(role.getName(), role);
+        Role role = getRoleByName(roleName);
+        boolean result = m_entries.remove(roleName, role);
+        return result ? role : null;
     }
 }

Modified: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleCheckerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleCheckerTest.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleCheckerTest.java (original)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleCheckerTest.java Thu Nov 22 14:57:51 2012
@@ -16,13 +16,13 @@
  */
 package org.apache.felix.useradmin.impl;
 
-import org.apache.felix.useradmin.impl.RoleChecker;
-import org.apache.felix.useradmin.impl.RoleRepository;
-import org.apache.felix.useradmin.impl.role.GroupImpl;
-import org.apache.felix.useradmin.impl.role.UserImpl;
-
 import junit.framework.TestCase;
 
+import org.apache.felix.useradmin.RoleFactory;
+import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+
 
 /**
  * Test cases for {@link RoleChecker}.
@@ -30,12 +30,13 @@ import junit.framework.TestCase;
 public class RoleCheckerTest extends TestCase {
     
     private RoleChecker m_roleChecker;
+    private Role m_anyone;
 
     /**
      * Tests that a user always implies itself.
      */
     public void testUserAlwaysImpliesItself() {
-        UserImpl user = new UserImpl("foo");
+        User user = RoleFactory.createUser("foo");
         
         assertTrue(m_roleChecker.isImpliedBy(user, user));
     }
@@ -44,8 +45,8 @@ public class RoleCheckerTest extends Tes
      * Test that a user does never imply a group to which it is not a member.
      */
     public void testUserDoesNotImplyNotImpliedGroup() {
-        UserImpl user = new UserImpl("foo");
-        GroupImpl group = new GroupImpl("bar");
+        User user = RoleFactory.createUser("foo");
+        Group group = RoleFactory.createGroup("bar");
         
         assertFalse(m_roleChecker.isImpliedBy(user, group));
     }
@@ -54,10 +55,10 @@ public class RoleCheckerTest extends Tes
      * Test that a user does never imply a group to which it is not a member.
      */
     public void testUserImpliesImpliedGroup() {
-        UserImpl user = new UserImpl("foo");
+        User user = RoleFactory.createUser("foo");
         
-        GroupImpl group = new GroupImpl("bar");
-        group.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group group = RoleFactory.createGroup("bar");
+        group.addRequiredMember(m_anyone);
         group.addMember(user);
 
         assertTrue(m_roleChecker.isImpliedBy(group, user));
@@ -67,9 +68,9 @@ public class RoleCheckerTest extends Tes
      * Test that a user does never imply a group to which it is not a member.
      */
     public void testGroupDoesNotImplyNotImpliedUser() {
-        UserImpl user = new UserImpl("foo");
+        User user = RoleFactory.createUser("foo");
         
-        GroupImpl group = new GroupImpl("bar");
+        Group group = RoleFactory.createGroup("bar");
         group.addMember(user);
         
         assertFalse(m_roleChecker.isImpliedBy(user, group));
@@ -79,9 +80,9 @@ public class RoleCheckerTest extends Tes
      * Test that a group does never imply a group to which it is a required member.
      */
     public void testGroupDoesNotImplySameRequiredGroup() {
-        UserImpl user = new UserImpl("foo");
+        User user = RoleFactory.createUser("foo");
         
-        GroupImpl group = new GroupImpl("bar");
+        Group group = RoleFactory.createGroup("bar");
         group.addRequiredMember(group);
         group.addMember(user);
         
@@ -92,9 +93,9 @@ public class RoleCheckerTest extends Tes
      * Test that a group does never imply a group to which it is a basic member.
      */
     public void testGroupDoesNotImplySameGroup() {
-        UserImpl user = new UserImpl("foo");
+        User user = RoleFactory.createUser("foo");
         
-        GroupImpl group = new GroupImpl("bar");
+        Group group = RoleFactory.createGroup("bar");
         group.addMember(group);
         group.addMember(user);
         
@@ -105,25 +106,25 @@ public class RoleCheckerTest extends Tes
      * Test that a membership can be implied for users belonging to multiple required groups.
      */
     public void testRequiredRolesMultipleRequiredGroupsOk() {
-        UserImpl elmer = new UserImpl("elmer");
-        UserImpl pepe = new UserImpl("pepe");
-        UserImpl bugs = new UserImpl("bugs");
-        UserImpl daffy = new UserImpl("daffy");
+        User elmer = RoleFactory.createUser("elmer");
+        User pepe = RoleFactory.createUser("pepe");
+        User bugs = RoleFactory.createUser("bugs");
+        User daffy = RoleFactory.createUser("daffy");
         
-        GroupImpl administrators = new GroupImpl("administrators");
-        administrators.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group administrators = RoleFactory.createGroup("administrators");
+        administrators.addRequiredMember(m_anyone);
         administrators.addMember(elmer);
         administrators.addMember(pepe);
         administrators.addMember(bugs);
 
-        GroupImpl family = new GroupImpl("family");
-        family.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group family = RoleFactory.createGroup("family");
+        family.addRequiredMember(m_anyone);
         family.addMember(elmer);
         family.addMember(pepe);
         family.addMember(daffy);
 
-        GroupImpl alarmSystemActivation = new GroupImpl("alarmSystemActivation");
-        alarmSystemActivation.addMember(RoleRepository.USER_ANYONE);
+        Group alarmSystemActivation = RoleFactory.createGroup("alarmSystemActivation");
+        alarmSystemActivation.addMember(m_anyone);
         alarmSystemActivation.addRequiredMember(administrators);
         alarmSystemActivation.addRequiredMember(family);
 
@@ -137,25 +138,25 @@ public class RoleCheckerTest extends Tes
      * Test that a membership can be implied for users belonging to multiple non-required groups.
      */
     public void testRequiredRolesMultipleGroupsOk() {
-        UserImpl elmer = new UserImpl("elmer");
-        UserImpl pepe = new UserImpl("pepe");
-        UserImpl bugs = new UserImpl("bugs");
-        UserImpl daffy = new UserImpl("daffy");
+        User elmer = RoleFactory.createUser("elmer");
+        User pepe = RoleFactory.createUser("pepe");
+        User bugs = RoleFactory.createUser("bugs");
+        User daffy = RoleFactory.createUser("daffy");
         
-        GroupImpl administrators = new GroupImpl("administrators");
-        administrators.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group administrators = RoleFactory.createGroup("administrators");
+        administrators.addRequiredMember(m_anyone);
         administrators.addMember(elmer);
         administrators.addMember(pepe);
         administrators.addMember(bugs);
 
-        GroupImpl family = new GroupImpl("family");
-        family.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group family = RoleFactory.createGroup("family");
+        family.addRequiredMember(m_anyone);
         family.addMember(elmer);
         family.addMember(pepe);
         family.addMember(daffy);
 
-        GroupImpl alarmSystemActivation = new GroupImpl("alarmSystemActivation");
-        alarmSystemActivation.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group alarmSystemActivation = RoleFactory.createGroup("alarmSystemActivation");
+        alarmSystemActivation.addRequiredMember(m_anyone);
         alarmSystemActivation.addMember(administrators);
         alarmSystemActivation.addMember(family);
 
@@ -169,33 +170,33 @@ public class RoleCheckerTest extends Tes
      * Test that a membership can be implied for users belonging to multiple non-required groups.
      */
     public void testVotersRequiredMembersOk() {
-        GroupImpl citizens = new GroupImpl("citizen");
-        citizens.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group citizens = RoleFactory.createGroup("citizen");
+        citizens.addRequiredMember(m_anyone);
         
-        GroupImpl adults = new GroupImpl("adult");
-        adults.addRequiredMember(RoleRepository.USER_ANYONE);
+        Group adults = RoleFactory.createGroup("adult");
+        adults.addRequiredMember(m_anyone);
         
-        GroupImpl voters = new GroupImpl("voter");
+        Group voters = RoleFactory.createGroup("voter");
         voters.addRequiredMember(citizens);
         voters.addRequiredMember(adults);
-        voters.addMember(RoleRepository.USER_ANYONE);
+        voters.addMember(m_anyone);
         
         
         // Elmer belongs to the citizens and adults...
-        UserImpl elmer = new UserImpl("elmer");
+        User elmer = RoleFactory.createUser("elmer");
         citizens.addMember(elmer);
         adults.addMember(elmer);
         
         // Pepe belongs to the citizens, but is not an adult...
-        UserImpl pepe = new UserImpl("pepe");
+        User pepe = RoleFactory.createUser("pepe");
         citizens.addMember(pepe);
         
         // Bugs is an adult, but is not a citizen...
-        UserImpl bugs = new UserImpl("bugs");
+        User bugs = RoleFactory.createUser("bugs");
         adults.addMember(bugs);
         
         // Daffy is not an adult, neither a citizen...
-        UserImpl daffy = new UserImpl("daffy");
+        User daffy = RoleFactory.createUser("daffy");
 
         assertTrue(m_roleChecker.isImpliedBy(voters, elmer));
         assertFalse(m_roleChecker.isImpliedBy(voters, pepe));
@@ -209,6 +210,8 @@ public class RoleCheckerTest extends Tes
     protected void setUp() throws Exception {
         super.setUp();
         
+        m_anyone = RoleFactory.createRole(Role.USER_ANYONE);
+
         m_roleChecker = new RoleChecker();
     }
 }

Modified: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositorySecurityTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositorySecurityTest.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositorySecurityTest.java (original)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositorySecurityTest.java Thu Nov 22 14:57:51 2012
@@ -29,6 +29,8 @@ import org.osgi.service.useradmin.Role;
  */
 public class RoleRepositorySecurityTest extends TestCase {
 
+    private static final String TEST_USER_NAME = "testUser";
+    
     private RoleRepository m_roleManager;
     private TestSecurityManager m_securityManager;
 
@@ -37,8 +39,8 @@ public class RoleRepositorySecurityTest 
      */
     public void testAddRoleWithPermissionsOk() throws SecurityException {
         m_securityManager.m_allowed = true;
-       
-        m_roleManager.addRole(RoleRepository.USER_ANYONE);
+
+        m_roleManager.addRole(TEST_USER_NAME, Role.USER);
     }
 
     /**
@@ -46,7 +48,7 @@ public class RoleRepositorySecurityTest 
      */
     public void testAddRoleWithoutPermissionsFails() throws SecurityException {
         try {
-            m_roleManager.addRole(RoleRepository.USER_ANYONE);
+            m_roleManager.addRole(TEST_USER_NAME, Role.USER);
             
             fail("Expected SecurityException!");
         } catch (SecurityException e) {
@@ -81,7 +83,7 @@ public class RoleRepositorySecurityTest 
     public void testRemoveRoleWithPermissionsOk() throws SecurityException {
         m_securityManager.m_allowed = true;
         
-        m_roleManager.removeRole(RoleRepository.USER_ANYONE);
+        m_roleManager.removeRole(TEST_USER_NAME);
     }
 
     /**
@@ -89,7 +91,7 @@ public class RoleRepositorySecurityTest 
      */
     public void testRemoveRoleWithoutPermissionsFails() throws SecurityException {
         try {
-            m_roleManager.removeRole(RoleRepository.USER_ANYONE);
+            m_roleManager.removeRole(TEST_USER_NAME);
             
             fail("Expected SecurityException!");
         } catch (SecurityException e) {

Modified: felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositoryTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositoryTest.java?rev=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositoryTest.java (original)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositoryTest.java Thu Nov 22 14:57:51 2012
@@ -20,39 +20,38 @@ package org.apache.felix.useradmin.impl;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Dictionary;
 import java.util.List;
-import java.util.Properties;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
 import junit.framework.TestCase;
 
 import org.apache.felix.framework.FilterImpl;
-import org.apache.felix.useradmin.impl.role.GroupImpl;
-import org.apache.felix.useradmin.impl.role.UserImpl;
 import org.osgi.framework.Filter;
+import org.osgi.service.useradmin.Group;
 import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
 
 /**
  * Test cases for {@link RoleRepository}.
  */
 public class RoleRepositoryTest extends TestCase {
 
-    private RoleRepository m_roleManager;
+    private RoleRepository m_roleRepository;
     private CountDownLatch m_latch;
     
     /**
      * Tests whether adding a new role to a group causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testAddBasicRoleYieldsEventOk() throws Exception {
-        final GroupImpl role = (GroupImpl) m_roleManager.addRole(new GroupImpl("foo"));
+        final Group role = (Group) m_roleRepository.addRole("foo", Role.GROUP);
         
         m_latch = new CountDownLatch(1);
         
         new Thread(new Runnable() {
             public void run() {
-                role.addMember(RoleRepository.USER_ANYONE);
+                Role anyone = m_roleRepository.getRoleByName(Role.USER_ANYONE);
+                role.addMember(anyone);
             };
         }).start();
 
@@ -64,7 +63,7 @@ public class RoleRepositoryTest extends 
      */
     public void testAddNullRoleChangeListenerFail() throws Exception {
         try {
-            m_roleManager.addRoleChangeListener(null);
+            m_roleRepository.addRoleChangeListener(null);
             fail("Expected IllegalArgumentException!");
         } catch (IllegalArgumentException e) {
             // Ok; expected...
@@ -75,22 +74,28 @@ public class RoleRepositoryTest extends 
      * Tests that adding a predefined role is not allowed.
      */
     public void testAddPredefineRoleFails() {
-        Role role = RoleRepository.USER_ANYONE;
-        assertNull(m_roleManager.addRole(role));
-        assertEquals(0, m_roleManager.getRoles(null).size());
+        try {
+            assertNull(m_roleRepository.addRole(Role.USER_ANYONE, Role.ROLE));
+            fail("IllegalArgumentException expected!");
+        }
+        catch (IllegalArgumentException e)
+        {
+            // Ok; expected
+        }
     }
 
     /**
      * Tests whether adding a new role to a group causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testAddRequiredRoleYieldsEventOk() throws Exception {
-        final GroupImpl role = (GroupImpl) m_roleManager.addRole(new GroupImpl("foo"));
+        final Group role = (Group) m_roleRepository.addRole("foo", Role.GROUP);
 
         m_latch = new CountDownLatch(1);
         
         new Thread(new Runnable() {
             public void run() {
-                role.addRequiredMember(RoleRepository.USER_ANYONE);
+                Role anyone = m_roleRepository.getRoleByName(Role.USER_ANYONE);
+                role.addRequiredMember(anyone);
             };
         }).start();
 
@@ -102,7 +107,7 @@ public class RoleRepositoryTest extends 
      */
     public void testAddRoleChangeListenerOk() throws Exception {
         // Should succeed...
-        m_roleManager.addRoleChangeListener(new RoleChangeListener() {
+        m_roleRepository.addRoleChangeListener(new RoleChangeListener() {
             public void propertyAdded(Role role, Object key, Object value) {
             }
             
@@ -124,22 +129,19 @@ public class RoleRepositoryTest extends 
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#addRole(Role)}.
      */
     public void testAddRoleOfSameTypeTwiceFail() {
-        UserImpl role = new UserImpl("foo");
+        assertNotNull(m_roleRepository.addRole("foo", Role.USER));
+        assertEquals(1, m_roleRepository.getRoles(null).size());
 
-        assertSame(role, m_roleManager.addRole(role));
-        assertEquals(1, m_roleManager.getRoles(null).size());
-
-        assertNull(m_roleManager.addRole(role));
-        assertEquals(1, m_roleManager.getRoles(null).size());
+        assertNull(m_roleRepository.addRole("foo", Role.USER));
+        assertEquals(1, m_roleRepository.getRoles(null).size());
     }
 
     /**
      * Tests that adding a role works.
      */
     public void testAddRoleOk() {
-        UserImpl role = new UserImpl("foo");
-        assertSame(role, m_roleManager.addRole(role));
-        assertEquals(1, m_roleManager.getRoles(null).size());
+        assertNotNull(m_roleRepository.addRole("foo", Role.USER));
+        assertEquals(1, m_roleRepository.getRoles(null).size());
     }
 
     /**
@@ -148,7 +150,7 @@ public class RoleRepositoryTest extends 
     public void testAddRolePropertyYieldsEventOk() throws Exception {
         m_latch = new CountDownLatch(1);
         
-        final Role role = m_roleManager.addRole(new UserImpl("john.doe"));
+        final Role role = m_roleRepository.addRole("john.doe", Role.USER);
         
         new Thread(new Runnable() {
             public void run() {
@@ -160,21 +162,11 @@ public class RoleRepositoryTest extends 
     }
 
     /**
-     * Tests that adding a role that does not inherit from RoleImpl does not work and yields an exception.
+     * Tests that adding a null-role does not work and yields an exception.
      */
-    public void testAddRoleWithInvalidRoleFail() {
+    public void testAddRoleWithNullRoleFail() {
         try {
-            m_roleManager.addRole(new Role() {
-                public String getName() {
-                    return "A User";
-                }
-                public Dictionary getProperties() {
-                    return new Properties();
-                }
-                public int getType() {
-                    return Role.USER;
-                }
-            });
+            m_roleRepository.addRole(null, Role.USER);
             
             fail("Exception expected!");
         } catch (IllegalArgumentException e) {
@@ -183,11 +175,11 @@ public class RoleRepositoryTest extends 
     }
 
     /**
-     * Tests that adding a null-role does not work and yields an exception.
+     * Tests that adding a role with an invalid type does not work and yields an exception.
      */
-    public void testAddRoleWithNullRoleFail() {
+    public void testAddRoleWithInvalidRoleTypeFail() {
         try {
-            m_roleManager.addRole(null);
+            m_roleRepository.addRole("role", Role.ROLE);
             
             fail("Exception expected!");
         } catch (IllegalArgumentException e) {
@@ -199,14 +191,11 @@ public class RoleRepositoryTest extends 
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#addRole(Role)}.
      */
     public void testAddRoleWithSameNameTwiceFail() {
-        UserImpl role1 = new UserImpl("foo");
-        GroupImpl role2 = new GroupImpl("foo");
+        assertNotNull(m_roleRepository.addRole("foo", Role.USER));
+        assertEquals(1, m_roleRepository.getRoles(null).size());
 
-        assertSame(role1, m_roleManager.addRole(role1));
-        assertEquals(1, m_roleManager.getRoles(null).size());
-
-        assertNull(m_roleManager.addRole(role2));
-        assertEquals(1, m_roleManager.getRoles(null).size());
+        assertNull(m_roleRepository.addRole("foo", Role.GROUP));
+        assertEquals(1, m_roleRepository.getRoles(null).size());
     }
 
     /**
@@ -215,7 +204,7 @@ public class RoleRepositoryTest extends 
     public void testAddUserCredentialYieldsEventOk() throws Exception {
         m_latch = new CountDownLatch(1);
         
-        final UserImpl role = (UserImpl) m_roleManager.addRole(new UserImpl("john.doe"));
+        final User role = (User) m_roleRepository.addRole("john.doe", Role.USER);
         
         new Thread(new Runnable() {
             public void run() {
@@ -230,7 +219,7 @@ public class RoleRepositoryTest extends 
      * Tests whether changing an existing property to a role causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testChangeRolePropertyYieldsEventOk() throws Exception {
-        final Role role = m_roleManager.addRole(new UserImpl("john.doe"));
+        final Role role = m_roleRepository.addRole("john.doe", Role.USER);
         role.getProperties().put("key", "value");
         
         m_latch = new CountDownLatch(1);
@@ -248,7 +237,7 @@ public class RoleRepositoryTest extends 
      * Tests whether changing an existing credential for a user causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testChangeUserCredentialYieldsEventOk() throws Exception {
-        final UserImpl role = (UserImpl) m_roleManager.addRole(new UserImpl("john.doe"));
+        final User role = (User) m_roleRepository.addRole("john.doe", Role.USER);
         role.getCredentials().put("key", "value");
         
         m_latch = new CountDownLatch(1);
@@ -266,85 +255,88 @@ public class RoleRepositoryTest extends 
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#getRoleByName(java.lang.String)}.
      */
     public void testGetRoleByName() {
-        Role role1 = m_roleManager.addRole(new UserImpl("foo"));
-        Role role2 = m_roleManager.addRole(new GroupImpl("bar"));
+        Role role1 = m_roleRepository.addRole("foo", Role.USER);
+        Role role2 = m_roleRepository.addRole("bar", Role.GROUP);
 
-        assertSame(role1, m_roleManager.getRoleByName("foo"));
-        assertSame(role2, m_roleManager.getRoleByName("bar"));
-        assertNull(m_roleManager.getRoleByName("qux"));
+        assertEquals(role1, m_roleRepository.getRoleByName("foo"));
+        assertEquals(role2, m_roleRepository.getRoleByName("bar"));
+        assertNull(m_roleRepository.getRoleByName("qux"));
     }
 
     /**
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#getRoles(org.osgi.framework.Filter)}.
      */
     public void testGetRolesWithFilterOk() throws Exception {
-        Role role1 = m_roleManager.addRole(new UserImpl("foo"));
+        Role role1 = m_roleRepository.addRole("foo", Role.USER);
         role1.getProperties().put("key", "value1");
         role1.getProperties().put("keyA", "valueA");
-        Role role2 = m_roleManager.addRole(new GroupImpl("bar"));
+        Role role2 = m_roleRepository.addRole("bar", Role.GROUP);
         role2.getProperties().put("key", "value2");
         role2.getProperties().put("keyB", "value1");
         
         Filter filter;
 
         filter = new FilterImpl("(key=value1)");
-        assertSameRoles(new Role[]{ role1 }, m_roleManager.getRoles(filter));
+        assertSameRoles(new Role[]{ role1 }, m_roleRepository.getRoles(filter));
 
         filter = new FilterImpl("(key=value2)");
-        assertSameRoles(new Role[]{ role2 }, m_roleManager.getRoles(filter));
+        assertSameRoles(new Role[]{ role2 }, m_roleRepository.getRoles(filter));
 
         filter = new FilterImpl("(key=value*)");
-        assertSameRoles(new Role[]{ role1, role2 }, m_roleManager.getRoles(filter));
+        assertSameRoles(new Role[]{ role1, role2 }, m_roleRepository.getRoles(filter));
 
         filter = new FilterImpl("(|(key=value1)(keyB=value1))");
-        assertSameRoles(new Role[]{ role1, role2 }, m_roleManager.getRoles(filter));
+        assertSameRoles(new Role[]{ role1, role2 }, m_roleRepository.getRoles(filter));
     }
 
     /**
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#getRoles(String, String)}.
      */
     public void testGetRolesWithKeyValuePairOk() throws Exception {
-        Role role1 = m_roleManager.addRole(new UserImpl("foo"));
+        Role role1 = m_roleRepository.addRole("foo", Role.USER);
         role1.getProperties().put("key", "value1");
         role1.getProperties().put("keyA", "valueA");
-        Role role2 = m_roleManager.addRole(new GroupImpl("bar"));
+        Role role2 = m_roleRepository.addRole("bar", Role.GROUP);
         role2.getProperties().put("key", "value2");
         role2.getProperties().put("keyB", "value1");
-        
-        assertSameRoles(new Role[]{ role1 }, m_roleManager.getRoles("key", "value1"));
-        assertSameRoles(new Role[]{ role2 }, m_roleManager.getRoles("key", "value2"));
-        assertSameRoles(new Role[0], m_roleManager.getRoles("key", "value"));
+
+        assertSameRoles(new Role[]{ role1 }, m_roleRepository.getRoles("key", "value1"));
+        assertSameRoles(new Role[]{ role2 }, m_roleRepository.getRoles("key", "value2"));
+        assertSameRoles(new Role[0], m_roleRepository.getRoles("key", "value"));
     }
 
     /**
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#getRoles(org.osgi.framework.Filter)}.
      */
     public void testGetRolesWithoutFilterOk() {
-        Role role1 = m_roleManager.addRole(new UserImpl("foo"));
-        Role role2 = m_roleManager.addRole(new GroupImpl("bar"));
+        Role role1 = m_roleRepository.addRole("foo", Role.USER);
+        Role role2 = m_roleRepository.addRole("bar", Role.GROUP);
         
-        assertSameRoles(new Role[]{ role2, role1 }, m_roleManager.getRoles(null));
+        assertSameRoles(new Role[]{ role2, role1 }, m_roleRepository.getRoles(null));
     }
 
     /**
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#getRoleByName(java.lang.String)}.
      */
     public void testGetUserAnyoneRoleByName() {
-        assertSame(RoleRepository.USER_ANYONE, m_roleManager.getRoleByName(Role.USER_ANYONE));
+        Role anyone = m_roleRepository.getRoleByName(Role.USER_ANYONE);
+        assertEquals(Role.USER_ANYONE, anyone.getName());
+        assertEquals(Role.ROLE, anyone.getType());
     }
 
     /**
      * Tests whether removing a role from a group causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testRemoveBasicRoleYieldsEventOk() throws Exception {
-        final GroupImpl role = (GroupImpl) m_roleManager.addRole(new GroupImpl("foo"));
-        role.addMember(RoleRepository.USER_ANYONE);
+        final Role anyone = m_roleRepository.getRoleByName(Role.USER_ANYONE);
+        final Group role = (Group) m_roleRepository.addRole("bar", Role.GROUP);
+        role.addMember(anyone);
         
         m_latch = new CountDownLatch(1);
         
         new Thread(new Runnable() {
             public void run() {
-                role.removeMember(RoleRepository.USER_ANYONE);
+                role.removeMember(anyone);
             };
         }).start();
 
@@ -355,23 +347,20 @@ public class RoleRepositoryTest extends 
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#removeRole(Role)}.
      */
     public void testRemoveExistingRoleOk() {
-        UserImpl role = new UserImpl("foo");
-        assertSame(role, m_roleManager.addRole(role));
+        assertNotNull(m_roleRepository.addRole("foo", Role.USER));
         
-        assertTrue(m_roleManager.removeRole(role));
-        assertEquals(0, m_roleManager.getRoles(null).size());
+        assertTrue(m_roleRepository.removeRole("foo"));
+        assertEquals(0, m_roleRepository.getRoles(null).size());
     }
 
     /**
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#removeRole(Role)}.
      */
     public void testRemoveNonExistingRoleOk() {
-        UserImpl role1 = new UserImpl("foo");
-        UserImpl role2 = new UserImpl("bar");
-        assertSame(role1, m_roleManager.addRole(role1));
+        assertNotNull(m_roleRepository.addRole("foo", Role.USER));
 
-        assertFalse(m_roleManager.removeRole(role2));
-        assertEquals(1, m_roleManager.getRoles(null).size());
+        assertFalse(m_roleRepository.removeRole("qux"));
+        assertEquals(1, m_roleRepository.getRoles(null).size());
     }
 
     /**
@@ -379,7 +368,7 @@ public class RoleRepositoryTest extends 
      */
     public void testRemoveNullRoleChangeListenerFail() throws Exception {
         try {
-            m_roleManager.removeRoleChangeListener(null);
+            m_roleRepository.removeRoleChangeListener(null);
             fail("Expected IllegalArgumentException!");
         } catch (IllegalArgumentException e) {
             // Ok; expected...
@@ -390,26 +379,26 @@ public class RoleRepositoryTest extends 
      * Test method for {@link org.apache.felix.useradmin.impl.RoleRepository#removeRole(Role)}.
      */
     public void testRemovePredefinedRoleFails() {
-        m_roleManager.addRole(new UserImpl("foo"));
+        m_roleRepository.addRole("foo", Role.USER);
 
-        Role role = RoleRepository.USER_ANYONE;
-        assertFalse(m_roleManager.removeRole(role));
+        assertFalse(m_roleRepository.removeRole(Role.USER_ANYONE));
 
-        assertEquals(1, m_roleManager.getRoles(null).size());
+        assertEquals(1, m_roleRepository.getRoles(null).size());
     }
 
     /**
      * Tests whether removing a role from a group causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testRemoveRequiredRoleYieldsEventOk() throws Exception {
-        final GroupImpl role = (GroupImpl) m_roleManager.addRole(new GroupImpl("foo"));
-        role.addRequiredMember(RoleRepository.USER_ANYONE);
+        final Role anyone = m_roleRepository.getRoleByName(Role.USER_ANYONE);
+        final Group role = (Group) m_roleRepository.addRole("bar", Role.GROUP);
+        role.addRequiredMember(anyone);
         
         m_latch = new CountDownLatch(1);
         
         new Thread(new Runnable() {
             public void run() {
-                role.removeMember(RoleRepository.USER_ANYONE);
+                role.removeMember(anyone);
             };
         }).start();
 
@@ -421,7 +410,7 @@ public class RoleRepositoryTest extends 
      */
     public void testRemoveRoleChangeListenerOk() throws Exception {
         // Should succeed...
-        m_roleManager.removeRoleChangeListener(new RoleChangeListener() {
+        m_roleRepository.removeRoleChangeListener(new RoleChangeListener() {
             public void propertyAdded(Role role, Object key, Object value) {
             }
             
@@ -443,7 +432,7 @@ public class RoleRepositoryTest extends 
      * Tests whether removing a property from a role causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testRemoveRolePropertyYieldsEventOk() throws Exception {
-        final Role role = m_roleManager.addRole(new UserImpl("john.doe"));
+        final Role role = m_roleRepository.addRole("john.doe", Role.USER);
         role.getProperties().put("key", "value");
         
         m_latch = new CountDownLatch(1);
@@ -458,34 +447,11 @@ public class RoleRepositoryTest extends 
     }
 
     /**
-     * Tests that adding a role that does not inherit from RoleImpl does not work and yields an exception.
-     */
-    public void testRemoveRoleWithInvalidRoleFail() {
-        try {
-            m_roleManager.removeRole(new Role() {
-                public String getName() {
-                    return "A User";
-                }
-                public Dictionary getProperties() {
-                    return new Properties();
-                }
-                public int getType() {
-                    return Role.USER;
-                }
-            });
-            
-            fail("Exception expected!");
-        } catch (IllegalArgumentException e) {
-            // Ok; expected
-        }
-    }
-
-    /**
      * Tests that removing a null-role does not work and yields an exception.
      */
     public void testRemoveRoleWithNullRoleFail() {
         try {
-            m_roleManager.removeRole(null);
+            m_roleRepository.removeRole(null);
             
             fail("Exception expected!");
         } catch (IllegalArgumentException e) {
@@ -497,7 +463,7 @@ public class RoleRepositoryTest extends 
      * Tests whether removing a credential from a user causes an event to be emitted to the {@link RoleRepository}.
      */
     public void testRemoveUserCredentialYieldsEventOk() throws Exception {
-        final UserImpl role = (UserImpl) m_roleManager.addRole(new UserImpl("john.doe"));
+        final User role = (User) m_roleRepository.addRole("john.doe", Role.USER);
         role.getCredentials().put("key", "value");
         
         m_latch = new CountDownLatch(1);
@@ -517,8 +483,8 @@ public class RoleRepositoryTest extends 
     protected void setUp() throws Exception {
         super.setUp();
 
-        m_roleManager = new RoleRepository(new MemoryRoleRepositoryStore());
-        m_roleManager.addRoleChangeListener(new RoleChangeListener() {
+        m_roleRepository = new RoleRepository(new MemoryRoleRepositoryStore());
+        m_roleRepository.addRoleChangeListener(new RoleChangeListener() {
             public void propertyAdded(Role role, Object key, Object value) {
                 if (m_latch != null) {
                     m_latch.countDown();
@@ -549,8 +515,6 @@ public class RoleRepositoryTest extends 
                 }
             }
         });
-        
-        m_roleManager.start();
     }
 
     /**

Modified: 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=1412574&r1=1412573&r2=1412574&view=diff
==============================================================================
--- felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java (original)
+++ felix/trunk/useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java Thu Nov 22 14:57:51 2012
@@ -22,7 +22,7 @@ import java.util.List;
 import junit.framework.TestCase;
 
 import org.apache.felix.framework.FilterImpl;
-import org.apache.felix.useradmin.impl.role.UserImpl;
+import org.apache.felix.useradmin.RoleFactory;
 import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.service.event.Event;
@@ -347,7 +347,8 @@ public class UserAdminImplTest extends T
      * Tests that obtaining the authorization for a non-existing user yields null.
      */
     public void testGetAuthorizationForNonExistingUserOk() {
-        Authorization auth = m_userAdmin.getAuthorization(new UserImpl("foo"));
+        User nonExistingUser = RoleFactory.createUser("non-existing-user");
+        Authorization auth = m_userAdmin.getAuthorization(nonExistingUser);
 
         assertNotNull(auth);
 
@@ -865,8 +866,6 @@ public class UserAdminImplTest extends T
                 return new FilterImpl(filter);
             }
         };
-
-        m_roleRepository.start();
         m_dispatcher.start();
     }
     
@@ -874,7 +873,6 @@ public class UserAdminImplTest extends T
      * {@inheritDoc}
      */
     protected void tearDown() throws Exception {
-        m_roleRepository.stop();
         m_dispatcher.stop();
         
         super.tearDown();



Mime
View raw message