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;
019:
020: import javax.naming.CommunicationException;
021: import javax.naming.NamingException;
022: import javax.naming.TimeLimitExceededException;
023: import javax.naming.directory.InvalidSearchFilterException;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.harmony.jndi.internal.parser.AttributeTypeAndValuePair;
028: import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
029: import org.apache.harmony.security.asn1.ASN1Integer;
030:
031: public class LdapUtilsTest extends TestCase {
032: public void test_getExceptionFromResult() {
033: String message = "error message";
034: LdapResult result = getLdapResult(0, message);
035: NamingException ex = LdapUtils.getExceptionFromResult(result);
036: assertNull(ex);
037:
038: // error code map to CommunicationException
039: result = getLdapResult(2, message);
040: ex = LdapUtils.getExceptionFromResult(result);
041: assertTrue(ex instanceof CommunicationException);
042: assertEquals("[LDAP: error code 2 - error message]", ex
043: .getMessage());
044:
045: // error code not in map
046: result = getLdapResult(100, message);
047: ex = LdapUtils.getExceptionFromResult(result);
048: assertTrue(ex instanceof NamingException);
049: assertEquals("[LDAP: error code 100 - error message]", ex
050: .getMessage());
051:
052: // empty error message
053: result = getLdapResult(3, "");
054: ex = LdapUtils.getExceptionFromResult(result);
055: assertTrue(ex instanceof TimeLimitExceededException);
056: assertEquals("[LDAP: error code 3]", ex.getMessage());
057: }
058:
059: public void test_parseFilter() throws Exception {
060: try {
061: LdapUtils.parseFilter(null, new Object[0]);
062: fail("Should throw NullPointerException");
063: } catch (NullPointerException e) {
064: // expected
065: }
066:
067: try {
068: LdapUtils.parseFilter("object=*", null);
069: fail("Should throw InvalidSearchFilterException");
070: } catch (InvalidSearchFilterException e) {
071: // expected
072: }
073:
074: Filter filter = LdapUtils.parseFilter("(cn={0})",
075: new Object[] { "value" });
076: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
077: assertTrue(filter.getValue() instanceof AttributeTypeAndValuePair);
078: AttributeTypeAndValuePair pair = (AttributeTypeAndValuePair) filter
079: .getValue();
080: assertEquals("cn", pair.getType());
081: assertEquals("value", pair.getValue());
082:
083: filter = LdapUtils.parseFilter("(cn=test)", null);
084: assertEquals(Filter.EQUALITY_MATCH_FILTER, filter.getType());
085: assertTrue(filter.getValue() instanceof AttributeTypeAndValuePair);
086: pair = (AttributeTypeAndValuePair) filter.getValue();
087: assertEquals("cn", pair.getType());
088: assertEquals("test", pair.getValue());
089: }
090:
091: private LdapResult getLdapResult(int errorCode, String message) {
092: LdapResult result = new LdapResult();
093: if (message == null) {
094:
095: }
096: Object[] values = new Object[] {
097: ASN1Integer.fromIntValue(errorCode),
098: Utils.getBytes(""), Utils.getBytes(message), null };
099: result.decodeValues(values);
100: return result;
101: }
102: }
|