Return-Path: Delivered-To: apmail-incubator-directory-cvs-archive@www.apache.org Received: (qmail 56807 invoked from network); 18 Nov 2004 14:09:56 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 18 Nov 2004 14:09:56 -0000 Received: (qmail 72246 invoked by uid 500); 18 Nov 2004 14:09:55 -0000 Delivered-To: apmail-incubator-directory-cvs-archive@incubator.apache.org Received: (qmail 72209 invoked by uid 500); 18 Nov 2004 14:09:55 -0000 Mailing-List: contact directory-cvs-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: directory-dev@incubator.apache.org Delivered-To: mailing list directory-cvs@incubator.apache.org Received: (qmail 72188 invoked by uid 99); 18 Nov 2004 14:09:54 -0000 X-ASF-Spam-Status: No, hits=-10.0 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Thu, 18 Nov 2004 06:09:50 -0800 Received: (qmail 56699 invoked by uid 65534); 18 Nov 2004 14:09:48 -0000 Date: 18 Nov 2004 14:09:48 -0000 Message-ID: <20041118140948.56690.qmail@minotaur.apache.org> From: vtence@apache.org To: directory-cvs@incubator.apache.org Subject: svn commit: r76236 - in incubator/directory/janus/trunk/sandbox/src: java/org/apache/janus/authentication java/org/apache/janus/authentication/attribute java/org/apache/janus/script java/org/apache/janus/script/xml test/org/apache/janus/authentication/attribute test/org/apache/janus/script/xml test/org/apache/janus/testmodel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: vtence Date: Thu Nov 18 06:09:47 2004 New Revision: 76236 Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CyclicAssociationException.java - copied, changed from r73655, incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/CyclicAssociationException.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/MutableInformationProvider.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/InformationProviderBuilder.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/InformationProviderBuilderMonitor.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/NullInformationProviderMonitor.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/AbstractDom4JInformationProviderBuilder.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JGroupBuilder.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRoleBuilder.java incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JUtils.java incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JGroupBuilderTest.java incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRoleBuilderTest.java incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Roles.java Removed: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/CyclicAssociationException.java Modified: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Usernames.java Log: Some significant changes: added builders for GroupProviders and RoleProviders and moved some classes to better suited packages Copied: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CyclicAssociationException.java (from r73655, incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/CyclicAssociationException.java) ============================================================================== --- incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/CyclicAssociationException.java (original) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CyclicAssociationException.java Thu Nov 18 06:09:47 2004 @@ -14,7 +14,7 @@ * limitations under the License. * */ -package org.apache.janus.authentication.attribute; +package org.apache.janus.authentication; import java.security.Principal; Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/MutableInformationProvider.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/MutableInformationProvider.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,27 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.authentication; + +import java.security.Principal; +import java.util.Collection; + +public interface MutableInformationProvider extends InformationProvider +{ + boolean addAllAttributes( Principal principal, Collection attributes ); + + boolean addAttribute( Principal principal, Principal attribute ); +} Modified: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java ============================================================================== --- incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java (original) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java Thu Nov 18 06:09:47 2004 @@ -16,7 +16,8 @@ */ package org.apache.janus.authentication.attribute; -import org.apache.janus.authentication.InformationProvider; +import org.apache.janus.authentication.CyclicAssociationException; +import org.apache.janus.authentication.MutableInformationProvider; import javax.security.auth.Subject; import java.security.Principal; @@ -26,7 +27,7 @@ import java.util.Iterator; import java.util.Map; -public class AttributeProvider implements InformationProvider +public class AttributeProvider implements MutableInformationProvider { private final Map m_attributes; Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/InformationProviderBuilder.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/InformationProviderBuilder.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,24 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script; + +import org.apache.janus.authentication.MutableInformationProvider; + +public interface InformationProviderBuilder +{ + void buildProvider( MutableInformationProvider groupProvider ); +} Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/InformationProviderBuilderMonitor.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/InformationProviderBuilderMonitor.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,28 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script; + +import org.apache.janus.authentication.CyclicAssociationException; + +import java.security.Principal; + +public interface InformationProviderBuilderMonitor +{ + void cyclicAssociation( CyclicAssociationException e ); + + void duplicateAttribute( Principal principal, Principal attribute ); +} Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/NullInformationProviderMonitor.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/NullInformationProviderMonitor.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,32 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script; + +import org.apache.janus.authentication.CyclicAssociationException; + +import java.security.Principal; + +public class NullInformationProviderMonitor implements InformationProviderBuilderMonitor +{ + public void cyclicAssociation( CyclicAssociationException e ) + { + } + + public void duplicateAttribute( Principal principal, Principal attribute ) + { + } +} Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/AbstractDom4JInformationProviderBuilder.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/AbstractDom4JInformationProviderBuilder.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,67 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script.xml; + +import org.apache.janus.authentication.MutableInformationProvider; +import org.apache.janus.authentication.CyclicAssociationException; +import org.apache.janus.authentication.attribute.GroupPrincipal; +import org.apache.janus.authentication.realm.UsernamePrincipal; +import org.apache.janus.script.InformationProviderBuilderMonitor; +import org.apache.janus.script.InformationProviderBuilder; +import org.dom4j.Element; + +import java.security.Principal; + +public abstract class AbstractDom4JInformationProviderBuilder implements InformationProviderBuilder +{ + protected final Element m_root; + protected final InformationProviderBuilderMonitor m_monitor; + + public AbstractDom4JInformationProviderBuilder( Element root, InformationProviderBuilderMonitor monitor ) + { + m_root = root; + m_monitor = monitor; + } + + protected void addAttribute( MutableInformationProvider groupProvider, Principal principal, Principal attribute ) + { + boolean added = false; + try + { + added = groupProvider.addAttribute( principal, attribute ); + if (!added) m_monitor.duplicateAttribute( principal, attribute ); + } + catch ( CyclicAssociationException e ) + { + m_monitor.cyclicAssociation( e ); + } + } + + protected Principal getUserPrincipal( Element element ) + { + String username = element.attributeValue( "username" ); + Principal usernamePrincipal = new UsernamePrincipal( username ); + return usernamePrincipal; + } + + protected Principal getGroupPrincipal( Element element ) + { + String groupName = element.attributeValue( "groupname" ); + Principal groupPrincipal = new GroupPrincipal( groupName ); + return groupPrincipal; + } +} Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JGroupBuilder.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JGroupBuilder.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,102 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script.xml; + +import org.apache.janus.authentication.MutableInformationProvider; +import org.apache.janus.authentication.attribute.GroupPrincipal; +import org.apache.janus.script.InformationProviderBuilderMonitor; +import org.apache.janus.script.NullInformationProviderMonitor; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; + +import java.io.Reader; +import java.security.Principal; +import java.util.Iterator; +import java.util.List; + +public class Dom4JGroupBuilder extends AbstractDom4JInformationProviderBuilder +{ + public static Dom4JGroupBuilder fromReader( Reader reader ) throws DocumentException + { + return fromReader( reader, new NullInformationProviderMonitor() ); + } + + public static Dom4JGroupBuilder fromReader( Reader reader, InformationProviderBuilderMonitor monitor ) throws DocumentException + { + Document doc = Dom4JUtils.readDocument( reader ); + return new Dom4JGroupBuilder( doc, monitor ); + } + + public Dom4JGroupBuilder( Document doc ) + { + this( doc.getRootElement() ); + } + + public Dom4JGroupBuilder( Document doc, InformationProviderBuilderMonitor monitor ) + { + this( doc.getRootElement(), monitor ); + } + + public Dom4JGroupBuilder( Element root ) + { + this( root, new NullInformationProviderMonitor() ); + } + + public Dom4JGroupBuilder( Element root, InformationProviderBuilderMonitor monitor ) + { + super( root, monitor ); + } + + public void buildProvider( MutableInformationProvider provider ) + { + addGroupsToUsers( provider, m_root.elements( "user" ) ); + addGroupsToGroups( provider, m_root.elements( "group" ) ); + } + + private void addGroupsToUsers( MutableInformationProvider provider, List users ) + { + for ( Iterator it = users.iterator(); it.hasNext(); ) + { + Element element = ( Element ) it.next(); + Principal user = getUserPrincipal( element ); + Principal group = getGroupAttribute( element ); + + addAttribute( provider, user, group ); + } + } + + private void addGroupsToGroups( MutableInformationProvider provider, List groups ) + { + for ( Iterator it = groups.iterator(); it.hasNext(); ) + { + Element element = ( Element ) it.next(); + Principal subGroup = getGroupPrincipal( element ); + Principal group = getGroupAttribute( element ); + + addAttribute( provider, subGroup, group ); + } + } + + private Principal getGroupAttribute( Element element ) + { + String groupName = element.attributeValue( "group" ); + Principal groupPrincipal = new GroupPrincipal( groupName ); + return groupPrincipal; + } +} + Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRoleBuilder.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRoleBuilder.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,121 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script.xml; + +import org.apache.janus.authentication.MutableInformationProvider; +import org.apache.janus.authentication.attribute.RolePrincipal; +import org.apache.janus.script.InformationProviderBuilderMonitor; +import org.apache.janus.script.NullInformationProviderMonitor; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; + +import java.io.Reader; +import java.security.Principal; +import java.util.Iterator; +import java.util.List; + +public class Dom4JRoleBuilder extends AbstractDom4JInformationProviderBuilder +{ + public static Dom4JRoleBuilder fromReader( Reader reader ) throws DocumentException + { + return fromReader( reader, new NullInformationProviderMonitor() ); + } + + public static Dom4JRoleBuilder fromReader( Reader reader, InformationProviderBuilderMonitor monitor ) throws DocumentException + { + Document doc = Dom4JUtils.readDocument( reader ); + return new Dom4JRoleBuilder( doc, monitor ); + } + + public Dom4JRoleBuilder( Document doc ) + { + this( doc.getRootElement() ); + } + + public Dom4JRoleBuilder( Document doc, InformationProviderBuilderMonitor monitor ) + { + this( doc.getRootElement(), monitor ); + } + + public Dom4JRoleBuilder( Element root ) + { + this( root, new NullInformationProviderMonitor() ); + } + + public Dom4JRoleBuilder( Element root, InformationProviderBuilderMonitor monitor ) + { + super( root, monitor ); + } + + public void buildProvider( MutableInformationProvider provider ) + { + addRolesToUsers( provider, m_root.elements( "user" ) ); + addRolesToGroups( provider, m_root.elements( "group" ) ); + addRolesToRoles( provider, m_root.elements( "role" ) ); + } + + private void addRolesToUsers( MutableInformationProvider provider, List users ) + { + for ( Iterator it = users.iterator(); it.hasNext(); ) + { + Element element = ( Element ) it.next(); + Principal user = getUserPrincipal( element ); + Principal role = getRoleAttribute( element ); + + addAttribute( provider, user, role ); + } + } + + private void addRolesToGroups( MutableInformationProvider provider, List groups ) + { + for ( Iterator it = groups.iterator(); it.hasNext(); ) + { + Element element = ( Element ) it.next(); + Principal group = getGroupPrincipal( element ); + Principal role = getRoleAttribute( element ); + + addAttribute( provider, group, role ); + } + } + + private void addRolesToRoles( MutableInformationProvider provider, List groups ) + { + for ( Iterator it = groups.iterator(); it.hasNext(); ) + { + Element element = ( Element ) it.next(); + Principal subRole = getRolePrincipal( element ); + Principal role = getRoleAttribute( element ); + + addAttribute( provider, subRole, role ); + } + } + + private Principal getRolePrincipal( Element element ) + { + String roleName = element.attributeValue( "rolename" ); + Principal rolePrincipal = new RolePrincipal( roleName ); + return rolePrincipal; + } + + private Principal getRoleAttribute( Element element ) + { + String roleName = element.attributeValue( "role" ); + Principal rolePrincipal = new RolePrincipal( roleName ); + return rolePrincipal; + } +} Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JUtils.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JUtils.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,34 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script.xml; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; + +import java.io.Reader; + +public final class Dom4JUtils +{ + public static Document readDocument( Reader reader ) throws DocumentException + { + final SAXReader parser = new SAXReader(); + return parser.read( reader ); + } + + private Dom4JUtils() {} +} Modified: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java ============================================================================== --- incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java (original) +++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java Thu Nov 18 06:09:47 2004 @@ -21,6 +21,7 @@ import org.apache.janus.testmodel.Groups; import org.apache.janus.testmodel.Subjects; import org.apache.janus.testmodel.Usernames; +import org.apache.janus.authentication.CyclicAssociationException; import javax.security.auth.Subject; import java.security.Principal; Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JGroupBuilderTest.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JGroupBuilderTest.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,157 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script.xml; + +import org.apache.janus.authentication.CyclicAssociationException; +import org.apache.janus.authentication.MutableInformationProvider; +import org.apache.janus.authentication.attribute.AttributeProvider; +import org.apache.janus.script.InformationProviderBuilderMonitor; +import org.apache.janus.testmodel.Groups; +import org.apache.janus.testmodel.Usernames; +import org.jmock.Mock; +import org.jmock.MockObjectTestCase; +import org.jmock.core.Constraint; + +import java.io.StringReader; + +/** + * TODO: Move some tests to abstract test class + */ +public class Dom4JGroupBuilderTest extends MockObjectTestCase +{ + public void testCanAddGroupsToUsers() throws Exception + { + Dom4JGroupBuilder builder = Dom4JGroupBuilder.fromReader( new StringReader( userWithTwoGroups() ) ); + + Mock mockProvider = new Mock( MutableInformationProvider.class ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Usernames.joe() ), eq( Groups.men() ) ).will( returnValue( true ) ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Usernames.joe() ), eq( Groups.geeks() ) ).will( returnValue( true ) ); + + builder.buildProvider( ( MutableInformationProvider ) mockProvider.proxy() ); + + mockProvider.verify(); + } + + private String userWithTwoGroups() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + ""; + return content; + } + + public void testCanAddGroupsToGroups() throws Exception + { + Dom4JGroupBuilder builder = Dom4JGroupBuilder.fromReader( new StringReader( groupWithTwoGroups() ) ); + + Mock mockProvider = new Mock( MutableInformationProvider.class ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Groups.canadians() ), eq( Groups.men() ) ).will( returnValue( true ) ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Groups.canadians() ), eq( Groups.geeks() ) ).will( returnValue( true ) ); + + builder.buildProvider( ( MutableInformationProvider ) mockProvider.proxy() ); + + mockProvider.verify(); + } + + private String groupWithTwoGroups() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + ""; + return content; + } + + public void testNotifiesOfDuplicateAttributes() throws Exception + { + Mock mockMonitor = new Mock( InformationProviderBuilderMonitor.class ); + Dom4JGroupBuilder builder = Dom4JGroupBuilder.fromReader( new StringReader( twiceSameAttribute() ), + ( InformationProviderBuilderMonitor ) mockMonitor.proxy() ); + AttributeProvider provider = new AttributeProvider(); + + mockMonitor.expects( once() ).method( "duplicateAttribute" ).with( eq( Usernames.joe() ), eq( Groups.men() ) ); + + builder.buildProvider( provider ); + mockMonitor.verify(); + } + + private String twiceSameAttribute() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + ""; + return content; + } + + public void testNotifiesOfCyclicAssociations() throws Exception + { + Mock mockMonitor = new Mock( InformationProviderBuilderMonitor.class ); + Dom4JGroupBuilder builder = Dom4JGroupBuilder.fromReader( new StringReader( withCyclicAssociation() ), + ( InformationProviderBuilderMonitor ) mockMonitor.proxy() ); + AttributeProvider provider = new AttributeProvider(); + + CyclicAssociationException expected = new CyclicAssociationException( Groups.men(), Groups.canadians() ); + mockMonitor.expects( once() ).method( "cyclicAssociation" ).with( new CyclicAssociationExceptionConstraint( expected ) ); + + builder.buildProvider( provider ); + mockMonitor.verify(); + } + + private String withCyclicAssociation() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + ""; + return content; + } + + public static class CyclicAssociationExceptionConstraint implements Constraint + { + private final CyclicAssociationException m_exception; + + public CyclicAssociationExceptionConstraint( CyclicAssociationException exception ) + { + m_exception = exception; + } + + public boolean eval( Object o ) + { + if (!(o instanceof CyclicAssociationException)) return false; + + CyclicAssociationException other = (CyclicAssociationException) o; + if (!m_exception.getPrincipal().equals( other.getPrincipal()) ) return false; + if (!m_exception.getAttribute().equals( other.getAttribute()) ) return false; + + return true; + } + + public StringBuffer describeTo( StringBuffer buffer ) + { + buffer.append( m_exception.toString() ); + + return buffer; + } + + } +} Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRoleBuilderTest.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRoleBuilderTest.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,104 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.script.xml; + +import org.apache.janus.authentication.CyclicAssociationException; +import org.apache.janus.authentication.MutableInformationProvider; +import org.apache.janus.authentication.attribute.AttributeProvider; +import org.apache.janus.script.InformationProviderBuilderMonitor; +import org.apache.janus.testmodel.Groups; +import org.apache.janus.testmodel.Usernames; +import org.apache.janus.testmodel.Roles; +import org.jmock.Mock; +import org.jmock.MockObjectTestCase; +import org.jmock.core.Constraint; + +import java.io.StringReader; + +public class Dom4JRoleBuilderTest extends MockObjectTestCase +{ + public void testCanAddRolesToUsers() throws Exception + { + Dom4JRoleBuilder builder = Dom4JRoleBuilder.fromReader( new StringReader( userWithTwoRoles() ) ); + + Mock mockProvider = new Mock( MutableInformationProvider.class ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Usernames.joe() ), eq( Roles.user() ) ).will( returnValue( true ) ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Usernames.joe() ), eq( Roles.developer() ) ).will( returnValue( true ) ); + + builder.buildProvider( ( MutableInformationProvider ) mockProvider.proxy() ); + + mockProvider.verify(); + } + + private String userWithTwoRoles() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + ""; + return content; + } + + public void testCanAddRolesToGroups() throws Exception + { + Dom4JRoleBuilder builder = Dom4JRoleBuilder.fromReader( new StringReader( groupsWithRoles() ) ); + + Mock mockProvider = new Mock( MutableInformationProvider.class ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Groups.men() ), eq( Roles.user() ) ).will( returnValue( true ) ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Groups.geeks() ), eq( Roles.developer() ) ).will( returnValue( true ) ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Groups.geeks() ), eq( Roles.tester() ) ).will( returnValue( true ) ); + + builder.buildProvider( ( MutableInformationProvider ) mockProvider.proxy() ); + + mockProvider.verify(); + } + + private String groupsWithRoles() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + ""; + return content; + } + + public void testCanAddRolesToRoles() throws Exception + { + Dom4JRoleBuilder builder = Dom4JRoleBuilder.fromReader( new StringReader( roleWithTwoRoles() ) ); + + Mock mockProvider = new Mock( MutableInformationProvider.class ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Roles.developer() ), eq( Roles.user() ) ).will( returnValue( true ) ); + mockProvider.expects( once() ).method( "addAttribute" ).with( eq( Roles.developer() ), eq( Roles.tester() ) ).will( returnValue( true ) ); + + builder.buildProvider( ( MutableInformationProvider ) mockProvider.proxy() ); + + mockProvider.verify(); + } + + private String roleWithTwoRoles() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + ""; + return content; + } +} Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Roles.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Roles.java Thu Nov 18 06:09:47 2004 @@ -0,0 +1,46 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.janus.testmodel; + +import org.apache.janus.authentication.attribute.RolePrincipal; + +import java.security.Principal; + +public class Roles +{ + public static Principal user() + { + return new RolePrincipal( "user" ); + } + + public static Principal developer() + { + return new RolePrincipal( "developer" ); + } + + public static Principal tester() + { + return new RolePrincipal( "tester" ); + } + + public static Principal manager() + { + return new RolePrincipal( "manager" ); + } + + private Roles() {} +} Modified: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Usernames.java ============================================================================== --- incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Usernames.java (original) +++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Usernames.java Thu Nov 18 06:09:47 2004 @@ -22,6 +22,6 @@ { public static UsernamePrincipal joe() { - return new UsernamePrincipal( "jblow" ); + return new UsernamePrincipal( "joeblow" ); } }