Repeats the existing tests with String id and confirm that the tests pass. Patch by: amitmanjhi Review by: rjrjr (desk review) git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@8836 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/test/com/google/gwt/requestfactory/RequestFactorySuite.java b/user/test/com/google/gwt/requestfactory/RequestFactorySuite.java index c04d1f4..2e7f96e 100644 --- a/user/test/com/google/gwt/requestfactory/RequestFactorySuite.java +++ b/user/test/com/google/gwt/requestfactory/RequestFactorySuite.java
@@ -19,6 +19,7 @@ import com.google.gwt.requestfactory.client.EditorTest; import com.google.gwt.requestfactory.client.FindServiceTest; import com.google.gwt.requestfactory.client.RequestFactoryExceptionHandlerTest; +import com.google.gwt.requestfactory.client.RequestFactoryStringTest; import com.google.gwt.requestfactory.client.RequestFactoryTest; import com.google.gwt.requestfactory.client.impl.DeltaValueStoreJsonImplTest; import com.google.gwt.requestfactory.client.impl.ProxyJsoImplTest; @@ -38,6 +39,7 @@ suite.addTestSuite(ValueStoreJsonImplTest.class); suite.addTestSuite(DeltaValueStoreJsonImplTest.class); suite.addTestSuite(RequestFactoryTest.class); + suite.addTestSuite(RequestFactoryStringTest.class); suite.addTestSuite(RequestFactoryExceptionHandlerTest.class); suite.addTestSuite(FindServiceTest.class); return suite;
diff --git a/user/test/com/google/gwt/requestfactory/client/RequestFactoryStringTest.java b/user/test/com/google/gwt/requestfactory/client/RequestFactoryStringTest.java new file mode 100644 index 0000000..18aaa45 --- /dev/null +++ b/user/test/com/google/gwt/requestfactory/client/RequestFactoryStringTest.java
@@ -0,0 +1,694 @@ +/* + * 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; + +import com.google.gwt.requestfactory.client.impl.ProxyImpl; +import com.google.gwt.requestfactory.shared.Receiver; +import com.google.gwt.requestfactory.shared.RequestObject; +import com.google.gwt.requestfactory.shared.ServerFailure; +import com.google.gwt.requestfactory.shared.SimpleBarProxy; +import com.google.gwt.requestfactory.shared.SimpleFooStringProxy; +import com.google.gwt.requestfactory.shared.Violation; + +import java.util.List; +import java.util.Set; + +/** + * Tests for {@link com.google.gwt.requestfactory.shared.RequestFactory}. + */ +public class RequestFactoryStringTest extends RequestFactoryTestBase { + /* + * DO NOT USE finishTest(). Instead, call finishTestAndReset(); + */ + + private class FailFixAndRefire<T> extends Receiver<T> { + + private final SimpleFooStringProxy proxy; + private final RequestObject<T> request; + private boolean voidReturnExpected; + + FailFixAndRefire(SimpleFooStringProxy proxy, RequestObject<T> request) { + this.proxy = request.edit(proxy); + this.request = request; + } + + @Override + public void onSuccess(T response) { + /* + * Make sure your class path includes: + * + * tools/lib/apache/log4j/log4j-1.2.16.jar + * tools/lib/hibernate/validator/hibernate-validator-4.1.0.Final.jar + * tools/lib/slf4j/slf4j-api/slf4j-api-1.6.1.jar + * tools/lib/slf4j/slf4j-log4j12/slf4j-log4j12-1.6.1.jar + */ + fail("Violations expected (you might be missing some jars, " + + "see the comment above this line)"); + } + + @Override + public void onViolation(Set<Violation> errors) { + + // size violation expected + + assertEquals(1, errors.size()); + Violation error = errors.iterator().next(); + assertEquals("userName", error.getPath()); + assertEquals("size must be between 3 and 30", error.getMessage()); + assertEquals(proxy.stableId(), error.getProxyId()); + + // Now re-used the request to fix the edit + + proxy.setUserName("long enough"); + request.fire(new Receiver<T>() { + @Override + public void onSuccess(T response) { + if (voidReturnExpected) { + assertNull(response); + } else { + assertEquals(proxy.stableId(), + ((SimpleFooStringProxy) response).stableId()); + } + finishTestAndReset(); + } + }); + } + + void doVoidTest() { + voidReturnExpected = true; + doTest(); + } + + void doTest() { + proxy.setUserName("a"); // too short + request.fire(this); + } + } + + @Override + public String getModuleName() { + return "com.google.gwt.requestfactory.RequestFactorySuite"; + } + + public void testDummyCreate() { + delayTestFinish(5000); + + final SimpleFooStringProxy foo = req.create(SimpleFooStringProxy.class); + Object futureId = foo.getId(); + assertEquals(futureId, foo.getId()); + assertTrue(((ProxyImpl) foo).isFuture()); + RequestObject<SimpleFooStringProxy> fooReq = req.simpleFooStringRequest().persistAndReturnSelf( + foo); + fooReq.fire(new Receiver<SimpleFooStringProxy>() { + + @Override + public void onSuccess(final SimpleFooStringProxy returned) { + Object futureId = foo.getId(); + assertEquals(futureId, foo.getId()); + assertTrue(((ProxyImpl) foo).isFuture()); + + checkStableIdEquals(foo, returned); + finishTestAndReset(); + } + }); + } + + public void testDummyCreateBar() { + delayTestFinish(5000); + + final SimpleBarProxy foo = req.create(SimpleBarProxy.class); + Object futureId = foo.getId(); + assertEquals(futureId, foo.getId()); + assertTrue(((ProxyImpl) foo).isFuture()); + RequestObject<SimpleBarProxy> fooReq = req.simpleBarRequest().persistAndReturnSelf( + foo); + fooReq.fire(new Receiver<SimpleBarProxy>() { + + @Override + public void onSuccess(final SimpleBarProxy returned) { + Object futureId = foo.getId(); + assertEquals(futureId, foo.getId()); + assertTrue(((ProxyImpl) foo).isFuture()); + + checkStableIdEquals(foo, returned); + finishTestAndReset(); + } + }); + } + + public void testFetchEntity() { + delayTestFinish(5000); + req.simpleFooStringRequest().findSimpleFooStringById("999x").fire( + new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy response) { + assertEquals(42, (int) response.getIntId()); + assertEquals("GWT", response.getUserName()); + assertEquals(8L, (long) response.getLongField()); + assertEquals(com.google.gwt.requestfactory.shared.SimpleEnum.FOO, + response.getEnumField()); + assertEquals(null, response.getBarField()); + finishTestAndReset(); + } + }); + } + + public void testFetchEntityWithRelation() { + delayTestFinish(5000); + req.simpleFooStringRequest().findSimpleFooStringById("999x").with("barField").fire( + new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy response) { + assertEquals(42, (int) response.getIntId()); + assertEquals("GWT", response.getUserName()); + assertEquals(8L, (long) response.getLongField()); + assertEquals(com.google.gwt.requestfactory.shared.SimpleEnum.FOO, + response.getEnumField()); + assertNotNull(response.getBarField()); + finishTestAndReset(); + } + }); + } + + public void testGetEventBus() { + assertEquals(eventBus, req.getEventBus()); + } + + public void testGetListStringId() { + delayTestFinish(5000); + + // String ids + req.simpleBarRequest().findAll().fire(new Receiver<List<SimpleBarProxy>>() { + @Override + public void onSuccess(List<SimpleBarProxy> response) { + assertEquals(2, response.size()); + for (SimpleBarProxy bar : response) { + assertNotNull(bar.stableId()); + finishTestAndReset(); + } + } + }); + } + + public void testGetListLongId() { + delayTestFinish(5000); + + // Long ids + req.simpleFooStringRequest().findAll().with("barField.userName").fire( + new Receiver<List<SimpleFooStringProxy>>() { + @Override + public void onSuccess(List<SimpleFooStringProxy> response) { + assertEquals(1, response.size()); + for (SimpleFooStringProxy foo : response) { + assertNotNull(foo.stableId()); + assertEquals("FOO", foo.getBarField().getUserName()); + finishTestAndReset(); + } + } + }); + } + + /* + * tests that (a) any method can have a side effect that is handled correctly. + * (b) instance methods are handled correctly and (c) a request cannot be + * reused after a successful response is received. (Yet?) + */ + public void testMethodWithSideEffects() { + delayTestFinish(5000); + + req.simpleFooStringRequest().findSimpleFooStringById("999x").fire( + new Receiver<SimpleFooStringProxy>() { + + @Override + public void onSuccess(SimpleFooStringProxy newFoo) { + final RequestObject<Long> mutateRequest = req.simpleFooStringRequest().countSimpleFooWithUserNameSideEffect( + newFoo); + newFoo = mutateRequest.edit(newFoo); + newFoo.setUserName("Ray"); + mutateRequest.fire(new Receiver<Long>() { + @Override + public void onSuccess(Long response) { + assertCannotFire(mutateRequest); + assertEquals(new Long(1L), response); + // TODO: listen to create events also + + // confirm that the instance method did have the desired + // sideEffect. + req.simpleFooStringRequest().findSimpleFooStringById("999x").fire( + new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy finalFoo) { + assertEquals("Ray", finalFoo.getUserName()); + finishTestAndReset(); + } + }); + } + + }); + + try { + newFoo.setUserName("Barney"); + fail(); + } catch (IllegalStateException e) { + /* pass, cannot change a request that is in flight */ + } + } + }); + } + + /* + * TODO: all these tests should check the final values. It will be easy when + * we have better persistence than the singleton pattern. + */ + public void testPersistExistingEntityExistingRelation() { + delayTestFinish(5000); + + req.simpleBarRequest().findSimpleBarById("999L").fire( + new Receiver<SimpleBarProxy>() { + @Override + public void onSuccess(final SimpleBarProxy barProxy) { + req.simpleFooStringRequest().findSimpleFooStringById("999x").fire( + new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy fooProxy) { + RequestObject<Void> updReq = req.simpleFooStringRequest().persist( + fooProxy); + fooProxy = updReq.edit(fooProxy); + fooProxy.setBarField(barProxy); + updReq.fire(new Receiver<Void>() { + @Override + public void onSuccess(Void response) { + + finishTestAndReset(); + } + }); + } + }); + } + }); + } + + /* + * Find Entity Create Entity2 Relate Entity2 to Entity Persist Entity + */ + public void testPersistExistingEntityNewRelation() { + delayTestFinish(5000); + + // 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.simpleFooStringRequest().findSimpleFooStringById("999x").fire( + new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy response) { + + // Found the foo, edit it + RequestObject<Void> fooReq = req.simpleFooStringRequest().persist( + response); + response = fooReq.edit(response); + response.setBarField(persistedBar); + fooReq.fire(new Receiver<Void>() { + @Override + public void onSuccess(Void response) { + + // Foo was persisted, fetch it again check the goods + req.simpleFooStringRequest().findSimpleFooStringById("999x").with( + "barField.userName").fire( + new Receiver<SimpleFooStringProxy>() { + + // Here it is + @Override + public void onSuccess(SimpleFooStringProxy finalFooProxy) { + assertEquals("Amit", + finalFooProxy.getBarField().getUserName()); + finishTestAndReset(); + } + }); + } + }); + } + }); + } + }); + } + + /* + * Find Entity2 Create Entity, Persist Entity Relate Entity2 to Entity Persist + * Entity + */ + public void testPersistNewEntityExistingRelation() { + delayTestFinish(5000); + SimpleFooStringProxy newFoo = req.create(SimpleFooStringProxy.class); + + final RequestObject<Void> fooReq = req.simpleFooStringRequest().persist(newFoo); + + newFoo = fooReq.edit(newFoo); + newFoo.setUserName("Ray"); + + final SimpleFooStringProxy finalFoo = newFoo; + req.simpleBarRequest().findSimpleBarById("999L").fire( + new Receiver<SimpleBarProxy>() { + @Override + public void onSuccess(SimpleBarProxy response) { + finalFoo.setBarField(response); + fooReq.fire(new Receiver<Void>() { + @Override + public void onSuccess(Void response) { + req.simpleFooStringRequest().findSimpleFooStringById("999x").fire( + new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy finalFooProxy) { + // newFoo hasn't been persisted, so userName is the old + // value. + assertEquals("GWT", finalFooProxy.getUserName()); + finishTestAndReset(); + } + }); + } + }); + } + }); + } + + /* + * Create Entity, Persist Entity Create Entity2, Perist Entity2 relate Entity2 + * to Entity Persist + */ + public void testPersistNewEntityNewRelation() { + delayTestFinish(5000); + SimpleFooStringProxy newFoo = req.create(SimpleFooStringProxy.class); + SimpleBarProxy newBar = req.create(SimpleBarProxy.class); + + final RequestObject<SimpleFooStringProxy> fooReq = req.simpleFooStringRequest().persistAndReturnSelf( + newFoo); + + newFoo = fooReq.edit(newFoo); + newFoo.setUserName("Ray"); + + final RequestObject<SimpleBarProxy> barReq = req.simpleBarRequest().persistAndReturnSelf( + newBar); + newBar = barReq.edit(newBar); + newBar.setUserName("Amit"); + + fooReq.fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(final SimpleFooStringProxy persistedFoo) { + barReq.fire(new Receiver<SimpleBarProxy>() { + @Override + public void onSuccess(final SimpleBarProxy persistedBar) { + assertEquals("Ray", persistedFoo.getUserName()); + final RequestObject<Void> fooReq2 = req.simpleFooStringRequest().persist( + persistedFoo); + SimpleFooStringProxy editablePersistedFoo = fooReq2.edit(persistedFoo); + editablePersistedFoo.setBarField(persistedBar); + fooReq2.fire(new Receiver<Void>() { + @Override + public void onSuccess(Void response) { + req.simpleFooStringRequest().findSimpleFooStringById("999x").with( + "barField.userName").fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy finalFooProxy) { + assertEquals("Amit", + finalFooProxy.getBarField().getUserName()); + finishTestAndReset(); + } + }); + } + }); + } + }); + } + }); + } + + public void testPersistRecursiveRelation() { + delayTestFinish(5000); + + SimpleFooStringProxy rayFoo = req.create(SimpleFooStringProxy.class); + final RequestObject<SimpleFooStringProxy> persistRay = req.simpleFooStringRequest().persistAndReturnSelf( + rayFoo); + rayFoo = persistRay.edit(rayFoo); + rayFoo.setUserName("Ray"); + rayFoo.setFooField(rayFoo); + persistRay.fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(final SimpleFooStringProxy persistedRay) { + finishTestAndReset(); + } + }); + } + + public void testPersistRelation() { + delayTestFinish(5000); + + SimpleFooStringProxy rayFoo = req.create(SimpleFooStringProxy.class); + final RequestObject<SimpleFooStringProxy> persistRay = req.simpleFooStringRequest().persistAndReturnSelf( + rayFoo); + rayFoo = persistRay.edit(rayFoo); + rayFoo.setUserName("Ray"); + + persistRay.fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(final SimpleFooStringProxy persistedRay) { + SimpleBarProxy amitBar = req.create(SimpleBarProxy.class); + final RequestObject<SimpleBarProxy> persistAmit = req.simpleBarRequest().persistAndReturnSelf( + amitBar); + amitBar = persistAmit.edit(amitBar); + amitBar.setUserName("Amit"); + + persistAmit.fire(new Receiver<SimpleBarProxy>() { + @Override + public void onSuccess(SimpleBarProxy persistedAmit) { + + final RequestObject<SimpleFooStringProxy> persistRelationship = req.simpleFooStringRequest().persistAndReturnSelf( + persistedRay).with("barField"); + SimpleFooStringProxy newRec = persistRelationship.edit(persistedRay); + newRec.setBarField(persistedAmit); + + persistRelationship.fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy relatedRay) { + assertEquals("Amit", relatedRay.getBarField().getUserName()); + finishTestAndReset(); + } + }); + } + }); + } + }); + } + + public void testProxysAsInstanceMethodParams() { + delayTestFinish(5000); + req.simpleFooStringRequest().findSimpleFooStringById("999x").fire( + new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy response) { + SimpleBarProxy bar = req.create(SimpleBarProxy.class); + RequestObject<String> helloReq = req.simpleFooStringRequest().hello( + response, bar); + bar = helloReq.edit(bar); + bar.setUserName("BAR"); + helloReq.fire(new Receiver<String>() { + @Override + public void onSuccess(String response) { + assertEquals("Greetings BAR from GWT", response); + finishTestAndReset(); + } + }); + } + }); + } + + public void testServerFailure() { + delayTestFinish(5000); + + SimpleFooStringProxy newFoo = req.create(SimpleFooStringProxy.class); + final RequestObject<SimpleFooStringProxy> persistRequest = req.simpleFooStringRequest().persistAndReturnSelf( + newFoo); + + final SimpleFooStringProxy mutableFoo = persistRequest.edit(newFoo); + mutableFoo.setPleaseCrash(42); // 42 is the crash causing magic number + + persistRequest.fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onFailure(ServerFailure error) { + assertEquals("Server Error: THIS EXCEPTION IS EXPECTED BY A TEST", + error.getMessage()); + assertEquals("", error.getExceptionType()); + assertEquals("", error.getStackTraceString()); + + // Now show that we can fix the error and try again with the same + // request + + mutableFoo.setPleaseCrash(24); // Only 42 crashes + persistRequest.fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy response) { + finishTestAndReset(); + } + }); + } + + public void onSuccess(SimpleFooStringProxy response) { + fail("Failure expected but onSuccess() was called"); + } + + @Override + public void onViolation(Set<Violation> errors) { + fail("Failure expected but onViolation() was called"); + } + }); + } + + public void testStableId() { + delayTestFinish(5000); + + final SimpleFooStringProxy foo = req.create(SimpleFooStringProxy.class); + final Object futureId = foo.getId(); + assertTrue(((ProxyImpl) foo).isFuture()); + RequestObject<SimpleFooStringProxy> fooReq = req.simpleFooStringRequest().persistAndReturnSelf( + foo); + + final SimpleFooStringProxy newFoo = fooReq.edit(foo); + assertEquals(futureId, foo.getId()); + assertTrue(((ProxyImpl) foo).isFuture()); + assertEquals(futureId, newFoo.getId()); + assertTrue(((ProxyImpl) newFoo).isFuture()); + + newFoo.setUserName("GWT basic user"); + fooReq.fire(new Receiver<SimpleFooStringProxy>() { + + @Override + public void onSuccess(final SimpleFooStringProxy returned) { + assertEquals(futureId, foo.getId()); + assertTrue(((ProxyImpl) foo).isFuture()); + assertEquals(futureId, newFoo.getId()); + assertTrue(((ProxyImpl) newFoo).isFuture()); + + assertFalse(((ProxyImpl) returned).isFuture()); + + checkStableIdEquals(foo, returned); + checkStableIdEquals(newFoo, returned); + + RequestObject<SimpleFooStringProxy> editRequest = req.simpleFooStringRequest().persistAndReturnSelf( + returned); + final SimpleFooStringProxy editableFoo = editRequest.edit(returned); + editableFoo.setUserName("GWT power user"); + editRequest.fire(new Receiver<SimpleFooStringProxy>() { + + @Override + public void onSuccess(SimpleFooStringProxy returnedAfterEdit) { + checkStableIdEquals(editableFoo, returnedAfterEdit); + assertEquals(returnedAfterEdit.getId(), returned.getId()); + finishTestAndReset(); + } + }); + } + }); + } + + public void testViolationAbsent() { + delayTestFinish(5000); + + SimpleFooStringProxy newFoo = req.create(SimpleFooStringProxy.class); + final RequestObject<Void> fooReq = req.simpleFooStringRequest().persist(newFoo); + + newFoo = fooReq.edit(newFoo); + newFoo.setUserName("Amit"); // will not cause violation. + + fooReq.fire(new Receiver<Void>() { + @Override + public void onSuccess(Void ignore) { + finishTestAndReset(); + } + }); + } + + public void testViolationsOnCreate() { + delayTestFinish(5000); + + SimpleFooStringProxy newFoo = req.create(SimpleFooStringProxy.class); + final RequestObject<SimpleFooStringProxy> create = req.simpleFooStringRequest().persistAndReturnSelf( + newFoo); + new FailFixAndRefire<SimpleFooStringProxy>(newFoo, create).doTest(); + } + + public void testViolationsOnCreateVoidReturn() { + delayTestFinish(5000); + + SimpleFooStringProxy newFoo = req.create(SimpleFooStringProxy.class); + final RequestObject<Void> create = req.simpleFooStringRequest().persist(newFoo); + new FailFixAndRefire<Void>(newFoo, create).doVoidTest(); + } + + public void testViolationsOnEdit() { + delayTestFinish(5000); + + fooCreationRequest().fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy returned) { + RequestObject<SimpleFooStringProxy> editRequest = req.simpleFooStringRequest().persistAndReturnSelf( + returned); + new FailFixAndRefire<SimpleFooStringProxy>(returned, editRequest).doTest(); + } + }); + } + + public void testViolationsOnEditVoidReturn() { + delayTestFinish(5000); + + fooCreationRequest().fire(new Receiver<SimpleFooStringProxy>() { + @Override + public void onSuccess(SimpleFooStringProxy returned) { + RequestObject<Void> editRequest = req.simpleFooStringRequest().persist( + returned); + new FailFixAndRefire<Void>(returned, editRequest).doVoidTest(); + } + }); + } + + private void assertCannotFire(final RequestObject<Long> mutateRequest) { + try { + mutateRequest.fire(new Receiver<Long>() { + public void onSuccess(Long response) { + fail("Should not be called"); + } + }); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + /* cannot reuse a successful request, mores the pity */ + } + } + + private RequestObject<SimpleFooStringProxy> fooCreationRequest() { + SimpleFooStringProxy originalFoo = req.create(SimpleFooStringProxy.class); + final RequestObject<SimpleFooStringProxy> fooReq = req.simpleFooStringRequest().persistAndReturnSelf( + originalFoo); + originalFoo = fooReq.edit(originalFoo); + originalFoo.setUserName("GWT User"); + return fooReq; + } +}
diff --git a/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java b/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java index f137c2c..b13ac8c 100644 --- a/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java +++ b/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java
@@ -16,7 +16,6 @@ package com.google.gwt.requestfactory.client; import com.google.gwt.requestfactory.client.impl.ProxyImpl; -import com.google.gwt.requestfactory.shared.EntityProxy; import com.google.gwt.requestfactory.shared.Receiver; import com.google.gwt.requestfactory.shared.RequestObject; import com.google.gwt.requestfactory.shared.ServerFailure; @@ -188,7 +187,9 @@ assertEquals(eventBus, req.getEventBus()); } - public void testGetList() { + public void testGetListStringId() { + delayTestFinish(5000); + // String ids req.simpleBarRequest().findAll().fire(new Receiver<List<SimpleBarProxy>>() { @Override @@ -196,9 +197,14 @@ assertEquals(2, response.size()); for (SimpleBarProxy bar : response) { assertNotNull(bar.stableId()); + finishTestAndReset(); } } }); + } + + public void testGetListLongId() { + delayTestFinish(5000); // Long ids req.simpleFooRequest().findAll().with("barField.userName").fire( @@ -208,7 +214,8 @@ assertEquals(1, response.size()); for (SimpleFooProxy foo : response) { assertNotNull(foo.stableId()); - assertEquals("GWT", foo.getBarField().getUserName()); + assertEquals("FOO", foo.getBarField().getUserName()); + finishTestAndReset(); } } }); @@ -676,17 +683,6 @@ } } - private void checkStableIdEquals(EntityProxy expected, - EntityProxy actual) { - assertNotSame(expected.stableId(), actual.stableId()); - assertEquals(expected.stableId(), actual.stableId()); - assertEquals(expected.stableId().hashCode(), actual.stableId().hashCode()); - - // No assumptions about the proxy objects (being proxies and all) - assertNotSame(expected, actual); - assertFalse(expected.equals(actual)); - } - private RequestObject<SimpleFooProxy> fooCreationRequest() { SimpleFooProxy originalFoo = req.create(SimpleFooProxy.class); final RequestObject<SimpleFooProxy> fooReq = req.simpleFooRequest().persistAndReturnSelf(
diff --git a/user/test/com/google/gwt/requestfactory/client/RequestFactoryTestBase.java b/user/test/com/google/gwt/requestfactory/client/RequestFactoryTestBase.java index 458a419..54af522 100644 --- a/user/test/com/google/gwt/requestfactory/client/RequestFactoryTestBase.java +++ b/user/test/com/google/gwt/requestfactory/client/RequestFactoryTestBase.java
@@ -19,6 +19,7 @@ import com.google.gwt.event.shared.EventBus; import com.google.gwt.event.shared.SimpleEventBus; import com.google.gwt.junit.client.GWTTestCase; +import com.google.gwt.requestfactory.shared.EntityProxy; import com.google.gwt.requestfactory.shared.Receiver; import com.google.gwt.requestfactory.shared.SimpleRequestFactory; @@ -41,22 +42,41 @@ } protected void finishTestAndReset() { - final boolean[] reallyDone = {false, false}; + final boolean[] reallyDone = {false, false, false}; req.simpleFooRequest().reset().fire(new Receiver<Void>() { public void onSuccess(Void response) { reallyDone[0] = true; - if (reallyDone[0] && reallyDone[1]) { + if (reallyDone[0] && reallyDone[1] && reallyDone[2]) { + finishTest(); + } + } + }); + req.simpleFooStringRequest().reset().fire(new Receiver<Void>() { + public void onSuccess(Void response) { + reallyDone[1] = true; + if (reallyDone[0] && reallyDone[1] && reallyDone[2]) { finishTest(); } } }); req.simpleBarRequest().reset().fire(new Receiver<Void>() { public void onSuccess(Void response) { - reallyDone[1] = true; - if (reallyDone[0] && reallyDone[1]) { + reallyDone[2] = true; + if (reallyDone[0] && reallyDone[1] && reallyDone[2]) { finishTest(); } } }); } + + protected void checkStableIdEquals(EntityProxy expected, + EntityProxy actual) { + assertNotSame(expected.stableId(), actual.stableId()); + assertEquals(expected.stableId(), actual.stableId()); + assertEquals(expected.stableId().hashCode(), actual.stableId().hashCode()); + + // No assumptions about the proxy objects (being proxies and all) + assertNotSame(expected, actual); + assertFalse(expected.equals(actual)); + } }
diff --git a/user/test/com/google/gwt/requestfactory/server/SimpleFooString.java b/user/test/com/google/gwt/requestfactory/server/SimpleFooString.java new file mode 100644 index 0000000..13f05c9 --- /dev/null +++ b/user/test/com/google/gwt/requestfactory/server/SimpleFooString.java
@@ -0,0 +1,395 @@ +/* + * 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.server; + +import com.google.gwt.requestfactory.shared.Id; +import com.google.gwt.requestfactory.shared.SimpleEnum; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.validation.constraints.Size; + +/** + * Domain object for SimpleFooStringRequest. Ugly copy of SimpleFoo, just + * changes id to String. + */ +public class SimpleFooString { + /** + * DO NOT USE THIS UGLY HACK DIRECTLY! Call {@link #get} instead. + */ + private static SimpleFooString jreTestSingleton = new SimpleFooString(); + + private static Long nextId = 1L; + + public static Long countSimpleFoo() { + return 1L; + } + + public static List<SimpleFooString> findAll() { + return Collections.singletonList(get()); + } + + public static SimpleFooString findSimpleFooString(String id) { + return findSimpleFooStringById(id); + } + + public static SimpleFooString findSimpleFooStringById(String id) { + get().setId(id); + return get(); + } + + public static synchronized SimpleFooString get() { + HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest(); + if (req == null) { + // May be in a JRE test case, use the singleton + return jreTestSingleton; + } else { + /* + * This will not behave entirely correctly unless we have a servlet filter + * that doesn't allow any requests to be processed unless they're + * associated with an existing session. + */ + SimpleFooString value = (SimpleFooString) req.getSession().getAttribute( + SimpleFooString.class.getCanonicalName()); + if (value == null) { + value = reset(); + } + return value; + } + } + + public static SimpleFooString getSingleton() { + return get(); + } + + public static synchronized SimpleFooString reset() { + SimpleFooString instance = new SimpleFooString(); + HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest(); + if (req == null) { + jreTestSingleton = instance; + } else { + req.getSession().setAttribute(SimpleFooString.class.getCanonicalName(), + instance); + } + return instance; + } + + @SuppressWarnings("unused") + private static Integer privateMethod() { + return 0; + } + + @Id + private String id = "1x"; + + Integer version = 1; + + @Size(min = 3, max = 30) + private String userName; + private String password; + + private Character charField; + private Long longField; + + private BigDecimal bigDecimalField; + + private BigInteger bigIntField; + private Integer intId = -1; + private Short shortField; + + private Byte byteField; + + private Date created; + private Double doubleField; + + private Float floatField; + + private SimpleEnum enumField; + private Boolean boolField; + + private Boolean otherBoolField; + private Integer pleaseCrashField; + + private SimpleBar barField; + private SimpleFooString fooField; + + private String nullField; + private SimpleBar barNullField; + + public SimpleFooString() { + intId = 42; + version = 1; + userName = "GWT"; + longField = 8L; + enumField = SimpleEnum.FOO; + created = new Date(); + barField = SimpleBar.getSingleton(); + boolField = true; + nullField = null; + barNullField = null; + pleaseCrashField = 0; + } + + public Long countSimpleFooWithUserNameSideEffect() { + get().setUserName(userName); + return 1L; + } + + public SimpleBar getBarField() { + return barField; + } + + public SimpleBar getBarNullField() { + return barNullField; + } + + /** + * @return the bigDecimalField + */ + public BigDecimal getBigDecimalField() { + return bigDecimalField; + } + + /** + * @return the bigIntegerField + */ + public BigInteger getBigIntField() { + return bigIntField; + } + + public Boolean getBoolField() { + return boolField; + } + + /** + * @return the byteField + */ + public Byte getByteField() { + return byteField; + } + + /** + * @return the charField + */ + public Character getCharField() { + return charField; + } + + public Date getCreated() { + return created; + } + + /** + * @return the doubleField + */ + public Double getDoubleField() { + return doubleField; + } + + public SimpleEnum getEnumField() { + return enumField; + } + + /** + * @return the floatField + */ + public Float getFloatField() { + return floatField; + } + + public SimpleFooString getFooField() { + return fooField; + } + + public String getId() { + return id; + } + + public Integer getIntId() { + return intId; + } + + public Long getLongField() { + return longField; + } + + public String getNullField() { + return nullField; + } + + /** + * @return the otherBoolField + */ + public Boolean getOtherBoolField() { + return otherBoolField; + } + + public String getPassword() { + return password; + } + + public Integer getPleaseCrash() { + return pleaseCrashField; + } + + /** + * @return the shortField + */ + public Short getShortField() { + return shortField; + } + + public String getUserName() { + return userName; + } + + public Integer getVersion() { + return version; + } + + public String hello(SimpleBar bar) { + return "Greetings " + bar.getUserName() + " from " + getUserName(); + } + + public void persist() { + setId(nextId++ + "x"); + } + + public SimpleFooString persistAndReturnSelf() { + persist(); + return this; + } + + public void setBarField(SimpleBar barField) { + this.barField = barField; + } + + public void setBarNullField(SimpleBar barNullField) { + this.barNullField = barNullField; + } + + /** + * @param bigDecimalField the bigDecimalField to set + */ + public void setBigDecimalField(BigDecimal bigDecimalField) { + this.bigDecimalField = bigDecimalField; + } + + /** + * @param bigIntegerField the bigIntegerField to set + */ + public void setBigIntField(BigInteger bigIntegerField) { + this.bigIntField = bigIntegerField; + } + + public void setBoolField(Boolean bool) { + boolField = bool; + } + + /** + * @param byteField the byteField to set + */ + public void setByteField(Byte byteField) { + this.byteField = byteField; + } + + /** + * @param charField the charField to set + */ + public void setCharField(Character charField) { + this.charField = charField; + } + + public void setCreated(Date created) { + this.created = created; + } + + /** + * @param doubleField the doubleField to set + */ + public void setDoubleField(Double doubleField) { + this.doubleField = doubleField; + } + + public void setEnumField(SimpleEnum enumField) { + this.enumField = enumField; + } + + /** + * @param floatField the floatField to set + */ + public void setFloatField(Float floatField) { + this.floatField = floatField; + } + + public void setFooField(SimpleFooString fooField) { + this.fooField = fooField; + } + + public void setId(String id) { + this.id = id; + } + + public void setIntId(Integer id) { + this.intId = id; + } + + public void setLongField(Long longField) { + this.longField = longField; + } + + public void setNullField(String nullField) { + this.nullField = nullField; + } + + /** + * @param otherBoolField the otherBoolField to set + */ + public void setOtherBoolField(Boolean otherBoolField) { + this.otherBoolField = otherBoolField; + } + + public void setPleaseCrash(Integer crashIf42) { + if (crashIf42 == 42) { + throw new UnsupportedOperationException("THIS EXCEPTION IS EXPECTED BY A TEST"); + } + pleaseCrashField = crashIf42; + } + + public void setPassword(String password) { + this.password = password; + } + + /** + * @param shortField the shortField to set + */ + public void setShortField(Short shortField) { + this.shortField = shortField; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public void setVersion(Integer version) { + this.version = version; + } +}
diff --git a/user/test/com/google/gwt/requestfactory/shared/BaseFooProxy.java b/user/test/com/google/gwt/requestfactory/shared/BaseFooProxy.java new file mode 100644 index 0000000..b2aca2d --- /dev/null +++ b/user/test/com/google/gwt/requestfactory/shared/BaseFooProxy.java
@@ -0,0 +1,106 @@ +/* + * 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.shared; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Date; + +/** + * A simple proxy used for testing. Has an int field and date field. Add other + * data types as their support gets built in. + */ +public interface BaseFooProxy extends EntityProxy { + + SimpleBarProxy getBarField(); + + SimpleBarProxy getBarNullField(); + + BigDecimal getBigDecimalField(); + + BigInteger getBigIntField(); + + Boolean getBoolField(); + + Byte getByteField(); + + Character getCharField(); + + Date getCreated(); + + Double getDoubleField(); + + SimpleEnum getEnumField(); + + Float getFloatField(); + + BaseFooProxy getFooField(); + + Integer getIntId(); + + Long getLongField(); + + String getNullField(); + + Boolean getOtherBoolField(); + + Integer getPleaseCrash(); + + String getPassword(); + + Short getShortField(); + + String getUserName(); + + void setBarField(SimpleBarProxy barField); + + void setBarNullField(SimpleBarProxy barNullField); + + void setBigDecimalField(BigDecimal d); + + void setBigIntField(BigInteger i); + + void setBoolField(Boolean boolField); + + void setByteField(Byte b); + + void setCharField(Character c); + + void setCreated(Date created); + + void setDoubleField(Double d); + + void setFloatField(Float f); + + void setFooField(BaseFooProxy fooField); + + void setIntId(Integer intId); + + void setLongField(Long longField); + + void setNullField(String nullField); + + void setOtherBoolField(Boolean boolField); + + void setPassword(String password); + + void setPleaseCrash(Integer dummy); + + void setShortField(Short s); + + void setUserName(String userName); + +}
diff --git a/user/test/com/google/gwt/requestfactory/shared/SimpleFooProxy.java b/user/test/com/google/gwt/requestfactory/shared/SimpleFooProxy.java index 5de9711..3931c71 100644 --- a/user/test/com/google/gwt/requestfactory/shared/SimpleFooProxy.java +++ b/user/test/com/google/gwt/requestfactory/shared/SimpleFooProxy.java
@@ -17,95 +17,12 @@ import com.google.gwt.requestfactory.server.SimpleFoo; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Date; - /** - * A simple entity used for testing. Has an int field and date field. Add other - * data types as their support gets built in. + * A simple extension of AbstractFooProxy with Long id. */ @ProxyFor(SimpleFoo.class) -public interface SimpleFooProxy extends EntityProxy { +public interface SimpleFooProxy extends BaseFooProxy { Long getId(); - SimpleBarProxy getBarField(); - - SimpleBarProxy getBarNullField(); - - BigDecimal getBigDecimalField(); - - BigInteger getBigIntField(); - - Boolean getBoolField(); - - Byte getByteField(); - - Character getCharField(); - - Date getCreated(); - - Double getDoubleField(); - - SimpleEnum getEnumField(); - - Float getFloatField(); - - SimpleFooProxy getFooField(); - - Integer getIntId(); - - Long getLongField(); - - String getNullField(); - - Boolean getOtherBoolField(); - - Integer getPleaseCrash(); - - String getPassword(); - - Short getShortField(); - - String getUserName(); - - void setBarField(SimpleBarProxy barField); - - void setBarNullField(SimpleBarProxy barNullField); - - void setBigDecimalField(BigDecimal d); - - void setBigIntField(BigInteger i); - - void setBoolField(Boolean boolField); - - void setByteField(Byte b); - - void setCharField(Character c); - - void setCreated(Date created); - - void setDoubleField(Double d); - - void setFloatField(Float f); - - void setFooField(SimpleFooProxy fooField); - - void setIntId(Integer intId); - - void setLongField(Long longField); - - void setNullField(String nullField); - - void setOtherBoolField(Boolean boolField); - - void setPassword(String password); - - void setPleaseCrash(Integer dummy); - - void setShortField(Short s); - - void setUserName(String userName); - EntityProxyId<SimpleFooProxy> stableId(); }
diff --git a/user/test/com/google/gwt/requestfactory/shared/SimpleFooStringProxy.java b/user/test/com/google/gwt/requestfactory/shared/SimpleFooStringProxy.java new file mode 100644 index 0000000..4ef55ce --- /dev/null +++ b/user/test/com/google/gwt/requestfactory/shared/SimpleFooStringProxy.java
@@ -0,0 +1,28 @@ +/* + * 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.shared; + +import com.google.gwt.requestfactory.server.SimpleFooString; + +/** + * An extension of AbstractFooProxy with String id. + */ +@ProxyFor(SimpleFooString.class) +public interface SimpleFooStringProxy extends BaseFooProxy { + String getId(); + + EntityProxyId<SimpleFooStringProxy> stableId(); +}
diff --git a/user/test/com/google/gwt/requestfactory/shared/SimpleFooStringRequest.java b/user/test/com/google/gwt/requestfactory/shared/SimpleFooStringRequest.java new file mode 100644 index 0000000..3851341 --- /dev/null +++ b/user/test/com/google/gwt/requestfactory/shared/SimpleFooStringRequest.java
@@ -0,0 +1,44 @@ +/* + * 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.shared; + +/** + * Do nothing test interface. + */ +@Service(com.google.gwt.requestfactory.server.SimpleFooString.class) +public interface SimpleFooStringRequest { + RequestObject<Long> countSimpleFoo(); + + @Instance + RequestObject<Long> countSimpleFooWithUserNameSideEffect(SimpleFooStringProxy proxy); + + ProxyListRequest<SimpleFooStringProxy> findAll(); + + ProxyRequest<SimpleFooStringProxy> findSimpleFooStringById(String id); + + RequestObject<Integer> privateMethod(); + + @Instance + RequestObject<Void> persist(SimpleFooStringProxy proxy); + + @Instance + ProxyRequest<SimpleFooStringProxy> persistAndReturnSelf(SimpleFooStringProxy proxy); + + RequestObject<Void> reset(); + + @Instance + RequestObject<String> hello(SimpleFooStringProxy instance, SimpleBarProxy proxy); +}
diff --git a/user/test/com/google/gwt/requestfactory/shared/SimpleRequestFactory.java b/user/test/com/google/gwt/requestfactory/shared/SimpleRequestFactory.java index aaea4dc..f763381 100644 --- a/user/test/com/google/gwt/requestfactory/shared/SimpleRequestFactory.java +++ b/user/test/com/google/gwt/requestfactory/shared/SimpleRequestFactory.java
@@ -22,5 +22,7 @@ SimpleFooRequest simpleFooRequest(); + SimpleFooStringRequest simpleFooStringRequest(); + SimpleBarRequest simpleBarRequest(); }