directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ersi...@apache.org
Subject svn commit: r415257 - in /directory/trunks/apacheds/server-unit/src/test: java/org/apache/directory/server/ resources/org/ resources/org/apache/ resources/org/apache/directory/ resources/org/apache/directory/server/
Date Mon, 19 Jun 2006 07:19:30 GMT
Author: ersiner
Date: Mon Jun 19 00:19:29 2006
New Revision: 415257

URL: http://svn.apache.org/viewvc?rev=415257&view=rev
Log:
Tested SP + Trigger Service over the wire (with SUN's LCF) and it works nice.
server-unit and ServerTriggerServiceTest in particular needs some polishing.

Added:
    directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/AbstractServerTriggerServiceTest.java
    directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java
    directory/trunks/apacheds/server-unit/src/test/resources/org/
    directory/trunks/apacheds/server-unit/src/test/resources/org/apache/
    directory/trunks/apacheds/server-unit/src/test/resources/org/apache/directory/
    directory/trunks/apacheds/server-unit/src/test/resources/org/apache/directory/server/
    directory/trunks/apacheds/server-unit/src/test/resources/org/apache/directory/server/BackupUtilities.java

Added: directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/AbstractServerTriggerServiceTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/AbstractServerTriggerServiceTest.java?rev=415257&view=auto
==============================================================================
--- directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/AbstractServerTriggerServiceTest.java
(added)
+++ directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/AbstractServerTriggerServiceTest.java
Mon Jun 19 00:19:29 2006
@@ -0,0 +1,180 @@
+/*
+ *   Copyright 2006 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.directory.server;
+
+
+import java.util.Hashtable;
+
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.ldap.LdapContext;
+
+import org.apache.directory.server.core.partition.DirectoryPartitionNexus;
+import org.apache.directory.server.core.subtree.SubentryService;
+import org.apache.directory.server.unit.AbstractServerTest;
+
+
+/**
+ * A base class used for trigger service tests.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev:$
+ */
+public abstract class AbstractServerTriggerServiceTest extends AbstractServerTest
+{
+    /**
+     * Gets a context at ou=system as the admin user.
+     *
+     * @return the admin context at ou=system
+     * @throws NamingException if there are problems creating the context
+     */
+    public DirContext getContextAsAdmin() throws NamingException
+    {
+        return getContextAsAdmin( DirectoryPartitionNexus.SYSTEM_PARTITION_SUFFIX );
+    }
+
+
+    /**
+     * Gets a context at some dn within the directory as the admin user.
+     * Should be a dn of an entry under ou=system since no other partitions
+     * are enabled.
+     *
+     * @param dn the DN of the context to get
+     * @return the context for the DN as the admin user
+     * @throws NamingException if is a problem initializing or getting the context
+     */
+    public DirContext getContextAsAdmin( String dn ) throws NamingException
+    {
+        Hashtable env = ( Hashtable ) sysRoot.getEnvironment().clone();
+        env.put( DirContext.PROVIDER_URL, dn );
+        env.put( DirContext.SECURITY_AUTHENTICATION, "simple" );
+        env.put( DirContext.SECURITY_PRINCIPAL, DirectoryPartitionNexus.ADMIN_PRINCIPAL );
+        env.put( DirContext.SECURITY_CREDENTIALS, "secret" );
+        return new InitialDirContext( env );
+    }
+
+
+    /**
+     * Gets the context at ou=system as a specific user.
+     *
+     * @param user the DN of the user to get the context as
+     * @param password the password of the user
+     * @return the context as the user
+     * @throws NamingException if the user does not exist or authx fails
+     */
+    public DirContext getContextAs( Name user, String password ) throws NamingException
+    {
+        return getContextAs( user, password, DirectoryPartitionNexus.SYSTEM_PARTITION_SUFFIX
);
+    }
+
+
+    /**
+     * Gets the context at any DN under ou=system as a specific user.
+     *
+     * @param user the DN of the user to get the context as
+     * @param password the password of the user
+     * @param dn the distinguished name of the entry to get the context for
+     * @return the context representing the entry at the dn as a specific user
+     * @throws NamingException if the does not exist or authx fails
+     */
+    public DirContext getContextAs( Name user, String password, String dn ) throws NamingException
+    {
+        Hashtable env = ( Hashtable ) sysRoot.getEnvironment().clone();
+        env.put( DirContext.PROVIDER_URL, dn );
+        env.put( DirContext.SECURITY_AUTHENTICATION, "simple" );
+        env.put( DirContext.SECURITY_PRINCIPAL, user.toString() );
+        env.put( DirContext.SECURITY_CREDENTIALS, password );
+        return new InitialDirContext( env );
+    }
+
+
+    public void deleteTriggerSubentry( LdapContext adminCtx, String cn ) throws NamingException
+    {
+        adminCtx.destroySubcontext( "cn=" + cn );
+    }
+
+
+    /**
+     * Creates an trigger subentry under ou=system whose subtree covers
+     * the entire naming context.
+     *
+     * @param cn the common name and rdn for the subentry
+     * @param triggerSpec the prescriptive trigger specification attribute value
+     * @throws NamingException if there is a problem creating the subentry
+     */
+    public void createTriggerSubentry( LdapContext adminCtx, String cn, String triggerSpec
) throws NamingException
+    {
+        createTiggerSubentry( adminCtx, cn, "{}", triggerSpec );
+    }
+
+
+    /**
+     * Creates an access control subentry under ou=system whose coverage is
+     * determined by the given subtree specification.
+     *
+     * @param cn the common name and rdn for the subentry
+     * @param subtree the subtreeSpecification for the subentry
+     * @param triggerSpec the prescriptive Trigger Specification attribute value
+     * @throws NamingException if there is a problem creating the subentry
+     */
+    public void createTiggerSubentry( LdapContext adminCtx, String cn, String subtree, String
triggerSpec ) throws NamingException
+    {
+        // modify ou=system to be an AP for an Trigger AA if it is not already
+        Attributes ap = adminCtx.getAttributes( "", new String[]
+            { "administrativeRole" } );
+        Attribute administrativeRole = ap.get( "administrativeRole" );
+        if ( administrativeRole == null || !administrativeRole.contains( SubentryService.TRIGGER_AREA
) )
+        {
+            Attributes changes = new BasicAttributes( "administrativeRole", SubentryService.TRIGGER_AREA,
true );
+            adminCtx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
+        }
+
+        // now add the Trigger subentry below ou=system
+        Attributes subentry = new BasicAttributes( "cn", cn, true );
+        Attribute objectClass = new BasicAttribute( "objectClass" );
+        subentry.put( objectClass );
+        objectClass.add( "top" );
+        objectClass.add( "subentry" );
+        objectClass.add( "triggerSubentry" );
+        subentry.put( "subtreeSpecification", subtree );
+        subentry.put( "prescriptiveTrigger", triggerSpec );
+        adminCtx.createSubcontext( "cn=" + cn, subentry );
+    }
+
+
+    /**
+     * Adds and entryTrigger attribute to an entry specified by a relative name
+     * with respect to ou=system
+     *
+     * @param rdn a name relative to ou=system
+     * @param triggerSpec the entryTrigger attribute value
+     * @throws NamingException if there is a problem adding the attribute
+     */
+    public void addEntryTrigger( LdapContext adminCtx, Name rdn, String triggerSpec ) throws
NamingException
+    {
+        Attributes changes = new BasicAttributes( "entryTrigger", triggerSpec, true );
+        adminCtx.modifyAttributes( rdn, DirContext.ADD_ATTRIBUTE, changes );
+    }
+
+}

Added: directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java?rev=415257&view=auto
==============================================================================
--- directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java
(added)
+++ directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java
Mon Jun 19 00:19:29 2006
@@ -0,0 +1,141 @@
+/*
+ *   Copyright 2006 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.directory.server;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+import org.apache.directory.server.ldap.support.extended.StoredProcedureExtendedOperationHandler;
+
+
+/**
+ * Integration tests for TriggerService.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev:$
+ */
+public class DefaultServerTriggerServiceTest extends AbstractServerTriggerServiceTest
+{
+    private LdapContext ctx;
+    
+    public void setUp() throws Exception
+    {
+        Set handlers = new HashSet( super.configuration.getExtendedOperationHandlers() );
+        handlers.add( new StoredProcedureExtendedOperationHandler() );
+        super.configuration.setExtendedOperationHandlers( handlers );
+        
+        super.setUp();
+
+        Hashtable env = new Hashtable();
+        env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
+        env.put( "java.naming.provider.url", "ldap://localhost:" + port + "/ou=system" );
+        env.put( "java.naming.security.principal", "uid=admin,ou=system" );
+        env.put( "java.naming.security.credentials", "secret" );
+        env.put( "java.naming.security.authentication", "simple" );
+        ctx = new InitialLdapContext( env, null );
+    }
+    
+    public void tearDown() throws Exception
+    {
+        ctx.close();
+        ctx = null;
+        
+        super.tearDown();
+    }
+    
+    private void loadStoredProcedureUnit( LdapContext ctx, String fullClassName ) throws
NamingException
+    {
+        int lastDot = fullClassName.lastIndexOf( '.' );
+        String classFileName = fullClassName.substring( lastDot + 1 ) + ".class";
+        
+        URL url = getClass().getResource( classFileName );
+        InputStream in = getClass().getResourceAsStream( classFileName );
+        File file = new File( url.getFile() );
+        int size = ( int ) file.length();
+        byte[] buf = new byte[size];
+        try
+        {
+            in.read( buf );
+            in.close();
+        }
+        catch ( IOException e )
+        {
+            NamingException ne = new NamingException();
+            ne.setRootCause( e );
+            throw ne;
+        }
+        
+        Attributes attributes = new BasicAttributes( "objectClass", "top", true );
+        attributes.get( "objectClass" ).add( "javaClass" );
+        attributes.put( "fullyQualifiedClassName", fullClassName );
+        attributes.put( "byteCode", buf );
+        
+        ctx.createSubcontext( "fullyQualifiedClassName=" + fullClassName, attributes );
+    }
+    
+    public void testAfterDeleteBackupDeletedEntry() throws NamingException
+    {
+        // Load the stored procedure unit which has the stored procedure to be triggered.
+        loadStoredProcedureUnit( ctx, BackupUtilities.class.getName() );
+        
+        // Create a container for backing up deleted entries.
+        Attributes backupContext = new BasicAttributes( "ou", "backupContext", true );
+        Attribute objectClass = new BasicAttribute( "objectClass" );
+        backupContext.put( objectClass );
+        objectClass.add( "top" );
+        objectClass.add( "organizationalUnit" );
+        sysRoot.createSubcontext( "ou=backupContext", backupContext );
+        
+        // Create the Triger Specification within a Trigger Subentry.
+        createTriggerSubentry( ctx, "triggerSubentry1",
+            "AFTER delete CALL \"" + BackupUtilities.class.getName() + ".backupDeleted\"
( $name, $operationPrincipal, $deletedEntry )" );
+        
+        // Create a test entry which is selected by the Trigger Subentry.
+        Attributes testEntry = new BasicAttributes( "ou", "testou", true );
+        objectClass = new BasicAttribute( "objectClass" );
+        testEntry.put( objectClass );
+        objectClass.add( "top" );
+        objectClass.add( "organizationalUnit" );
+        sysRoot.createSubcontext( "ou=testou", testEntry );
+        
+        // Delete the test entry in order to fire the Trigger.
+        sysRoot.destroySubcontext( "ou=testou" );
+        
+        // ------------------------------------------
+        // The trigger should be fired at this point.
+        // ------------------------------------------
+        
+        // Check if the Trigger really worked (backed up the deleted entry).
+        assertNotNull( sysRoot.lookup( "ou=testou,ou=backupContext" ) );
+    }
+    
+}

Added: directory/trunks/apacheds/server-unit/src/test/resources/org/apache/directory/server/BackupUtilities.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/resources/org/apache/directory/server/BackupUtilities.java?rev=415257&view=auto
==============================================================================
--- directory/trunks/apacheds/server-unit/src/test/resources/org/apache/directory/server/BackupUtilities.java
(added)
+++ directory/trunks/apacheds/server-unit/src/test/resources/org/apache/directory/server/BackupUtilities.java
Mon Jun 19 00:19:29 2006
@@ -0,0 +1,19 @@
+package org.apache.directory.server;
+
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.ldap.LdapContext;
+
+
+public class BackupUtilities
+{
+    public static void backupDeleted( LdapContext ctx, Name deletedEntryName, Name operationPrincipal,
Attributes deletedEntry ) throws NamingException
+    {
+        System.out.println( "User \"" + operationPrincipal + "\" has deleted entry \"" +
deletedEntryName + "\"" );
+        System.out.println( "Entry content was: " + deletedEntry );
+        LdapContext backupCtx = ( LdapContext ) ctx.lookup( "ou=backupContext,ou=system"
);
+        backupCtx.createSubcontext( deletedEntryName.get( 1 ), deletedEntry );
+        System.out.println( "Backed up deleted entry to \"" + ( ( LdapContext ) backupCtx.lookup(
deletedEntryName.get( 1 ) ) ).getNameInNamespace() + "\"" );
+    }
+}



Mime
View raw message