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 javax.naming.directory.SearchControls;
021:
022: import junit.framework.TestCase;
023:
024: public class LdapUrlParserTest extends TestCase {
025: private static final String TEST_FILE = "/parser/parser.url.test";
026:
027: private LdapUrlParser parser;
028:
029: private SearchControls controls;
030:
031: private org.apache.harmony.jndi.provider.ldap.Filter filter;
032:
033: public void test_grammar() throws Exception {
034: parser = new LdapUrlParser(getClass().getClassLoader()
035: .getResourceAsStream(TEST_FILE));
036: parser.test();
037: }
038:
039: public void test_parseURL() throws Exception {
040: parser = new LdapUrlParser("ldap://192.168.1.3:666");
041: parser.parseURL();
042: // default dn is empty
043: assertEquals("", parser.getBaseObject());
044: assertFalse(parser.hasFilter());
045: assertNull(parser.getFilter());
046: assertFalse(parser.hasAttributes());
047: assertFalse(parser.hasScope());
048: assertFalse(parser.hasExtensions());
049: assertNull(parser.getControls());
050: assertEquals("192.168.1.3", parser.getHost());
051: assertEquals(666, parser.getPort());
052:
053: parser = new LdapUrlParser(
054: "ldap://192.168.1.3/o=University,c=US");
055: parser.parseURL();
056: assertEquals("o=University,c=US", parser.getBaseObject());
057: assertFalse(parser.hasFilter());
058: assertNull(parser.getFilter());
059: assertFalse(parser.hasAttributes());
060: assertFalse(parser.hasScope());
061: assertFalse(parser.hasExtensions());
062: assertNull(parser.getControls());
063: assertEquals("192.168.1.3", parser.getHost());
064: // default port is 389
065: assertEquals(389, parser.getPort());
066:
067: parser = new LdapUrlParser(
068: "ldaps://192.168.1.3/o=University,c=US?postalAddress");
069: parser.parseURL();
070: assertEquals("o=University,c=US", parser.getBaseObject());
071: assertEquals("192.168.1.3", parser.getHost());
072: assertNotNull(parser.getControls());
073: assertTrue(parser.hasAttributes());
074: assertFalse(parser.hasScope());
075: assertFalse(parser.hasExtensions());
076: controls = parser.getControls();
077: assertEquals(SearchControls.ONELEVEL_SCOPE, controls
078: .getSearchScope());
079: assertEquals(1, controls.getReturningAttributes().length);
080: assertEquals("postalAddress",
081: controls.getReturningAttributes()[0]);
082: assertNull(parser.getFilter());
083: // default ssl port is 636
084: assertEquals(636, parser.getPort());
085:
086: parser = new LdapUrlParser(
087: "ldap://192.168.1.3/o=University,c=US?postalAddress,o");
088: parser.parseURL();
089: assertEquals("o=University,c=US", parser.getBaseObject());
090: assertEquals("192.168.1.3", parser.getHost());
091: assertTrue(parser.hasAttributes());
092: assertNotNull(parser.getControls());
093: assertFalse(parser.hasScope());
094: assertFalse(parser.hasExtensions());
095: controls = parser.getControls();
096: assertEquals(SearchControls.ONELEVEL_SCOPE, controls
097: .getSearchScope());
098: assertEquals(2, controls.getReturningAttributes().length);
099: assertEquals("postalAddress",
100: controls.getReturningAttributes()[0]);
101: assertEquals("o", controls.getReturningAttributes()[1]);
102: assertNull(parser.getFilter());
103: // default port is 389
104: assertEquals(389, parser.getPort());
105:
106: parser = new LdapUrlParser("ldap:///o=University,c=US");
107: parser.parseURL();
108: assertEquals("o=University,c=US", parser.getBaseObject());
109: assertNull(parser.getFilter());
110: assertNull(parser.getControls());
111: // default host is 'localhost'
112: assertEquals("localhost", parser.getHost());
113: // default port is 389
114: assertEquals(389, parser.getPort());
115:
116: parser = new LdapUrlParser(
117: "ldap://192.168.1.3/o=University,c=US??sub");
118: parser.parseURL();
119: assertEquals("o=University,c=US", parser.getBaseObject());
120: assertEquals("192.168.1.3", parser.getHost());
121: assertFalse(parser.hasAttributes());
122: assertTrue(parser.hasScope());
123: assertFalse(parser.hasExtensions());
124: assertNotNull(parser.getControls());
125: controls = parser.getControls();
126: assertEquals(SearchControls.SUBTREE_SCOPE, controls
127: .getSearchScope());
128:
129: parser = new LdapUrlParser(
130: "ldap://192.168.1.3/o=University,c=US??wrong");
131: try {
132: parser.parseURL();
133: fail("Should raise ParseException");
134: } catch (ParseException e) {
135: // expected
136: }
137:
138: parser = new LdapUrlParser(
139: "ldap://192.168.1.3/c=US?o=University?sub?(objectClass=*)");
140: parser.parseURL();
141: assertEquals("c=US", parser.getBaseObject());
142: assertEquals("192.168.1.3", parser.getHost());
143: assertNotNull(parser.getControls());
144: assertTrue(parser.hasAttributes());
145: assertTrue(parser.hasScope());
146: assertFalse(parser.hasExtensions());
147: controls = parser.getControls();
148: assertEquals(SearchControls.SUBTREE_SCOPE, controls
149: .getSearchScope());
150: assertTrue(parser.hasFilter());
151: assertNotNull(parser.getFilter());
152:
153: parser = new LdapUrlParser(
154: "ldap://192.168.1.3/c=US?o=University?sub?(objectClass=*)");
155: parser.parseURL();
156: assertEquals("c=US", parser.getBaseObject());
157: assertEquals("192.168.1.3", parser.getHost());
158: assertNotNull(parser.getControls());
159: controls = parser.getControls();
160: assertEquals(SearchControls.SUBTREE_SCOPE, controls
161: .getSearchScope());
162: assertNotNull(parser.getFilter());
163:
164: }
165:
166: public void test_parseURL_special_char() throws Exception {
167: parser = new LdapUrlParser(
168: "ldap:///o=University%20of%20Michigan,c=US");
169: parser.parseURL();
170: assertEquals("o=University of Michigan,c=US", parser
171: .getBaseObject());
172:
173: }
174: }
|