Author: ate
Date: Fri Mar 19 13:09:04 2010
New Revision: 925207
URL: http://svn.apache.org/viewvc?rev=925207&view=rev
Log:
JS2-1136: Cleanup and strengthening the Security Entity/LDAP mapping
- adding new AbstractLDAPSecurityTestCase and an EmbeddedApacheDSTestService to
go with
Added:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java
(with props)
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java
(with props)
Added:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java?rev=925207&view=auto
==============================================================================
---
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java
(added)
+++
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java
Fri Mar 19 13:09:04 2010
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * @version $Id$
+ * @author <a href="mailto:[email protected]">Ate Douma</a>
+ *
+ */
+public abstract class AbstractLDAPSecurityTestCase extends
AbstractSecurityTestcase
+{
+ private static EmbeddedApacheDSTestService ldapService;
+ private boolean ldapTestSetupRun = false;
+
+ public AbstractLDAPSecurityTestCase()
+ {
+ ldapService = new EmbeddedApacheDSTestService(getLdapBaseDN(),
getLdapPort(), getLdapWorkingDir());
+ }
+
+ public void ldapTestSetup() throws Exception
+ {
+ if (ldapService != null)
+ {
+ ldapService.start();
+ }
+ ldapTestSetupRun = true;
+ }
+
+ public void ldapTestTeardown() throws Exception
+ {
+ if (ldapService != null)
+ {
+ ldapService.stop();
+ }
+ ldapService = null;
+ ldapTestSetupRun = true;
+ }
+
+ @Override
+ public void setUp() throws Exception
+ {
+ if (ldapService != null && !ldapTestSetupRun)
+ {
+ if (ldapService.isRunning())
+ {
+ File[] ldifs = getLdifs();
+ for (int i = 0; i < ldifs.length; i++)
+ {
+ ldapService.loadLdif(ldifs[i]);
+ }
+ super.setUp();
+ }
+ }
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ if (ldapService != null && !ldapTestSetupRun)
+ {
+ if (ldapService.isRunning())
+ {
+ super.tearDown();
+ ldapService.revert();
+ }
+ }
+ }
+
+ protected String getLdapBaseDN()
+ {
+ return "o=sevenSeas";
+ }
+
+ protected int getLdapPort()
+ {
+ return 10389;
+ }
+
+ protected File getLdapWorkingDir()
+ {
+ return new File(getBaseDir()+"target/_apacheds");
+ }
+
+ protected File[] getLdifs() throws Exception
+ {
+ return new File[] {new
File(getBaseDir()+"target/test-classes/JETSPEED-INF/directory/config/apacheds/init.ldif")};
+ }
+
+ protected String getBeanDefinitionFilterCategories()
+ {
+ return "security,ldapSecurity,transaction,cache,jdbcDS";
+ }
+
+ /**
+ * Override the location of the test properties by using the jetspeed
properties found in the default package.
+ * Make sure to have your unit test copy in jetspeed properties into the
class path root like:
+ <blockquote><pre>
+ <resource>
+ <path>conf/jetspeed</path>
+ <include>*.properties</include>
+ </resource>
+ </pre></blockquote>
+ */
+ @Override
+ protected Properties getInitProperties()
+ {
+ Properties props = new Properties();
+ try
+ {
+ InputStream is =
this.getClass().getClassLoader().getResourceAsStream("jetspeed.properties");
+ if (is != null)
+ props.load(is);
+ } catch (FileNotFoundException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return props;
+ }
+
+ protected String[] getConfigurations()
+ {
+ String[] confs = super.getConfigurations();
+ List<String> confList = new ArrayList<String>(Arrays.asList(confs));
+ confList.add("security-ldap.xml");
+ return confList.toArray(new String[0]);
+ }
+}
Propchange:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/AbstractLDAPSecurityTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java?rev=925207&view=auto
==============================================================================
---
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java
(added)
+++
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java
Fri Mar 19 13:09:04 2010
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security;
+
+import java.io.File;
+
+import org.apache.directory.server.core.DefaultDirectoryService;
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.entry.ServerEntry;
+import org.apache.directory.server.core.partition.Partition;
+import
org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
+import org.apache.directory.server.ldap.LdapServer;
+import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
+import org.apache.directory.server.protocol.shared.transport.TcpTransport;
+import org.apache.directory.shared.ldap.name.LdapDN;
+
+/**
+ * @version $Id$
+ * @author <a href="mailto:[email protected]">Ate Douma</a>
+ *
+ */
+public class EmbeddedApacheDSTestService
+{
+ private DirectoryService service;
+ private LdapServer server;
+ private boolean running;
+ private File workingDir;
+ private int port;
+ private String baseDN;
+ private long changeLogRevision;
+
+ public EmbeddedApacheDSTestService(String baseDN, int port, File
workingDir)
+ {
+ this.baseDN = baseDN;
+ this.port = port;
+ this.workingDir = workingDir;
+ }
+
+ public boolean isRunning()
+ {
+ return running;
+ }
+
+ public void start() throws Exception
+ {
+ if (workingDir.exists() && !deleteDir(workingDir))
+ {
+ throw new Exception("Cannot delete apacheds working Directory:
"+workingDir.getAbsolutePath());
+ }
+
+ // Initialize the LDAP service
+ service = new DefaultDirectoryService();
+
+ // Disable the ChangeLog system
+ service.getChangeLog().setEnabled( true );
+ service.setDenormalizeOpAttrsEnabled( true );
+
+ Partition partition = new JdbmPartition();
+ partition.setId( "foo" );
+ partition.setSuffix( baseDN );
+ service.addPartition( partition );
+
+ service.setWorkingDirectory(workingDir);
+ server = new LdapServer();
+ server.setDirectoryService(service);
+ server.setTransports(new TcpTransport(port));
+ service.startup();
+ server.start();
+
+ // Inject the sevenSeas root entry if it does not already exist
+ if (!service.getAdminSession().exists(partition.getSuffixDn()))
+ {
+ LdapDN dn = new LdapDN( baseDN );
+ ServerEntry entry = service.newEntry( dn );
+ entry.add( "objectClass", "top", "domain", "extensibleObject" );
+ entry.add( "dc", "foo" );
+ service.getAdminSession().add( entry );
+ }
+ running = true;
+ changeLogRevision = service.getChangeLog().getCurrentRevision();
+ }
+
+ public void stop() throws Exception
+ {
+ server.stop();
+ service.shutdown();
+ server = null;
+ service = null;
+ if (workingDir.exists())
+ {
+ deleteDir(workingDir);
+ }
+ running = false;
+ }
+
+ public void loadLdif(File ldif) throws Exception
+ {
+ LdifFileLoader loader = new LdifFileLoader(service.getAdminSession(),
ldif.getAbsolutePath());
+ loader.execute();
+ }
+
+ public void revert() throws Exception
+ {
+ if (changeLogRevision < service.getChangeLog().getCurrentRevision())
+ {
+ changeLogRevision = service.revert(changeLogRevision);
+ }
+ }
+
+ private static boolean deleteDir(File dir)
+ {
+ if (dir.isDirectory())
+ {
+ String[] children = dir.list();
+ for (int i=0; i < children.length; i++)
+ {
+ if (!deleteDir(new File(dir, children[i])))
+ {
+ return false;
+ }
+ }
+ }
+ return dir.delete();
+ }
+}
Propchange:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/EmbeddedApacheDSTestService.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]