Add ability to GWTMockUtilities to stub out Messages for better unit testing.

Review at http://gwt-code-reviews.appspot.com/1581804


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10723 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/junit/GWTDummyBridge.java b/user/src/com/google/gwt/junit/GWTDummyBridge.java
index f3adda7..ca0cb6e 100644
--- a/user/src/com/google/gwt/junit/GWTDummyBridge.java
+++ b/user/src/com/google/gwt/junit/GWTDummyBridge.java
@@ -17,6 +17,7 @@
 
 import com.google.gwt.core.client.GWTBridge;
 import com.google.gwt.dev.About;
+import com.google.gwt.i18n.client.Messages;
 
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -29,12 +30,26 @@
 class GWTDummyBridge extends GWTBridge {
   private static final Logger logger = Logger.getLogger(GWTDummyBridge.class.getName());
 
+  private boolean fakeMessages = false;
+  
   /**
    * Returns null.
    */
   @Override
   public <T> T create(Class<?> classLiteral) {
-    return null;
+    if (fakeMessages && (classLiteral != null ) && Messages.class.isAssignableFrom(classLiteral)) {
+      return (T) FakeMessagesMaker.create((Class<? extends Messages>) classLiteral);
+    } else {
+      return null;
+    }
+  }
+  
+  /**
+   * Makes the create() method return mock/fake Messages, when the specified
+   * class is assignable to Messages.
+   */
+  public void enableMockMessages() {
+    fakeMessages = true;
   }
 
   /**
diff --git a/user/src/com/google/gwt/junit/GWTMockUtilities.java b/user/src/com/google/gwt/junit/GWTMockUtilities.java
index 906aea4..63f1e4a 100644
--- a/user/src/com/google/gwt/junit/GWTMockUtilities.java
+++ b/user/src/com/google/gwt/junit/GWTMockUtilities.java
@@ -26,6 +26,8 @@
  * other UIObjects.
  */
 public class GWTMockUtilities {
+  
+  private static GWTDummyBridge dummyBridge;
 
   /**
    * Replace the normal GWT.create() behavior with a method that returns null
@@ -66,12 +68,24 @@
    * </pre>
    */
   public static void disarm() {
-    GWTBridge bridge = new GWTDummyBridge();
-    setGwtBridge(bridge);
+    dummyBridge = new GWTDummyBridge();
+    setGwtBridge(dummyBridge);
   }
 
   public static void restore() {
     setGwtBridge(null);
+    dummyBridge = null;
+  }
+  
+  /**
+   * After {@see #disarm()} replaces the normal GWT.create(), this method
+   * enables Messages to be faked out. The fake instance creation is delegated
+   * to {@see FakeMessagesMaker}.  
+   */
+  public static void returnMockMessages() {
+    if (dummyBridge != null) {
+      dummyBridge.enableMockMessages();
+    }
   }
 
   /**
diff --git a/user/test/com/google/gwt/junit/GWTMockUtilitiesTest.java b/user/test/com/google/gwt/junit/GWTMockUtilitiesTest.java
new file mode 100644
index 0000000..6c52099
--- /dev/null
+++ b/user/test/com/google/gwt/junit/GWTMockUtilitiesTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2011 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.junit;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.i18n.client.Messages;
+import com.google.gwt.i18n.client.LocalizableResource.Description;
+import com.google.gwt.i18n.client.Messages.DefaultMessage;
+
+import junit.framework.TestCase;
+
+/**
+ * Test of {@see GWTMockUtilities}
+ */
+public class GWTMockUtilitiesTest extends TestCase {
+  interface MyMessages extends Messages {
+    @DefaultMessage("Isn''t this the fakiest?")
+    @Description("A sample message to be tested.")
+    String myMessage();
+  }
+
+  public void testWithoutDisarm() {
+    try {
+      GWT.create(MyMessages.class);
+      fail("Calling GWT.create() without disarming should have failed.");
+    } catch (UnsupportedOperationException ex) {
+      // expected this exception
+    }
+  }
+  
+  public void testDisarm() {
+    GWTMockUtilities.disarm();
+    assertNull(GWT.create(MyMessages.class));
+    GWTMockUtilities.restore();
+  }
+  
+  public void testReturnMockMessages() {
+    GWTMockUtilities.disarm();
+    GWTMockUtilities.returnMockMessages();
+    assertNotNull(GWT.create(MyMessages.class));
+    assertNull(GWT.create(String.class));
+    assertNull(GWT.create(null));
+    GWTMockUtilities.restore();
+  }
+}
diff --git a/user/test/com/google/gwt/junit/JUnitSuite.java b/user/test/com/google/gwt/junit/JUnitSuite.java
index 72ed980..e225dff 100644
--- a/user/test/com/google/gwt/junit/JUnitSuite.java
+++ b/user/test/com/google/gwt/junit/JUnitSuite.java
@@ -41,6 +41,7 @@
     suite.addTestSuite(CompileStrategyTest.class);
 
     suite.addTestSuite(FakeMessagesMakerTest.class);
+    suite.addTestSuite(GWTMockUtilitiesTest.class);
     suite.addTestSuite(JUnitMessageQueueTest.class);
     suite.addTestSuite(GWTTestCaseNoClientTest.class);