001package org.andromda.core.common; 002 003import java.io.BufferedReader; 004import java.io.File; 005import java.io.FileNotFoundException; 006import java.io.FileReader; 007import java.io.IOException; 008import junit.framework.TestCase; 009 010/** 011 * A simple test to check the operation of the ExceptionRecorder. 012 * 013 * @author Martin West 014 */ 015public class ExceptionRecorderTest 016 extends TestCase 017{ 018 /** 019 * Test that a .exc file is created and it has at least the file header 020 * string. 021 */ 022 public void testRecordStringThrowableString() 023 { 024 Exception ex1 = new Exception("ExceptionRecorder Test"); 025 Exception ex2 = new Exception(ex1); 026 String filename = ExceptionRecorder.instance().record("Test message", ex2, "test"); 027 File excFile = new File(filename); 028 assertTrue( 029 "exception file not created:" + excFile, 030 excFile.exists()); 031 FileReader fr = null; 032 BufferedReader br = null; 033 try 034 { 035 fr = new FileReader(excFile); 036 br = new BufferedReader(fr); 037 String inline; 038 inline = br.readLine(); 039 assertTrue( 040 "First line not header line", 041 ExceptionRecorder.FILE_HEADER.equals(inline)); 042 for (int ctr = 0; ctr < 10; ctr++) 043 { 044 if ((inline = br.readLine()) != null) 045 { 046 if (inline.startsWith(ExceptionRecorder.RUN_SYSTEM)) 047 { 048 String sysver; 049 try 050 { 051 sysver = System.getProperty("os.name") + System.getProperty("os.version"); 052 } 053 catch (Exception e) 054 { 055 sysver = ExceptionRecorder.INFORMATION_UNAVAILABLE; 056 } 057 assertTrue( 058 "Incorrect " + ExceptionRecorder.RUN_SYSTEM, 059 inline.endsWith(sysver)); 060 } 061 if (inline.startsWith(ExceptionRecorder.RUN_JDK)) 062 { 063 String jdkver; 064 try 065 { 066 jdkver = System.getProperty("java.vm.vendor") + System.getProperty("java.vm.version"); 067 } 068 catch (Exception e) 069 { 070 jdkver = ExceptionRecorder.INFORMATION_UNAVAILABLE; 071 } 072 assertTrue( 073 "Incorrect " + ExceptionRecorder.RUN_JDK, 074 inline.endsWith(jdkver)); 075 } 076 } 077 } 078 } 079 catch (FileNotFoundException e) 080 { 081 fail(e.getMessage()); 082 } 083 catch (IOException e) 084 { 085 fail(e.getMessage()); 086 } 087 finally 088 { 089 try 090 { 091 // Close the file. 092 if (fr != null) {fr.close();} 093 } 094 catch (Exception e) 095 { 096 // ignore 097 } 098 try 099 { 100 // Clean up since the .exc gets created 101 // in the andromda directory and not a 102 // target directory. 103 excFile.delete(); 104 } 105 catch (Exception e) 106 { 107 // ignore 108 } 109 try 110 { 111 if (br != null) {br.close();} 112 } 113 catch (Exception e) 114 { 115 // ignore 116 } 117 } 118 } 119}