1 23 24 package org.enhydra.xml.xmlc.ssi; 25 26 import java.io.File ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 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 ; 36 37 40 public class SSIRead { 41 45 private static boolean DEBUG = false; 46 47 48 private static final int DEFAULT_BUF_SIZE = 13; 49 50 51 private TestCaseBase fTest; 52 53 54 int fReadSize; 55 56 57 public SSIRead(TestCaseBase test, 58 int readSize) { 59 fTest = test; 60 fReadSize = readSize; 61 } 62 63 64 public SSIRead(TestCaseBase test) { 65 this(test, DEFAULT_BUF_SIZE); 66 } 67 68 71 private void ssiCopyBuffered(File inFile, 72 PrintWriter out) throws IOException { 73 char[] buf = new char[fReadSize]; 74 SSIReader ssiReader = new SSIReader(new InputSource (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 91 private void ssiCopyChar(File inFile, 92 PrintWriter out) throws IOException { 93 int ch; 94 SSIReader ssiReader = new SSIReader(new InputSource (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 110 private void ssiCopyWrite(File inFile, 111 PrintWriter out) throws IOException { 112 if (fReadSize == 1) { 113 ssiCopyChar(inFile, out); 114 } else { 115 ssiCopyBuffered(inFile, out); 116 } 117 } 118 119 120 private String mkOutExt(File inFile) { 121 String path = inFile.getPath(); 122 123 int dotIdx = path.lastIndexOf('.'); 125 return path.substring(dotIdx+1); 126 } 127 128 131 public void runOkTest(String relInFile) { 132 File inFile = fTest.getInputFile(relInFile); 133 String outExt = mkOutExt(inFile); 134 File outFile = fTest.getResultFile(outExt); 135 File expectFile = fTest.getExpectedFile(outExt); 136 137 try { 138 PrintWriter out = new PrintWriter (new FileWriter (outFile), true); 139 try { 140 ssiCopyWrite(inFile, out); 141 } finally { 142 out.close(); 143 } 144 } catch (IOException except) { 145 throw new TestException(except); 146 } 147 148 TestDiff differ = fTest.getDiffer(); 149 differ.diff(expectFile, outFile); 150 } 151 152 155 public void runFailTest(String relInFile) { 156 File inFile = fTest.getInputFile(relInFile); 157 String outExt = mkOutExt(inFile); 158 File outFile = fTest.getResultFile(outExt); 159 File expectFile = fTest.getExpectedFile(outExt); 160 161 try { 162 PrintWriter out = new PrintWriter (new FileWriter (outFile), true); 163 boolean failed = false; 164 try { 165 ssiCopyWrite(inFile, out); 166 } catch (IOException 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 except) { 176 throw new TestException(except); 177 } 178 179 fTest.getDiffer().diff(expectFile, outFile); 180 } 181 } 182 | Popular Tags |