Fix NPE with invalid sourceLevel (and tests).

Fixed the testing infrastructure that was passing tests when it
should not.

Fixed a "what is essential is invisible to the eye" kind of bug
in ArgHandlerSourceLevel.

Change-Id: I9838b6a235a5961fb7e04570f12745aa6b674497
(cherry picked from commit 1082c7efbd95bf4d892ed407ec68cf070e610cb3)
diff --git a/dev/core/src/com/google/gwt/dev/util/arg/ArgHandlerSourceLevel.java b/dev/core/src/com/google/gwt/dev/util/arg/ArgHandlerSourceLevel.java
index cff6e98..52096ef 100644
--- a/dev/core/src/com/google/gwt/dev/util/arg/ArgHandlerSourceLevel.java
+++ b/dev/core/src/com/google/gwt/dev/util/arg/ArgHandlerSourceLevel.java
@@ -47,7 +47,8 @@
 
   @Override
   public String[] getTagArgs() {
-    return new String[]{"[" + AUTO_SELECT + "," + Joiner.on(",").join(SourceLevel.values()) + "]"};
+    return new String[]{"[" + Joiner.on(", ").skipNulls().join(AUTO_SELECT, null,
+        SourceLevel.values()) + "]"};
   }
 
   @Override
@@ -57,9 +58,9 @@
       return true;
     }
     SourceLevel level = SourceLevel.fromString(value);
-    if (value == null) {
-      System.err.println("Source level must be one of [" + AUTO_SELECT +
-          Joiner.on(",").join(SourceLevel.values()) + "].");
+    if (level == null) {
+      System.err.println("Source level must be one of [" +
+          Joiner.on(", ").skipNulls().join(AUTO_SELECT, null, SourceLevel.values()) + "].");
       return false;
     }
     options.setSourceLevel(level);
diff --git a/dev/core/test/com/google/gwt/dev/ArgProcessorBaseTest.java b/dev/core/test/com/google/gwt/dev/ArgProcessorBaseTest.java
index 11ce7a1..ece89dd 100644
--- a/dev/core/test/com/google/gwt/dev/ArgProcessorBaseTest.java
+++ b/dev/core/test/com/google/gwt/dev/ArgProcessorBaseTest.java
@@ -44,16 +44,17 @@
   }
 
   public void testOptionOrderIsPrecedenceArgs() {
-    assertProcessSuccess(argProcessor);
+    assertProcessSuccess(argProcessor, new String[0]);
     assertEquals(9, options.getOptimizationLevel());
 
-    assertProcessSuccess(argProcessor, "-optimize", "5");
+    assertProcessSuccess(argProcessor, new String[] {"-optimize", "5"});
     assertEquals(5, options.getOptimizationLevel());
 
-    assertProcessSuccess(argProcessor, "-optimize", "5", "-draftCompile");
+    assertProcessSuccess(argProcessor, new String[] {"-optimize", "5", "-draftCompile"});
     assertEquals(0, options.getOptimizationLevel());
 
-    assertProcessSuccess(argProcessor, "-optimize", "5", "-draftCompile", "-optimize", "9");
+    assertProcessSuccess(argProcessor,
+        new String[] {"-optimize", "5", "-draftCompile", "-optimize", "9"});
     assertEquals(9, options.getOptimizationLevel());
   }
 }
diff --git a/dev/core/test/com/google/gwt/dev/ArgProcessorTestBase.java b/dev/core/test/com/google/gwt/dev/ArgProcessorTestBase.java
index 6ce9012..12bd4d9 100644
--- a/dev/core/test/com/google/gwt/dev/ArgProcessorTestBase.java
+++ b/dev/core/test/com/google/gwt/dev/ArgProcessorTestBase.java
@@ -1,12 +1,12 @@
 /*
  * Copyright 2009 Google Inc.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
- * 
+ *
  * http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -20,33 +20,12 @@
 import junit.framework.TestCase;
 
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
 import java.io.PrintStream;
 
 /**
  * Base class for argument processor testing.
  */
 public abstract class ArgProcessorTestBase extends TestCase {
-
-  private static class MockOutputStream extends OutputStream {
-    private boolean isEmpty = true;
-
-    public boolean isEmpty() {
-      return isEmpty;
-    }
-
-    @Override
-    public void write(byte[] b, int off, int len) throws IOException {
-      isEmpty = false;
-    }
-
-    @Override
-    public void write(int b) throws IOException {
-      isEmpty = false;
-    }
-  }
-
   /*
    * The "compute installation directory" dance.
    */
@@ -62,20 +41,25 @@
   }
 
   protected static void assertProcessFailure(ArgProcessorBase argProcessor,
-      String... args) {
+      String stringContainedInErrorMessage, String[] args) {
     PrintStream oldErrStream = System.err;
-    MockOutputStream myErrStream = new MockOutputStream();
+    ByteArrayOutputStream myErrStream = new ByteArrayOutputStream();
     try {
       System.setErr(new PrintStream(myErrStream, true));
       assertFalse(argProcessor.processArgs(args));
     } finally {
+      System.err.flush();
       System.setErr(oldErrStream);
     }
-    assertFalse(myErrStream.isEmpty());
+    String outputString = new String(myErrStream.toByteArray()).split("[\n\r]")[0];
+    assertFalse(outputString.isEmpty());
+
+    assertTrue("\"" + stringContainedInErrorMessage + "\" is not part of the error message: \""
+        + outputString + "\"", outputString.contains(stringContainedInErrorMessage));
   }
 
   protected static void assertProcessSuccess(ArgProcessorBase argProcessor,
-      String... args) {
+      String[] args) {
     PrintStream oldErrStream = System.err;
     ByteArrayOutputStream myErrStream = new ByteArrayOutputStream();
     try {
diff --git a/dev/core/test/com/google/gwt/dev/CompilerTest.java b/dev/core/test/com/google/gwt/dev/CompilerTest.java
index 643ecf3..ed988fc 100644
--- a/dev/core/test/com/google/gwt/dev/CompilerTest.java
+++ b/dev/core/test/com/google/gwt/dev/CompilerTest.java
@@ -35,10 +35,10 @@
   }
 
   public void testAllValidArgs() {
-    assertProcessSuccess(argProcessor, "-logLevel", "DEBUG", "-style",
+    assertProcessSuccess(argProcessor, new String[] {"-logLevel", "DEBUG", "-style",
         "PRETTY", "-ea", "-XdisableAggressiveOptimization", "-gen", "myGen",
         "-war", "myWar", "-workDir", "myWork", "-extra", "myExtra",
-        "-localWorkers", "2", "-sourceLevel", "1.7", "c.g.g.h.H", "my.Module");
+        "-localWorkers", "2", "-sourceLevel", "1.7", "c.g.g.h.H", "my.Module"});
 
     assertEquals(new File("myGen").getAbsoluteFile(),
         options.getGenDir().getAbsoluteFile());
@@ -65,7 +65,7 @@
   }
 
   public void testDefaultArgs() {
-    assertProcessSuccess(argProcessor, "c.g.g.h.H");
+    assertProcessSuccess(argProcessor, new String[] {"c.g.g.h.H"});
 
     assertEquals(null, options.getGenDir());
     assertEquals(new File("war").getAbsoluteFile(),
@@ -89,9 +89,11 @@
   }
 
   public void testForbiddenArgs() {
-    assertProcessFailure(argProcessor, "-out", "www");
-    assertProcessFailure(argProcessor, "-sourceLevel", "ssss");
-    assertProcessFailure(argProcessor, "-sourceLevel", "1.5");
+    assertProcessFailure(argProcessor, "Unknown argument", new String[] {"-out", "www"});
+    assertProcessFailure(argProcessor, "Source level must be one of",
+        new String[] {"-sourceLevel", "ssss"});
+    assertProcessFailure(argProcessor, "Source level must be one of",
+        new String[] {"-sourceLevel", "1.5"});
   }
 
   /**
diff --git a/dev/core/test/com/google/gwt/dev/HostedModeTest.java b/dev/core/test/com/google/gwt/dev/HostedModeTest.java
index 2a37387..edb55af 100644
--- a/dev/core/test/com/google/gwt/dev/HostedModeTest.java
+++ b/dev/core/test/com/google/gwt/dev/HostedModeTest.java
@@ -1,12 +1,12 @@
 /*
  * Copyright 2009 Google Inc.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
- * 
+ *
  * http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -46,11 +46,11 @@
   }
 
   public void testAllValidArgs() {
-    assertProcessSuccess(argProcessor, "-port", "8080", "-whitelist", "white",
+    assertProcessSuccess(argProcessor, new String[] {"-port", "8080", "-whitelist", "white",
         "-blacklist", "black", "-logLevel", "DEBUG", "-noserver", "-server",
         MySCL.class.getName(), "-gen", "myGen", "-war", "myWar", "-workDir",
         "myWork", "-extra", "myExtra", "-startupUrl", "http://www.google.com/",
-        "-startupUrl", "foo", "c.g.g.h.H", "my.Module");
+        "-startupUrl", "foo", "c.g.g.h.H", "my.Module"});
 
     assertNotNull(BrowserWidgetHostChecker.matchWhitelisted("white"));
     assertNotNull(BrowserWidgetHostChecker.matchBlacklisted("black"));
@@ -93,7 +93,7 @@
   }
 
   public void testNoServer() {
-    assertProcessSuccess(argProcessor, "-noserver", "c.g.g.h.H");
+    assertProcessSuccess(argProcessor, new String[] {"-noserver", "c.g.g.h.H"});
 
     assertTrue(options.isNoServer());
     assertNull(options.getServletContainerLauncher());
@@ -103,8 +103,8 @@
   }
 
   public void testNoServerOverridesServer() {
-    assertProcessSuccess(argProcessor, "-server", MySCL.class.getName(),
-        "-noserver", "c.g.g.h.H");
+    assertProcessSuccess(argProcessor,
+        new String[] {"-server", MySCL.class.getName(), "-noserver", "c.g.g.h.H"});
 
     assertTrue(options.isNoServer());
     assertSame(MySCL.class, options.getServletContainerLauncher().getClass());
@@ -114,7 +114,7 @@
   }
 
   public void testDefaultArgs() {
-    assertProcessSuccess(argProcessor, "c.g.g.h.H");
+    assertProcessSuccess(argProcessor, new String[] {"c.g.g.h.H"});
 
     assertEquals(null, options.getGenDir());
     assertEquals(new File("war").getAbsoluteFile(),
@@ -142,6 +142,6 @@
   }
 
   public void testForbiddenArgs() {
-    assertProcessFailure(argProcessor, "-out", "www");
+    assertProcessFailure(argProcessor, "Unknown argument", new String[] {"-out", "www"});
   }
 }