001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.coyote.http11;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.FileOutputStream;
021: import java.io.InputStream;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.net.Socket;
025: import java.util.Locale;
026:
027: import org.apache.coyote.Adapter;
028: import org.apache.coyote.ActionCode;
029: import org.apache.coyote.Processor;
030:
031: /**
032: * File tester.
033: * <p>
034: * This tester is initialized with an adapter (it will use the HTTP/1.1
035: * processor), and will then accept an input file (which will contain the
036: * input data), and an output file, to which the result of the request(s)
037: * will be written.
038: *
039: * @author Remy Maucherat
040: */
041: public class FileTester {
042:
043: // -------------------------------------------------------------- Constants
044:
045: // ----------------------------------------------------------- Constructors
046:
047: /**
048: * Construct a new file tester using the two files specified. The contents
049: * of the output file will be overwritten when the test is run.
050: *
051: * @param adapter Coyote adapter to use for this test
052: * @param processor Coyote processor to use for this test
053: * @param inputFile File containing the HTTP requests in plain text
054: * @param outputFile File containing the HTTP responses
055: */
056: public FileTester(Adapter adapter, Processor processor,
057: File inputFile, File outputFile) {
058:
059: processor.setAdapter(adapter);
060:
061: this .adapter = adapter;
062: this .processor = processor;
063: this .inputFile = inputFile;
064: this .outputFile = outputFile;
065:
066: }
067:
068: // --------------------------------------------------------- Static Methods
069:
070: /**
071: * Utility main method, which will use the HTTP/1.1 processor with the
072: * test adapter.
073: *
074: * @param args Command line arguments to be processed
075: */
076: public static void main(String args[]) throws Exception {
077:
078: if (args.length < 2) {
079: System.out.println("Incorrect number of arguments");
080: return;
081: }
082:
083: // The first argument is the input file
084: File inputFile = new File(args[0]);
085:
086: // The second argument is the output file
087: File outputFile = new File(args[1]);
088:
089: Adapter testAdapter = new RandomAdapter();
090: Http11Processor http11Processor = new Http11Processor();
091: http11Processor.setSocket(new Socket("127.0.0.1", 8080));
092: http11Processor.action(ActionCode.ACTION_START, null);
093:
094: FileTester tester = new FileTester(testAdapter,
095: http11Processor, inputFile, outputFile);
096: tester.test();
097:
098: }
099:
100: // ----------------------------------------------------- Instance Variables
101:
102: /**
103: * File containing the input data.
104: */
105: protected File inputFile;
106:
107: /**
108: * File containing the output data.
109: */
110: protected File outputFile;
111:
112: /**
113: * Coyote adapter to use.
114: */
115: protected Adapter adapter;
116:
117: /**
118: * Coyote processor to use.
119: */
120: protected Processor processor;
121:
122: // --------------------------------------------------------- Public Methods
123:
124: /**
125: * Process the test.
126: */
127: public void test() throws Exception {
128:
129: InputStream is = new FileInputStream(inputFile);
130: OutputStream os = new FileOutputStream(outputFile);
131:
132: processor.process(is, os);
133:
134: }
135:
136: }
|