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: import javax.naming.NamingException;
024: import javax.naming.ldap.LdapContext;
025: import javax.naming.spi.ResolveResult;
026:
027: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
028: import org.apache.harmony.jndi.provider.ldap.mock.BindResponse;
029: import org.apache.harmony.jndi.provider.ldap.mock.MockLdapServer;
030:
031: import junit.framework.TestCase;
032:
033: public class ldapURLContextTest extends TestCase {
034: private MockLdapServer server;
035:
036: @Override
037: public void setUp() {
038: server = new MockLdapServer();
039: server.start();
040: }
041:
042: @Override
043: public void tearDown() {
044: server.stop();
045: }
046:
047: public void testGetRootURLContext() throws Exception {
048: MockLdapURLContext context = new MockLdapURLContext();
049: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
050: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
051: null) });
052:
053: ResolveResult result = context.getRootURLContext(server
054: .getURL(), null);
055:
056: assertEquals("", result.getRemainingName().toString());
057: assertTrue(result.getResolvedObj() instanceof LdapContextImpl);
058: }
059:
060: public void testGetRootURLContext2() throws Exception {
061: Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();
062: initialEnv.put(Context.REFERRAL, "throw");
063: initialEnv.put("test.getRootURLContext", "test");
064:
065: MockLdapURLContext context = new MockLdapURLContext(initialEnv);
066:
067: Hashtable<Object, Object> env = new Hashtable<Object, Object>();
068: env.put(Context.REFERRAL, "ignore");
069: env.put("test.getRootURLContext", "GetRootURLContext");
070:
071: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
072: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
073: null) });
074:
075: ResolveResult result = context.getRootURLContext(server
076: .getURL(), env);
077:
078: assertEquals("", result.getRemainingName().toString());
079: assertTrue(result.getResolvedObj() instanceof LdapContextImpl);
080:
081: LdapContext newContext = (LdapContext) result.getResolvedObj();
082: Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) newContext
083: .getEnvironment();
084:
085: assertEquals("ignore", newEnv.get(Context.REFERRAL));
086: assertEquals("GetRootURLContext", newEnv
087: .get("test.getRootURLContext"));
088: }
089:
090: public void testGetRootURLContext3() throws Exception {
091: Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();
092: initialEnv.put(Context.REFERRAL, "throw");
093: initialEnv.put("test.getRootURLContext", "test");
094:
095: MockLdapURLContext context = new MockLdapURLContext(initialEnv);
096:
097: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
098: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
099: null) });
100:
101: ResolveResult result = context.getRootURLContext(server
102: .getURL(), null);
103:
104: assertEquals("", result.getRemainingName().toString());
105: assertTrue(result.getResolvedObj() instanceof LdapContextImpl);
106:
107: LdapContext newContext = (LdapContext) result.getResolvedObj();
108: Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) newContext
109: .getEnvironment();
110:
111: assertEquals("throw", newEnv.get(Context.REFERRAL));
112: assertEquals("test", newEnv.get("test.getRootURLContext"));
113: }
114:
115: public static class MockLdapURLContext extends ldapURLContext {
116: public MockLdapURLContext() {
117: super ();
118: }
119:
120: public MockLdapURLContext(Hashtable<Object, Object> env) {
121: super (env);
122: }
123:
124: @Override
125: public ResolveResult getRootURLContext(String url,
126: Hashtable<?, ?> env) throws NamingException {
127: return super.getRootURLContext(url, env);
128: }
129: }
130: }
|