KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > mock > MockHttpURLConnection


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.mock;
21
22 import java.io.InputStream JavaDoc;
23
24 import java.net.HttpURLConnection JavaDoc;
25 import java.net.URL JavaDoc;
26
27 /**
28  * Mock implementation of <code>HttpURLConnection</code>.
29  *
30  * @version $Id: MockHttpURLConnection.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
31  */

32 public class MockHttpURLConnection extends HttpURLConnection JavaDoc
33 {
34     /**
35      * Store the header fields that the <code>getHeaderField()</code> will
36      * return.
37      */

38     private String JavaDoc getHeaderFieldValue;
39
40     /**
41      * Store the input streams that the <code>getInputStream()</code> will
42      * return.
43      */

44     private InputStream JavaDoc getInputStreamValue;
45
46     // -----------------------------------------------------------------------
47
// Methods overriding those from HttpURLConnection
48
// -----------------------------------------------------------------------
49

50     /**
51      * @param theURL the underlying URL
52      */

53     public MockHttpURLConnection(URL JavaDoc theURL)
54     {
55         super(theURL);
56     }
57
58     // -----------------------------------------------------------------------
59
// Methods added on top of those found in HttpURLConnection
60
// -----------------------------------------------------------------------
61

62     /**
63      * Sets the header field value that will be returned by
64      * <code>getHeaderField()</code>.
65      *
66      * @param theValue the header field value
67      */

68     public void setExpectedGetHeaderField(String JavaDoc theValue)
69     {
70         this.getHeaderFieldValue = theValue;
71     }
72
73     /**
74      * Sets the input stream value that will be returned by
75      * <code>getInputStream()</code>.
76      *
77      * @param theValue the input stream value
78      */

79     public void setExpectedGetInputStream(InputStream JavaDoc theValue)
80     {
81         this.getInputStreamValue = theValue;
82     }
83
84     /**
85      * @see HttpURLConnection#getHeaderField(int)
86      */

87     public String JavaDoc getHeaderField(int theFieldNumber)
88     {
89         if (this.getHeaderFieldValue == null)
90         {
91             throw new RuntimeException JavaDoc(
92                 "Must call setExpectedGetHeaderField() first !");
93         }
94
95         return this.getHeaderFieldValue;
96     }
97
98     /**
99      * @see HttpURLConnection#getInputStream()
100      */

101     public InputStream JavaDoc getInputStream()
102     {
103         if (this.getInputStreamValue == null)
104         {
105             throw new RuntimeException JavaDoc(
106                 "Must call setExpectedGetInputStream() first !");
107         }
108
109         return this.getInputStreamValue;
110     }
111
112     // -----------------------------------------------------------------------
113
// Methods needed because HttpURLConnection is an abstract class
114
// -----------------------------------------------------------------------
115

116     /**
117      * @see HttpURLConnection#usingProxy()
118      */

119     public boolean usingProxy()
120     {
121         return false;
122     }
123
124     /**
125      * @see HttpURLConnection#disconnect()
126      */

127     public void disconnect()
128     {
129     }
130
131     /**
132      * @see HttpURLConnection#connect()
133      */

134     public void connect()
135     {
136     }
137 }
138
Popular Tags