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.io.IOException;
021:
022: import javax.naming.ldap.Control;
023:
024: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
025: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
026: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
027: import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
028: import org.apache.harmony.security.asn1.ASN1Integer;
029:
030: public class MockLdapClient extends LdapClient {
031:
032: private ASN1Encodable request;
033:
034: private ASN1Decodable response;
035:
036: public MockLdapClient() {
037: super ();
038: }
039:
040: @Override
041: public LdapMessage doOperation(int opIndex,
042: ASN1Encodable requestOp, ASN1Decodable responseOp,
043: Control[] controls) throws IOException {
044: request = requestOp;
045: response = responseOp;
046: if (response instanceof LdapResult) {
047: LdapResult result = (LdapResult) response;
048: Object[] values = new Object[] {
049: ASN1Integer.fromIntValue(0), Utils.getBytes(""),
050: Utils.getBytes(""), null };
051: result.decodeValues(values);
052: }
053:
054: if (response instanceof SearchOp) {
055: SearchOp op = (SearchOp) response;
056: op.setSearchResult(new LdapSearchResult() {
057: public LdapResult getResult() {
058: LdapResult rs = new LdapResult();
059: Object[] values = new Object[] {
060: ASN1Integer.fromIntValue(0),
061: Utils.getBytes(""), Utils.getBytes(""),
062: null };
063: rs.decodeValues(values);
064: return rs;
065: }
066: });
067: }
068:
069: if (opIndex == LdapASN1Constant.OP_BIND_REQUEST) {
070: Object[] values = new Object[5];
071: values[0] = ASN1Integer.fromIntValue(0);
072: values[1] = Utils.getBytes("");
073: values[2] = Utils.getBytes("");
074: responseOp.decodeValues(values);
075: }
076:
077: return new LdapMessage(response);
078: }
079:
080: @Override
081: public int addPersistentSearch(SearchOp op) throws IOException {
082: request = op.getRequest();
083: response = op.getResponse();
084: return new LdapMessage(response).getMessageId();
085: }
086:
087: @Override
088: public void doOperationWithoutResponse(int opIndex,
089: ASN1Encodable op, Control[] controls) throws IOException {
090: request = op;
091: }
092:
093: public ASN1Encodable getRequest() {
094: return request;
095: }
096:
097: public ASN1Decodable getResponse() {
098: return response;
099: }
100:
101: public void setResponse(ASN1Decodable response) {
102: this.response = response;
103: }
104:
105: }
|