001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.driver.ldap;
017:
018: import scriptella.AbstractTestCase;
019: import scriptella.spi.MockParametersCallbacks;
020: import scriptella.spi.ParametersCallback;
021: import scriptella.spi.QueryCallback;
022: import scriptella.util.ProxyAdapter;
023:
024: import javax.naming.NamingEnumeration;
025: import javax.naming.directory.BasicAttributes;
026: import javax.naming.directory.SearchResult;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: /**
032: * Tests for {@link SearchFilterQuery}.
033: *
034: * @author Fyodor Kupolov
035: * @version 1.0
036: */
037: public class SearchFilterQueryTest extends AbstractTestCase {
038: private boolean closed;
039: private int rows;
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043: closed = false;
044: rows = 0;
045: }
046:
047: public void testExecute() {
048: QueryCallback qc = new QueryCallback() {
049: public void processRow(final ParametersCallback parameters) {
050: assertEquals("uid" + rows, parameters
051: .getParameter("uid"));
052: assertEquals("search" + rows, parameters
053: .getParameter("cn"));
054: assertEquals("cn=search" + rows
055: + ", ou=ldap, dc=scriptella", parameters
056: .getParameter("dn"));
057: rows++;
058: }
059: };
060:
061: SearchFilterQuery q = new SearchFilterQuery(null,
062: MockParametersCallbacks.UNSUPPORTED, qc) {
063: protected NamingEnumeration<SearchResult> query(
064: final LdapConnection connection, final String filter) {
065: List<SearchResult> res = new ArrayList<SearchResult>();
066: for (int i = 0; i < 2; i++) {
067: BasicAttributes a = new BasicAttributes("uid",
068: "uid" + i);
069: a.put("cn", "search" + i);
070: SearchResult sr = new SearchResult("cn=search" + i
071: + ", ou=ldap, dc=scriptella", null, a);
072: sr.setNameInNamespace(sr.getName());
073: res.add(sr);
074: }
075: final Iterator<SearchResult> it = res.iterator();
076: return new NamingEnumeration<SearchResult>() {
077:
078: public SearchResult next() {
079: return it.next();
080: }
081:
082: public boolean hasMore() {
083: return it.hasNext();
084: }
085:
086: public void close() {
087: closed = true;
088: }
089:
090: public boolean hasMoreElements() {
091: return hasMore();
092: }
093:
094: public SearchResult nextElement() {
095: return next();
096: }
097: };
098: }
099: };
100: q.execute("filter");//in this test case filter doesn't matter
101: assertTrue("Naming enumeration must be closed after iteration",
102: closed);
103: assertEquals(2, rows);
104: }
105:
106: /**
107: * Tests if variables are substituted in a passed filter.
108: */
109: public void testExecuteSubstitution() {
110: SearchFilterQuery q = new SearchFilterQuery(null,
111: MockParametersCallbacks.SIMPLE, null) {
112: protected NamingEnumeration<SearchResult> query(
113: final LdapConnection connection, final String filter) {
114: assertEquals("test *filter* *a**b*", filter);
115: return new ProxyAdapter<NamingEnumeration<SearchResult>>(
116: NamingEnumeration.class) {
117: public boolean hasMoreElements() {
118: return false;
119: }
120: }.getProxy();
121:
122: }
123: };
124: q.execute("test $filter ${a+b}");
125: }
126: }
|