1 33 34 package edu.rice.cs.util; 35 36 import junit.framework.TestCase; 37 import java.io.FileReader ; 38 import java.io.BufferedReader ; 39 import java.io.IOException ; 40 import java.io.FileWriter ; 41 import java.io.File ; 42 import java.text.ParseException ; 43 import java.util.Date ; 44 import java.util.Random ; 45 46 import edu.rice.cs.drjava.model.MultiThreadedTestCase; 47 import edu.rice.cs.plt.io.IOUtil; 48 49 52 public class LogTest extends MultiThreadedTestCase { 53 54 static final int SHORT_TIME = 10000; 56 static final int DATE_END = 28; 58 59 private class LogTestThread extends Thread { 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 e ) { 72 e.printStackTrace(); 73 fail("testConcurrent failed: sleep interrupted"); 74 } 75 _log.log( "Test message" ); 76 } 77 78 } 79 80 81 @SuppressWarnings ("deprecation") 82 private static Date parse(String s) { 83 try { return new Date (Date.parse(s.substring(0, DATE_END))); } catch(RuntimeException e) { return null; } } 86 87 90 public void testLog() throws IOException { 91 File 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 fin = new BufferedReader (new FileReader (file1)); 98 Date earlier = new Date (new Date ().getTime() - SHORT_TIME); 99 Date now = new Date (); 100 101 String s0 = fin.readLine(); 102 Date 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 s1 = fin.readLine(); 110 Date 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 s2 = fin.readLine(); 115 Date 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 s3 = fin.readLine(); 120 Date 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 130 public void testExceptionPrinting() throws IOException { 131 File file2 = IOUtil.createAndMarkTempFile("logtest002",".txt"); 132 Log log2 = new Log(file2, true); 133 135 try { throw new ArrayIndexOutOfBoundsException (); } 137 catch (ArrayIndexOutOfBoundsException e) { 138 log2.log("Message 1", e); 140 } 141 142 try { throw new NullPointerException (); } 143 catch (NullPointerException e) { 144 log2.log("Message 2", e.getStackTrace()); 146 } 147 148 BufferedReader fin = new BufferedReader (new FileReader (file2)); 149 Date earlier = new Date (new Date ().getTime() - SHORT_TIME); 150 Date now = new Date (); 151 152 String s0 = fin.readLine(); 153 Date 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 s1 = fin.readLine(); 158 Date 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 String s2; 165 Date time2; 166 do { 167 s2 = fin.readLine(); 168 time2 = parse(s2); } 171 while (time2 == null); 172 173 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 191 public void testConcurrentWrites() throws IOException , InterruptedException { 192 File file3 = IOUtil.createAndMarkTempFile("logtest003",".txt"); 193 Log log3 = new Log(file3, true); 194 Random r = new Random (); 195 Thread [] threads = new Thread [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 fin = new BufferedReader (new FileReader (file3)); 201 Date earlier = new Date (new Date ().getTime() - SHORT_TIME); 202 Date now = new Date (); 203 String s0 = fin.readLine(); 204 Date 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 s1 = fin.readLine(); 210 Date 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 |