Allow LogConfiguration to be used with GWTMockUtilities.disarm()

Also fixes a small error in HtmlLogFormatter.

Fixes issue 8050 and issue 5447

Change-Id: Ia52e4ce2dcd2f4c750534d53bcb17cd842b87e69
Review-Link: https://gwt-review.googlesource.com/#/c/2111/

Review by: mdempsky@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@11551 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/logging/client/HtmlLogFormatter.java b/user/src/com/google/gwt/logging/client/HtmlLogFormatter.java
index ad1dd73..74cc1c1 100644
--- a/user/src/com/google/gwt/logging/client/HtmlLogFormatter.java
+++ b/user/src/com/google/gwt/logging/client/HtmlLogFormatter.java
@@ -37,7 +37,7 @@
   // TODO(unnurg): Handle the outputting of Throwables.
   @Override
   public String format(LogRecord event) {
-    StringBuilder html = new StringBuilder(getHtmlPrefix(event));
+    StringBuilder html = new StringBuilder();
     html.append(getHtmlPrefix(event));
     html.append(getRecordInfo(event, " "));
     html.append(getEscaped(event.getMessage()));
diff --git a/user/src/com/google/gwt/logging/client/LogConfiguration.java b/user/src/com/google/gwt/logging/client/LogConfiguration.java
index ef94ca4..fda4c75 100644
--- a/user/src/com/google/gwt/logging/client/LogConfiguration.java
+++ b/user/src/com/google/gwt/logging/client/LogConfiguration.java
@@ -40,8 +40,7 @@
   /**
    * Implementation which does nothing and compiles out if logging is disabled.
    */
-  private static class LogConfigurationImplNull
-  implements LogConfigurationImpl {
+  private static class LogConfigurationImplNull implements LogConfigurationImpl {
     public void configureClientSideLogging() { }
 
     public boolean loggingIsEnabled() {
@@ -56,8 +55,7 @@
   /**
    * Implementation which is used when logging is enabled.
    */
-  private static class LogConfigurationImplRegular
-  implements LogConfigurationImpl {
+  private static class LogConfigurationImplRegular implements LogConfigurationImpl {
     // Keep a reference to the root logger after we configure it because
     // if we don't, the JRE implementation of LogManager (which is used in
     // Dev Mode) will sometimes garbage collect it, since they only keep
@@ -65,6 +63,8 @@
     private Logger root;
 
     public void configureClientSideLogging() {
+      assert GWT.isClient();
+
       root = Logger.getLogger("");
       // In DevMode, this root logger will have a parent and we do not want this
       // root logger to pass messages to it's parent, so we set
@@ -153,8 +153,7 @@
   /**
    * Implementation which is used when logging.enabled is set to SEVERE.
    */
-  private static class LogConfigurationImplSevere
-  extends LogConfigurationImplRegular {
+  private static class LogConfigurationImplSevere extends LogConfigurationImplRegular {
     @Override
     public boolean loggingIsEnabled(Level level) {
       return level.intValue() >= 1000;
@@ -164,22 +163,28 @@
   /**
    * Implementation which is used when logging.enabled is set to WARNING.
    */
-  private static class LogConfigurationImplWarning
-  extends LogConfigurationImplRegular {
+  private static class LogConfigurationImplWarning extends LogConfigurationImplRegular {
     @Override
     public boolean loggingIsEnabled(Level level) {
       return level.intValue() >= 900;
     }
   }
 
-  private static LogConfigurationImpl impl =
-    GWT.create(LogConfigurationImplNull.class);
+  private static LogConfigurationImpl impl = GWT.create(LogConfigurationImplNull.class);
 
   public static boolean loggingIsEnabled() {
+    if (impl == null) {
+      // case when GWTMockUtilities.disarm(); should be optimized out when compiled to JS
+      return true;
+    }
     return impl.loggingIsEnabled();
   }
 
   public static boolean loggingIsEnabled(Level level) {
+    if (impl == null) {
+      // case when GWTMockUtilities.disarm(); should be optimized out when compiled to JS
+      return true;
+    }
     return impl.loggingIsEnabled(level);
   }
 
diff --git a/user/test/com/google/gwt/logging/LoggingJreSuite.java b/user/test/com/google/gwt/logging/LoggingJreSuite.java
new file mode 100644
index 0000000..8a88852
--- /dev/null
+++ b/user/test/com/google/gwt/logging/LoggingJreSuite.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2013 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
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.logging;
+
+import com.google.gwt.logging.client.LogConfigurationJreTest;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Logging tests that tests functionality in JRE.
+ */
+public class LoggingJreSuite {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite("Non-browser tests for com.google.gwt.logging");
+    suite.addTestSuite(LogConfigurationJreTest.class);
+    return suite;
+  }
+}
diff --git a/user/test/com/google/gwt/logging/client/LogConfigurationJreTest.java b/user/test/com/google/gwt/logging/client/LogConfigurationJreTest.java
new file mode 100644
index 0000000..26ef9a5
--- /dev/null
+++ b/user/test/com/google/gwt/logging/client/LogConfigurationJreTest.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2013 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
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.logging.client;
+
+import com.google.gwt.junit.GWTMockUtilities;
+
+import junit.framework.TestCase;
+
+import java.util.logging.Level;
+
+/**
+ * Tests that LogConfiguration can be used outside a GWT context (e.g. JRE tests)
+ */
+public class LogConfigurationJreTest extends TestCase {
+
+  @Override
+  public void setUp() {
+    GWTMockUtilities.disarm();
+  }
+
+  @Override
+  public void tearDown() {
+    GWTMockUtilities.restore();
+  }
+
+  public void testLogConfiguration() {
+    assertTrue(LogConfiguration.loggingIsEnabled());
+    assertTrue(LogConfiguration.loggingIsEnabled(Level.FINEST));
+  }
+}