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 java.util.Hashtable;
021:
022: import javax.naming.Context;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
027: import org.apache.harmony.jndi.provider.ldap.mock.BindResponse;
028: import org.apache.harmony.jndi.provider.ldap.mock.MockLdapServer;
029:
030: public class ReferralExceptionTest extends TestCase {
031: private MockLdapServer server;
032:
033: public void setUp() {
034: server = new MockLdapServer();
035: server.start();
036: }
037:
038: public void tearDown() {
039: server.stop();
040: }
041:
042: public void testGetReferralInfo() throws Exception {
043: Hashtable<Object, Object> env = new Hashtable<Object, Object>();
044: env.put(Context.REFERRAL, "throw");
045: String[] referrals = new String[] { "ldap://localhost:389",
046: "ldap://localhost:333" };
047: ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
048: referrals, env);
049: // try {
050: // context.search("", null);
051: // } catch (ReferralException e) {
052: // ex = e;
053: // }
054:
055: assertEquals(referrals[0], ex.getReferralInfo());
056:
057: assertTrue(ex.skipReferral());
058:
059: assertEquals(referrals[1], ex.getReferralInfo());
060:
061: assertFalse(ex.skipReferral());
062:
063: assertNull(ex.getReferralInfo());
064: }
065:
066: public void testGetReferralContext() throws Exception {
067: Hashtable<Object, Object> env = new Hashtable<Object, Object>();
068:
069: env.put(Context.REFERRAL, "throw");
070: env.put("test.getReferralContext", "GetReferralContext");
071:
072: String[] referrals = new String[] { server.getURL() };
073: ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
074: referrals, env);
075:
076: assertEquals(referrals[0], ex.getReferralInfo());
077:
078: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
079: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
080: null) });
081:
082: Context refContext = ex.getReferralContext();
083:
084: Hashtable<Object, Object> refEnv = (Hashtable<Object, Object>) refContext
085: .getEnvironment();
086:
087: assertEquals(env.get(Context.REFERRAL), refEnv
088: .get(Context.REFERRAL));
089: assertEquals(env.get("test.getReferralContext"), refEnv
090: .get("test.getReferralContext"));
091:
092: assertFalse(ex.skipReferral());
093:
094: assertNull(ex.getReferralInfo());
095: }
096:
097: public void testGetReferralContext2() throws Exception {
098: Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();
099:
100: initialEnv.put(Context.REFERRAL, "throw");
101: initialEnv.put("test.getReferralContext", "GetReferralContext");
102:
103: String[] referrals = new String[] { server.getURL() };
104: ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
105: referrals, initialEnv);
106:
107: Hashtable<Object, Object> env = new Hashtable<Object, Object>();
108:
109: env.put(Context.REFERRAL, "follow");
110: env.put("test.getReferralContext", "changed");
111:
112: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
113: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
114: null) });
115:
116: assertEquals(referrals[0], ex.getReferralInfo());
117: Context refContext = ex.getReferralContext(env);
118:
119: Hashtable<Object, Object> refEnv = (Hashtable<Object, Object>) refContext
120: .getEnvironment();
121:
122: assertEquals(env.get(Context.REFERRAL), refEnv
123: .get(Context.REFERRAL));
124: assertEquals(env.get("test.getReferralContext"), refEnv
125: .get("test.getReferralContext"));
126:
127: assertFalse(ex.skipReferral());
128:
129: assertNull(ex.getReferralInfo());
130: }
131: }
|