1 /*
2 * Copyright 1999,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.coyote.http11;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.InputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.net.Socket;
25 import java.util.Locale;
26
27 import org.apache.coyote.Adapter;
28 import org.apache.coyote.ActionCode;
29 import org.apache.coyote.Processor;
30
31 /**
32 * File tester.
33 * <p>
34 * This tester is initialized with an adapter (it will use the HTTP/1.1
35 * processor), and will then accept an input file (which will contain the
36 * input data), and an output file, to which the result of the request(s)
37 * will be written.
38 *
39 * @author Remy Maucherat
40 */
41 public class FileTester {
42
43
44 // -------------------------------------------------------------- Constants
45
46
47 // ----------------------------------------------------------- Constructors
48
49
50 /**
51 * Construct a new file tester using the two files specified. The contents
52 * of the output file will be overwritten when the test is run.
53 *
54 * @param adapter Coyote adapter to use for this test
55 * @param processor Coyote processor to use for this test
56 * @param inputFile File containing the HTTP requests in plain text
57 * @param outputFile File containing the HTTP responses
58 */
59 public FileTester(Adapter adapter, Processor processor,
60 File inputFile, File outputFile) {
61
62 processor.setAdapter(adapter);
63
64 this.adapter = adapter;
65 this.processor = processor;
66 this.inputFile = inputFile;
67 this.outputFile = outputFile;
68
69 }
70
71
72 // --------------------------------------------------------- Static Methods
73
74
75 /**
76 * Utility main method, which will use the HTTP/1.1 processor with the
77 * test adapter.
78 *
79 * @param args Command line arguments to be processed
80 */
81 public static void main(String args[])
82 throws Exception {
83
84 if (args.length < 2) {
85 System.out.println("Incorrect number of arguments");
86 return;
87 }
88
89 // The first argument is the input file
90 File inputFile = new File(args[0]);
91
92 // The second argument is the output file
93 File outputFile = new File(args[1]);
94
95 Adapter testAdapter = new RandomAdapter();
96 Http11Processor http11Processor = new Http11Processor();
97 http11Processor.setSocket(new Socket("127.0.0.1", 8080));
98 http11Processor.action(ActionCode.ACTION_START, null);
99
100 FileTester tester = new FileTester(testAdapter, http11Processor,
101 inputFile, outputFile);
102 tester.test();
103
104 }
105
106
107 // ----------------------------------------------------- Instance Variables
108
109
110 /**
111 * File containing the input data.
112 */
113 protected File inputFile;
114
115
116 /**
117 * File containing the output data.
118 */
119 protected File outputFile;
120
121
122 /**
123 * Coyote adapter to use.
124 */
125 protected Adapter adapter;
126
127
128 /**
129 * Coyote processor to use.
130 */
131 protected Processor processor;
132
133
134 // --------------------------------------------------------- Public Methods
135
136
137 /**
138 * Process the test.
139 */
140 public void test()
141 throws Exception {
142
143 InputStream is = new FileInputStream(inputFile);
144 OutputStream os = new FileOutputStream(outputFile);
145
146 processor.process(is, os);
147
148 }
149
150
151 }