001: /*
002: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.errorStream
003:
004: Licensed to the Apache Software Foundation (ASF) under one or more
005: contributor license agreements. See the NOTICE file distributed with
006: this work for additional information regarding copyright ownership.
007: The ASF licenses this file to You under the Apache License, Version 2.0
008: (the "License"); you may not use this file except in compliance with
009: the License. You may obtain a copy of the License at
010:
011: http://www.apache.org/licenses/LICENSE-2.0
012:
013: Unless required by applicable law or agreed to in writing, software
014: distributed under the License is distributed on an "AS IS" BASIS,
015: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: See the License for the specific language governing permissions and
017: limitations under the License.
018:
019: */
020:
021: package org.apache.derbyTesting.functionTests.tests.lang;
022:
023: import java.io.File;
024: import java.io.OutputStream;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.PrintStream;
028: import java.util.Properties;
029: import java.sql.Connection;
030: import java.sql.Driver;
031: import java.sql.DriverManager;
032: import java.sql.DriverPropertyInfo;
033: import java.sql.SQLException;
034: import javax.sql.DataSource;
035:
036: import org.apache.derby.tools.ij;
037: import org.apache.derbyTesting.functionTests.util.TestUtil;
038:
039: /*
040: * Partial test of semantics for the three derby.stream.error.* flags.
041: * See their description in the Tuning Guide.
042: * Limitations:
043: * - The test uses only OutputStream values for .field and .method
044: * (not Writer, which is also allowed)
045: * - Negative test don't exercise all ways to fail; the test uses
046: * non-existence, but not non-accessability
047: * - Tests precedence, but only for valid values (e.g. missing: "what if
048: * non-existing file is specified AND also a method or field":
049: * Fallback should be System.err )
050: *
051: */
052:
053: final class AssertException extends Exception {
054: AssertException(String e) {
055: super (e);
056: };
057: }
058:
059: public class errorStream {
060: private static String derbyHome;
061:
062: private static Properties sysProps;
063: private static final String FILE_PROP = "derby.stream.error.file";
064: private static final String METHOD_PROP = "derby.stream.error.method";
065: private static final String FIELD_PROP = "derby.stream.error.field";
066:
067: /*
068: * database names are constructed as <database>-<runNo>
069: */
070: private static final String database = "VombatusUrsinusHirsutus";
071:
072: /* runNo keeps track of which run we are in: Derby is booted
073: * several times; once for each combination of properties we want
074: * to test the behavior of.
075: */
076: private static int runNo = 0;
077:
078: /*
079: * File used when FILE_PROP is set, it maps to file
080: * <database>-file-<runNo>.log
081: */
082: private static File fileStreamFile;
083:
084: /* see doc for getStream below */
085: private static OutputStream methodStream;
086: private static File methodStreamFile;
087:
088: /*
089: * Field fieldStream used by Derby when FIELD_PROP is set,
090: * so it needs to be public. Maps to file <database>-field-<runNo>.log
091: */
092: public static OutputStream fieldStream;
093: private static File fieldStreamFile;
094:
095: /*
096: * Field errStream used as redirection for System.err to be able
097: * to checks its (non-)use in the scenarios. We first tried to
098: * merge it with System.out and let the harness compare outputs,
099: * but this gave intermittent merging differences, so abandoned.
100: * Maps to file <database>-err-<runNo>.log
101: */
102: private static OutputStream errStream;
103: private static File errStreamFile;
104:
105: /*
106: * Method getStream used by Derby when METHOD_PROP
107: * is set. Maps to file <database>-method-<runNo>.log
108: */
109: public static OutputStream getStream() {
110: return methodStream;
111: }
112:
113: private static String makeStreamFilename(String type) {
114: return database + "-" + type + "-" + runNo + ".log";
115: }
116:
117: private static String makeDatabaseName() {
118: return database + "-" + runNo;
119: }
120:
121: private static void openStreams() throws IOException {
122:
123: runNo += 1;
124:
125: try {
126: fileStreamFile = new File(derbyHome,
127: makeStreamFilename("file"));
128:
129: methodStreamFile = new File(derbyHome,
130: makeStreamFilename("method"));
131: methodStream = new FileOutputStream(methodStreamFile);
132:
133: fieldStreamFile = new File(derbyHome,
134: makeStreamFilename("field"));
135: fieldStream = new FileOutputStream(fieldStreamFile);
136:
137: errStreamFile = new File(derbyHome,
138: makeStreamFilename("err"));
139: errStream = new FileOutputStream(errStreamFile);
140: System.setErr(new PrintStream(errStream));
141: } catch (IOException e) {
142: System.out.println("Could not open stream files");
143: throw e;
144: }
145: }
146:
147: private static void closeStreams() throws IOException {
148: try {
149: methodStream.close();
150: fieldStream.close();
151: errStream.close();
152:
153: // reset until next scenario, no expected output
154: System.setErr(System.out);
155: } catch (IOException e) {
156: System.out.println("Could not close stream files");
157: throw e;
158: }
159: }
160:
161: private static void assertEmpty(File f) throws AssertException,
162: IOException {
163: if (!(f.exists() && (f.length() == 0))) {
164: AssertException e = new AssertException(
165: "assertEmpty failed: : " + f.getCanonicalPath());
166: throw e;
167: }
168: }
169:
170: private static void assertNonEmpty(File f) throws AssertException,
171: IOException {
172: if (!f.exists() || (f.length() == 0)) {
173: AssertException e = new AssertException(
174: "assertNonEmpty failed:" + f.getCanonicalPath());
175: throw e;
176: }
177: }
178:
179: private static void assertNonExisting(File f)
180: throws AssertException, IOException {
181: if (f.exists()) {
182: AssertException e = new AssertException(
183: "assertNonExisting failed: " + f.getCanonicalPath());
184: throw e;
185: }
186: }
187:
188: private static void resetProps() {
189: sysProps.remove(FILE_PROP);
190: sysProps.remove(METHOD_PROP);
191: sysProps.remove(FIELD_PROP);
192: }
193:
194: private static void bootDerby() throws SQLException {
195: Properties attrs = new Properties();
196: attrs.setProperty("databaseName", makeDatabaseName());
197: attrs.setProperty("createDatabase", "create");
198: DataSource ds = TestUtil.getDataSource(attrs);
199: try {
200: Connection conn = ds.getConnection();
201: conn.close();
202: } catch (SQLException e) {
203: System.out.println("Derby boot failed: "
204: + attrs.getProperty("databaseName") + " : "
205: + e.getSQLState() + ": " + e.getMessage());
206: throw e;
207: }
208: }
209:
210: private static void shutdownDerby() throws AssertException,
211: SQLException {
212: Properties attrs = new Properties();
213: attrs.setProperty("databaseName", "");
214: attrs.setProperty("shutdownDatabase", "shutdown");
215: DataSource ds = TestUtil.getDataSource(attrs);
216: try {
217: Connection conn = ds.getConnection();
218: AssertException e = new AssertException("shutdown failed: "
219: + makeDatabaseName());
220: throw e;
221: } catch (SQLException e) {
222: System.out.println("shutdown ok: " + e.getSQLState() + ":"
223: + e.getMessage());
224: }
225: }
226:
227: private static void checkFile() throws AssertException,
228: IOException, SQLException {
229: openStreams();
230:
231: resetProps();
232: sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
233:
234: bootDerby();
235: shutdownDerby();
236:
237: closeStreams();
238:
239: assertNonEmpty(fileStreamFile);
240: assertEmpty(methodStreamFile);
241: assertEmpty(fieldStreamFile);
242: assertEmpty(errStreamFile);
243: }
244:
245: private static void checkWrongFile() throws AssertException,
246: IOException, SQLException {
247: openStreams();
248:
249: sysProps.put(FILE_PROP, new File(derbyHome + "foo", // erroneous path
250: makeStreamFilename("file")).getCanonicalPath());
251:
252: bootDerby();
253: shutdownDerby();
254:
255: closeStreams();
256:
257: assertNonExisting(fileStreamFile);
258: assertEmpty(methodStreamFile);
259: assertEmpty(fieldStreamFile);
260: assertNonEmpty(errStreamFile);
261: }
262:
263: private static void checkMethod() throws AssertException,
264: IOException, SQLException {
265: openStreams();
266:
267: resetProps();
268: sysProps.put(METHOD_PROP,
269: "org.apache.derbyTesting.functionTests.tests.lang."
270: + "errorStream.getStream");
271:
272: bootDerby();
273: shutdownDerby();
274:
275: closeStreams();
276:
277: assertNonExisting(fileStreamFile);
278: assertNonEmpty(methodStreamFile);
279: assertEmpty(fieldStreamFile);
280: assertEmpty(errStreamFile);
281: }
282:
283: private static void checkWrongMethod() throws AssertException,
284: IOException, SQLException {
285: openStreams();
286:
287: resetProps();
288: sysProps.put(METHOD_PROP,
289: "org.apache.derbyTesting.functionTests.tests.lang."
290: + "errorStream.nonExistingGetStream");
291:
292: bootDerby();
293: shutdownDerby();
294:
295: closeStreams();
296:
297: assertNonExisting(fileStreamFile);
298: assertEmpty(methodStreamFile);
299: assertEmpty(fieldStreamFile);
300: assertNonEmpty(errStreamFile);
301: }
302:
303: private static void checkField() throws AssertException,
304: IOException, SQLException {
305: openStreams();
306:
307: resetProps();
308: sysProps.put(FIELD_PROP,
309: "org.apache.derbyTesting.functionTests.tests.lang."
310: + "errorStream.fieldStream");
311:
312: bootDerby();
313: shutdownDerby();
314:
315: closeStreams();
316:
317: assertNonExisting(fileStreamFile);
318: assertEmpty(methodStreamFile);
319: assertNonEmpty(fieldStreamFile);
320: assertEmpty(errStreamFile);
321: }
322:
323: private static void checkWrongField() throws AssertException,
324: IOException, SQLException {
325: openStreams();
326:
327: resetProps();
328: sysProps.put(FIELD_PROP,
329: "org.apache.derbyTesting.functionTests.tests.lang."
330: + "errorStream.nonExistingFieldStream");
331:
332: bootDerby();
333: shutdownDerby();
334:
335: closeStreams();
336:
337: assertNonExisting(fileStreamFile);
338: assertEmpty(methodStreamFile);
339: assertEmpty(fieldStreamFile);
340: assertNonEmpty(errStreamFile);
341: }
342:
343: private static void checkFileOverMethod() throws AssertException,
344: IOException, SQLException {
345: openStreams();
346:
347: resetProps();
348: sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
349: sysProps.put(METHOD_PROP,
350: "org.apache.derbyTesting.functionTests.tests.lang."
351: + "errorStream.getStream");
352:
353: bootDerby();
354: shutdownDerby();
355:
356: closeStreams();
357:
358: assertNonEmpty(fileStreamFile);
359: assertEmpty(methodStreamFile);
360: assertEmpty(fieldStreamFile);
361: assertEmpty(errStreamFile);
362: }
363:
364: private static void checkFileOverField() throws AssertException,
365: IOException, SQLException {
366: openStreams();
367:
368: resetProps();
369: sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
370: sysProps.put(FIELD_PROP,
371: "org.apache.derbyTesting.functionTests.tests.lang."
372: + "errorStream.fieldStream");
373:
374: bootDerby();
375: shutdownDerby();
376:
377: closeStreams();
378:
379: assertNonEmpty(fileStreamFile);
380: assertEmpty(methodStreamFile);
381: assertEmpty(fieldStreamFile);
382: assertEmpty(errStreamFile);
383: }
384:
385: private static void checkFileOverMethodAndField()
386: throws AssertException, IOException, SQLException {
387: openStreams();
388:
389: resetProps();
390: sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
391: sysProps.put(METHOD_PROP,
392: "org.apache.derbyTesting.functionTests.tests.lang."
393: + "errorStream.getStream");
394: sysProps.put(FIELD_PROP,
395: "org.apache.derbyTesting.functionTests.tests.lang."
396: + "errorStream.fieldStream");
397:
398: bootDerby();
399: shutdownDerby();
400:
401: closeStreams();
402:
403: assertNonEmpty(fileStreamFile);
404: assertEmpty(methodStreamFile);
405: assertEmpty(fieldStreamFile);
406: assertEmpty(errStreamFile);
407: }
408:
409: private static void checkMethodOverField() throws AssertException,
410: IOException, SQLException {
411: openStreams();
412:
413: resetProps();
414: sysProps.put(METHOD_PROP,
415: "org.apache.derbyTesting.functionTests.tests.lang."
416: + "errorStream.getStream");
417: sysProps.put(FIELD_PROP,
418: "org.apache.derbyTesting.functionTests.tests.lang."
419: + "errorStream.fieldStream");
420:
421: bootDerby();
422: shutdownDerby();
423:
424: closeStreams();
425:
426: assertNonExisting(fileStreamFile);
427: assertNonEmpty(methodStreamFile);
428: assertEmpty(fieldStreamFile);
429: assertEmpty(errStreamFile);
430: }
431:
432: public static void main(String[] args) {
433: try {
434: ij.getPropertyArg(args);
435: sysProps = System.getProperties();
436: derbyHome = sysProps.getProperty("derby.system.home");
437:
438: System.out.println("Test errorStream starting");
439:
440: checkFile();
441: checkWrongFile();
442:
443: checkMethod();
444: checkWrongMethod();
445:
446: checkField();
447: checkWrongField();
448:
449: checkFileOverMethod();
450: checkFileOverField();
451: checkFileOverMethodAndField();
452:
453: checkMethodOverField();
454:
455: System.out
456: .println("Test errorStream finished successfully");
457: } catch (Exception e) {
458: System.out.println("Test errorStream failed: "
459: + e.getMessage());
460: e.printStackTrace();
461: }
462: ;
463: }
464: }
|