Author: aidan
Date: Wed Aug 19 15:46:35 2009
New Revision: 805848

URL: http://svn.apache.org/viewvc?rev=805848&view=rev
Log:
QPID-2060: make sure we clean up the logfile after each test run.

Optionally print out the whole thing if the log monitor fails to find the text.

Modified:
    
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitor.java
    
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitorTest.java

Modified: 
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitor.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitor.java?rev=805848&r1=805847&r2=805848&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitor.java
 (original)
+++ 
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitor.java
 Wed Aug 19 15:46:35 2009
@@ -31,6 +31,7 @@
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -118,7 +119,7 @@
      * @throws java.io.FileNotFoundException if the Log file can nolonger be 
found
      * @throws IOException                   thrown when reading the log file
      */
-    public boolean waitForMessage(String message, long wait)
+    public boolean waitForMessage(String message, long wait, boolean 
printFileOnFailure)
             throws FileNotFoundException, IOException
     {
         // Loop through alerts until we're done or wait ms seconds have passed,
@@ -126,20 +127,35 @@
         BufferedReader reader = new BufferedReader(new FileReader(_logfile));
         boolean found = false;
         long endtime = System.currentTimeMillis() + wait;
+        ArrayList<String> contents = new ArrayList<String>();
         while (!found && System.currentTimeMillis() < endtime)
         {
             while (reader.ready())
             {
                 String line = reader.readLine();
+                contents.add(line);
                 if (line.contains(message))
                 {
                     found = true;
                 }
             }
         }
-
+        if (!found && printFileOnFailure)
+        {
+            for (String line : contents)
+            {
+                System.out.println(line);
+            }
+        }
         return found;
     }
+    
+
+    public boolean waitForMessage(String messageCountAlert, long 
alertLogWaitPeriod) throws FileNotFoundException, IOException
+    {
+       return waitForMessage(messageCountAlert, alertLogWaitPeriod, true);
+    }
+
 
     /**
      * Read the log file in to memory as a String

Modified: 
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitorTest.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitorTest.java?rev=805848&r1=805847&r2=805848&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitorTest.java
 (original)
+++ 
qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/LogMonitorTest.java
 Wed Aug 19 15:46:35 2009
@@ -30,25 +30,25 @@
 public class LogMonitorTest extends TestCase
 {
 
+    private LogMonitor _monitor;
+
+    @Override
+    public void setUp() throws Exception
+    {
+        _monitor = new LogMonitor();
+        _monitor.getMonitoredFile().deleteOnExit(); // Make sure we clean up
+    }
+    
     /**
      * Test that a new file is created when attempting to set up a monitor with
      * the default constructor.
      */
     public void testMonitor()
     {
-        // Validate that a NPE is thrown with null input
-        try
-        {
-            LogMonitor montior = new LogMonitor();
-            //Validte that the monitor is now running on a new file
-            assertTrue("New file does not have correct name:" + montior.
-                    getMonitoredFile().getName(),
-                       
montior.getMonitoredFile().getName().contains("LogMonitor"));
-        }
-        catch (IOException ioe)
-        {
-            fail("IOE thrown:" + ioe);
-        }
+        //Validate that the monitor is now running on a new file
+        assertTrue("New file does not have correct name:" + _monitor.
+                getMonitoredFile().getName(),
+                _monitor.getMonitoredFile().getName().contains("LogMonitor"));
     }
 
     /**
@@ -63,13 +63,11 @@
         File testFile = File.createTempFile("testMonitorFile", ".log");
         testFile.deleteOnExit();
 
-        LogMonitor monitor;
-
         //Ensure that we can create a monitor on a file
         try
         {
-            monitor = new LogMonitor(testFile);
-            assertEquals(testFile, monitor.getMonitoredFile());
+            _monitor = new LogMonitor(testFile);
+            assertEquals(testFile, _monitor.getMonitoredFile());
         }
         catch (IOException ioe)
         {
@@ -136,13 +134,12 @@
      */
     public void testFindMatches_Match() throws IOException
     {
-        LogMonitor monitor = new LogMonitor();
 
         String message = getName() + ": Test Message";
 
         Logger.getRootLogger().warn(message);
 
-        validateLogContainsMessage(monitor, message);
+        validateLogContainsMessage(_monitor, message);
     }
 
     /**
@@ -152,21 +149,17 @@
      */
     public void testFindMatches_NoMatch() throws IOException
     {
-        LogMonitor monitor = new LogMonitor();
-
         String message = getName() + ": Test Message";
 
         Logger.getRootLogger().warn(message);
 
         String notLogged = "This text was not logged";
 
-        validateLogDoesNotContainsMessage(monitor, notLogged);
+        validateLogDoesNotContainsMessage(_monitor, notLogged);
     }
 
     public void testWaitForMessage_Found() throws IOException
     {
-        LogMonitor monitor = new LogMonitor();
-
         String message = getName() + ": Test Message";
 
         long TIME_OUT = 2000;
@@ -174,13 +167,11 @@
         logMessageWithDelay(message, TIME_OUT / 2);
 
         assertTrue("Message was not logged ",
-                   monitor.waitForMessage(message, TIME_OUT));
+                    _monitor.waitForMessage(message, TIME_OUT));
     }
 
     public void testWaitForMessage_Timeout() throws IOException
     {
-        LogMonitor monitor = new LogMonitor();
-
         String message = getName() + ": Test Message";
 
         long TIME_OUT = 2000;
@@ -189,41 +180,37 @@
 
         // Verify that we can time out waiting for a message
         assertFalse("Message was logged ",
-                    monitor.waitForMessage(message, TIME_OUT / 2));
+                    _monitor.waitForMessage(message, TIME_OUT / 2, false));
 
         // Verify that the message did eventually get logged.
         assertTrue("Message was never logged.",
-                   monitor.waitForMessage(message, TIME_OUT));
+                    _monitor.waitForMessage(message, TIME_OUT));
     }
 
     public void testReset() throws IOException
     {
-        LogMonitor monitor = new LogMonitor();
-
         String message = getName() + ": Test Message";
 
         Logger.getRootLogger().warn(message);
 
-        validateLogContainsMessage(monitor, message);
+        validateLogContainsMessage(_monitor, message);
 
         String LOG_RESET_TEXT = "Log Monitor Reset";
 
-        validateLogDoesNotContainsMessage(monitor, LOG_RESET_TEXT);
+        validateLogDoesNotContainsMessage(_monitor, LOG_RESET_TEXT);
 
-        monitor.reset();
+        _monitor.reset();
 
-        assertEquals("", monitor.readFile());
+        assertEquals("", _monitor.readFile());
     }
 
     public void testRead() throws IOException
     {
-        LogMonitor monitor = new LogMonitor();
-
         String message = getName() + ": Test Message";
 
         Logger.getRootLogger().warn(message);
 
-        String fileContents = monitor.readFile();
+        String fileContents = _monitor.readFile();
 
         assertTrue("Logged message not found when reading file.",
                    fileContents.contains(message));



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscr...@qpid.apache.org

Reply via email to