Revision: 7521
Author: guillaume.ry...@google.com
Date: Tue Feb  2 13:27:34 2010
Log: Adds explicit pure Java test support to GWTTestCase, via a setter.
Calling setForcePureJava(true) on a GWTTestCase forces running the test in pure Java mode (non-GWT). This feature has the same effect than returning null in getModuleName(), with the major difference that it can be called by test suite builders (which cannot reliably subclass tests in a generic way).
Fixes bug: gwtSetUp and gwtTearDown should be called in pure Java mode.
Also hardens existing JUnit tests to check that GWTTestCase tests are run in client or pure Java mode, as appropriate.

Review by: scottb, amitmanjhi

http://code.google.com/p/google-web-toolkit/source/detail?r=7521

Added:
 /trunk/user/test/com/google/gwt/junit/client/ForcePureJavaTest.java
 /trunk/user/test/com/google/gwt/junit/client/NullModuleNameTest.java
Modified:
 /trunk/user/src/com/google/gwt/junit/client/GWTTestCase.java
/trunk/user/super/com/google/gwt/junit/translatable/com/google/gwt/junit/client/GWTTestCase.java
 /trunk/user/test/com/google/gwt/junit/NonGwtTestSuite.java
 /trunk/user/test/com/google/gwt/junit/client/ModuleOneTest.java
 /trunk/user/test/com/google/gwt/junit/client/ModuleOneTest2.java
 /trunk/user/test/com/google/gwt/junit/client/ModuleTwoTest.java

=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/junit/client/ForcePureJavaTest.java Tue Feb 2 13:27:34 2010
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2010 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.client;
+
+import com.google.gwt.core.client.GWT;
+
+/**
+ * Test case running in pure Java mode (non-GWT) due to a call to
+ * {...@link GWTTestCase#setForcePureJava}.
+ */
+public class ForcePureJavaTest extends GWTTestCase {
+
+  private int gwtSetUpCalls;
+
+  public ForcePureJavaTest() {
+    setForcePureJava(true);
+  }
+
+  @Override
+  public String getModuleName() {
+    return "invalid.module";
+  }
+
+  @Override
+  protected void gwtSetUp() throws Exception {
+    gwtSetUpCalls++;
+    super.gwtSetUp();
+  }
+
+  public void testGwtSetUpCalled() {
+    assertEquals(1, gwtSetUpCalls);
+  }
+
+  public void testIsNotClient() {
+    assertFalse(GWT.isClient());
+  }
+
+  public void testSetForcePureJava_inTestMethod() {
+    setForcePureJava(false);
+  }
+
+  public void testIsNotClient_afterChangeForcePureJavaInTest() {
+    assertFalse(GWT.isClient());
+  }
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/junit/client/NullModuleNameTest.java Tue Feb 2 13:27:34 2010
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2010 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.client;
+
+import com.google.gwt.core.client.GWT;
+
+/**
+ * Test case running in pure Java mode (non-GWT) due to {...@link #getModuleName}
+ * returning <code>null</code>.
+ */
+public class NullModuleNameTest extends GWTTestCase {
+
+  private int gwtSetUpCalls;
+
+  @Override
+  public String getModuleName() {
+    return null;
+  }
+
+  @Override
+  protected void gwtSetUp() throws Exception {
+    gwtSetUpCalls++;
+    super.gwtSetUp();
+  }
+
+  public void testGwtSetUpCalled() {
+    assertEquals(1, gwtSetUpCalls);
+  }
+
+  public void testIsNotClient() {
+    assertFalse(GWT.isClient());
+  }
+}
=======================================
--- /trunk/user/src/com/google/gwt/junit/client/GWTTestCase.java Thu Jan 28 08:39:11 2010 +++ /trunk/user/src/com/google/gwt/junit/client/GWTTestCase.java Tue Feb 2 13:27:34 2010
@@ -163,6 +163,15 @@
    */
   protected TestResult testResult = null;

+  /**
+ * Whether this test case should be always run in pure Java mode (non-GWT).
+   * Setting this to <code>true</code> has the same effect as returning
+   * <code>null</code> in {...@link #getModuleName}.
+   *
+   * @see #isPureJava
+   */
+  private boolean forcePureJava;
+
   /**
    * The {...@link Strategy} used by this test.
    */
@@ -246,7 +255,10 @@
    * be included.
    *
* @return the fully qualified name of a module, or <code>null</code> to run
-   *         as a non-GWT test case
+   *         as a pure Java (non-GWT) test case (same effect as passing
+   *         <code>true</code> to {...@link #setForcePureJava})
+   *
+   * @see #isPureJava
    */
   public abstract String getModuleName();

@@ -266,14 +278,27 @@
    * Get the synthetic module name, which includes the synthetic extension
    * defined by the {...@link Strategy}.
    *
- * @return the synthetic module name or null if this is not really a GWT test + * @return the synthetic module name, or <code>null</code> if this test case
+   *         is run in pure Java mode (non-GWT)
+   *
+   * @see #isPureJava
    */
   public final String getSyntheticModuleName() {
-    String moduleName = getModuleName();
-    if (moduleName == null) {
+    if (isPureJava()) {
       return null;
-    }
-    return moduleName + "." + getStrategy().getSyntheticModuleExtension();
+    } else {
+ return getModuleName() + "." + getStrategy().getSyntheticModuleExtension();
+    }
+  }
+
+  /**
+ * Returns whether this test case should be run in pure Java mode (non-GWT). + * Returns <code>true</code> if and only if {...@link #getModuleName} returns
+   * <code>null</code>, or {...@link #setForcePureJava} was last invoked with
+   * <code>true</code>.
+   */
+  public boolean isPureJava() {
+    return forcePureJava || (getModuleName() == null);
   }

   /**
@@ -285,6 +310,22 @@
     testResult = result;
     super.run(result);
   }
+
+  /**
+ * Specifies whether this test case should be always run in pure Java mode
+   * (non-GWT). Passing <code>true</code> has the same effect as returning
+   * <code>null</code> in {...@link #getModuleName}. The setting is
+   * <code>false</code> by default.
+   *
+ * @param forcePureJava <code>true</code> to always run this test case in pure + * Java mode (non-GWT); <code>false</code> to run this test case in GWT + * mode if {...@link #getModuleName} does not return <code>null</code>
+   *
+   * @see #isPureJava
+   */
+  public void setForcePureJava(boolean forcePureJava) {
+    this.forcePureJava = forcePureJava;
+  }

   @Override
   public void setName(String name) {
@@ -378,7 +419,9 @@
* A replacement for JUnit's {...@link #setUp()} method. This method runs once * per test method in your subclass, just before your each test method runs * and can be used to perform initialization. Override this method instead of
-   * {...@link #setUp()}.
+   * {...@link #setUp()}. This method is run even in pure Java mode (non-GWT).
+   *
+   * @see #setForcePureJava
    */
   protected void gwtSetUp() throws Exception {
   }
@@ -387,7 +430,9 @@
* A replacement for JUnit's {...@link #tearDown()} method. This method runs once * per test method in your subclass, just after your each test method runs and
    * can be used to perform cleanup. Override this method instead of
-   * {...@link #tearDown()}.
+ * {...@link #tearDown()}. This method is run even in pure Java mode (non-GWT).
+   *
+   * @see #setForcePureJava
    */
   protected void gwtTearDown() throws Exception {
   }
@@ -405,12 +450,10 @@
+ "\" has none. Perhaps you used TestSuite.addTest() instead of addTestClass()?");
     }

-    String moduleName = getModuleName();
-    if (moduleName != null) {
-      JUnitShell.runTest(this, testResult);
-    } else {
-      // Run as a non-GWT test
+    if (isPureJava()) {
       super.runTest();
+    } else {
+      JUnitShell.runTest(this, testResult);
     }
   }

@@ -421,7 +464,9 @@
    */
   @Override
   protected final void setUp() throws Exception {
-    // implemented in the translatable version of this class
+    if (isPureJava()) {
+      gwtSetUp();
+    }
   }

   /**
@@ -439,6 +484,8 @@
    */
   @Override
   protected final void tearDown() throws Exception {
-    // implemented in the translatable version of this class
+    if (isPureJava()) {
+      gwtTearDown();
+    }
   }
 }
=======================================
--- /trunk/user/super/com/google/gwt/junit/translatable/com/google/gwt/junit/client/GWTTestCase.java Thu Jan 22 08:32:30 2009 +++ /trunk/user/super/com/google/gwt/junit/translatable/com/google/gwt/junit/client/GWTTestCase.java Tue Feb 2 13:27:34 2010
@@ -199,6 +199,10 @@
   }

   public abstract String getModuleName();
+
+  public boolean isPureJava() {
+    return false;
+  }

   @Override
   public void runBare() throws Throwable {
@@ -206,6 +210,11 @@
     runTest();
     // No tearDown call here; we do it from reportResults.
   }
+
+  public void setForcePureJava(boolean forcePureJava) {
+    // Ignore completely. The test is being run in GWT mode,
+    // hence assumed not to be pure Java.
+  }

   // CHECKSTYLE_OFF
   protected JUnitResult __getOrCreateTestResult() {
=======================================
--- /trunk/user/test/com/google/gwt/junit/NonGwtTestSuite.java Mon Nov 23 16:21:36 2009 +++ /trunk/user/test/com/google/gwt/junit/NonGwtTestSuite.java Tue Feb 2 13:27:34 2010
@@ -15,15 +15,18 @@
  */
 package com.google.gwt.junit;

+import com.google.gwt.junit.client.ForcePureJavaTest;
 import com.google.gwt.junit.client.ModuleOneTest;
 import com.google.gwt.junit.client.ModuleOneTest2;
 import com.google.gwt.junit.client.ModuleTwoTest;
+import com.google.gwt.junit.client.NullModuleNameTest;

 import junit.framework.Test;
 import junit.framework.TestSuite;

 /**
* Tests that a normal test suite will run even if modules are out of order.
+ * Also checks that tests are run in pure Java mode (non-GWT).
  */
 public class NonGwtTestSuite {

@@ -31,9 +34,11 @@
     // This is intentionally not a GWTTestSuite.
     TestSuite suite = new TestSuite();

+    suite.addTestSuite(ForcePureJavaTest.class);
     suite.addTestSuite(ModuleOneTest.class);
     suite.addTestSuite(ModuleTwoTest.class);
     suite.addTestSuite(ModuleOneTest2.class);
+    suite.addTestSuite(NullModuleNameTest.class);

     return suite;
   }
=======================================
--- /trunk/user/test/com/google/gwt/junit/client/ModuleOneTest.java Thu Sep 17 13:59:15 2009 +++ /trunk/user/test/com/google/gwt/junit/client/ModuleOneTest.java Tue Feb 2 13:27:34 2010
@@ -15,6 +15,8 @@
  */
 package com.google.gwt.junit.client;

+import com.google.gwt.core.client.GWT;
+
 /**
  * A test in the first module.
  */
@@ -24,7 +26,7 @@
     return "com.google.gwt.junit.JUnitTest";
   }

-  public void testTrue() {
-    assertTrue(true);
+  public void testIsClient() {
+    assertTrue(GWT.isClient());
   }
 }
=======================================
--- /trunk/user/test/com/google/gwt/junit/client/ModuleOneTest2.java Thu Sep 17 13:59:15 2009 +++ /trunk/user/test/com/google/gwt/junit/client/ModuleOneTest2.java Tue Feb 2 13:27:34 2010
@@ -15,6 +15,8 @@
  */
 package com.google.gwt.junit.client;

+import com.google.gwt.core.client.GWT;
+
 /**
  * Another test in the first module.
  */
@@ -24,7 +26,7 @@
     return "com.google.gwt.junit.JUnitTest";
   }

-  public void testTrue() {
-    assertTrue(true);
+  public void testIsClient() {
+    assertTrue(GWT.isClient());
   }
 }
=======================================
--- /trunk/user/test/com/google/gwt/junit/client/ModuleTwoTest.java Thu Sep 17 13:59:15 2009 +++ /trunk/user/test/com/google/gwt/junit/client/ModuleTwoTest.java Tue Feb 2 13:27:34 2010
@@ -15,6 +15,8 @@
  */
 package com.google.gwt.junit.client;

+import com.google.gwt.core.client.GWT;
+
 /**
  * A test in the second module.
  */
@@ -24,7 +26,7 @@
     return "com.google.gwt.junit.JUnitTest2";
   }

-  public void testTrue() {
-    assertTrue(true);
+  public void testIsClient() {
+    assertTrue(GWT.isClient());
   }
 }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to