01: package org.andromda.core.common;
02:
03: import java.io.BufferedReader;
04: import java.io.File;
05: import java.io.FileNotFoundException;
06: import java.io.FileReader;
07: import java.io.IOException;
08:
09: import junit.framework.TestCase;
10:
11: /**
12: * A simple test to check the operation of the ExceptionRecorder.
13: *
14: * @author Martin West
15: */
16: public class ExceptionRecorderTest extends TestCase {
17: /**
18: * Test that a .exc file is created and it has at least the file header
19: * string.
20: */
21: public void testRecordStringThrowableString() {
22: Exception ex1 = new Exception("ExceptionRecorder Test");
23: Exception ex2 = new Exception(ex1);
24: String filename = ExceptionRecorder.instance().record(
25: "Test message", ex2, "test");
26: File excFile = new File(filename);
27: assertTrue("exception file not created:" + excFile, excFile
28: .exists());
29: FileReader fr = null;
30: try {
31: fr = new FileReader(excFile);
32: BufferedReader br = new BufferedReader(fr);
33: String inline;
34: inline = br.readLine();
35: assertTrue("First line not header line",
36: ExceptionRecorder.FILE_HEADER.equals(inline));
37: for (int ctr = 0; ctr < 10; ctr++) {
38: if ((inline = br.readLine()) != null) {
39: if (inline.startsWith(ExceptionRecorder.RUN_SYSTEM)) {
40: String sysver;
41: try {
42: sysver = System.getProperty("os.name")
43: + System.getProperty("os.version");
44: } catch (Exception e) {
45: sysver = ExceptionRecorder.INFORMATION_UNAVAILABLE;
46: }
47: assertTrue("Incorrect "
48: + ExceptionRecorder.RUN_SYSTEM, inline
49: .endsWith(sysver));
50: }
51: if (inline.startsWith(ExceptionRecorder.RUN_JDK)) {
52: String jdkver;
53: try {
54: jdkver = System
55: .getProperty("java.vm.vendor")
56: + System
57: .getProperty("java.vm.version");
58: } catch (Exception e) {
59: jdkver = ExceptionRecorder.INFORMATION_UNAVAILABLE;
60: }
61: assertTrue("Incorrect "
62: + ExceptionRecorder.RUN_JDK, inline
63: .endsWith(jdkver));
64: }
65: }
66: }
67: } catch (FileNotFoundException e) {
68: fail(e.getMessage());
69: } catch (IOException e) {
70: fail(e.getMessage());
71: } finally {
72: try {
73: // Close the file.
74: fr.close();
75: } catch (Exception e) {
76: // ignore
77: }
78: try {
79: // Clean up since the .exc gets created
80: // in the andromda directory and not a
81: // target directory.
82: excFile.delete();
83: } catch (Exception e) {
84: // ignore
85: }
86: }
87: }
88: }
|