LDAP-267: Minor tweaks to XML namespace support more tests and reference documentation.

This commit is contained in:
Mattias Hellborg Arthursson
2013-10-08 17:02:40 +02:00
parent 795701fd58
commit a16646ca2c
24 changed files with 1034 additions and 523 deletions

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* 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.springframework.ldap.config;
import org.springframework.ldap.core.AuthenticationSource;
/**
* @author Mattias Hellborg Arthursson
*/
public class DummyAuthenticationSource implements AuthenticationSource {
@Override
public String getPrincipal() {
throw new UnsupportedOperationException();
}
@Override
public String getCredentials() {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* 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.springframework.ldap.config;
import org.springframework.ldap.core.support.DirContextAuthenticationStrategy;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import java.util.Hashtable;
/**
* @author Mattias Hellborg Arthursson
*/
public class DummyAuthenticationStrategy implements DirContextAuthenticationStrategy {
@Override
public void setupEnvironment(Hashtable<String, Object> env, String userDn, String password) throws NamingException {
throw new UnsupportedOperationException();
}
@Override
public DirContext processContextAfterCreation(DirContext ctx, String userDn, String password) throws NamingException {
throw new UnsupportedOperationException();
}
}

View File

@@ -18,10 +18,13 @@ package org.springframework.ldap.config;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.core.AuthenticationSource;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.DirContextAuthenticationStrategy;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.pool.factory.PoolingContextSource;
import org.springframework.ldap.pool.validation.DefaultDirContextValidator;
import org.springframework.ldap.support.LdapUtils;
@@ -32,7 +35,10 @@ import org.springframework.ldap.transaction.compensating.support.DefaultTempEntr
import org.springframework.ldap.transaction.compensating.support.DifferentSubtreeTempEntryRenamingStrategy;
import org.springframework.transaction.PlatformTransactionManager;
import javax.naming.CannotProceedException;
import javax.naming.CommunicationException;
import javax.naming.directory.SearchControls;
import java.util.Set;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
@@ -75,6 +81,48 @@ public class LdapTemplateNamespaceHandlerTest {
assertEquals(SearchControls.SUBTREE_SCOPE, getInternalState(ldapTemplate, "defaultSearchScope"));
}
@Test
public void verifyThatAnonymousReadOnlyContextWillNotBeWrappedInProxy() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-anonymous-read-only.xml");
ContextSource contextSource = ctx.getBean(ContextSource.class);
assertNotNull(contextSource);
assertTrue(contextSource instanceof LdapContextSource);
assertEquals(Boolean.TRUE, getInternalState(contextSource, "anonymousReadOnly"));
}
@Test(expected = BeansException.class)
public void verifyThatAnonymousReadOnlyAndTransactionalThrowsException() {
new ClassPathXmlApplicationContext("/ldap-namespace-config-anonymous-read-only-and-transactions.xml");
}
@Test(expected = BeansException.class)
public void verifyThatMissingUsernameThrowsException() {
new ClassPathXmlApplicationContext("/ldap-namespace-config-missing-username.xml");
}
@Test(expected = BeansException.class)
public void verifyThatMissingPasswordThrowsException() {
new ClassPathXmlApplicationContext("/ldap-namespace-config-missing-password.xml");
}
@Test
public void verifyReferences() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-references.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
AuthenticationSource authenticationSource = ctx.getBean(AuthenticationSource.class);
DirContextAuthenticationStrategy authenticationStrategy = ctx.getBean(DirContextAuthenticationStrategy.class);
Object baseEnv = ctx.getBean("baseEnvProps");
assertNotNull(outerContextSource);
assertTrue(outerContextSource instanceof TransactionAwareContextSourceProxy);
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertSame(authenticationSource, getInternalState(contextSource, "authenticationSource"));
assertSame(authenticationStrategy, getInternalState(contextSource, "authenticationStrategy"));
assertEquals(baseEnv, getInternalState(contextSource, "baseEnv"));
}
@Test
public void verifyParseWithCustomValues() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-values.xml");
@@ -93,7 +141,7 @@ public class LdapTemplateNamespaceHandlerTest {
assertEquals("apassword", getInternalState(contextSource, "password"));
assertArrayEquals(new String[]{"ldap://localhost:389"}, (Object[]) getInternalState(contextSource, "urls"));
assertEquals(Boolean.TRUE, getInternalState(contextSource, "pooled"));
assertEquals(Boolean.TRUE, getInternalState(contextSource, "anonymousReadOnly"));
assertEquals(Boolean.FALSE, getInternalState(contextSource, "anonymousReadOnly"));
assertEquals("follow", getInternalState(contextSource, "referral"));
assertSame(authenticationStrategy, getInternalState(contextSource, "authenticationStrategy"));
@@ -164,6 +212,7 @@ public class LdapTemplateNamespaceHandlerTest {
}
@Test
@SuppressWarnings("unchecked")
public void verifyParsePoolingDefaults() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-defaults.xml");
@@ -178,6 +227,10 @@ public class LdapTemplateNamespaceHandlerTest {
Object objectFactory = getInternalState(pooledContextSource, "dirContextPoolableObjectFactory");
assertNotNull(getInternalState(objectFactory, "contextSource"));
assertNull(getInternalState(objectFactory, "dirContextValidator"));
Set<Class<? extends Throwable>> nonTransientExceptions =
(Set<Class<? extends Throwable>>) getInternalState(objectFactory, "nonTransientExceptions");
assertEquals(1, nonTransientExceptions.size());
assertTrue(nonTransientExceptions.contains(CommunicationException.class));
GenericKeyedObjectPool objectPool = (GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
assertEquals(8, objectPool.getMaxActive());
@@ -208,6 +261,7 @@ public class LdapTemplateNamespaceHandlerTest {
}
@Test
@SuppressWarnings("unchecked")
public void verifyParsePoolingValidationSet() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-test-specified.xml");
@@ -230,5 +284,16 @@ public class LdapTemplateNamespaceHandlerTest {
SearchControls searchControls = ctx.getBean(SearchControls.class);
assertEquals("objectclass=person", validator.getFilter());
assertSame(searchControls, validator.getSearchControls());
Set<Class<? extends Throwable>> nonTransientExceptions =
(Set<Class<? extends Throwable>>) getInternalState(objectFactory, "nonTransientExceptions");
assertEquals(2, nonTransientExceptions.size());
assertTrue(nonTransientExceptions.contains(CommunicationException.class));
assertTrue(nonTransientExceptions.contains(CannotProceedException.class));
}
@Test(expected = BeansException.class)
public void verifyParseWithPoolingAndNativePoolingWillFail() {
new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-with-native.xml");
}
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source
password="apassword"
url="ldap://localhost:389"
username="uid=admin"
anonymous-read-only="true" />
<!-- This is not valid together with anonymous-read-only -->
<ldap:transaction-manager>
<ldap:default-renaming-strategy />
</ldap:transaction-manager>
<ldap:ldap-template />
</beans>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source
password="apassword"
url="ldap://localhost:389"
username="uid=admin"
anonymous-read-only="true" />
<ldap:ldap-template />
</beans>

View File

@@ -2,8 +2,8 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source password="apassword" url="ldap://localhost:389" username="uid=admin"/>
<ldap:ldap-template />

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source url="ldap://localhost:389" username="uid=admin"/>
<ldap:ldap-template />
</beans>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source password="apassword" url="ldap://localhost:389" />
</beans>

View File

@@ -15,7 +15,8 @@
tests-per-eviction-run="22"
validation-query-base="ou=test"
validation-query-filter="objectclass=person"
validation-query-search-controls-ref="searchControls"/>
validation-query-search-controls-ref="searchControls"
non-transient-exceptions="javax.naming.CannotProceedException,javax.naming.CommunicationException" />
</ldap:context-source>
<bean class="javax.naming.directory.SearchControls" id="searchControls" />

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<!--
The below is invalid, since native pooling is not supported together with Spring LDAP pooling.
-->
<ldap:context-source
password="apassword" url="ldap://localhost:389" username="uid=admin"
native-pooling="true">
<ldap:pooling />
</ldap:context-source>
<ldap:ldap-template />
</beans>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<ldap:context-source url="ldap://localhost:389"
authentication-source-ref="authenticationSource"
authentication-strategy-ref="authenticationStrategy"
base-env-props-ref="baseEnvProps" />
<bean class="org.springframework.ldap.config.DummyAuthenticationSource" id="authenticationSource" />
<bean class="org.springframework.ldap.config.DummyAuthenticationStrategy" id="authenticationStrategy" />
<util:map id="baseEnvProps">
<entry key="dummy" value="dummyValue" />
</util:map>
<ldap:ldap-template />
</beans>

View File

@@ -11,7 +11,6 @@
url="ldap://localhost:389"
username="uid=admin"
base="dc=261consulting,dc=com"
anonymous-read-only="true"
authentication-strategy-ref="authenticationStrategy"
native-pooling="true"
referral="follow" />