KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > LogTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util;
35
36 import junit.framework.TestCase;
37 import java.io.FileReader JavaDoc;
38 import java.io.BufferedReader JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.FileWriter JavaDoc;
41 import java.io.File JavaDoc;
42 import java.text.ParseException JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.Random JavaDoc;
45
46 import edu.rice.cs.drjava.model.MultiThreadedTestCase;
47 import edu.rice.cs.plt.io.IOUtil;
48
49 /** Test cases for {@link Log}.
50  * @version $Id: LogTest.java 4065 2007-01-17 23:19:29Z sigma_lmtd $
51  */

52 public class LogTest extends MultiThreadedTestCase {
53   
54   static final int SHORT_TIME = 10000; // few seconds in milliseconds
55

56   static final int DATE_END = 28; // the ending index of the date field in a log entry
57

58   /** A thread class that adds a log message after sleeping a given number of milliseconds */
59   private class LogTestThread extends Thread JavaDoc {
60     
61     Log _log;
62     int _millis;
63     
64     public LogTestThread(Log log, int millis) {
65       _log = log;
66       _millis = millis;
67     }
68     
69     public void run() {
70       try { sleep(_millis); }
71       catch (InterruptedException JavaDoc e ) {
72         e.printStackTrace();
73         fail("testConcurrent failed: sleep interrupted");
74       }
75       _log.log( "Test message" );
76     }
77     
78   }
79   
80   /** Parses a date printed by Date.toString(); returns null if there is a parse error. */
81   @SuppressWarnings JavaDoc("deprecation")
82   private static Date JavaDoc parse(String JavaDoc s) {
83     try { return new Date JavaDoc(Date.parse(s.substring(0, DATE_END))); } // the undeprecated version of parse DOES NOT WORK
84
catch(RuntimeException JavaDoc e) { return null; } // either IllegalArgument or StringIndexOutOfBounds
85
}
86   
87   /** Adds a couple of generic messages to a log, and then tests to make sure they are all correct, in the correct order,
88     * and their timestamps are within the past few seconds.
89     */

90   public void testLog() throws IOException JavaDoc {
91     File JavaDoc file1 = IOUtil.createAndMarkTempFile("logtest001",".txt");
92     Log log1 = new Log(file1, true);
93     log1.log("Message 1");
94     log1.log("Message 2");
95     log1.log("Message 3");
96     
97     BufferedReader JavaDoc fin = new BufferedReader JavaDoc(new FileReader JavaDoc(file1));
98     Date JavaDoc earlier = new Date JavaDoc(new Date JavaDoc().getTime() - SHORT_TIME);
99     Date JavaDoc now = new Date JavaDoc();
100     
101     String JavaDoc s0 = fin.readLine();
102 // System.err.println("s0 = " + s0);
103
// System.err.println("s0 converted to millis " + parse(s0));
104
// System.err.println("Current time in millis is: " + System.currentTimeMillis());
105
Date JavaDoc time0 = parse(s0);
106     assertTrue("Log opened within last few seconds", time0.compareTo(earlier) >= 0 && time0.compareTo(now) <= 0);
107     assertEquals("Log open message", "Log '" + file1.getName() + "' opened", s0.substring(30, 43+file1.getName().length()));
108     
109     String JavaDoc s1 = fin.readLine();
110     Date JavaDoc time1 = parse(s1);
111     assertTrue("Date of message 1 within last few seconds", time1.compareTo(earlier) >= 0 && time1.compareTo(now) <= 0);
112     assertEquals("Log message 1", "Message 1", s1.substring(30));
113     
114     String JavaDoc s2 = fin.readLine();
115     Date JavaDoc time2 = parse(s2);
116     assertTrue("Date of message 2 within last few seconds", time2.compareTo(earlier) >= 0 && time2.compareTo(now) <= 0);
117     assertEquals("Log message 2", "Message 2", s2.substring(30));
118     
119     String JavaDoc s3 = fin.readLine();
120     Date JavaDoc time3 = parse(s3);
121     assertTrue("Date of message 3 within last few seconds", time3.compareTo(earlier) >= 0 && time3.compareTo(now) <= 0);
122     assertEquals("Log message 3", "Message 3", s3.substring(30));
123   
124     fin.close();
125   }
126   
127   /** Tests the Exception printing methods in the Log file by throwing two exceptions and using the two types of log
128     * methods (one with the Throwable itself and the other with the the StackTraceElement[])
129     */

130   public void testExceptionPrinting() throws IOException JavaDoc {
131     File JavaDoc file2 = IOUtil.createAndMarkTempFile("logtest002",".txt");
132     Log log2 = new Log(file2, true);
133 // System.err.println("Starting testExceptionPrinting");
134

135     // Throw a couple of exceptions and log them
136
try { throw new ArrayIndexOutOfBoundsException JavaDoc(); }
137     catch (ArrayIndexOutOfBoundsException JavaDoc e) {
138       //e.printStackTrace();
139
log2.log("Message 1", e);
140     }
141     
142     try { throw new NullPointerException JavaDoc(); }
143     catch (NullPointerException JavaDoc e) {
144       //e.printStackTrace();
145
log2.log("Message 2", e.getStackTrace());
146     }
147     
148     BufferedReader JavaDoc fin = new BufferedReader JavaDoc(new FileReader JavaDoc(file2));
149     Date JavaDoc earlier = new Date JavaDoc(new Date JavaDoc().getTime() - SHORT_TIME);
150     Date JavaDoc now = new Date JavaDoc();
151     
152     String JavaDoc s0 = fin.readLine();
153     Date JavaDoc time0 = parse(s0);
154     assertTrue("Log opened within last few seconds", time0.compareTo(earlier) >= 0 && time0.compareTo(now) <= 0);
155     assertEquals("Log open message", "Log '" + file2.getName() + "' opened", s0.substring(30, 43+file2.getName().length()));
156     
157     String JavaDoc s1 = fin.readLine();
158     Date JavaDoc time1 = parse(s1);
159     assertTrue("Date of message 1 within last few seconds", time1.compareTo(earlier) >= 0 && time1.compareTo(now) <= 0);
160     assertEquals("Log message 1", "Message 1", s1.substring(30));
161     assertEquals("Log exception 1", "java.lang.ArrayIndexOutOfBoundsException", fin.readLine());
162     
163     // Since it's difficult to test the rest of the stack trace, just skip over it
164
String JavaDoc s2;
165     Date JavaDoc time2;
166     do {
167       s2 = fin.readLine();
168 // System.err.println("traceback line = " + s2);
169
time2 = parse(s2); // returns null if there is a parse error
170
}
171     while (time2 == null);
172     
173 // System.err.println("Skipped over traceback");
174

175     assertTrue("Date of message 2 within last few seconds", time2.compareTo(earlier) >= 0 && time2.compareTo(now) <= 0);
176     assertEquals("Log message 2", "Message 2", s2.substring(30));
177     assertEquals("Log exception 2 (trace line 1)",
178                  "edu.rice.cs.util.LogTest.testExceptionPrinting", fin.readLine().substring(0,46));
179
180     fin.close();
181   }
182   
183   private static final int NUM_THREADS = 50;
184   private static final int DELAY = 100;
185   
186   /** Attempts to test Log's behavior when called concurrently from several sources. Spawns NUM_THREADS LogTestThreads
187     * (see above)that wait a random number between 0 and DELAY milliseconds and then log a message. The function tests
188     * to make sure that the messages and dates are all intact (if the Log was not handling concurrent requests properly,
189     * the entries in the log may be corrupted).
190     */

191   public void testConcurrentWrites() throws IOException JavaDoc, InterruptedException JavaDoc {
192     File JavaDoc file3 = IOUtil.createAndMarkTempFile("logtest003",".txt");
193     Log log3 = new Log(file3, true);
194     Random JavaDoc r = new Random JavaDoc();
195     Thread JavaDoc[] threads = new Thread JavaDoc[NUM_THREADS];
196     for (int i = 0; i < NUM_THREADS; i++) threads[i] = new LogTestThread(log3, r.nextInt(DELAY));
197     for (int i = 0; i < NUM_THREADS; i++) threads[i].start();
198     for (int i = 0; i < NUM_THREADS; i++) threads[i].join();
199    
200     BufferedReader JavaDoc fin = new BufferedReader JavaDoc(new FileReader JavaDoc(file3));
201     Date JavaDoc earlier = new Date JavaDoc(new Date JavaDoc().getTime() - SHORT_TIME);
202     Date JavaDoc now = new Date JavaDoc();
203     String JavaDoc s0 = fin.readLine();
204     Date JavaDoc time0 = parse(s0);
205     assertTrue("Log opened within last 10 seconds", time0.compareTo(earlier) >= 0 && time0.compareTo(now) <= 0);
206     assertEquals("Log open message", "Log '" + file3.getName() + "' opened", s0.substring(30, 43+file3.getName().length()));
207     
208     for (int i = 0; i < NUM_THREADS; i++) {
209       String JavaDoc s1 = fin.readLine();
210       Date JavaDoc time1 = parse(s1);
211       assertTrue("Date of message within last 10 seconds", time1.compareTo(earlier) >= 0 && time1.compareTo(now) <= 0);
212       assertEquals("Log message", "Test message", s1.substring(30));
213     }
214     
215     fin.close();
216   }
217   
218 }
219
220
Popular Tags