001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.mock;
021:
022: import java.io.InputStream;
023:
024: import java.net.HttpURLConnection;
025: import java.net.URL;
026:
027: /**
028: * Mock implementation of <code>HttpURLConnection</code>.
029: *
030: * @version $Id: MockHttpURLConnection.java 238991 2004-05-22 11:34:50Z vmassol $
031: */
032: public class MockHttpURLConnection extends HttpURLConnection {
033: /**
034: * Store the header fields that the <code>getHeaderField()</code> will
035: * return.
036: */
037: private String getHeaderFieldValue;
038:
039: /**
040: * Store the input streams that the <code>getInputStream()</code> will
041: * return.
042: */
043: private InputStream getInputStreamValue;
044:
045: // -----------------------------------------------------------------------
046: // Methods overriding those from HttpURLConnection
047: // -----------------------------------------------------------------------
048:
049: /**
050: * @param theURL the underlying URL
051: */
052: public MockHttpURLConnection(URL theURL) {
053: super (theURL);
054: }
055:
056: // -----------------------------------------------------------------------
057: // Methods added on top of those found in HttpURLConnection
058: // -----------------------------------------------------------------------
059:
060: /**
061: * Sets the header field value that will be returned by
062: * <code>getHeaderField()</code>.
063: *
064: * @param theValue the header field value
065: */
066: public void setExpectedGetHeaderField(String theValue) {
067: this .getHeaderFieldValue = theValue;
068: }
069:
070: /**
071: * Sets the input stream value that will be returned by
072: * <code>getInputStream()</code>.
073: *
074: * @param theValue the input stream value
075: */
076: public void setExpectedGetInputStream(InputStream theValue) {
077: this .getInputStreamValue = theValue;
078: }
079:
080: /**
081: * @see HttpURLConnection#getHeaderField(int)
082: */
083: public String getHeaderField(int theFieldNumber) {
084: if (this .getHeaderFieldValue == null) {
085: throw new RuntimeException(
086: "Must call setExpectedGetHeaderField() first !");
087: }
088:
089: return this .getHeaderFieldValue;
090: }
091:
092: /**
093: * @see HttpURLConnection#getInputStream()
094: */
095: public InputStream getInputStream() {
096: if (this .getInputStreamValue == null) {
097: throw new RuntimeException(
098: "Must call setExpectedGetInputStream() first !");
099: }
100:
101: return this .getInputStreamValue;
102: }
103:
104: // -----------------------------------------------------------------------
105: // Methods needed because HttpURLConnection is an abstract class
106: // -----------------------------------------------------------------------
107:
108: /**
109: * @see HttpURLConnection#usingProxy()
110: */
111: public boolean usingProxy() {
112: return false;
113: }
114:
115: /**
116: * @see HttpURLConnection#disconnect()
117: */
118: public void disconnect() {
119: }
120:
121: /**
122: * @see HttpURLConnection#connect()
123: */
124: public void connect() {
125: }
126: }
|