continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From batkin...@apache.org
Subject svn commit: r1659453 - in /continuum/trunk/continuum-api/src: main/java/org/apache/continuum/utils/shell/ test/java/org/apache/continuum/utils/shell/
Date Fri, 13 Feb 2015 04:58:53 GMT
Author: batkinson
Date: Fri Feb 13 04:58:53 2015
New Revision: 1659453

URL: http://svn.apache.org/r1659453
Log:
Changed file consumer so command output can be tailed.

Added:
    continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java
Modified:
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java?rev=1659453&r1=1659452&r2=1659453&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
Fri Feb 13 04:58:53 2015
@@ -146,7 +146,6 @@ public class DefaultShellCommandHelper
         }
         finally
         {
-            fileConsumer.flush();
             fileConsumer.close();
         }
     }

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java?rev=1659453&r1=1659452&r2=1659453&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
Fri Feb 13 04:58:53 2015
@@ -20,6 +20,7 @@ package org.apache.continuum.utils.shell
  */
 
 import java.io.File;
+import java.io.FileDescriptor;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -43,6 +44,8 @@ public class FileOutputConsumer
         throws IOException
     {
         this.writer = new PrintWriter( new FileWriter( outputFile ) );
+        this.writer.close();  // make the file exist immediately
+        this.writer = new PrintWriter( new FileWriter( outputFile, true ), true );  // append
to created file
     }
 
     public void consume( String line )
@@ -51,12 +54,6 @@ public class FileOutputConsumer
             writer.println( line );
     }
 
-    public void flush()
-    {
-        if ( writer != null )
-            writer.flush();
-    }
-
     public void close()
     {
         if ( writer != null )

Added: continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java?rev=1659453&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java
(added)
+++ continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java
Fri Feb 13 04:58:53 2015
@@ -0,0 +1,114 @@
+package org.apache.continuum.utils.shell;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.*;
+
+/**
+ * @see org.apache.continuum.utils.shell.FileOutputConsumer
+ */
+public class FileOutputConsumerTest
+{
+
+    File outputFile;
+
+    FileOutputConsumer consumer;
+
+    @Before
+    public void setUp()
+        throws IOException
+    {
+        outputFile = File.createTempFile( FileOutputConsumerTest.class.getName(), "output"
);
+        outputFile.delete();  // we want to test whether it is created
+        assertFalse( "file should not exist", outputFile.exists() );
+        consumer = new FileOutputConsumer( outputFile );
+    }
+
+    @After
+    public void tearDown()
+    {
+        outputFile.delete();
+        consumer = null;
+    }
+
+    @Test
+    public void noOutputCreatesEmptyFile()
+        throws IOException
+    {
+        consumer.close();
+        assertFileEmpty();
+    }
+
+    @Test
+    public void fileExistsAfterClose()
+        throws IOException
+    {
+        String[] lines = { "first", "second", "third" };
+        writeContent( lines );
+        consumer.close();
+        assertFileExists();
+        assertFileContents( lines );
+    }
+
+    @Test
+    public void newConsumerTruncatesFile()
+        throws IOException
+    {
+        // Write content to the file
+        String[] content1 = { "should be overwritten" };
+        writeContent( content1 );
+        consumer.close();
+        assertFileExists();
+        assertFileContents( content1 );
+
+        // New consumer should truncate (overwrite) the previous contents
+        consumer = new FileOutputConsumer( outputFile );
+        assertFileExists();
+        assertFileEmpty();
+    }
+
+    @Test
+    public void contentsAvailableBeforeClose()
+        throws IOException
+    {
+        String[] content = { "contents", "are not", "critical" };
+        writeContent( content );
+        assertFileContents( content );
+    }
+
+    private void writeContent( String... lines )
+    {
+        for ( String line : lines )
+            consumer.consume( line );
+    }
+
+    private void assertFileExists()
+    {
+        assertTrue( "file should exist", outputFile.exists() );
+    }
+
+    private void assertFileEmpty()
+        throws IOException
+    {
+        assertEquals( "file should have been empty", "", FileUtils.readFileToString( outputFile
) );
+    }
+
+    private void assertFileContents( String... contents )
+        throws IOException
+    {
+        StringBuilder finalContents = new StringBuilder();
+        if ( contents.length >= 1 )
+        {
+            for ( String line : contents )
+                finalContents.append( String.format( "%s%n", line ) );
+        }
+        assertEquals( "file did not contain expected contents", finalContents.toString(),
+                      FileUtils.readFileToString( outputFile ) );
+    }
+}



Mime
View raw message