Fix for LDAP-152: Added filter classes to test for attribute precense, received from Jordan Hein.

This commit is contained in:
Mattias Arthursson
2009-01-03 10:53:10 +00:00
parent bf9ba15079
commit e3b178f5e1
4 changed files with 200 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2005-2008 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.filter;
import org.springframework.ldap.filter.AbstractFilter;
/**
* A convenience class that combines {@code NOT} behavior with {@code present}
* behavior to allow the user to check for the non-existence of a attribute. For
* an attribute to be {@code NOT present} it must not have any values set. To
* filter on attributes at are {@code present} use the {@link PresentFilter}.
*
* <pre>
* NotPresentFilter filter = new NotPresentFilter(&quot;foo*&quot;);
* System.out.println(filter.encode());
* </pre>
*
* would result in:
*
* <pre>
* (!(foo=*))
* </pre>
* @author Jordan Hein
*/
public class NotPresentFilter extends AbstractFilter {
private String attribute;
/**
* Creates a new instance of a not present filter for a particular
* attribute.
*
* @param attribute the attribute expected to be not-present (ie, unset, or
* null).
*/
public NotPresentFilter(String attribute) {
this.attribute = attribute;
}
public StringBuffer encode(StringBuffer buff) {
buff.append("(!(");
buff.append(attribute);
buff.append("=*))");
return buff;
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2005-2008 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.filter;
/**
* Filter that allows the user to check for the existence of a attribute. For an
* attribute to be {@code 'present'} it must contain a value. Attributes that do
* not contain a value are {@code 'NOT present'}. To filter on attributes that
* are {@code 'NOT present'} use the {@link NotPresentFilter} or use this filter
* in combination with a {@link NotFilter} .
*
* <pre>
* PresentFilter filter = new PresentFilter(&quot;foo*&quot;);
* System.out.println(filter.encode());
* </pre>
*
* would result in:
*
* <pre>
* (foo=*)
* </pre>
* @author Jordan Hein
*/
public class PresentFilter extends AbstractFilter {
private String attribute;
/**
* Creates a new instance of a present filter for a particular attribute.
*
* @param attribute the attribute expected to be present (ie, contains a
* value).
*/
public PresentFilter(String attribute) {
this.attribute = attribute;
}
public StringBuffer encode(StringBuffer buff) {
buff.append("(");
buff.append(attribute);
buff.append("=*)");
return buff;
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2005-2008 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.filter;
import junit.framework.TestCase;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.NotFilter;
public class NotPresentFilterTest extends TestCase {
public void testNotPresentFilter() {
NotPresentFilter presentFilter = new NotPresentFilter("foo");
assertEquals("(!(foo=*))", presentFilter.encode());
NotFilter notFilter = new NotFilter(new NotPresentFilter("foo"));
assertEquals("(!(!(foo=*)))", notFilter.encode());
AndFilter andFilter = new AndFilter();
andFilter.and(new NotPresentFilter("foo"));
andFilter.and(new NotPresentFilter("bar"));
assertEquals("(&(!(foo=*))(!(bar=*)))", andFilter.encode());
andFilter = new AndFilter();
andFilter.and(new NotPresentFilter("foo"));
andFilter.and(new NotFilter(new NotPresentFilter("bar")));
assertEquals("(&(!(foo=*))(!(!(bar=*))))", andFilter.encode());
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2005-2008 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.filter;
import junit.framework.TestCase;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.NotFilter;
public class PresentFilterTest extends TestCase {
public void testPresentFilter() {
PresentFilter presentFilter = new PresentFilter("foo");
assertEquals("(foo=*)", presentFilter.encode());
NotFilter notFilter = new NotFilter(new PresentFilter("foo"));
assertEquals("(!(foo=*))", notFilter.encode());
AndFilter andFilter = new AndFilter();
andFilter.and(new PresentFilter("foo"));
andFilter.and(new PresentFilter("bar"));
assertEquals("(&(foo=*)(bar=*))", andFilter.encode());
andFilter = new AndFilter();
andFilter.and(new PresentFilter("foo"));
andFilter.and(new NotFilter(new PresentFilter("bar")));
assertEquals("(&(foo=*)(!(bar=*)))", andFilter.encode());
}
}