KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > ssi > SSIRead


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: SSIRead.java,v 1.3 2005/01/26 08:29:25 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.ssi;
25
26 import java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30
31 import org.enhydra.xml.driver.TestCaseBase;
32 import org.enhydra.xml.driver.TestDiff;
33 import org.enhydra.xml.driver.TestException;
34 import org.enhydra.xml.xmlc.misc.SSIReader;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * Class to used in implementing SSI processing tests.
39  */

40 public class SSIRead {
41     /**
42      * Print various debugging info. Some tests will not pass if this
43      * is true, as it prints stack on IOExceptions.
44      */

45     private static boolean DEBUG = false;
46
47     /** Buffer size to use if not specified */
48     private static final int DEFAULT_BUF_SIZE = 13;
49
50     /** Test case we are assocaited with. */
51     private TestCaseBase fTest;
52
53     /** Size of read to do */
54     int fReadSize;
55
56     /** Constructor */
57     public SSIRead(TestCaseBase test,
58                    int readSize) {
59         fTest = test;
60         fReadSize = readSize;
61     }
62
63     /** Constructor */
64     public SSIRead(TestCaseBase test) {
65         this(test, DEFAULT_BUF_SIZE);
66     }
67
68     /**
69      * Read file with SSIReader.
70      */

71     private void ssiCopyBuffered(File JavaDoc inFile,
72                                  PrintWriter JavaDoc out) throws IOException JavaDoc {
73         char[] buf = new char[fReadSize];
74         SSIReader ssiReader = new SSIReader(new InputSource JavaDoc(inFile.getPath()), null);
75         int readLen;
76         try {
77             do {
78                 readLen = ssiReader.read(buf, 0, fReadSize);
79                 if (readLen > 0) {
80                     out.write(buf, 0, readLen);
81                 }
82             } while (readLen != -1);
83         } finally {
84             ssiReader.close();
85         }
86     }
87
88     /**
89      * Read file with SSIReader, one char at a tupe
90      */

91     private void ssiCopyChar(File JavaDoc inFile,
92                              PrintWriter JavaDoc out) throws IOException JavaDoc {
93         int ch;
94         SSIReader ssiReader = new SSIReader(new InputSource JavaDoc(inFile.getPath()), null);
95         try {
96             do {
97                 ch = ssiReader.read();
98                 if (ch >= 0) {
99                     out.write(ch);
100                 }
101             } while (ch != -1);
102         } finally {
103             ssiReader.close();
104         }
105     }
106     
107     /**
108      * Read and output the file.
109      */

110     private void ssiCopyWrite(File JavaDoc inFile,
111                               PrintWriter JavaDoc out) throws IOException JavaDoc {
112         if (fReadSize == 1) {
113             ssiCopyChar(inFile, out);
114         } else {
115             ssiCopyBuffered(inFile, out);
116         }
117     }
118
119     /** Get the output extension */
120     private String JavaDoc mkOutExt(File JavaDoc inFile) {
121         String JavaDoc path = inFile.getPath();
122
123         // Use same extension as input file.
124
int dotIdx = path.lastIndexOf('.');
125         return path.substring(dotIdx+1);
126     }
127
128     /**
129      * Read file with specified buffer size and verify.
130      */

131     public void runOkTest(String JavaDoc relInFile) {
132         File JavaDoc inFile = fTest.getInputFile(relInFile);
133         String JavaDoc outExt = mkOutExt(inFile);
134         File JavaDoc outFile = fTest.getResultFile(outExt);
135         File JavaDoc expectFile = fTest.getExpectedFile(outExt);
136
137         try {
138             PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(outFile), true);
139             try {
140                 ssiCopyWrite(inFile, out);
141             } finally {
142                 out.close();
143             }
144         } catch (IOException JavaDoc except) {
145             throw new TestException(except);
146         }
147
148         TestDiff differ = fTest.getDiffer();
149         differ.diff(expectFile, outFile);
150     }
151
152     /**
153      * Run a test that should fail.
154      */

155     public void runFailTest(String JavaDoc relInFile) {
156         File JavaDoc inFile = fTest.getInputFile(relInFile);
157         String JavaDoc outExt = mkOutExt(inFile);
158         File JavaDoc outFile = fTest.getResultFile(outExt);
159         File JavaDoc expectFile = fTest.getExpectedFile(outExt);
160
161         try {
162             PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(outFile), true);
163             boolean failed = false;
164             try {
165                 ssiCopyWrite(inFile, out);
166             } catch (IOException JavaDoc except) {
167                 out.println("Test is expected to fail: " + except);
168                 failed = true;
169             } finally {
170                 out.close();
171             }
172             if (!failed) {
173                 throw new TestException("test should have failed");
174             }
175         } catch (IOException JavaDoc except) {
176             throw new TestException(except);
177         }
178
179         fTest.getDiffer().diff(expectFile, outFile);
180     }
181 }
182
Popular Tags