Skip to content

Commit f768265

Browse files
committed
Implement proximity filter ~=
1 parent 7af94ea commit f768265

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2005-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ldap.filter;
18+
19+
/**
20+
* A filter for 'proximity'. The following code:
21+
*
22+
* <pre>
23+
* ProximityFilter filter = new ProximityFilter(&quot;cn&quot;, &quot;Some CN&quot;);
24+
* System.out.println(filter.encode());
25+
* </pre>
26+
*
27+
* would result in:
28+
*
29+
* <pre>
30+
* (cn~=Some CN)
31+
* </pre>
32+
*
33+
* @author Moritz Kobel
34+
*/
35+
public class ProximityFilter extends EqualsFilter {
36+
37+
private static final String PROXIMITY_SIGN = "~=";
38+
39+
public ProximityFilter(String attribute, String value) {
40+
super(attribute, value);
41+
}
42+
43+
/*
44+
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
45+
*/
46+
protected String getCompareString() {
47+
return PROXIMITY_SIGN;
48+
}
49+
50+
}

Diff for: core/src/main/java/org/springframework/ldap/query/ConditionCriteria.java

+11
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,15 @@ public interface ConditionCriteria {
105105
*/
106106
ConditionCriteria not();
107107

108+
/**
109+
* Appends an {@link org.springframework.ldap.filter.ProximityFilter}.
110+
* @param value the value to compare with.
111+
* @return an ContainerCriteria instance that can be used to continue append more
112+
* criteria or as the LdapQuery instance to be used as instance to e.g.
113+
* {@link org.springframework.ldap.core.LdapOperations#search(LdapQuery, org.springframework.ldap.core.ContextMapper)}.
114+
*
115+
* @see org.springframework.ldap.filter.EqualsFilter
116+
*/
117+
ContainerCriteria proximity(String value);
118+
108119
}

Diff for: core/src/main/java/org/springframework/ldap/query/DefaultConditionCriteria.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.ldap.filter.LikeFilter;
2424
import org.springframework.ldap.filter.NotFilter;
2525
import org.springframework.ldap.filter.PresentFilter;
26+
import org.springframework.ldap.filter.ProximityFilter;
2627
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;
2728

2829
/**
@@ -72,6 +73,11 @@ public ContainerCriteria isPresent() {
7273
return appendToParent(new PresentFilter(this.attribute));
7374
}
7475

76+
@Override
77+
public ContainerCriteria proximity(String value) {
78+
return appendToParent(new ProximityFilter(this.attribute, value));
79+
}
80+
7581
private ContainerCriteria appendToParent(Filter filter) {
7682
return this.parent.append(negateIfApplicable(filter));
7783
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2005-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ldap.filter;
18+
19+
import com.gargoylesoftware.base.testing.EqualsTester;
20+
import org.junit.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* @author Moritz Kobel
26+
*/
27+
public class ProximityFilterTests {
28+
29+
@Test
30+
public void testEncode() {
31+
32+
EqualsFilter eqq = new ProximityFilter("foo", "*bar(fie)");
33+
34+
StringBuffer buff = new StringBuffer();
35+
eqq.encode(buff);
36+
37+
assertThat(buff.toString()).isEqualTo("(foo~=\\2abar\\28fie\\29)");
38+
39+
}
40+
41+
@Test
42+
public void testEquals() {
43+
ProximityFilter originalObject = new ProximityFilter("a", "b");
44+
ProximityFilter identicalObject = new ProximityFilter("a", "b");
45+
ProximityFilter differentObject = new ProximityFilter("b", "b");
46+
ProximityFilter subclassObject = new ProximityFilter("a", "b") {
47+
};
48+
49+
new EqualsTester(originalObject, identicalObject, differentObject, subclassObject);
50+
}
51+
52+
}

Diff for: core/src/test/java/org/springframework/ldap/query/LdapQueryBuilderTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ public void buildPresent() {
7373
assertThat(result.filter().encode()).isEqualTo("(cn=*)");
7474
}
7575

76+
@Test
77+
public void buildProximity() {
78+
LdapQuery result = LdapQueryBuilder.query().where("cn").proximity("John Doe");
79+
80+
assertThat(result.filter().encode()).isEqualTo("(cn~=John Doe)");
81+
}
82+
7683
@Test
7784
public void buildHardcodedFilter() {
7885
LdapQuery result = LdapQueryBuilder.query().filter("(cn=Person*)");

0 commit comments

Comments
 (0)