SimpleFoo now allows real creates and lookups. Review at http://gwt-code-reviews.appspot.com/900801 Review by: pdr@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@8821 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java b/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java index c2fec22..23eb58e 100644 --- a/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java +++ b/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java
@@ -250,38 +250,51 @@ public void testPersistExistingEntityNewRelation() { delayTestFinish(5000); - SimpleBarProxy newBar = req.create(SimpleBarProxy.class); + // Make a new bar + SimpleBarProxy makeABar = req.create(SimpleBarProxy.class); + RequestObject<SimpleBarProxy> persistRequest = req.simpleBarRequest().persistAndReturnSelf( + makeABar); + makeABar = persistRequest.edit(makeABar); + makeABar.setUserName("Amit"); - final RequestObject<Void> barReq = req.simpleBarRequest().persist(newBar); - newBar = barReq.edit(newBar); - newBar.setUserName("Amit"); + persistRequest.fire(new Receiver<SimpleBarProxy>() { + @Override + public void onSuccess(final SimpleBarProxy persistedBar) { - 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>() { + // 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(); + } + }); } }); } }); - } - }); + } + }); } /*
diff --git a/user/test/com/google/gwt/requestfactory/server/SimpleBar.java b/user/test/com/google/gwt/requestfactory/server/SimpleBar.java index bfda0ac..5a7c838 100644 --- a/user/test/com/google/gwt/requestfactory/server/SimpleBar.java +++ b/user/test/com/google/gwt/requestfactory/server/SimpleBar.java
@@ -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; @@ -86,13 +99,19 @@ 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() {