brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [3/4] brooklyn-server git commit: Fix CloudExplorerLiveTest
Date Wed, 18 Jan 2017 10:41:10 GMT
Fix CloudExplorerLiveTest

Use System.setOut() so it gets the real stdout/stderr.
Had to make test more lenient, as this also gets log statements in 
stdout.

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/88a0f067
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/88a0f067
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/88a0f067

Branch: refs/heads/master
Commit: 88a0f067f92e94e3c8c9106cbe898edf31680e51
Parents: fabfcdb
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Jan 17 22:02:43 2017 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Jan 18 09:48:53 2017 +0000

----------------------------------------------------------------------
 .../brooklyn/cli/CloudExplorerLiveTest.java     | 59 ++++++++++++++++----
 1 file changed, 48 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/88a0f067/server-cli/src/test/java/org/apache/brooklyn/cli/CloudExplorerLiveTest.java
----------------------------------------------------------------------
diff --git a/server-cli/src/test/java/org/apache/brooklyn/cli/CloudExplorerLiveTest.java b/server-cli/src/test/java/org/apache/brooklyn/cli/CloudExplorerLiveTest.java
index 4511d51..cda96a5 100644
--- a/server-cli/src/test/java/org/apache/brooklyn/cli/CloudExplorerLiveTest.java
+++ b/server-cli/src/test/java/org/apache/brooklyn/cli/CloudExplorerLiveTest.java
@@ -32,6 +32,7 @@ import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Joiner;
 import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
@@ -174,34 +175,70 @@ public class CloudExplorerLiveTest {
         Cli<BrooklynCommand> parser = new Main().cliBuilder().build();
         
         BrooklynCommand command = parser.parse(args);
-        command.stdout = new PrintStream(stdoutStream);
-        command.stderr = new PrintStream(stderrStream);
-        command.stdin = instream;
+        
+        InputStream origIn = System.in;
+        PrintStream origOut = System.out;
+        PrintStream origErr = System.err;
         try {
+            System.setIn(instream);
+            System.setOut(new PrintStream(stdoutStream));
+            System.setErr(new PrintStream(stderrStream));
+
             command.call();
         } finally {
+            System.setIn(origIn);
+            System.setOut(origOut);
+            System.setOut(origErr);
             stdout = new String(stdoutStream.toByteArray());
             stderr = new String(stderrStream.toByteArray());
         }
     }
-    
+
     private List<String> assertAndStipSingleLocationHeader(String stdout) {
+        return assertAndStipSingleLocationHeader(stdout, false);
+    }
+    
+    /**
+     * When running in non-strict mode, it filters out non-matching lines. For example, if
there
+     * are log statements in the middle of the output then it still lets the test pass.
+     */
+    private List<String> assertAndStipSingleLocationHeader(String stdout, boolean strict)
{
         List<String> lines = ImmutableList.copyOf(Splitter.on("\n").omitEmptyStrings().split(stdout));
 
-        String errmsg = "lines="+lines;
+        String errmsg = "lines=\n"+Joiner.on("\n").join(lines);
         
         int nextLineCount = 0;
-        assertEquals(lines.get(nextLineCount++), "Location {", errmsg);
-        assertEquals(lines.get(lines.size()-1), "}", errmsg);
-        assertTrue(lines.get(nextLineCount++).startsWith("\tprovider: "), errmsg);
-        assertTrue(lines.get(nextLineCount++).startsWith("\tdisplayName: "), errmsg);
-        assertTrue(lines.get(nextLineCount++).startsWith("\tidentity: "), errmsg);
+        
+        // TODO Should be stricter - e.g. don't get noisy logging before the real output
+        if (!strict) {
+            boolean atStart = false;
+            while (!atStart && lines.size() > nextLineCount) {
+                if (lines.get(nextLineCount).equals("Location {")) {
+                    atStart = true;
+                } else {
+                    nextLineCount++;
+                }
+            }
+            assertTrue(atStart, errmsg);
+        }
+        
+        assertEquals(lines.get(nextLineCount++), "Location {", "lineNum="+nextLineCount+";
" + errmsg);
+        assertEquals(lines.get(lines.size()-1), "}", "lineNum="+nextLineCount+"; " + errmsg);
+        assertTrue(lines.get(nextLineCount++).startsWith("\tprovider: "), "lineNum="+nextLineCount+";
" + errmsg);
+        assertTrue(lines.get(nextLineCount++).startsWith("\tdisplayName: "), "lineNum="+nextLineCount+";
" + errmsg);
+        assertTrue(lines.get(nextLineCount++).startsWith("\tidentity: "), "lineNum="+nextLineCount+";
" + errmsg);
         if (lines.get(nextLineCount).startsWith("\tendpoint: ")) nextLineCount++;
         if (lines.get(nextLineCount).startsWith("\tregion: ")) nextLineCount++;
         
         List<String> result = Lists.newArrayList();
         for (String line : lines.subList(nextLineCount, lines.size()-1)) {
-            assertTrue(line.startsWith("\t"), errmsg);
+            if (strict) {
+                assertTrue(line.startsWith("\t"), "lineNum="+nextLineCount+"; " + errmsg);
+            } else {
+                if (!line.startsWith("\t")) {
+                    continue; // ignore line (maybe a log line in the middle?!)
+                }
+            }
             result.add(line.substring(1));
         }
         return result;


Mime
View raw message