Revision: 8925
Author: [email protected]
Date: Sun Oct  3 11:34:32 2010
Log: Fix NullPointerException in SimpleEntityProxyId.
Patch by: bobv
Review by: rjrjr
Suggested by: pjulien

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

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

Added:
 /trunk/user/test/com/google/gwt/requestfactory/client/impl
/trunk/user/test/com/google/gwt/requestfactory/client/impl/SimpleEntityProxyIdTest.java
Modified:
/trunk/user/src/com/google/gwt/requestfactory/client/impl/SimpleEntityProxyId.java
 /trunk/user/test/com/google/gwt/requestfactory/RequestFactoryJreSuite.java

=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/requestfactory/client/impl/SimpleEntityProxyIdTest.java Sun Oct 3 11:34:32 2010
@@ -0,0 +1,85 @@
+/*
+ * 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.requestfactory.client.impl;
+
+import com.google.gwt.requestfactory.shared.EntityProxy;
+import com.google.gwt.requestfactory.shared.SimpleBarProxy;
+import com.google.gwt.requestfactory.shared.SimpleFooProxy;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests {...@link SimpleEntityProxyId}.
+ */
+public class SimpleEntityProxyIdTest extends TestCase {
+
+  public void testEquality() {
+    SimpleEntityProxyId<EntityProxy> client1 = id(EntityProxy.class, 1);
+    // equal to self
+    assertTrue(isStable(client1, client1));
+    // equal to identical client id
+    assertTrue(isStable(client1, id(EntityProxy.class, 1)));
+
+    // Persist and check again
+    client1.setServerId("server1");
+    // equal to self
+    assertTrue(isStable(client1, client1));
+    // equal to identical client id
+    assertTrue(isStable(client1, id(EntityProxy.class, 1)));
+
+ SimpleEntityProxyId<EntityProxy> server1 = id(EntityProxy.class, "server1");
+    assertTrue(isStable(server1, id(EntityProxy.class, "server1")));
+
+    /*
+ * Compare a server-only id the persisted client id, this should be false
+     * since the hashcodes would vary.
+     */
+    assertFalse(isStable(client1, server1));
+  }
+
+  public void testInequality() {
+ assertFalse(isStable(id(EntityProxy.class, 1), id(EntityProxy.class, 2)));
+
+    assertFalse(isStable(id(EntityProxy.class, "server1"), id(
+        EntityProxy.class, "server2")));
+
+    // Same client-side id, but different types
+ assertFalse(isStable(id(SimpleFooProxy.class, 1), id(SimpleBarProxy.class,
+        1)));
+
+    // Same server id, but different types
+    assertFalse(isStable(id(SimpleFooProxy.class, "server1"), id(
+        SimpleBarProxy.class, "server1")));
+  }
+
+  private <T extends EntityProxy> SimpleEntityProxyId<T> id(Class<T> clazz,
+      int clientId) {
+    return new SimpleEntityProxyId<T>(clazz, clientId);
+  }
+
+  private <T extends EntityProxy> SimpleEntityProxyId<T> id(Class<T> clazz,
+      String serverId) {
+    return new SimpleEntityProxyId<T>(clazz, serverId);
+  }
+
+  /**
+ * Assert that the id behaves with the stable sematics that are desired for
+   * client code.
+   */
+ private boolean isStable(SimpleEntityProxyId<?> a, SimpleEntityProxyId<?> b) {
+    return a.equals(b) && b.equals(a) && a.hashCode() == b.hashCode();
+  }
+}
=======================================
--- /trunk/user/src/com/google/gwt/requestfactory/client/impl/SimpleEntityProxyId.java Fri Oct 1 18:15:55 2010 +++ /trunk/user/src/com/google/gwt/requestfactory/client/impl/SimpleEntityProxyId.java Sun Oct 3 11:34:32 2010
@@ -76,6 +76,9 @@
   }

   public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
     if (!(o instanceof SimpleEntityProxyId<?>)) {
       return false;
     }
@@ -83,8 +86,19 @@
     if (!proxyClass.equals(other.proxyClass)) {
       return false;
     }
-    return (clientId != NEVER_EPHEMERAL && clientId == other.clientId)
-        || serverId.equals(other.serverId);
+
+    if (clientId != NEVER_EPHEMERAL && clientId == other.clientId) {
+      /*
+ * Unexpected: It should be the case that locally-created ids are never
+       * aliased and will be caught by the first if statement.
+       */
+      return true;
+    }
+
+    if (serverId != null && serverId.equals(other.serverId)) {
+      return true;
+    }
+    return false;
   }

   public int getClientId() {
=======================================
--- /trunk/user/test/com/google/gwt/requestfactory/RequestFactoryJreSuite.java Fri Oct 1 18:15:55 2010 +++ /trunk/user/test/com/google/gwt/requestfactory/RequestFactoryJreSuite.java Sun Oct 3 11:34:32 2010
@@ -15,6 +15,7 @@
  */
 package com.google.gwt.requestfactory;

+import com.google.gwt.requestfactory.client.impl.SimpleEntityProxyIdTest;
 import com.google.gwt.requestfactory.server.JsonRequestProcessorTest;
import com.google.gwt.requestfactory.server.ReflectionBasedOperationRegistryTest;
 import com.google.gwt.requestfactory.server.RequestPropertyTest;
@@ -29,6 +30,7 @@
   public static Test suite() {
     TestSuite suite = new TestSuite(
         "requestfactory package tests that require the JRE");
+    suite.addTestSuite(SimpleEntityProxyIdTest.class);
     suite.addTestSuite(JsonRequestProcessorTest.class);
     suite.addTestSuite(ReflectionBasedOperationRegistryTest.class);
     suite.addTestSuite(RequestPropertyTest.class);

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

Reply via email to