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