Adding additional testing for GWT RPC. Some custom serialized objects were previously untested, at least explicitly. Testing put in place now provides coverage to test future changes. Review at http://gwt-code-reviews.appspot.com/1441804 Review by: unnurg@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10261 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/test/com/google/gwt/user/LoggingRPCSuite.gwt.xml b/user/test/com/google/gwt/user/LoggingRPCSuite.gwt.xml new file mode 100644 index 0000000..6f589f6 --- /dev/null +++ b/user/test/com/google/gwt/user/LoggingRPCSuite.gwt.xml
@@ -0,0 +1,25 @@ +<!-- --> +<!-- Copyright 2007 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 --> +<!-- 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. License for the specific language governing permissions and --> +<!-- limitations under the License. --> + +<module> + <inherits name='com.google.gwt.user.User' /> + <inherits name="com.google.gwt.logging.Logging"/> + + <define-property name='rpc.enforceTypeVersioning' values="true, false" /> + <set-property name='rpc.enforceTypeVersioning' value='true' /> + + <servlet path='/loggingrpc' + class='com.google.gwt.user.server.rpc.LoggingRPCTestServiceImpl' /> + +</module>
diff --git a/user/test/com/google/gwt/user/LoggingRPCSuite.java b/user/test/com/google/gwt/user/LoggingRPCSuite.java new file mode 100644 index 0000000..0e29062 --- /dev/null +++ b/user/test/com/google/gwt/user/LoggingRPCSuite.java
@@ -0,0 +1,48 @@ +/* + * Copyright 2011 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.user; + +import com.google.gwt.dev.BootStrapPlatform; +import com.google.gwt.junit.tools.GWTTestSuite; +import com.google.gwt.user.client.rpc.LoggingRPCTest; + + +import junit.framework.Test; + +/** + * A collection of TestCases for the RPC system. + */ +public class LoggingRPCSuite { + + static { + /* + * Required for OS X Leopard. This call ensures we have a valid context + * ClassLoader. Many of the tests test low-level RPC mechanisms and rely on + * a ClassLoader to resolve classes and resources. + */ + BootStrapPlatform.applyPlatformHacks(); + } + + public static Test suite() { + GWTTestSuite suite = new GWTTestSuite( + "Test for com.google.gwt.user.client.rpc.core.java.util.logging"); + + // GWTTestCases + suite.addTestSuite(LoggingRPCTest.class); + + return suite; + } +}
diff --git a/user/test/com/google/gwt/user/RPCSuite.gwt.xml b/user/test/com/google/gwt/user/RPCSuite.gwt.xml index 6ea787a..938877b 100644 --- a/user/test/com/google/gwt/user/RPCSuite.gwt.xml +++ b/user/test/com/google/gwt/user/RPCSuite.gwt.xml
@@ -21,6 +21,8 @@ <servlet path='/echo' class='com.google.gwt.user.server.rpc.MixedSerializableEchoServiceImpl' /> <servlet path='/collections' class='com.google.gwt.user.server.rpc.CollectionsTestServiceImpl' /> + <servlet path='/corejava' + class='com.google.gwt.user.server.rpc.CoreJavaTestServiceImpl' /> <servlet path='/customfieldserializers' class='com.google.gwt.user.server.rpc.CustomFieldSerializerTestServiceImpl' /> <servlet path='/enums'
diff --git a/user/test/com/google/gwt/user/RPCSuite.java b/user/test/com/google/gwt/user/RPCSuite.java index 42a0881..4252113 100644 --- a/user/test/com/google/gwt/user/RPCSuite.java +++ b/user/test/com/google/gwt/user/RPCSuite.java
@@ -28,6 +28,7 @@ import com.google.gwt.rpc.client.RpcValueTypesTest; import com.google.gwt.user.client.rpc.CollectionsTest; import com.google.gwt.user.client.rpc.CollectionsTestWithTypeObfuscation; +import com.google.gwt.user.client.rpc.CoreJavaTest; import com.google.gwt.user.client.rpc.CustomFieldSerializerTest; import com.google.gwt.user.client.rpc.CustomFieldSerializerTestWithTypeObfuscation; import com.google.gwt.user.client.rpc.EnumsTest; @@ -102,6 +103,7 @@ suite.addTestSuite(EnumsTest.class); suite.addTestSuite(InheritanceTest.class); suite.addTestSuite(CollectionsTest.class); + suite.addTestSuite(CoreJavaTest.class); suite.addTestSuite(CustomFieldSerializerTest.class); suite.addTestSuite(ExceptionsTest.class); suite.addTestSuite(ObjectGraphTest.class);
diff --git a/user/test/com/google/gwt/user/client/rpc/CoreJavaTest.java b/user/test/com/google/gwt/user/client/rpc/CoreJavaTest.java new file mode 100644 index 0000000..c15202c --- /dev/null +++ b/user/test/com/google/gwt/user/client/rpc/CoreJavaTest.java
@@ -0,0 +1,67 @@ +/* + * Copyright 2011 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.user.client.rpc; + +import com.google.gwt.core.client.GWT; + +import java.math.MathContext; +import java.math.RoundingMode; + +/** + * Test cases for Java core emulation classes in GWT RPC. + * + * Logging is tested in a separate suite because it requires logging enabled + * in gwt.xml. Otherwise, only MathContext has non-trivial content for RPC. + * + */ +public class CoreJavaTest extends RpcTestBase { + + public static boolean isValid(MathContext value) { + return createMathContext().equals(value); + } + + private static MathContext createMathContext() { + return new MathContext(5, RoundingMode.CEILING); + } + + private CoreJavaTestServiceAsync coreJavaTestService; + + public void testMathContext() { + CoreJavaTestServiceAsync service = getServiceAsync(); + final MathContext expected = createMathContext(); + + delayTestFinishForRpc(); + service.echoMathContext(expected, new AsyncCallback<MathContext>() { + public void onFailure(Throwable caught) { + TestSetValidator.rethrowException(caught); + } + + public void onSuccess(MathContext result) { + assertNotNull(result); + assertTrue(isValid(result)); + finishTest(); + } + }); + } + + private CoreJavaTestServiceAsync getServiceAsync() { + if (coreJavaTestService == null) { + coreJavaTestService = (CoreJavaTestServiceAsync) GWT.create(CoreJavaTestService.class); + } + return coreJavaTestService; + } +}
diff --git a/user/test/com/google/gwt/user/client/rpc/CoreJavaTestService.java b/user/test/com/google/gwt/user/client/rpc/CoreJavaTestService.java new file mode 100644 index 0000000..df52416 --- /dev/null +++ b/user/test/com/google/gwt/user/client/rpc/CoreJavaTestService.java
@@ -0,0 +1,39 @@ +/* + * Copyright 2011 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.user.client.rpc; + +import java.math.MathContext; + +/** + * Test service for serialization of GWT core.java.util.logging emulations. + */ +@RemoteServiceRelativePath("corejava") +public interface CoreJavaTestService extends RemoteService { + /** + * A custom exception for the CoreJava test. + */ + final class CoreJavaTestServiceException extends Exception { + public CoreJavaTestServiceException() { + } + + public CoreJavaTestServiceException(String msg) { + super(msg); + } + } + + MathContext echoMathContext(MathContext value) throws CoreJavaTestServiceException; +}
diff --git a/user/test/com/google/gwt/user/client/rpc/CoreJavaTestServiceAsync.java b/user/test/com/google/gwt/user/client/rpc/CoreJavaTestServiceAsync.java new file mode 100644 index 0000000..4c433ba --- /dev/null +++ b/user/test/com/google/gwt/user/client/rpc/CoreJavaTestServiceAsync.java
@@ -0,0 +1,28 @@ +/* + * Copyright 2011 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.user.client.rpc; + +import java.math.MathContext; + +/** + * Async interface for serialization of GWT core.java.util.logging emulations. + */ +public interface CoreJavaTestServiceAsync { + + void echoMathContext(MathContext expected, AsyncCallback<MathContext> asyncCallback); +}
diff --git a/user/test/com/google/gwt/user/client/rpc/CustomFieldSerializerTest.java b/user/test/com/google/gwt/user/client/rpc/CustomFieldSerializerTest.java index 4c67fdd..3fa2eb5 100644 --- a/user/test/com/google/gwt/user/client/rpc/CustomFieldSerializerTest.java +++ b/user/test/com/google/gwt/user/client/rpc/CustomFieldSerializerTest.java
@@ -57,7 +57,9 @@ } /** - * Tests that the custom field serializers are actually called. + * Tests that the custom field serializers are actually called when the + * custom field serializer does not derive from + * {@link CustomFieldSerializer} */ public void testCustomFieldSerialization() { CustomFieldSerializerTestServiceAsync service = getServiceAsync(); @@ -83,6 +85,9 @@ /** * Test that custom serializers that call readObject() inside instantiate (as * is required for most immutable classes) work. + * + * This also checks that custom <code>instantiate</code> works when the + * custom serializer does not implement {@link CustomFieldSerializer}. */ public void testSerializableImmutables() { CustomFieldSerializerTestServiceAsync service = getServiceAsync();
diff --git a/user/test/com/google/gwt/user/client/rpc/LoggingRPCTest.java b/user/test/com/google/gwt/user/client/rpc/LoggingRPCTest.java new file mode 100644 index 0000000..b451e60 --- /dev/null +++ b/user/test/com/google/gwt/user/client/rpc/LoggingRPCTest.java
@@ -0,0 +1,118 @@ +/* + * Copyright 2011 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.user.client.rpc; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.junit.client.GWTTestCase; + +import java.util.logging.Level; +import java.util.logging.LogRecord; + +/** + * Test cases for Generic Collections in GWT RPC. + * + */ +public class LoggingRPCTest extends GWTTestCase { + + @Override + public String getModuleName() { + return "com.google.gwt.user.LoggingRPCSuite"; + } + + private static final LogRecord expectedLogRecord = createLogRecord(); + + public static boolean isValid(LogRecord value) { + if (!expectedLogRecord.getLevel().toString().equals(value.getLevel().toString())) { + return false; + } + + if (!expectedLogRecord.getMessage().equals(value.getMessage())) { + return false; + } + + if (expectedLogRecord.getMillis() != value.getMillis()) { + return false; + } + + if (!expectedLogRecord.getLoggerName().equals(value.getLoggerName())) { + return false; + } + + Throwable expectedCause = expectedLogRecord.getThrown(); + Throwable valueCause = value.getThrown(); + while (expectedCause != null) { + if (valueCause == null) { + return false; + } + + if (!expectedCause.getMessage().equals(valueCause.getMessage())) { + return false; + } + + // Do not compare trace as it is not stable across RPC. + + expectedCause = expectedCause.getCause(); + valueCause = valueCause.getCause(); + } + if (valueCause != null) { + return false; + } + + return true; + } + + private static LogRecord createLogRecord() { + LogRecord result = new LogRecord(Level.INFO, "Test Log Record"); + + // Only set serialized fields. + + result.setLoggerName("Test Logger Name"); + result.setMillis(1234567); + + Throwable thrown = new Throwable("Test LogRecord Throwable 1"); + thrown.initCause(new Throwable("Test LogRecord Throwable cause")); + result.setThrown(thrown); + + return result; + } + + private LoggingRPCTestServiceAsync loggingRPCTestService; + + public void testLogRecord() { + LoggingRPCTestServiceAsync service = getServiceAsync(); + delayTestFinish(15000); + service.echoLogRecord(expectedLogRecord, new AsyncCallback<LogRecord>() { + public void onFailure(Throwable caught) { + TestSetValidator.rethrowException(caught); + } + + public void onSuccess(LogRecord result) { + assertNotNull(result); + assertTrue(isValid(result)); + finishTest(); + } + }); + finishTest(); + } + + private LoggingRPCTestServiceAsync getServiceAsync() { + if (loggingRPCTestService == null) { + loggingRPCTestService = (LoggingRPCTestServiceAsync) GWT.create(LoggingRPCTestService.class); + } + return loggingRPCTestService; + } +}
diff --git a/user/test/com/google/gwt/user/client/rpc/LoggingRPCTestService.java b/user/test/com/google/gwt/user/client/rpc/LoggingRPCTestService.java new file mode 100644 index 0000000..f597cd2 --- /dev/null +++ b/user/test/com/google/gwt/user/client/rpc/LoggingRPCTestService.java
@@ -0,0 +1,39 @@ +/* + * Copyright 2011 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.user.client.rpc; + +import java.util.logging.LogRecord; + +/** + * Test service for serialization of GWT core.java.util.logging emulations. + */ +@RemoteServiceRelativePath("loggingrpc") +public interface LoggingRPCTestService extends RemoteService { + /** + * A custom exception for the LoggingRPC test. + */ + final class LoggingRPCTestServiceException extends Exception { + public LoggingRPCTestServiceException() { + } + + public LoggingRPCTestServiceException(String msg) { + super(msg); + } + } + + LogRecord echoLogRecord(LogRecord value) throws LoggingRPCTestServiceException; +}
diff --git a/user/test/com/google/gwt/user/client/rpc/LoggingRPCTestServiceAsync.java b/user/test/com/google/gwt/user/client/rpc/LoggingRPCTestServiceAsync.java new file mode 100644 index 0000000..6e680a0 --- /dev/null +++ b/user/test/com/google/gwt/user/client/rpc/LoggingRPCTestServiceAsync.java
@@ -0,0 +1,28 @@ +/* + * Copyright 2011 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.user.client.rpc; + +import java.util.logging.LogRecord; + +/** + * Async interface for serialization of GWT core.java emulations. + */ +public interface LoggingRPCTestServiceAsync { + + void echoLogRecord(LogRecord value, AsyncCallback<LogRecord> callback); +}
diff --git a/user/test/com/google/gwt/user/server/rpc/CoreJavaTestServiceImpl.java b/user/test/com/google/gwt/user/server/rpc/CoreJavaTestServiceImpl.java new file mode 100644 index 0000000..e318d22 --- /dev/null +++ b/user/test/com/google/gwt/user/server/rpc/CoreJavaTestServiceImpl.java
@@ -0,0 +1,36 @@ +/* + * Copyright 2011 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.user.server.rpc; + +import com.google.gwt.user.client.rpc.CoreJavaTest; +import com.google.gwt.user.client.rpc.CoreJavaTestService; + +import java.math.MathContext; + +/** + * Remote service implementation for serialization of GWT core.java emulations. + */ +public class CoreJavaTestServiceImpl extends HybridServiceServlet implements CoreJavaTestService { + + public MathContext echoMathContext(MathContext value) throws CoreJavaTestServiceException { + if (!CoreJavaTest.isValid(value)) { + throw new CoreJavaTestServiceException(); + } + + return value; + } +}
diff --git a/user/test/com/google/gwt/user/server/rpc/LoggingRPCTestServiceImpl.java b/user/test/com/google/gwt/user/server/rpc/LoggingRPCTestServiceImpl.java new file mode 100644 index 0000000..cb61242 --- /dev/null +++ b/user/test/com/google/gwt/user/server/rpc/LoggingRPCTestServiceImpl.java
@@ -0,0 +1,44 @@ +/* + * Copyright 2011 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.user.server.rpc; + +import com.google.gwt.user.client.rpc.LoggingRPCTest; +import com.google.gwt.user.client.rpc.LoggingRPCTestService; + +import java.util.logging.LogRecord; + +/** + * Remote service implementation for serialization of GWT core.java.util.logging + * emulations. + */ +public class LoggingRPCTestServiceImpl extends HybridServiceServlet implements + LoggingRPCTestService { + + public LogRecord echoLogRecord(LogRecord value) throws LoggingRPCTestServiceException { + /* + * Don't check the stack trace on the server side, because the expected + * result it is comparing against came from a server-side instance of + * LoggingRPCTest, and hence has a different stack trace from the streamed + * version. + */ + if (!LoggingRPCTest.isValid(value)) { + throw new LoggingRPCTestServiceException(); + } + + return value; + } +}