4444import java .util .Random ;
4545
4646import edu .rice .cs .drjava .model .MultiThreadedTestCase ;
47+ import edu .rice .cs .plt .io .IOUtil ;
4748
4849/** Test cases for {@link Log}.
49- * @version $Id: FileOpsTest.java 3896 2006-06-26 21:19:25Z rcartwright $
50+ * @version $Id$
5051 */
5152public class LogTest extends MultiThreadedTestCase {
5253
53- static final String FILENAME_PREFIX = "$#%#$" ;
54- static final File FILE1 = new File (FILENAME_PREFIX + "logtest1.txt" );
55- static final File FILE2 = new File (FILENAME_PREFIX + "logtest2.txt" );
56- static final File FILE3 = new File (FILENAME_PREFIX + "logtest3.txt" );
57-
5854 static final int SHORT_TIME = 10000 ; // few seconds in milliseconds
5955
6056 static final int DATE_END = 28 ; // the ending index of the date field in a log entry
@@ -81,27 +77,6 @@ public void run() {
8177
8278 }
8379
84- public void setUp () throws Exception {
85- super .setUp ();
86-
87- // Clear log files if they exist, so beginning of files are written by these tests
88- new FileWriter (FILE1 ).close ();
89- new FileWriter (FILE2 ).close ();
90- new FileWriter (FILE3 ).close ();
91- }
92-
93- public void tearDown () throws Exception {
94- // Delete log files
95- /* Note: this code does not currently delete the files.
96- * TO DO: Find a way to delete these files so the build directory does not contain more clutter.
97- */
98- FILE1 .deleteOnExit ();
99- FILE2 .deleteOnExit ();
100- FILE3 .deleteOnExit ();
101-
102- super .tearDown ();
103- }
104-
10580 /** Parses a date printed by Date.toString(); returns null if there is a parse error. */
10681 @ SuppressWarnings ("deprecation" )
10782 private static Date parse (String s ) {
@@ -113,6 +88,7 @@ private static Date parse(String s) {
11388 * and their timestamps are within the past few seconds.
11489 */
11590 public void testLog () throws IOException {
91+ File FILE1 = IOUtil .createAndMarkTempFile ("logtest001" ,".txt" );
11692 Log log1 = new Log (FILE1 , true );
11793 log1 .log ("Message 1" );
11894 log1 .log ("Message 2" );
@@ -128,7 +104,7 @@ public void testLog() throws IOException {
128104// System.err.println("Current time in millis is: " + System.currentTimeMillis());
129105 Date time0 = parse (s0 );
130106 assertTrue ("Log opened within last few seconds" , time0 .compareTo (earlier ) >= 0 && time0 .compareTo (now ) <= 0 );
131- assertEquals ("Log open message" , "Log '" + FILE1 .getName () + "' opened" , s0 .substring (30 , 60 ));
107+ assertEquals ("Log open message" , "Log '" + FILE1 .getName () + "' opened" , s0 .substring (30 , 43 + FILE1 . getName (). length () ));
132108
133109 String s1 = fin .readLine ();
134110 Date time1 = parse (s1 );
@@ -152,6 +128,7 @@ public void testLog() throws IOException {
152128 * methods (one with the Throwable itself and the other with the the StackTraceElement[])
153129 */
154130 public void testExceptionPrinting () throws IOException {
131+ File FILE2 = IOUtil .createAndMarkTempFile ("logtest002" ,".txt" );
155132 Log log2 = new Log (FILE2 , true );
156133// System.err.println("Starting testExceptionPrinting");
157134
@@ -175,7 +152,7 @@ public void testExceptionPrinting() throws IOException {
175152 String s0 = fin .readLine ();
176153 Date time0 = parse (s0 );
177154 assertTrue ("Log opened within last few seconds" , time0 .compareTo (earlier ) >= 0 && time0 .compareTo (now ) <= 0 );
178- assertEquals ("Log open message" , "Log '" + FILE2 .getName () + "' opened" , s0 .substring (30 , 60 ));
155+ assertEquals ("Log open message" , "Log '" + FILE2 .getName () + "' opened" , s0 .substring (30 , 43 + FILE2 . getName (). length () ));
179156
180157 String s1 = fin .readLine ();
181158 Date time1 = parse (s1 );
@@ -212,7 +189,7 @@ public void testExceptionPrinting() throws IOException {
212189 * the entries in the log may be corrupted).
213190 */
214191 public void testConcurrentWrites () throws IOException , InterruptedException {
215-
192+ File FILE3 = IOUtil . createAndMarkTempFile ( "logtest003" , ".txt" );
216193 Log log3 = new Log (FILE3 , true );
217194 Random r = new Random ();
218195 Thread [] threads = new Thread [NUM_THREADS ];
@@ -223,11 +200,10 @@ public void testConcurrentWrites() throws IOException, InterruptedException {
223200 BufferedReader fin = new BufferedReader (new FileReader (FILE3 ));
224201 Date earlier = new Date (new Date ().getTime () - SHORT_TIME );
225202 Date now = new Date ();
226-
227203 String s0 = fin .readLine ();
228204 Date time0 = parse (s0 );
229205 assertTrue ("Log opened within last 10 seconds" , time0 .compareTo (earlier ) >= 0 && time0 .compareTo (now ) <= 0 );
230- assertEquals ("Log open message" , "Log '" + FILE3 .getName () + "' opened" , s0 .substring (30 , 60 ));
206+ assertEquals ("Log open message" , "Log '" + FILE3 .getName () + "' opened" , s0 .substring (30 , 43 + FILE3 . getName (). length () ));
231207
232208 for (int i = 0 ; i < NUM_THREADS ; i ++) {
233209 String s1 = fin .readLine ();
0 commit comments