Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 26548 invoked from network); 12 Dec 2006 15:26:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 12 Dec 2006 15:26:00 -0000 Received: (qmail 47062 invoked by uid 500); 12 Dec 2006 15:26:07 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 47025 invoked by uid 500); 12 Dec 2006 15:26:07 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 47014 invoked by uid 99); 12 Dec 2006 15:26:07 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Dec 2006 07:26:07 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Dec 2006 07:25:49 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id A52E71A9831; Tue, 12 Dec 2006 07:24:39 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r486187 [11/49] - in /directory/trunks/triplesec: ./ admin-api/ admin-api/src/ admin-api/src/main/ admin-api/src/main/java/ admin-api/src/main/java/org/ admin-api/src/main/java/org/safehaus/ admin-api/src/main/java/org/safehaus/triplesec/ a... Date: Tue, 12 Dec 2006 15:24:14 -0000 To: commits@directory.apache.org From: tbennett@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061212152439.A52E71A9831@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/Roles.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/Roles.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/Roles.java (added) +++ directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/Roles.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,382 @@ +/* + * 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.safehaus.triplesec.guardian; + + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + + +/** + * Represnets an immutable set of {@link Role}s. + * + * @author Trustin Lee + * @version $Rev: 52 $, $Date: 2005-08-19 23:03:36 -0400 (Fri, 19 Aug 2005) $ + */ +public class Roles implements Cloneable, Serializable +{ + private static final long serialVersionUID = 654756629481872197L; + /** An empty array of Role objects */ + private static final Role[] EMPTY_ROLE_ARRAY = new Role[0]; + + /** the name of the application this roles belong to */ + private final String applicationName; + /** Map<String roleName, Role role;> */ + private final Map roles = new HashMap(); + + + /** + * Creates a new instance. + * + * @param applicationName the name of the application this roles belong to + * @param roles the array of {@link Role}s that will belong to this role set + */ + public Roles( String applicationName, Role[] roles ) + { + // Check nulls and emptiness + if( applicationName == null ) + { + throw new NullPointerException( "applicationName" ); + } + if( applicationName.length() == 0 ) + { + throw new IllegalArgumentException( "applicationName is empty." ); + } + if( roles == null ) + { + roles = EMPTY_ROLE_ARRAY; + } + + this.applicationName = applicationName; + + // Add all roles while checking if application names are all + // same with what user specified. + for( int i = roles.length - 1; i >= 0; i -- ) + { + Role r = roles[ i ]; + if( r == null ) + { + continue; + } + + if( !applicationName.equals( r.getApplicationName() ) ) + { + throw new IllegalArgumentException( "Invalid applicationName: " + r.getApplicationName() ); + } + + this.roles.put( r.getName(), r ); + } + } + + + /** + * Returns the name of the application this roles belong to + * + * @return the name of the application this roles belong to + */ + public String getApplicationName() + { + return applicationName; + } + + + /** + * Returns true if and only if this set contains the specified + * role. + * + * @param role the role to find + * @return true if and only if this set contains the specified + * role + */ + public boolean contains( Role role ) + { + return applicationName.equals( role.getApplicationName() ) && + roles.containsKey( role.getName() ); + } + + + /** + * Returns true if and only if this set contains the {@link Role} + * with the specified roleName. + * + * @param roleName the name of the role to find + * @return true if and only if this set contains the specified + * roleName + */ + public boolean contains( String roleName ) + { + return roles.containsKey( roleName ); + } + + + /** + * Returns true if and only if this set contains all elements of + * the specified roles. + * + * @param roles another set of roles + * @return true if and only if this set contains all elements of + * the specified roles + */ + public boolean containsAll( Roles roles ) + { + checkApplicationName( roles ); + return this.roles.keySet().containsAll( roles.roles.keySet() ); + } + + + /** + * Returns the {@link Role} with the specified roleName. + * + * @param roleName the name of the role to find + * @return null if there's no role with the specified name + */ + public Role get( String roleName ) + { + return ( Role ) roles.get( roleName ); + } + + + /** + * Returns true if this set is empty. + * + * @return true if this set is empty + */ + public boolean isEmpty() + { + return roles.isEmpty(); + } + + + /** + * Returns the number of elements this set contains. + * + * @return the number of elements this set contains + */ + public int size() + { + return roles.size(); + } + + + /** + * Returns an {@link Iterator} that iterates all {@link Role}s this set contains. + * + * @return an {@link Iterator} that iterates all {@link Role}s this set contains + */ + public Iterator iterator() + { + return Collections.unmodifiableCollection( roles.values() ).iterator(); + } + + + /** + * Creates a new set of {@link Role}s which contains all elements of + * both this set and the specified set (OR operation). This operation never + * modifies this set. + * + * @param roles a set of roles to add + * @return a new set + */ + public Roles addAll( Roles roles ) + { + checkApplicationName( roles ); + Roles newRoles = ( Roles ) clone(); + newRoles.roles.putAll( roles.roles ); + return newRoles; + } + + + /** + * Creates a new set of {@link Role}s which contains elements of + * this set excluding what exists in the specified set (NAND operation). + * This operation never modifies this set. + * + * @param roles a set of roles to remove + * @return a new set + */ + public Roles removeAll( Roles roles ) + { + checkApplicationName( roles ); + Roles newRoles = ( Roles ) clone(); + newRoles.roles.keySet().removeAll( + roles.roles.keySet() ); + return newRoles; + } + + + /** + * Creates a new set of {@link Role}s which contains elements which + * exists in both this set and the specified set (AND operation). This + * operation never modifies this set. + * + * @param roles a set of roles to retain. + * @return a new set + */ + public Roles retainAll( Roles roles ) + { + checkApplicationName( roles ); + Roles newRoles = ( Roles ) clone(); + newRoles.roles.keySet().retainAll( + roles.roles.keySet() ); + return newRoles; + } + + + public Roles getDependentRoles( String permName ) + { + List dependents = new ArrayList(); + for ( Iterator ii = this.roles.values().iterator(); ii.hasNext(); /**/ ) + { + Role role = ( Role ) ii.next(); + if ( role.hasPermission( permName ) ) + { + dependents.add( role ); + } + } + + if ( dependents.size() == 0 ) + { + return new Roles( getApplicationName(), EMPTY_ROLE_ARRAY ); + } + + Role[] roleArray = new Role[dependents.size()]; + dependents.toArray( roleArray ); + return new Roles( getApplicationName(), roleArray ); + } + + + public Roles getDependentRoles( Permission perm ) + { + if ( ! perm.getApplicationName().equals( getApplicationName() ) ) + { + throw new IllegalArgumentException( "The permission '" + perm.getName() + "' is not " + + "\nassociated with this application. It is associated with " + perm.getApplicationName() ); + } + + List dependents = new ArrayList(); + for ( Iterator ii = this.roles.values().iterator(); ii.hasNext(); /**/ ) + { + Role role = ( Role ) ii.next(); + if ( role.hasPermission( perm ) ) + { + dependents.add( role ); + } + } + + if ( dependents.size() == 0 ) + { + return new Roles( getApplicationName(), EMPTY_ROLE_ARRAY ); + } + + Role[] roleArray = new Role[dependents.size()]; + dependents.toArray( roleArray ); + return new Roles( getApplicationName(), roleArray ); + } + + + // ------------------------------------------------------------------------ + // Object Overrides + // ------------------------------------------------------------------------ + + + public Object clone() + { + Role[] roleArray = new Role[ size() ]; + roleArray = ( Role[] ) roles.values().toArray( roleArray ); + return new Roles( applicationName, roleArray ); + } + + + public int hashCode() + { + return applicationName.hashCode() ^ roles.hashCode(); + } + + + public boolean equals( Object that ) + { + if( this == that ) + { + return true; + } + + if( that instanceof Roles ) + { + Roles thatP = ( Roles ) that; + // We don't compare application name because roles already + // contain it. + return this.roles.equals( thatP.roles ); + } + + return false; + } + + + public String toString() + { + StringBuffer buf = new StringBuffer(); + buf.append( "Roles(" ); + buf.append( applicationName ); + buf.append( ": " ); + + // Sort roles by name + Set sortedRoles = new TreeSet( roles.values() ); + Iterator i = sortedRoles.iterator(); + + // Add the first one + if( i.hasNext() ) + { + Role r = ( Role ) i.next(); + buf.append( r.getName() ); + + // Add others + while( i.hasNext() ) + { + r = ( Role ) i.next(); + buf.append( ", " ); + buf.append( r.getName() ); + } + } + else + { + buf.append( "empty" ); + } + + buf.append( ')' ); + + return buf.toString(); + } + + + private void checkApplicationName( Roles roles ) + { + if( !applicationName.equals( roles.getApplicationName() ) ) + { + throw new IllegalArgumentException( "Wrong application name: " + roles.getApplicationName() ); + } + } +} Added: directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/StoreConnectionException.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/StoreConnectionException.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/StoreConnectionException.java (added) +++ directory/trunks/triplesec/guardian-api/src/main/java/org/safehaus/triplesec/guardian/StoreConnectionException.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,76 @@ +/* + * 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.safehaus.triplesec.guardian; + + +/** + * A {@link GuardianException} which is thrown when {@link ConnectionDriver} + * failed to connect to {@link ApplicationPolicy} due to network, + * authentication, or parameter problems. + * + * @author Trustin Lee + * @version $Rev: 53 $, $Date: 2005-08-21 20:58:16 -0400 (Sun, 21 Aug 2005) $ + */ +public class StoreConnectionException extends GuardianException +{ + /** */ + private static final long serialVersionUID = -3699779444160471445L; + + + /** + * + */ + public StoreConnectionException() + { + super(); + } + + + /** + * + * @param message + */ + public StoreConnectionException( String message ) + { + super(message); + } + + + /** + * + * @param nested + */ + public StoreConnectionException( Throwable nested ) + { + super(nested); + } + + + /** + * + * @param message + * @param nested + */ + public StoreConnectionException( String message, Throwable nested ) + { + super(message, nested); + } + +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/AbstractEntityTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/AbstractEntityTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/AbstractEntityTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/AbstractEntityTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,134 @@ +/* + * 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.safehaus.triplesec.guardian; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * + * @author Trustin Lee + * @version $Rev: 52 $, $Date: 2005-08-19 23:03:36 -0400 (Fri, 19 Aug 2005) $ + */ +public abstract class AbstractEntityTest extends TestCase { + + private Object a1; + private Object a2; + private Object b1; + private Object b2; + private Object wrong; + + protected abstract Object newInstanceA1(); + protected abstract Object newInstanceA2(); + protected abstract Object newInstanceB1(); + protected abstract Object newInstanceB2(); + + protected Object newWrongInstance() + { + return new Object(); + } + + public void setUp() + { + a1 = newInstanceA1(); + a2 = newInstanceA2(); + b1 = newInstanceB1(); + b2 = newInstanceB2(); + wrong = newWrongInstance(); + } + + public void testEquals() + { + Assert.assertEquals( a1, a1 ); + Assert.assertEquals( a1, a2 ); + Assert.assertFalse( a1.equals( null ) ); + Assert.assertFalse( a1.equals( b1 ) ); + Assert.assertFalse( a1.equals( b2 ) ); + Assert.assertFalse( a1.equals( wrong ) ); + } + + public void testHashCode() + { + Assert.assertEquals( a1.hashCode(), a2.hashCode() ); + Assert.assertFalse( a1.hashCode() == b1.hashCode() ); + Assert.assertFalse( a1.hashCode() == b2.hashCode() ); + } + + public void testCompareTo() + { + if( !( a1 instanceof Comparable ) ) + { + return; + } + + Comparable a1 = ( Comparable ) this.a1; + + Assert.assertTrue( a1.compareTo( a1 ) == 0 ); + Assert.assertTrue( a1.compareTo( a2 ) == 0 ); + + try + { + a1.compareTo( null ); + Assert.fail( "Execption is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + + Assert.assertFalse( a1.compareTo( b1 ) == 0 ); + Assert.assertFalse( a1.compareTo( b2 ) == 0 ); + + try + { + a1.compareTo( wrong ); + Assert.fail( "Exception is not thrown." ); + } + catch( ClassCastException e ) + { + // OK + } + } + + public void testClone() throws Exception + { + Object a = a1; + Object b = a1.getClass().getMethod( "clone", null ).invoke( a1, null ); + Assert.assertEquals( a, b ); + _testClone( a, b ); + } + + protected void _testClone( Object a, Object b ) + { + } + + public void testToString() throws Exception + { + a1.toString(); + a2.toString(); + b1.toString(); + b2.toString(); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(AbstractEntityTest.class); + } + +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ApplicationPolicyFactoryTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ApplicationPolicyFactoryTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ApplicationPolicyFactoryTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ApplicationPolicyFactoryTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,242 @@ +/* + * 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.safehaus.triplesec.guardian; + +import java.util.Collections; +import java.util.Iterator; +import java.util.Properties; +import java.util.Set; + + +import junit.framework.Assert; +import junit.framework.TestCase; + +public class ApplicationPolicyFactoryTest extends TestCase +{ + + public static void main( String[] args ) + { + junit.textui.TestRunner.run( ApplicationPolicyFactoryTest.class ); + } + + protected void setUp() throws Exception + { + } + + protected void tearDown() throws Exception + { + } + + public void testDriverRegistration() throws Exception + { + ConnectionDriver testDriver1 = new TestConnectionDriver1(); + ConnectionDriver testDriver2 = new TestConnectionDriver2(); + + // Register driver and make sure it works. + Assert.assertTrue( ApplicationPolicyFactory.registerDriver( testDriver1 ) ); + Assert.assertTrue( ApplicationPolicyFactory.registerDriver( testDriver2 ) ); + Assert.assertFalse( ApplicationPolicyFactory.registerDriver( testDriver1 ) ); + ApplicationPolicy testStore = ApplicationPolicyFactory.newInstance( "test2:dummy", new Properties() ); + Assert.assertEquals( "Test", testStore.getApplicationName() ); + + // Deregister driver and make sure it doesn't work. + Assert.assertTrue( ApplicationPolicyFactory.deregisterDriver( testDriver1.getClass() ) ); + Assert.assertFalse( ApplicationPolicyFactory.deregisterDriver( testDriver1.getClass() ) ); + Assert.assertFalse( ApplicationPolicyFactory.deregisterDriver( Integer.class ) ); + try + { + ApplicationPolicyFactory.newInstance( "test:dummy", new Properties() ); + Assert.fail( "Exception is not thrown." ); + } + catch( NoConnectionDriverException e ) + { + // OK + } + } + + public void testConnectionRetry() + { + FailingConnectionDriver driver = new FailingConnectionDriver(); + ApplicationPolicyFactory.registerDriver( driver ); + + String url = "failure:dummy"; + Properties info = new Properties(); + + // No retries + try + { + ApplicationPolicyFactory.newInstance( url, null ); + Assert.fail( "Exception is not thrown." ); + } + catch( StoreConnectionException e ) + { + // OK + } + + // Two retries + driver.reset(); + info.setProperty( ApplicationPolicyFactory.RETRY_COUNT, "2" ); + ApplicationPolicyFactory.newInstance( url, info ); + + // Wrong retry count + driver.reset(); + info.setProperty( ApplicationPolicyFactory.RETRY_COUNT, "-1" ); + try + { + ApplicationPolicyFactory.newInstance( url, info ); + Assert.fail( "Exception is not thrown." ); + } + catch( StoreConnectionException e ) + { + // OK + } + + // With retry delay + driver.reset(); + info.setProperty( ApplicationPolicyFactory.RETRY_COUNT, "2" ); + info.setProperty( ApplicationPolicyFactory.RETRY_DELAY, "1" ); + ApplicationPolicyFactory.newInstance( url, info ); + + // With wrong retry delay + driver.reset(); + info.setProperty( ApplicationPolicyFactory.RETRY_COUNT, "2" ); + info.setProperty( ApplicationPolicyFactory.RETRY_DELAY, "-1" ); + ApplicationPolicyFactory.newInstance( url, info ); + } + + private static class TestConnectionDriver implements ConnectionDriver + { + private final String prefix; + + public TestConnectionDriver( String prefix ) + { + this.prefix = prefix; + } + + public boolean accept(String url) { + return url.startsWith( prefix ); + } + + public ApplicationPolicy newStore(String url, Properties info) throws GuardianException { + return new ApplicationPolicy() + { + public String getApplicationName() { + return "Test"; + } + + public Roles getRoles() { + return null; + } + + public Permissions getPermissions() { + return null; + } + + public Profile getProfile(String userName) { + return null; + } + + public void close() {} + + + public String getDescription() + { + return null; + } + + public boolean removePolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public boolean addPolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public Set getDependentProfileNames( Role role ) throws GuardianException + { + return null; + } + + public Set getDependentProfileNames( Permission permission ) throws GuardianException + { + return null; + } + + public Set getUserProfileIds( String userName ) throws GuardianException + { + return Collections.EMPTY_SET; + } + + public Iterator getProfileIdIterator() throws GuardianException + { + return null; + } + + public Profile getAdminProfile() + { + return null; + } + }; + } + } + + private static class TestConnectionDriver1 extends TestConnectionDriver + { + public TestConnectionDriver1() + { + super( "test1" ); + } + } + + private static class TestConnectionDriver2 extends TestConnectionDriver + { + public TestConnectionDriver2() + { + super( "test2" ); + } + } + + private static class FailingConnectionDriver extends TestConnectionDriver + { + private int counter = 0; + public FailingConnectionDriver() + { + super( "failure" ); + } + + public void reset() + { + counter = 0; + } + + public ApplicationPolicy newStore( String url, Properties info ) throws GuardianException + { + counter++; + if( counter == 3 ) + { + return super.newStore( url, info ); + } + + throw new StoreConnectionException(); + } + } +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ExceptionTests.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ExceptionTests.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ExceptionTests.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ExceptionTests.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,58 @@ +/* + * 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.safehaus.triplesec.guardian; + + +import junit.framework.TestCase; + + +/** + * Just here to make clover happy with Exceptions. + * + * @author Alex Karasulu + * @version $Rev: 25 $ + */ +public class ExceptionTests extends TestCase +{ + public void testGuardianExceptionTests() + { + GuardianException e = new GuardianException(); + assertNotNull( e ); + e = new GuardianException( "some message" ); + assertNotNull( e ); + e = new GuardianException( new NullPointerException() ); + assertNotNull( e ); + e = new GuardianException( "some message", new NullPointerException() ); + assertNotNull( e ); + } + + + public void testNoConnectionDriverExceptionTests() + { + NoConnectionDriverException e = new NoConnectionDriverException(); + assertNotNull( e ); + e = new NoConnectionDriverException( "some message" ); + assertNotNull( e ); + e = new NoConnectionDriverException( new NullPointerException() ); + assertNotNull( e ); + e = new NoConnectionDriverException( "some message", new NullPointerException() ); + assertNotNull( e ); + } +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,109 @@ +/* + * 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.safehaus.triplesec.guardian; + + +/** + * + * @author Trustin Lee + * @version $Rev: 52 $, $Date: 2005-08-19 23:03:36 -0400 (Fri, 19 Aug 2005) $ + */ +public class PermissionTest extends AbstractEntityTest +{ + + protected Object newInstanceA1() + { + return new Permission( "app1", "perm1" ); + } + + + protected Object newInstanceA2() + { + return new Permission( "app1", "perm1" ); + } + + + protected Object newInstanceB1() + { + return new Permission( "app1", "perm2" ); + } + + + protected Object newInstanceB2() + { + return new Permission( "app2", "perm1" ); + } + + + public void testInstantiation() + { + try + { + new Permission( "test", null ); + fail( "Exception is not thrown." ); + } + catch ( NullPointerException e ) + { + // OK + } + try + { + new Permission( null, "test" ); + fail( "Exception is not thrown." ); + } + catch ( NullPointerException e ) + { + // OK + } + try + { + new Permission( "test", "" ); + fail( "Exception is not thrown." ); + } + catch ( IllegalArgumentException e ) + { + // OK + } + try + { + new Permission( "", "test" ); + fail( "Exception is not thrown." ); + } + catch ( IllegalArgumentException e ) + { + // OK + } + } + + + public void testPropeties() + { + Permission p = new Permission( "a", "b", "c" ); + assertEquals( "a", p.getApplicationName() ); + assertEquals( "b", p.getName() ); + assertEquals( "c", p.getDescription() ); + } + + + public static void main( String[] args ) + { + junit.textui.TestRunner.run( PermissionTest.class ); + } +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionsTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionsTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionsTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/PermissionsTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,222 @@ +/* + * 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.safehaus.triplesec.guardian; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import junit.framework.Assert; + + +/** + * + * + * @author Trustin Lee + * @version $Rev: 52 $, $Date: 2005-08-19 23:03:36 -0400 (Fri, 19 Aug 2005) $ + * + */ +public class PermissionsTest extends AbstractEntityTest +{ + protected Object newInstanceA1() + { + return new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + new Permission( "app1", "perm2" ), + new Permission( "app1", "perm3" ), + }); + } + + protected Object newInstanceA2() + { + return new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + new Permission( "app1", "perm2" ), + new Permission( "app1", "perm3" ), + }); + } + + protected Object newInstanceB1() + { + return new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + }); + } + + protected Object newInstanceB2() + { + return new Permissions( "app2", new Permission[0] ); + } + + public void testInstantiation() + { + // Test null values + try + { + new Permissions( null, null ); + Assert.fail( "Execption is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + + // Test empty values + try + { + new Permissions( "", null ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // Test null elements + Permissions perms = new Permissions( "app1", new Permission[] { + null, null, null, + }); + Assert.assertTrue( perms.isEmpty() ); + + // Test mismatching application names + try + { + new Permissions( "app1", new Permission[] { + new Permission( "app2", "perm1" ), + }); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + //OK + } + + Assert.assertTrue( perms.isEmpty() ); + } + + public void testProperties() + { + Permission p1 = new Permission( "app1", "perm1" ); + Permission p2 = new Permission( "app1", "perm2" ); + Permission p3 = new Permission( "app1", "perm3" ); + Permissions perms = new Permissions( "app1", new Permission[] { + p1, p2, p3, + }); + + Assert.assertEquals( "app1", perms.getApplicationName() ); + Assert.assertEquals( 3, perms.size() ); + Assert.assertTrue( perms.contains( p1 ) ); + Assert.assertTrue( perms.contains( p2 ) ); + Assert.assertTrue( perms.contains( p3 ) ); + Assert.assertTrue( perms.contains( p1.getName() ) ); + Assert.assertTrue( perms.contains( p2.getName() ) ); + Assert.assertTrue( perms.contains( p3.getName() ) ); + Assert.assertEquals( p1, perms.get( p1.getName() ) ); + Assert.assertEquals( p2, perms.get( p2.getName() ) ); + Assert.assertEquals( p3, perms.get( p3.getName() ) ); + + // Test iterator integrity + Set allPerms = new HashSet(); + allPerms.add( p1 ); + allPerms.add( p2 ); + allPerms.add( p3 ); + for( Iterator i = perms.iterator(); i.hasNext(); ) + { + Permission p = ( Permission ) i.next(); + Assert.assertTrue( allPerms.contains( p ) ); + allPerms.remove( p ); + } + } + + public void testSetOperations() + { + Permissions perms1 = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + }); + Permissions perms2 = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm2" ), + }); + Permissions perms12 = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + new Permission( "app1", "perm2" ), + }); + Permissions wrongPerms = new Permissions( "wrongApp", null ); + + + // addAll + Assert.assertEquals( perms12, perms1.addAll( perms2 ) ); + Assert.assertEquals( perms1, perms1.addAll( perms1 ) ); + try + { + perms1.addAll( wrongPerms ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // removeAll + Assert.assertEquals( perms1, perms12.removeAll( perms2 ) ); + Assert.assertEquals( perms1, perms1.removeAll( perms2 ) ); + try + { + perms1.removeAll( wrongPerms ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // retainAll + Assert.assertEquals( perms1, perms12.retainAll( perms1 ) ); + Assert.assertEquals( + new Permissions( "app1", null ), perms1.retainAll( perms2 ) ); + try + { + perms1.retainAll( wrongPerms ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // containsAll + Assert.assertTrue( perms12.containsAll( perms12 ) ); + Assert.assertFalse( perms1.containsAll( perms12 ) ); + try + { + perms1.containsAll( wrongPerms ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + } + + public static void main( String[] args ) + { + junit.textui.TestRunner.run( PermissionsTest.class ); + } + +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ProfileTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ProfileTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ProfileTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/ProfileTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,401 @@ +/* + * 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.safehaus.triplesec.guardian; + +import java.security.AccessControlException; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + + +import junit.framework.Assert; + +/** + * @author Alex Karasulu + * @version $Rev: 72 $ + */ +public class ProfileTest extends AbstractEntityTest +{ + private static final ApplicationPolicy STORE1 = new TestApplicationPolicyStore( + "app1" ); + + private static final ApplicationPolicy STORE2 = new TestApplicationPolicyStore( + "app2" ); + + protected Object newInstanceA1() + { + return new Profile( STORE1, "trustin", "trustin", null, null, null, false ); + } + + protected Object newInstanceA2() + { + return new Profile( STORE1, "trustin", "trustin", null, null, null, false ); + } + + protected Object newInstanceB1() + { + return new Profile( STORE1, "alex", "alex", null, null, null, false ); + } + + protected Object newInstanceB2() + { + return new Profile( STORE2, "trustin", "trustin", null, null, null, false ); + } + + public void testInstantiation() + { + Roles roles = new Roles( "app1", new Role[] { + new Role( STORE1, "role1", new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + })), + }); + Permissions grants = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + }); + Permissions denials = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm2" ), + }); + + // Test null parameters + try + { + new Profile( null, "trustin", "trustin", roles, grants, denials, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + new Profile( STORE1, null, "trustin", roles, grants, denials, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + + // Test empty fields + try + { + new Profile( STORE1, "", "trustin", roles, grants, denials, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + try + { + new Profile( new TestApplicationPolicyStore( "" ), "role1", "trustin", roles, grants, denials, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // Test unknown permissions + Permissions wrongPerms = new Permissions( "app1", new Permission[] { + new Permission( "app1", "wrongPerm" ), + }); + try + { + + new Profile( STORE1, "trustin", "trustin", roles, wrongPerms, denials, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + try + { + + new Profile( STORE1, "trustin", "trustin", roles, grants, wrongPerms, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + + // Test mismatching application names. + try + { + new Profile( STORE2, "role1", "trustin", roles, null, null, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + try + { + new Profile( STORE2, "role1", "trustin", null, grants, null, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + try + { + new Profile( STORE2, "role1", "trustin", null, null, denials, false ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + Profile p = new Profile( STORE1, "role1", "trustin", null, null, null, false ); + Assert.assertEquals( 0, p.getRoles().size() ); + Assert.assertEquals( 0, p.getGrants().size() ); + Assert.assertEquals( 0, p.getDenials().size() ); + assertEquals( "trustin", p.getUserName() ); + } + + public void testProperties() + { + Roles roles = new Roles( "app1", new Role[] { + new Role( STORE1, "role1", new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm2" ), + new Permission( "app1", "perm3" ), + new Permission( "app1", "perm4" ), + })), + }); + Permissions grants = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + new Permission( "app1", "perm2" ), + }); + Permissions denials = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm3" ), + }); + + Profile p = new Profile( STORE1, "trustin", "trustin", roles, grants, denials, "test description", false ); + assertEquals( "app1", p.getApplicationName() ); + assertEquals( "trustin", p.getProfileId() ); + assertEquals( roles, p.getRoles() ); + assertEquals( grants, p.getGrants() ); + assertEquals( denials, p.getDenials() ); + assertEquals( "test description", p.getDescription() ); + + Permissions effectivePermissions = new Permissions( "app1", new Permission[] { + new Permission( "app1", "perm1" ), + new Permission( "app1", "perm2" ), + new Permission( "app1", "perm4" ), + }); + assertEquals( effectivePermissions, p.getEffectivePermissions() ); + + assertTrue( p.isInRole( "role1" ) ); + } + + public void testRolePermissions() + { + Permission perm = new Permission( "app1", "perm1" ); + Permission wrongPerm = new Permission( "app1", "perm2" ); + Permissions perms = new Permissions( "app1", new Permission[] { perm, } ); + + // Effective permissions will be: 'perm1' + Profile p = new Profile( + STORE1, "trustin", "trustin", + new Roles( "app1", null ), + perms, null, false ); + + // Check existing permissions + p.checkPermission( perm ); + p.checkPermission( perm, "unused" ); + p.checkPermission( perm.getName() ); + p.checkPermission( perm.getName(), "unused" ); + assertTrue( p.hasPermission( perm ) ); + assertTrue( p.hasPermission( perm.getName() ) ); + assertFalse( p.hasPermission( "nonexistant" ) ); + + // Check null parameters + try + { + p.checkPermission( ( Permission ) null ); + Assert.fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + p.checkPermission( ( String ) null ); + Assert.fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + p.checkPermission( ( Permission ) null, "unused" ); + Assert.fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + p.checkPermission( ( String ) null, "unused" ); + Assert.fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + + // Check non-existing permissions + try + { + p.checkPermission( wrongPerm ); + Assert.fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + try + { + p.checkPermission( wrongPerm, "unused" ); + Assert.fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + try + { + p.checkPermission( wrongPerm.getName() ); + Assert.fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + try + { + p.checkPermission( wrongPerm.getName(), "unused" ); + Assert.fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + } + + + protected void _testClone( Object a, Object b ) + { + Profile pa = ( Profile ) a; + Profile pb = ( Profile ) b; + Assert.assertEquals( pa.getRoles(), pb.getRoles() ); + Assert.assertEquals( pa.getGrants(), pb.getGrants() ); + Assert.assertEquals( pa.getDenials(), pb.getDenials() ); + } + + private static class TestApplicationPolicyStore implements + ApplicationPolicy + { + private final String appName; + + public TestApplicationPolicyStore( String appName ) + { + this.appName = appName; + } + + public String getApplicationName() + { + return appName; + } + + public Roles getRoles() + { + return null; + } + + public Permissions getPermissions() + { + Permission[] perms = new Permission[] { + new Permission( appName, "perm1" ), + new Permission( appName, "perm2" ), + new Permission( appName, "perm3" ), + new Permission( appName, "perm4" ), + }; + return new Permissions( appName, perms ); + } + + public Profile getProfile( String userName ) + { + return null; + } + + + public String getDescription() + { + return null; + } + + + public void close() {} + + public boolean removePolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public boolean addPolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public Set getDependentProfileNames( Role role ) throws GuardianException + { + return null; + } + + public Set getDependentProfileNames( Permission permission ) throws GuardianException + { + return null; + } + + public Set getUserProfileIds( String userName ) throws GuardianException + { + return Collections.EMPTY_SET; + } + + public Iterator getProfileIdIterator() throws GuardianException + { + return null; + } + + public Profile getAdminProfile() + { + return null; + } + } +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RoleTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RoleTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RoleTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RoleTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,333 @@ +/* + * 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.safehaus.triplesec.guardian; + + +import java.security.AccessControlException; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + + + +/** + * @author Alex Karasulu + * @version $Rev: 74 $ + */ +public class RoleTest extends AbstractEntityTest +{ + private static final ApplicationPolicy STORE1 = new TestApplicationPolicyStore( + "app1" ); + + private static final ApplicationPolicy STORE2 = new TestApplicationPolicyStore( + "app2" ); + + protected Object newInstanceA1() + { + return new Role( STORE1, "role1", null ); + } + + protected Object newInstanceA2() + { + return new Role( STORE1, "role1", null ); + } + + protected Object newInstanceB1() + { + return new Role( STORE1, "role2", null ); + } + + protected Object newInstanceB2() + { + return new Role( STORE2, "role1", null ); + } + + public void testInstantiation() + { + Permissions perms = new Permissions( "app1", null ); + + // Test null parameters + try + { + new Role( null, "role1", perms ); + fail( "Execption is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + new Role( STORE1, null, perms ); + fail( "Execption is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + + // Test empty fields + try + { + new Role( STORE2, "", perms ); + fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + try + { + new Role( new TestApplicationPolicyStore( "" ), "role1", perms ); + fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // Test unknown permissions + try + { + Permissions wrongPerms = new Permissions( "app1", new Permission[] { + new Permission( "app1", "wrongPerm" ), + }); + + new Role( STORE1, "role1", wrongPerms ); + fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + + // Test mismatching application names. + try + { + new Role( STORE2, "role1", perms ); + fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + Role r = new Role( STORE1, "role1", null ); + assertEquals( 0, r.getGrants().size() ); + } + + public void testProperties() + { + Permission perm1= new Permission( "app1", "perm1" ); + Permissions perms = new Permissions( "app1", new Permission[] { + perm1, + new Permission( "app1", "perm2" ), + new Permission( "app1", "perm3" ), } ); + + Role r = new Role( STORE1, "role1", perms, "test description" ); + assertEquals( "app1", r.getApplicationName() ); + assertEquals( "role1", r.getName() ); + assertEquals( perms, r.getGrants() ); + assertEquals( "test description", r.getDescription() ); + assertTrue( r.hasPermission( perm1 ) ) ; + assertTrue( r.hasPermission( perm1.getName() ) ) ; + } + + public void testRolePermissions() + { + Permission perm = new Permission( "app1", "perm1" ); + Permission wrongPerm = new Permission( "app1", "perm2" ); + Permissions perms = new Permissions( "app1", new Permission[] { perm, } ); + + Role r = new Role( STORE1, "role1", perms ); + + // Check existing permissions + r.checkPermission( perm ); + assertTrue( r.hasPermission( perm.getName() ) ); + assertTrue( r.hasPermission( perm ) ); + r.checkPermission( perm, "unused" ); + r.checkPermission( perm.getName() ); + r.checkPermission( perm.getName(), "unused" ); + + // Check null parameters + try + { + r.checkPermission( ( Permission ) null ); + fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + r.checkPermission( ( String ) null ); + fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + r.checkPermission( ( Permission ) null, "unused" ); + fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + try + { + r.checkPermission( ( String ) null, "unused" ); + fail( "Exception is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + + // Check non-existing permissions + try + { + r.checkPermission( wrongPerm ); + fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + try + { + r.checkPermission( wrongPerm, "unused" ); + fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + try + { + r.checkPermission( wrongPerm.getName() ); + fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + try + { + r.checkPermission( wrongPerm.getName(), "unused" ); + fail( "Exception is not thrown." ); + } + catch( AccessControlException e ) + { + // OK + } + } + + + + protected void _testClone( Object a, Object b ) + { + Role ra = ( Role ) a; + Role rb = ( Role ) b; + assertEquals( ra.getGrants(), rb.getGrants() ); + } + + private static class TestApplicationPolicyStore implements + ApplicationPolicy + { + private final String appName; + + public TestApplicationPolicyStore( String appName ) + { + this.appName = appName; + } + + public String getApplicationName() + { + return appName; + } + + public Roles getRoles() + { + return null; + } + + public Permissions getPermissions() + { + Permission[] perms = new Permission[] { + new Permission( appName, "perm1" ), + new Permission( appName, "perm2" ), + new Permission( appName, "perm3" ), + }; + return new Permissions( appName, perms ); + } + + public Profile getProfile( String userName ) + { + return null; + } + + public String getDescription() + { + return null; + } + + public void close() {} + + public boolean removePolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public boolean addPolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public Set getDependentProfileNames( Role role ) throws GuardianException + { + return null; + } + + public Set getDependentProfileNames( Permission permission ) throws GuardianException + { + return null; + } + + public Set getUserProfileIds( String userName ) throws GuardianException + { + return Collections.EMPTY_SET; + } + + public Iterator getProfileIdIterator() throws GuardianException + { + return null; + } + + public Profile getAdminProfile() + { + return null; + } + } +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RolesTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RolesTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RolesTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/RolesTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,337 @@ +/* + * 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.safehaus.triplesec.guardian; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + + +import junit.framework.Assert; + + +/** + * + * + * @author Trustin Lee + * @version $Rev: 72 $, $Date: 2005-11-07 21:37:46 -0500 (Mon, 07 Nov 2005) $ + */ +public class RolesTest extends AbstractEntityTest +{ + private static final ApplicationPolicy STORE1 = new TestApplicationPolicyStore( + "app1" ); + + private static final ApplicationPolicy STORE2 = new TestApplicationPolicyStore( + "app2" ); + + protected Object newInstanceA1() + { + return new Roles( "app1", new Role[] { + new Role( STORE1, "role1", null ), + new Role( STORE1, "role2", null ), + new Role( STORE1, "role3", null ), + }); + } + + protected Object newInstanceA2() + { + return new Roles( "app1", new Role[] { + new Role( STORE1, "role1", null ), + new Role( STORE1, "role2", null ), + new Role( STORE1, "role3", null ), + }); + } + + protected Object newInstanceB1() + { + return new Roles( "app1", new Role[] { + new Role( STORE1, "role1", null ), + }); + } + + protected Object newInstanceB2() + { + return new Roles( "app2", null ); + } + + public void testInstantiation() + { + // Test null values + try + { + new Roles( null, null ); + Assert.fail( "Execption is not thrown." ); + } + catch( NullPointerException e ) + { + // OK + } + + // Test empty values + try + { + new Roles( "", null ); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // Test null elements + Roles roles = new Roles( "app1", new Role[] { + null, null, null, + }); + Assert.assertTrue( roles.isEmpty() ); + + // Test mismatching application names + try + { + new Roles( "app1", new Role[] { + new Role( STORE2, "role1", null ), + }); + Assert.fail( "Execption is not thrown." ); + } + catch( IllegalArgumentException e ) + { + //OK + } + + Assert.assertTrue( roles.isEmpty() ); + } + + public void testProperties() + { + Role r1 = new Role( STORE1, "role1", null ); + Role r2 = new Role( STORE1, "role2", null ); + Role r3 = new Role( STORE1, "role3", null ); + Roles roles = new Roles( "app1", new Role[] { + r1, r2, r3, + }); + + Assert.assertEquals( "app1", roles.getApplicationName() ); + Assert.assertEquals( 3, roles.size() ); + Assert.assertTrue( roles.contains( r1 ) ); + Assert.assertTrue( roles.contains( r2 ) ); + Assert.assertTrue( roles.contains( r3 ) ); + Assert.assertTrue( roles.contains( r1.getName() ) ); + Assert.assertTrue( roles.contains( r2.getName() ) ); + Assert.assertTrue( roles.contains( r3.getName() ) ); + Assert.assertEquals( r1, roles.get( r1.getName() ) ); + Assert.assertEquals( r2, roles.get( r2.getName() ) ); + Assert.assertEquals( r3, roles.get( r3.getName() ) ); + + // Test iterator integrity + Set allRoles = new HashSet(); + allRoles.add( r1 ); + allRoles.add( r2 ); + allRoles.add( r3 ); + for( Iterator i = roles.iterator(); i.hasNext(); ) + { + Role p = ( Role ) i.next(); + Assert.assertTrue( allRoles.contains( p ) ); + allRoles.remove( p ); + } + } + + public void testSetOperations() + { + Roles roles1 = new Roles( "app1", new Role[] { + new Role( STORE1, "role1", null ), + }); + Roles roles2 = new Roles( "app1", new Role[] { + new Role( STORE1, "role2", null ), + }); + Roles roles12 = new Roles( "app1", new Role[] { + new Role( STORE1, "role1", null ), + new Role( STORE1, "role2", null ), + }); + Roles wrongRoles = new Roles( "wrongApp", null ); + + + // addAll + Assert.assertEquals( roles12, roles1.addAll( roles2 ) ); + Assert.assertEquals( roles1, roles1.addAll( roles1 ) ); + try + { + roles1.addAll( wrongRoles ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // removeAll + Assert.assertEquals( roles1, roles12.removeAll( roles2 ) ); + Assert.assertEquals( roles1, roles1.removeAll( roles2 ) ); + try + { + roles1.removeAll( wrongRoles ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // retainAll + Assert.assertEquals( roles1, roles12.retainAll( roles1 ) ); + Assert.assertEquals( + new Roles( "role1", null ), roles1.retainAll( roles2 ) ); + try + { + roles1.retainAll( wrongRoles ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + + // containsAll + Assert.assertTrue( roles12.containsAll( roles12 ) ); + Assert.assertFalse( roles1.containsAll( roles12 ) ); + try + { + roles1.containsAll( wrongRoles ); + Assert.fail( "Exception is not thrown." ); + } + catch( IllegalArgumentException e ) + { + // OK + } + } + + + public void testGetDependentRoles() + { + Role role1 = new Role( STORE1, "role1", STORE1.getPermissions() ); + Role role2 = new Role( STORE1, "role2", null ); + Roles roles12 = new Roles( "app1", new Role[] { role1, role2 }); + + Roles dependents = roles12.getDependentRoles( "perm1" ); + assertEquals( 1, dependents.size() ); + assertEquals( role1, dependents.get( "role1" ) ); + + dependents = roles12.getDependentRoles( STORE1.getPermissions().get( "perm1" ) ); + assertEquals( 1, dependents.size() ); + assertEquals( role1, dependents.get( "role1" ) ); + + dependents = roles12.getDependentRoles( "perm99" ); + assertEquals( 0, dependents.size() ); + + dependents = roles12.getDependentRoles( new Permission( "app1", "perm99" ) ); + assertEquals( 0, dependents.size() ); + + try + { + dependents = roles12.getDependentRoles( new Permission( "blah", "perm99" ) ); + fail( "Should never get here due to an exception" ); + } + catch ( IllegalArgumentException e ) + { + } + } + + + public static void main( String[] args ) + { + junit.textui.TestRunner.run( RolesTest.class ); + } + + private static class TestApplicationPolicyStore implements ApplicationPolicy + { + private final String appName; + + public TestApplicationPolicyStore( String appName ) + { + this.appName = appName; + } + + public String getApplicationName() + { + return appName; + } + + public Roles getRoles() + { + return null; + } + + public Permissions getPermissions() + { + Permission[] perms = new Permission[] { + new Permission( appName, "perm1" ), + new Permission( appName, "perm2" ), + new Permission( appName, "perm3" ), + }; + return new Permissions( appName, perms ); + } + + public Profile getProfile( String userName ) + { + return null; + } + + public String getDescription() + { + return null; + } + + public void close() {} + + public boolean removePolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public boolean addPolicyListener( PolicyChangeListener listener ) + { + return false; + } + + public Set getDependentProfileNames( Role role ) throws GuardianException + { + return null; + } + + public Set getDependentProfileNames( Permission permission ) throws GuardianException + { + return null; + } + + public Set getUserProfileIds( String userName ) throws GuardianException + { + return Collections.EMPTY_SET; + } + + public Iterator getProfileIdIterator() throws GuardianException + { + return null; + } + + public Profile getAdminProfile() + { + return null; + } + } +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicy.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicy.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicy.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicy.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,216 @@ +/* + * 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.safehaus.triplesec.guardian.mock; + + +import org.safehaus.triplesec.guardian.*; + +import java.util.*; + + +/** + * A mock implementation of an ApplicationPolicyStore for testing purposes. + * + * @author Alex Karasulu + * @version $Rev: 72 $ + */ +class MockApplicationPolicy implements ApplicationPolicy +{ + private final Roles roles; + private final Permissions perms; + private final String name; + private final Map profileByName; + + + public MockApplicationPolicy() + { + name = "mockApplication"; + profileByName = new HashMap(); + Set permSet = new HashSet(); + Set roleSet = new HashSet(); + + // -------------------------------------------------------------------------------- + // add permissions + // -------------------------------------------------------------------------------- + + Permission perm0 = new Permission( name, "mockPerm0" ); permSet.add( perm0 ); + Permission perm1 = new Permission( name, "mockPerm1" ); permSet.add( perm1 ); + Permission perm2 = new Permission( name, "mockPerm2" ); permSet.add( perm2 ); + Permission perm3 = new Permission( name, "mockPerm3" ); permSet.add( perm3 ); + Permission perm4 = new Permission( name, "mockPerm4" ); permSet.add( perm4 ); + Permission perm5 = new Permission( name, "mockPerm5" ); permSet.add( perm5 ); + Permission perm6 = new Permission( name, "mockPerm6" ); permSet.add( perm6 ); + Permission perm7 = new Permission( name, "mockPerm7" ); permSet.add( perm7 ); + Permission perm8 = new Permission( name, "mockPerm8" ); permSet.add( perm8 ); + Permission perm9 = new Permission( name, "mockPerm9" ); permSet.add( perm9 ); + + Permission[] permArray = ( Permission[] ) permSet.toArray( new Permission[0] ); + perms = new Permissions( name, permArray ); + + // -------------------------------------------------------------------------------- + // add roles + // -------------------------------------------------------------------------------- + + // role without any permissions toggled + Permissions grants = new Permissions( name, new Permission[0] ); + Role role0 = new Role( this, "mockRole0", grants ); + roleSet.add( role0 ); + + // role with permission mockPerm0 + grants = new Permissions( name, new Permission[] {perm0}); + Role role1 = new Role( this, "mockRole1", grants ); + roleSet.add( role1 ); + + // role with permission mockPerm1 + grants = new Permissions( name, new Permission[] {perm1}); + Role role2 = new Role( this, "mockRole2", grants ); + roleSet.add( role2 ); + + // role with permission mockPerm2 and mochPerm3 + grants = new Permissions( name, new Permission[] {perm2, perm3}); + Role role3 = new Role( this, "mockRole3", grants ); + roleSet.add( role3 ); + + // role with permission mockPerm4, mockPerm5, mockPerm6, mockPerm7, mockPerm9 + grants = new Permissions( name, new Permission[] {perm4, perm5, perm6, perm7, perm9}); + Role role4 = new Role( this, "mockRole4", grants ); + roleSet.add( role4 ); + + Role[] rolesArray = ( Role [] ) roleSet.toArray( new Role[0] ); + roles = new Roles( name, rolesArray ); + + // -------------------------------------------------------------------------------- + // add profiles + // -------------------------------------------------------------------------------- + + // a profile that has no permissions at all, and no roles (basis case) + grants = new Permissions( name, new Permission[0] ); + Permissions denials = new Permissions( name, new Permission[0] ); + Roles roles = new Roles( name, new Role[0] ); + Profile profile = new Profile( this, "mockProfile0", "trustin", roles, grants, denials, false ); + profileByName.put( profile.getProfileId(), profile ); + + // a profile for checking union of role1 and role2 - inherits perm0 and perm1 + grants = new Permissions( name, new Permission[0] ); + denials = new Permissions( name, new Permission[0] ); + roles = new Roles( name, new Role[] { role1, role2 } ); + profile = new Profile( this, "mockProfile1", "trustin", roles, grants, denials, false ); + profileByName.put( profile.getProfileId(), profile ); + + // a profile for checking union of roles with grants - granted perm0 and inherits perm1 + grants = new Permissions( name, new Permission[] { perm0 } ); + denials = new Permissions( name, new Permission[0] ); + roles = new Roles( name, new Role[] { role2 } ); + profile = new Profile( this, "mockProfile2", "trustin", roles, grants, denials, false ); + profileByName.put( profile.getProfileId(), profile ); + + // a profile for checking union of roles with grants - granted perm0, perm7 and inherits perm2 and perm3 + grants = new Permissions( name, new Permission[] { perm0, perm7 } ); + denials = new Permissions( name, new Permission[0] ); + roles = new Roles( name, new Role[] { role3 } ); + profile = new Profile( this, "mockProfile3", "trustin", roles, grants, denials, false ); + profileByName.put( profile.getProfileId(), profile ); + + // a profile for checking union of roles with grants and denials + // granted perm0, in role3 and role4 but denied inherited perm7 + grants = new Permissions( name, new Permission[] { perm0 } ); + denials = new Permissions( name, new Permission[] { perm7 } ); + roles = new Roles( name, new Role[] { role3, role4 } ); + profile = new Profile( this, "mockProfile4", "trustin", roles, grants, denials, false ); + profileByName.put( profile.getProfileId(), profile ); + } + + + public String getApplicationName() + { + return name; + } + + + public Roles getRoles() + { + return roles; + } + + + public Permissions getPermissions() + { + return perms; + } + + + public Profile getProfile( String username ) + { + return ( Profile ) profileByName.get( username ); + } + + + public String getDescription() + { + return "a mock application"; + } + + + public void close() + { + } + + + public boolean removePolicyListener( PolicyChangeListener listener ) + { + return false; + } + + + public boolean addPolicyListener( PolicyChangeListener listener ) + { + return false; + } + + + public Set getDependentProfileNames( Role role ) throws GuardianException + { + return null; + } + + + public Set getDependentProfileNames( Permission permission ) throws GuardianException + { + return null; + } + + + public Set getUserProfileIds( String userName ) throws GuardianException + { + return Collections.EMPTY_SET; + } + + + public Iterator getProfileIdIterator() throws GuardianException + { + return null; + } + + + public Profile getAdminProfile() + { + return null; + } +} Added: directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicyTest.java URL: http://svn.apache.org/viewvc/directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicyTest.java?view=auto&rev=486187 ============================================================================== --- directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicyTest.java (added) +++ directory/trunks/triplesec/guardian-api/src/test/java/org/safehaus/triplesec/guardian/mock/MockApplicationPolicyTest.java Tue Dec 12 07:23:31 2006 @@ -0,0 +1,116 @@ +/* + * 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.safehaus.triplesec.guardian.mock; + + +import junit.framework.TestCase; +import org.safehaus.triplesec.guardian.ApplicationPolicyFactory; +import org.safehaus.triplesec.guardian.Profile; + + +/** + * Test cases for the mock application policy store. + * + * @author Alex Karasulu + * @version $Rev$ + */ +public class MockApplicationPolicyTest extends TestCase +{ + MockApplicationPolicy store; + + protected void setUp() throws Exception + { + super.setUp(); + Class.forName( "org.safehaus.triplesec.guardian.mock.MockConnectionDriver" ); + store = ( MockApplicationPolicy ) ApplicationPolicyFactory.newInstance( "mockApplication", null ); + } + + + protected void tearDown() throws Exception + { + super.tearDown(); + store.close(); + store = null; + } + + + public void testProfile0() + { + assertEquals( 5, store.getRoles().size() ); + Profile p = store.getProfile( "mockProfile0" ); + assertTrue( p.getEffectivePermissions().isEmpty() ); + assertTrue( p.getRoles().isEmpty() ); + } + + public void testProfile1() + { + Profile p = store.getProfile( "mockProfile1" ); + assertEquals( 2, p.getEffectivePermissions().size() ); + assertTrue( p.hasPermission( "mockPerm0" ) ); + assertTrue( p.hasPermission( "mockPerm1" ) ); + assertFalse( p.hasPermission( "mockPerm3") ); + assertEquals( 2, p.getRoles().size() ); + } + + public void testProfile2() + { + Profile p = store.getProfile( "mockProfile2" ); + assertEquals( 2, p.getEffectivePermissions().size() ); + assertTrue( p.hasPermission( "mockPerm0" ) ); + assertTrue( p.hasPermission( "mockPerm1" ) ); + assertFalse( p.hasPermission( "mockPerm3") ); + assertEquals( 1, p.getRoles().size() ); + assertTrue( p.getRoles().contains( "mockRole2" ) ); + } + + public void testProfile3() + { + Profile p = store.getProfile( "mockProfile3" ); + assertEquals( 4, p.getEffectivePermissions().size() ); + assertTrue( p.hasPermission( "mockPerm0" ) ); + assertTrue( p.hasPermission( "mockPerm7" ) ); + assertTrue( p.hasPermission( "mockPerm2" ) ); + assertTrue( p.hasPermission( "mockPerm3" ) ); + assertFalse( p.hasPermission( "mockPerm4" ) ); + assertEquals( 1, p.getRoles().size() ); + assertTrue( p.getRoles().contains( "mockRole3" ) ); + } + + public void testProfile4() + { + Profile p = store.getProfile( "mockProfile4" ); + assertEquals( 7, p.getEffectivePermissions().size() ); + assertTrue( p.hasPermission( "mockPerm0" ) ); + assertFalse( p.hasPermission( "mockPerm1" ) ); + assertTrue( p.hasPermission( "mockPerm2" ) ); + assertTrue( p.hasPermission( "mockPerm3" ) ); + assertTrue( p.hasPermission( "mockPerm4" ) ); + assertTrue( p.hasPermission( "mockPerm5" ) ); + assertTrue( p.hasPermission( "mockPerm6" ) ); + assertFalse( p.hasPermission( "mockPerm7" ) ); + assertFalse( p.hasPermission( "mockPerm8" ) ); + assertTrue( p.hasPermission( "mockPerm9" ) ); + + assertFalse( p.hasPermission( "mockPerm14" ) ); + assertEquals( 2, p.getRoles().size() ); + assertTrue( p.getRoles().contains( "mockRole3" ) ); + assertTrue( p.getRoles().contains( "mockRole4" ) ); + } +}