Revision: 8821
Author: gwt.mirror...@gmail.com
Date: Sun Sep 19 19:21:59 2010
Log: SimpleFoo now allows real creates and lookups.

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

Review by: p...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=8821

Modified:
/trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java
 /trunk/user/test/com/google/gwt/requestfactory/server/SimpleBar.java

=======================================
--- /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java Sun Sep 19 16:42:30 2010 +++ /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java Sun Sep 19 19:21:59 2010
@@ -250,38 +250,51 @@
   public void testPersistExistingEntityNewRelation() {
     delayTestFinish(5000);

-    SimpleBarProxy newBar = req.create(SimpleBarProxy.class);
-
- final RequestObject<Void> barReq = req.simpleBarRequest().persist(newBar);
-    newBar = barReq.edit(newBar);
-    newBar.setUserName("Amit");
-
-    final SimpleBarProxy finalNewBar = newBar;
-    req.simpleFooRequest().findSimpleFooById("999L").fire(
-        new Receiver<SimpleFooProxy>() {
-          @Override
-          public void onSuccess(SimpleFooProxy response) {
-            RequestObject<Void> fooReq = req.simpleFooRequest().persist(
-                response);
-            response = fooReq.edit(response);
-            response.setBarField(finalNewBar);
-            fooReq.fire(new Receiver<Void>() {
+    // Make a new bar
+    SimpleBarProxy makeABar = req.create(SimpleBarProxy.class);
+ RequestObject<SimpleBarProxy> persistRequest = req.simpleBarRequest().persistAndReturnSelf(
+        makeABar);
+    makeABar = persistRequest.edit(makeABar);
+    makeABar.setUserName("Amit");
+
+    persistRequest.fire(new Receiver<SimpleBarProxy>() {
+      @Override
+      public void onSuccess(final SimpleBarProxy persistedBar) {
+
+        // It was made, now find a foo to assign it to
+        req.simpleFooRequest().findSimpleFooById("999L").fire(
+            new Receiver<SimpleFooProxy>() {
               @Override
-              public void onSuccess(Void response) {
-                req.simpleFooRequest().findSimpleFooById("999L").with(
- "barField.userName").fire(new Receiver<SimpleFooProxy>() {
+              public void onSuccess(SimpleFooProxy response) {
+
+                // Found the foo, edit it
+ RequestObject<Void> fooReq = req.simpleFooRequest().persist(
+                    response);
+                response = fooReq.edit(response);
+                response.setBarField(persistedBar);
+                fooReq.fire(new Receiver<Void>() {
                   @Override
-                  public void onSuccess(SimpleFooProxy finalFooProxy) {
-                    // barReq hasn't been persisted, so old value
-                    assertEquals("FOO",
-                        finalFooProxy.getBarField().getUserName());
-                    finishTestAndReset();
+                  public void onSuccess(Void response) {
+
+                    // Foo was persisted, fetch it again check the goods
+                    req.simpleFooRequest().findSimpleFooById("999L").with(
+                        "barField.userName").fire(
+                        new Receiver<SimpleFooProxy>() {
+
+                          // Here it is
+                          @Override
+ public void onSuccess(SimpleFooProxy finalFooProxy) {
+                            assertEquals("Amit",
+                                finalFooProxy.getBarField().getUserName());
+                            finishTestAndReset();
+                          }
+                        });
                   }
                 });
               }
             });
-          }
-        });
+      }
+    });
   }

   /*
=======================================
--- /trunk/user/test/com/google/gwt/requestfactory/server/SimpleBar.java Sun Sep 19 16:42:30 2010 +++ /trunk/user/test/com/google/gwt/requestfactory/server/SimpleBar.java Sun Sep 19 18:13:59 2010
@@ -17,8 +17,10 @@

 import com.google.gwt.requestfactory.shared.Id;

-import java.util.Collections;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;

 import javax.servlet.http.HttpServletRequest;

@@ -29,16 +31,16 @@
   /**
    * DO NOT USE THIS UGLY HACK DIRECTLY! Call {...@link #get} instead.
    */
-  private static SimpleBar jreTestSingleton = new SimpleBar();
-
-  private static Long nextId = 1L;
+ private static Map<String, SimpleBar> jreTestSingleton = new HashMap<String, SimpleBar>();
+
+  private static long nextId = 2L;

   public static Long countSimpleBar() {
-    return 1L;
+      return (long) get().size();
   }

   public static List<SimpleBar> findAll() {
-    return Collections.singletonList(get());
+    return new ArrayList<SimpleBar>(get().values());
   }

   public static SimpleBar findSimpleBar(String id) {
@@ -46,11 +48,11 @@
   }

   public static SimpleBar findSimpleBarById(String id) {
-    get().setId(id);
-    return get();
+    return get().get(id);
   }

-  public static synchronized SimpleBar get() {
+  @SuppressWarnings("unchecked")
+  public static synchronized Map<String, SimpleBar> get() {
     HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
     if (req == null) {
       // May be in a JRE test case, use the the singleton
@@ -61,7 +63,7 @@
        * that doesn't allow any requests to be processed unless they're
        * associated with an existing session.
        */
-      SimpleBar value = (SimpleBar) req.getSession().getAttribute(
+ Map<String, SimpleBar> value = (Map<String, SimpleBar>) req.getSession().getAttribute(
           SimpleBar.class.getCanonicalName());
       if (value == null) {
         value = reset();
@@ -71,11 +73,22 @@
   }

   public static SimpleBar getSingleton() {
-    return get();
+    return findSimpleBar("1L");
   }

-  public static synchronized SimpleBar reset() {
-    SimpleBar instance = new SimpleBar();
+  public static synchronized Map<String, SimpleBar> reset() {
+    Map<String, SimpleBar> instance = new HashMap<String, SimpleBar>();
+    // fixtures
+    SimpleBar s1 = new SimpleBar();
+    s1.setId("1L");
+    s1.isNew = false;
+    instance.put(s1.getId(), s1);
+
+    SimpleBar s2 = new SimpleBar();
+    s2.setId("999L");
+    s2.isNew = false;
+    instance.put(s2.getId(), s2);
+
     HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
     if (req == null) {
       jreTestSingleton = instance;
@@ -85,14 +98,20 @@
     }
     return instance;
   }
+
+  static {
+    reset();
+  }

   Integer version = 1;

   @Id
-  private String id = "1L";
+  private String id = "999L";

   private String userName;

+  private boolean isNew = true;
+
   public SimpleBar() {
     version = 1;
     userName = "FOO";
@@ -111,8 +130,11 @@
   }

   public void persist() {
-    setId(Long.toString(nextId++));
-    get().setUserName(userName);
+    if (isNew) {
+      setId(Long.toString(nextId++));
+      isNew = false;
+      get().put(getId(), this);
+    }
   }

   public SimpleBar persistAndReturnSelf() {

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

Reply via email to