Revision: 9907
Author:   [email protected]
Date:     Fri Mar 25 07:04:13 2011
Log:      Cherry picking r9880 into releases/2.3m1

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

Modified:
/releases/2.3/user/src/com/google/gwt/requestfactory/server/LocatorServiceLayer.java /releases/2.3/user/src/com/google/gwt/requestfactory/server/ResolverServiceLayer.java /releases/2.3/user/src/com/google/gwt/requestfactory/server/ServiceLayer.java /releases/2.3/user/src/com/google/gwt/requestfactory/server/ServiceLayerDecorator.java /releases/2.3/user/test/com/google/gwt/requestfactory/RequestFactoryJreSuite.java /releases/2.3/user/test/com/google/gwt/requestfactory/RequestFactorySuite.java /releases/2.3/user/test/com/google/gwt/requestfactory/shared/ServiceInheritanceTest.java

=======================================
--- /releases/2.3/user/src/com/google/gwt/requestfactory/server/LocatorServiceLayer.java Tue Mar 22 11:45:18 2011 +++ /releases/2.3/user/src/com/google/gwt/requestfactory/server/LocatorServiceLayer.java Fri Mar 25 07:04:13 2011
@@ -56,7 +56,7 @@
     // Enclosing class may be a parent class, so invoke on service class
     Class<?> declaringClass = contextMethod.getDeclaringClass();
     Class<?> serviceClass =
- getTop().resolveServiceClass((Class<? extends RequestContext>) declaringClass); + getTop().resolveServiceClass(declaringClass.asSubclass(RequestContext.class));
     return locator.getInstance(serviceClass);
   }

=======================================
--- /releases/2.3/user/src/com/google/gwt/requestfactory/server/ResolverServiceLayer.java Tue Mar 22 11:45:18 2011 +++ /releases/2.3/user/src/com/google/gwt/requestfactory/server/ResolverServiceLayer.java Fri Mar 25 07:04:13 2011
@@ -119,7 +119,7 @@
   public Method resolveDomainMethod(Method requestContextMethod) {
     Class<?> declaringClass = requestContextMethod.getDeclaringClass();
     Class<?> searchIn =
- getTop().resolveServiceClass((Class<? extends RequestContext>) declaringClass); + getTop().resolveServiceClass(declaringClass.asSubclass(RequestContext.class));
     Class<?>[] parameterTypes = requestContextMethod.getParameterTypes();
     Class<?>[] domainArgs = new Class<?>[parameterTypes.length];
     for (int i = 0, j = domainArgs.length; i < j; i++) {
=======================================
--- /releases/2.3/user/test/com/google/gwt/requestfactory/RequestFactorySuite.java Fri Jan 14 04:27:42 2011 +++ /releases/2.3/user/test/com/google/gwt/requestfactory/RequestFactorySuite.java Fri Mar 25 07:04:13 2011
@@ -26,6 +26,7 @@
 import com.google.gwt.requestfactory.shared.BoxesAndPrimitivesTest;
 import com.google.gwt.requestfactory.shared.ComplexKeysTest;
 import com.google.gwt.requestfactory.shared.LocatorTest;
+import com.google.gwt.requestfactory.shared.ServiceInheritanceTest;

 import junit.framework.Test;

@@ -46,6 +47,7 @@
     suite.addTestSuite(RequestFactoryExceptionPropagationTest.class);
     suite.addTestSuite(RequestFactoryPolymorphicTest.class);
     suite.addTestSuite(RequestFactoryUnicodeEscapingTest.class);
+    suite.addTestSuite(ServiceInheritanceTest.class);
     return suite;
   }
 }
=======================================
--- /releases/2.3/user/test/com/google/gwt/requestfactory/shared/ServiceInheritanceTest.java Tue Mar 22 11:45:18 2011 +++ /releases/2.3/user/test/com/google/gwt/requestfactory/shared/ServiceInheritanceTest.java Fri Mar 25 07:04:13 2011
@@ -26,21 +26,17 @@
 public class ServiceInheritanceTest extends GWTTestCase {

   /**
-   * Generic locator returns an instance of the class named in
-   * the @{@link Service} annotation
+   * ServiceLocator that returns the base class or subclass implementation
+   * specified in the @{@link Service} annotation.
    */
-  public static class AnyServiceLocator implements ServiceLocator {
-
-    @Override
+  public static class SumServiceLocator implements ServiceLocator {
     public Object getInstance(Class<?> clazz) {
-      assertTrue(BaseImpl.class.isAssignableFrom(clazz));
-      try {
-        return clazz.newInstance();
-      } catch (InstantiationException e) {
-        throw new RuntimeException(e);
-      } catch (IllegalAccessException e) {
-        throw new RuntimeException(e);
-      }
+      if (BaseImpl.class.equals(clazz)) {
+        return new BaseImpl();
+      } else if (SubclassImpl.class.equals(clazz)) {
+        return new SubclassImpl();
+      }
+      return null;
     }
   }

@@ -48,42 +44,63 @@
    * The factory under test.
    */
   protected interface Factory extends RequestFactory {
-    SumService sumContext();
-    SumServiceBase sumBaseContext();
+    SumServiceBase baseContext();
+    SumServiceSub subContext();
   }

   /**
-   * Specifies a service which extends a base class
+   * Specifies the base class implementation
    */
-  @Service(value = SubclassImpl.class, locator = AnyServiceLocator.class)
-  interface SumService extends RequestContext {
+  @Service(value = BaseImpl.class, locator = SumServiceLocator.class)
+  interface SumServiceBase extends RequestContext {
     Request<Integer> add(int n);
+    Request<Integer> subtract(int n);
   }

   /**
-   * Specifies a service which is a base class
+   * Specifies the subclass implementation
    */
-  @Service(value = BaseImpl.class, locator = AnyServiceLocator.class)
-  interface SumServiceBase extends RequestContext {
+  @Service(value = SubclassImpl.class, locator = SumServiceLocator.class)
+  interface SumServiceSub extends RequestContext {
     Request<Integer> add(int n);
+    Request<Integer> subtract(int n);
   }

+  /**
+   * Base implementation of {@link SumServiceBase}
+   */
   static class BaseImpl {
-    protected int base;
+    protected int initialValue;

     public BaseImpl() {
-      base = 5;
+      initialValue = 5;
     }

     public Integer add(int n) {
-      return base + n;
+      return initialValue + n;
+    }
+
+    public Integer subtract(int n) {
+      return initialValue - n;
     }
   }

+  /**
+   * Subclass implementation of {@link SumServiceSub}
+   * inherits the add() method
+   */
   static class SubclassImpl extends BaseImpl {
     public SubclassImpl() {
-      // Distinguish from base service
-      base = 8;
+      /*
+       * Init with a different value to distinguish between base & subclass
+       * implementations in the tests
+       */
+      initialValue = 8;
+    }
+
+    @Override
+    public Integer subtract(int n) {
+      return 0;
     }
   }

@@ -97,12 +114,25 @@
   }

   /**
-   * Verify that the method can be invoked on the base class
-   * as well as the subclass
+   * Call a method inherited from a base class
+   */
+  public void testInvokeInheritedMethod() {
+    delayTestFinish(TEST_DELAY);
+    factory.subContext().add(13).fire(new Receiver<Integer>() {
+      @Override
+      public void onSuccess(Integer response) {
+        assertEquals((Integer) 21, response);
+        finishTest();
+      }
+    });
+  }
+
+  /**
+   * Call a method implemented in a base class
    */
   public void testInvokeMethodOnBaseClass() {
     delayTestFinish(TEST_DELAY);
-    factory.sumBaseContext().add(13).fire(new Receiver<Integer>() {
+    factory.baseContext().add(13).fire(new Receiver<Integer>() {
       @Override
       public void onSuccess(Integer response) {
         assertEquals((Integer) 18, response);
@@ -112,15 +142,14 @@
   }

   /**
-   * Verify that the method is invoked on the subclass,
-   * not the base class
+   * Call a method overridden in a subclass
    */
-  public void testInvokeMethodOnSubclass() {
+  public void testInvokeOverriddenMethod() {
     delayTestFinish(TEST_DELAY);
-    factory.sumContext().add(13).fire(new Receiver<Integer>() {
+    factory.subContext().subtract(3).fire(new Receiver<Integer>() {
       @Override
       public void onSuccess(Integer response) {
-        assertEquals((Integer) 21, response);
+        assertEquals((Integer) 0, response);
         finishTest();
       }
     });
@@ -133,7 +162,7 @@
   }

   @Override
-  protected void gwtSetUp() throws Exception {
+  protected void gwtSetUp() {
     factory = createFactory();
   }

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

Reply via email to