001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap.parser;
019:
020: import java.util.List;
021:
022: import org.apache.harmony.jndi.internal.parser.AttributeTypeAndValuePair;
023: import org.apache.harmony.jndi.provider.ldap.Filter;
024: import org.apache.harmony.jndi.provider.ldap.Filter.SubstringFilter;
025: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
026: import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
027:
028: import junit.framework.TestCase;
029:
030: public class FilterParserTest extends TestCase {
031:
032: private static final String TEST_FILE = "/parser/parser.filter.test";
033:
034: private FilterParser parser;
035:
036: private Filter filter;
037:
038: /**
039: * test whether correct filters can be accepted by the parser
040: *
041: * @throws Exception
042: */
043: public void test_grammar() throws Exception {
044: parser = new FilterParser(getClass().getClassLoader()
045: .getResourceAsStream(TEST_FILE));
046: parser.test();
047: }
048:
049: public void test_parse() throws Exception {
050: parser = new FilterParser("(cn=Babs Jensen)");
051: filter = parser.parse();
052: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
053: assertAttributeValueAssertion("cn", "Babs Jensen", filter
054: .getValue());
055:
056: parser = new FilterParser("(!(cn=Tim Howes))");
057: filter = parser.parse();
058: assertEquals(Filter.NOT_FILTER, filter.getType());
059: assertTrue(filter.getValue() instanceof Filter);
060: filter = (Filter) filter.getValue();
061: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
062: assertAttributeValueAssertion("cn", "Tim Howes", filter
063: .getValue());
064:
065: parser = new FilterParser(
066: "(&(objectClass=Person)(|(sn=Jensen)(cn=Babs J*)))");
067: filter = parser.parse();
068: assertEquals(Filter.AND_FILTER, filter.getType());
069: assertFalse(filter.isLeaf());
070: List<Filter> children = filter.getChildren();
071: assertEquals(2, children.size());
072: assertEquals(Filter.EQUALITY_MATCH_FILTER, children.get(0)
073: .getType());
074: assertAttributeValueAssertion("objectClass", "Person", children
075: .get(0).getValue());
076: filter = children.get(1);
077: assertEquals(Filter.OR_FILTER, filter.getType());
078: assertFalse(filter.isLeaf());
079: children = filter.getChildren();
080: assertEquals(2, children.size());
081: assertAttributeValueAssertion("sn", "Jensen", children.get(0)
082: .getValue());
083: filter = children.get(1);
084: assertEquals(Filter.SUBSTRINGS_FILTER, filter.getType());
085: assertEquals(SubstringFilter.class, filter.getValue()
086: .getClass());
087: SubstringFilter sub = (SubstringFilter) filter.getValue();
088: assertEquals("cn", sub.getType());
089: // TODO: not sure whether '*' should be part of 'any'
090: // assertEquals(1, sub.getSubstrings().size());
091: ChosenValue chosen = sub.getSubstrings().get(0);
092: assertEquals("Babs J", chosen.getValue());
093: assertEquals(0, chosen.getIndex());
094: }
095:
096: public void test_parse_special_char() throws Exception {
097: parser = new FilterParser("(cn=\\2Atest)");
098: filter = parser.parse();
099: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
100: assertAttributeValueAssertion("cn", "*test", filter.getValue());
101: }
102:
103: public void test_parse_argument() throws Exception {
104: parser = new FilterParser("(cn={0})");
105: parser.setArgs(new Object[] { "value" });
106: filter = parser.parse();
107: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
108: assertAttributeValueAssertion("cn", "value", filter.getValue());
109:
110: parser = new FilterParser("(cn=start{0}end)");
111: parser.setArgs(new Object[] { "value" });
112: filter = parser.parse();
113: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
114: assertAttributeValueAssertion("cn", "startvalueend", filter
115: .getValue());
116:
117: parser = new FilterParser("({2}cn{1}=test{0})");
118: parser.setArgs(new Object[] { "value0", "value1", "value2" });
119: filter = parser.parse();
120: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
121: assertAttributeValueAssertion("value2cnvalue1", "testvalue0",
122: filter.getValue());
123: }
124:
125: private void assertAttributeValueAssertion(String attrType,
126: String attrValue, Object filterValue) {
127: assertTrue(filterValue instanceof AttributeTypeAndValuePair);
128: AttributeTypeAndValuePair pair = (AttributeTypeAndValuePair) filterValue;
129: assertEquals(attrType, pair.getType());
130: assertEquals(attrValue, pair.getValue());
131: }
132: }
|