KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > errorStream


1 /*
2   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.errorStream
3
4   Licensed to the Apache Software Foundation (ASF) under one or more
5   contributor license agreements. See the NOTICE file distributed with
6   this work for additional information regarding copyright ownership.
7   The ASF licenses this file to You under the Apache License, Version 2.0
8   (the "License"); you may not use this file except in compliance with
9   the License. You may obtain a copy of the License at
10
11   http://www.apache.org/licenses/LICENSE-2.0
12
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18
19 */

20
21 package org.apache.derbyTesting.functionTests.tests.lang;
22
23 import java.io.File JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.Driver JavaDoc;
31 import java.sql.DriverManager JavaDoc;
32 import java.sql.DriverPropertyInfo JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35
36 import org.apache.derby.tools.ij;
37 import org.apache.derbyTesting.functionTests.util.TestUtil;
38
39 /*
40  * Partial test of semantics for the three derby.stream.error.* flags.
41  * See their description in the Tuning Guide.
42  * Limitations:
43  * - The test uses only OutputStream values for .field and .method
44  * (not Writer, which is also allowed)
45  * - Negative test don't exercise all ways to fail; the test uses
46  * non-existence, but not non-accessability
47  * - Tests precedence, but only for valid values (e.g. missing: "what if
48  * non-existing file is specified AND also a method or field":
49  * Fallback should be System.err )
50  *
51  */

52
53
54 final class AssertException extends Exception JavaDoc
55 {
56    AssertException (String JavaDoc e) {
57       super(e);
58    };
59 }
60
61 public class errorStream
62 {
63    private static String JavaDoc derbyHome;
64
65    private static Properties JavaDoc sysProps;
66    private static final String JavaDoc FILE_PROP = "derby.stream.error.file";
67    private static final String JavaDoc METHOD_PROP = "derby.stream.error.method";
68    private static final String JavaDoc FIELD_PROP = "derby.stream.error.field";
69
70    /*
71     * database names are constructed as <database>-<runNo>
72     */

73    private static final String JavaDoc database = "VombatusUrsinusHirsutus";
74
75    /* runNo keeps track of which run we are in: Derby is booted
76     * several times; once for each combination of properties we want
77     * to test the behavior of.
78     */

79    private static int runNo = 0;
80
81    /*
82     * File used when FILE_PROP is set, it maps to file
83     * <database>-file-<runNo>.log
84     */

85    private static File JavaDoc fileStreamFile;
86
87    /* see doc for getStream below */
88    private static OutputStream JavaDoc methodStream;
89    private static File JavaDoc methodStreamFile;
90
91    /*
92     * Field fieldStream used by Derby when FIELD_PROP is set,
93     * so it needs to be public. Maps to file <database>-field-<runNo>.log
94     */

95    public static OutputStream JavaDoc fieldStream;
96    private static File JavaDoc fieldStreamFile;
97
98    /*
99     * Field errStream used as redirection for System.err to be able
100     * to checks its (non-)use in the scenarios. We first tried to
101     * merge it with System.out and let the harness compare outputs,
102     * but this gave intermittent merging differences, so abandoned.
103     * Maps to file <database>-err-<runNo>.log
104     */

105    private static OutputStream JavaDoc errStream;
106    private static File JavaDoc errStreamFile;
107
108    /*
109     * Method getStream used by Derby when METHOD_PROP
110     * is set. Maps to file <database>-method-<runNo>.log
111     */

112    public static OutputStream JavaDoc getStream() {
113       return methodStream;
114    }
115
116    private static String JavaDoc makeStreamFilename(String JavaDoc type) {
117       return database + "-" + type + "-" + runNo + ".log";
118    }
119
120    private static String JavaDoc makeDatabaseName() {
121       return database + "-" + runNo;
122    }
123
124    private static void openStreams() throws IOException JavaDoc{
125
126       runNo += 1;
127
128       try {
129          fileStreamFile = new File JavaDoc(derbyHome, makeStreamFilename("file"));
130
131          methodStreamFile = new File JavaDoc(derbyHome, makeStreamFilename("method"));
132          methodStream = new FileOutputStream JavaDoc(methodStreamFile);
133
134          fieldStreamFile = new File JavaDoc(derbyHome, makeStreamFilename("field"));
135          fieldStream = new FileOutputStream JavaDoc(fieldStreamFile);
136
137      errStreamFile = new File JavaDoc(derbyHome, makeStreamFilename("err"));
138      errStream =new FileOutputStream JavaDoc(errStreamFile);
139      System.setErr(new PrintStream JavaDoc(errStream));
140       }
141       catch (IOException JavaDoc e) {
142          System.out.println("Could not open stream files");
143          throw e;
144       }
145    }
146
147
148    private static void closeStreams() throws IOException JavaDoc {
149       try {
150          methodStream.close();
151          fieldStream.close();
152      errStream.close();
153
154      // reset until next scenario, no expected output
155
System.setErr(System.out);
156       }
157       catch (IOException JavaDoc e) {
158          System.out.println("Could not close stream files");
159          throw e;
160       }
161    }
162
163
164    private static void assertEmpty(File JavaDoc f) throws AssertException,
165                                                   IOException JavaDoc {
166       if ( ! (f.exists() && (f.length() == 0)) ) {
167          AssertException e = new AssertException("assertEmpty failed: : " +
168                                                  f.getCanonicalPath());
169          throw e;
170       }
171    }
172
173
174    private static void assertNonEmpty(File JavaDoc f) throws AssertException,
175                                                      IOException JavaDoc {
176       if ( ! f.exists() || (f.length() == 0) ) {
177          AssertException e = new AssertException("assertNonEmpty failed:" +
178                                                  f.getCanonicalPath());
179          throw e;
180       }
181    }
182
183
184    private static void assertNonExisting(File JavaDoc f) throws AssertException,
185                                                         IOException JavaDoc {
186       if ( f.exists() ) {
187          AssertException e = new AssertException("assertNonExisting failed: " +
188                                                  f.getCanonicalPath());
189          throw e;
190       }
191    }
192
193
194    private static void resetProps () {
195       sysProps.remove(FILE_PROP);
196       sysProps.remove(METHOD_PROP);
197       sysProps.remove(FIELD_PROP);
198    }
199
200
201    private static void bootDerby () throws SQLException JavaDoc {
202       Properties JavaDoc attrs = new Properties JavaDoc();
203       attrs.setProperty("databaseName", makeDatabaseName());
204       attrs.setProperty("createDatabase", "create");
205       DataSource JavaDoc ds = TestUtil.getDataSource(attrs);
206       try {
207          Connection JavaDoc conn = ds.getConnection();
208          conn.close();
209       }
210       catch (SQLException JavaDoc e) {
211          System.out.println("Derby boot failed: " +
212                 attrs.getProperty("databaseName") + " : " +
213                 e.getSQLState() + ": " + e.getMessage());
214          throw e;
215       }
216    }
217
218
219    private static void shutdownDerby () throws AssertException, SQLException JavaDoc {
220       Properties JavaDoc attrs = new Properties JavaDoc();
221       attrs.setProperty("databaseName", "");
222       attrs.setProperty("shutdownDatabase", "shutdown");
223       DataSource JavaDoc ds = TestUtil.getDataSource(attrs);
224       try {
225          Connection JavaDoc conn = ds.getConnection();
226          AssertException e = new AssertException("shutdown failed: " +
227                                                  makeDatabaseName());
228          throw e;
229       }
230       catch (SQLException JavaDoc e) {
231          System.out.println("shutdown ok: " + e.getSQLState() + ":" +
232                             e.getMessage());
233       }
234    }
235
236
237    private static void checkFile() throws AssertException, IOException JavaDoc,
238                                           SQLException JavaDoc {
239       openStreams();
240
241       resetProps();
242       sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
243
244       bootDerby();
245       shutdownDerby();
246
247       closeStreams();
248
249       assertNonEmpty(fileStreamFile);
250       assertEmpty(methodStreamFile);
251       assertEmpty(fieldStreamFile);
252       assertEmpty(errStreamFile);
253    }
254
255
256    private static void checkWrongFile() throws AssertException, IOException JavaDoc,
257                                                SQLException JavaDoc {
258       openStreams();
259
260       sysProps.put(FILE_PROP,
261                    new File JavaDoc(derbyHome+"foo", // erroneous path
262
makeStreamFilename("file")).getCanonicalPath());
263
264       bootDerby();
265       shutdownDerby();
266       
267       closeStreams();
268
269       assertNonExisting(fileStreamFile);
270       assertEmpty(methodStreamFile);
271       assertEmpty(fieldStreamFile);
272       assertNonEmpty(errStreamFile);
273    }
274
275
276    private static void checkMethod() throws AssertException, IOException JavaDoc,
277                                             SQLException JavaDoc {
278       openStreams();
279
280       resetProps();
281       sysProps.put(METHOD_PROP,
282                    "org.apache.derbyTesting.functionTests.tests.lang."+
283                    "errorStream.getStream");
284
285       bootDerby();
286       shutdownDerby();
287
288       closeStreams();
289
290       assertNonExisting(fileStreamFile);
291       assertNonEmpty(methodStreamFile);
292       assertEmpty(fieldStreamFile);
293       assertEmpty(errStreamFile);
294    }
295
296
297    private static void checkWrongMethod() throws AssertException, IOException JavaDoc,
298                                                  SQLException JavaDoc {
299       openStreams();
300
301       resetProps();
302       sysProps.put(METHOD_PROP,
303                    "org.apache.derbyTesting.functionTests.tests.lang."+
304                    "errorStream.nonExistingGetStream");
305
306       bootDerby();
307       shutdownDerby();
308
309       closeStreams();
310
311       assertNonExisting(fileStreamFile);
312       assertEmpty(methodStreamFile);
313       assertEmpty(fieldStreamFile);
314       assertNonEmpty(errStreamFile);
315    }
316
317
318    private static void checkField() throws AssertException, IOException JavaDoc,
319                                            SQLException JavaDoc {
320       openStreams();
321
322       resetProps();
323       sysProps.put(FIELD_PROP,
324                    "org.apache.derbyTesting.functionTests.tests.lang."+
325                    "errorStream.fieldStream");
326
327       bootDerby();
328       shutdownDerby();
329
330       closeStreams();
331
332       assertNonExisting(fileStreamFile);
333       assertEmpty(methodStreamFile);
334       assertNonEmpty(fieldStreamFile);
335       assertEmpty(errStreamFile);
336    }
337
338
339    private static void checkWrongField() throws AssertException, IOException JavaDoc,
340                                                 SQLException JavaDoc {
341       openStreams();
342
343       resetProps();
344       sysProps.put(FIELD_PROP,
345                    "org.apache.derbyTesting.functionTests.tests.lang."+
346                    "errorStream.nonExistingFieldStream");
347
348       bootDerby();
349       shutdownDerby();
350
351       closeStreams();
352
353       assertNonExisting(fileStreamFile);
354       assertEmpty(methodStreamFile);
355       assertEmpty(fieldStreamFile);
356       assertNonEmpty(errStreamFile);
357    }
358
359    
360    private static void checkFileOverMethod() throws AssertException, IOException JavaDoc,
361                                                     SQLException JavaDoc {
362       openStreams();
363
364       resetProps();
365       sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
366       sysProps.put(METHOD_PROP,
367                    "org.apache.derbyTesting.functionTests.tests.lang."+
368                    "errorStream.getStream");
369
370       bootDerby();
371       shutdownDerby();
372
373       closeStreams();
374
375       assertNonEmpty(fileStreamFile);
376       assertEmpty(methodStreamFile);
377       assertEmpty(fieldStreamFile);
378       assertEmpty(errStreamFile);
379    }
380
381
382    private static void checkFileOverField() throws AssertException, IOException JavaDoc,
383                                                    SQLException JavaDoc {
384       openStreams();
385
386       resetProps();
387       sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
388       sysProps.put(FIELD_PROP,
389                    "org.apache.derbyTesting.functionTests.tests.lang."+
390                    "errorStream.fieldStream");
391
392       bootDerby();
393       shutdownDerby();
394
395       closeStreams();
396
397       assertNonEmpty(fileStreamFile);
398       assertEmpty(methodStreamFile);
399       assertEmpty(fieldStreamFile);
400       assertEmpty(errStreamFile);
401    }
402
403
404    private static void checkFileOverMethodAndField() throws AssertException,
405                                                             IOException JavaDoc,
406                                                             SQLException JavaDoc {
407       openStreams();
408
409       resetProps();
410       sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath());
411       sysProps.put(METHOD_PROP,
412                    "org.apache.derbyTesting.functionTests.tests.lang."+
413                    "errorStream.getStream");
414       sysProps.put(FIELD_PROP,
415                    "org.apache.derbyTesting.functionTests.tests.lang."+
416                    "errorStream.fieldStream");
417
418       bootDerby();
419       shutdownDerby();
420
421       closeStreams();
422
423       assertNonEmpty(fileStreamFile);
424       assertEmpty(methodStreamFile);
425       assertEmpty(fieldStreamFile);
426       assertEmpty(errStreamFile);
427    }
428
429
430    private static void checkMethodOverField() throws AssertException, IOException JavaDoc,
431                                                      SQLException JavaDoc {
432       openStreams();
433
434       resetProps();
435       sysProps.put(METHOD_PROP,
436                    "org.apache.derbyTesting.functionTests.tests.lang."+
437                    "errorStream.getStream");
438       sysProps.put(FIELD_PROP,
439                    "org.apache.derbyTesting.functionTests.tests.lang."+
440                    "errorStream.fieldStream");
441
442       bootDerby();
443       shutdownDerby();
444
445       closeStreams();
446
447       assertNonExisting(fileStreamFile);
448       assertNonEmpty(methodStreamFile);
449       assertEmpty(fieldStreamFile);
450       assertEmpty(errStreamFile);
451    }
452
453
454    public static void main(String JavaDoc[] args)
455    {
456       try {
457          ij.getPropertyArg(args);
458          sysProps = System.getProperties();
459          derbyHome = sysProps.getProperty("derby.system.home");
460
461          System.out.println("Test errorStream starting");
462
463          checkFile();
464          checkWrongFile();
465
466          checkMethod();
467          checkWrongMethod();
468      
469          checkField();
470          checkWrongField();
471
472          checkFileOverMethod();
473          checkFileOverField();
474          checkFileOverMethodAndField();
475          
476          checkMethodOverField();
477
478          System.out.println("Test errorStream finished successfully");
479       }
480       catch (Exception JavaDoc e) {
481          System.out.println("Test errorStream failed: " + e.getMessage());
482          e.printStackTrace();
483       };
484    }
485 }
486
Popular Tags