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/1424801
Review by: unnurg@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10175 8db76d5a-ed1c-0410-87a9-c151d255dfc7
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..f9065fb
--- /dev/null
+++ b/user/test/com/google/gwt/user/client/rpc/CoreJavaTest.java
@@ -0,0 +1,166 @@
+/*
+ * 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.logging.impl.LevelImplRegular;
+
+import java.math.MathContext;
+import java.math.RoundingMode;
+import java.util.logging.LogRecord;
+
+/**
+ * Test cases for Generic Collections in GWT RPC.
+ *
+ */
+public class CoreJavaTest extends RpcTestBase {
+
+ private static final LogRecord expectedLogRecord = createLogRecord();
+
+ public static boolean isValid(LogRecord value, boolean compareTrace) {
+ 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;
+ }
+
+ if (compareTrace) {
+ StackTraceElement[] expectedTrace = expectedCause.getStackTrace();
+ StackTraceElement[] valueTrace = valueCause.getStackTrace();
+ if ((expectedTrace == null) != (valueTrace == null)) {
+ return false;
+ }
+ if (expectedTrace != null) {
+ if (expectedTrace.length != valueTrace.length) {
+ return false;
+ }
+ for (int i = 0; i < expectedTrace.length; ++i) {
+ StackTraceElement expectedElement = expectedTrace[i];
+ StackTraceElement valueElement = valueTrace[i];
+
+ if (!expectedElement.equals(valueElement)) {
+ return false;
+ }
+ }
+ }
+ }
+
+ expectedCause = expectedCause.getCause();
+ valueCause = valueCause.getCause();
+ }
+ if (valueCause != null) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public static boolean isValid(MathContext value) {
+ return createMathContext().equals(value);
+ }
+
+ private static LogRecord createLogRecord() {
+ /*
+ * A LevelImplRegular log level is created here in order to circumvent
+ * normal logging system behavior. Without this, the Level is null unless
+ * logging is active, which we do not want for testing. Standard usage
+ * is new LogRecord(Level.INFO, "Test Log Record");
+ */
+ LevelImplRegular logLevel = new LevelImplRegular();
+ LogRecord result = new LogRecord(logLevel.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 static MathContext createMathContext() {
+ return new MathContext(5, RoundingMode.CEILING);
+ }
+
+ private CoreJavaTestServiceAsync coreJavaTestService;
+
+ public void testLogRecord() {
+ CoreJavaTestServiceAsync service = getServiceAsync();
+ delayTestFinishForRpc();
+ service.echoLogRecord(expectedLogRecord, new AsyncCallback<LogRecord>() {
+ public void onFailure(Throwable caught) {
+ TestSetValidator.rethrowException(caught);
+ }
+
+ public void onSuccess(LogRecord result) {
+ assertNotNull(result);
+ assertTrue(isValid(result, true));
+ finishTest();
+ }
+ });
+ finishTest();
+ }
+
+ 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..8745ad5
--- /dev/null
+++ b/user/test/com/google/gwt/user/client/rpc/CoreJavaTestService.java
@@ -0,0 +1,42 @@
+/*
+ * 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;
+import java.util.logging.LogRecord;
+
+/**
+ * Test service for serialization of GWT core.java 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);
+ }
+ }
+
+ LogRecord echoLogRecord(LogRecord value) throws CoreJavaTestServiceException;
+
+ 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..524c2a1
--- /dev/null
+++ b/user/test/com/google/gwt/user/client/rpc/CoreJavaTestServiceAsync.java
@@ -0,0 +1,31 @@
+/*
+ * 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;
+import java.util.logging.LogRecord;
+
+/**
+ * Async interface for serialization of GWT core.java emulations.
+ */
+public interface CoreJavaTestServiceAsync {
+
+ void echoLogRecord(LogRecord value, AsyncCallback<LogRecord> callback);
+
+ 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/server/rpc/CoreJavaTestServiceImpl.java b/user/test/com/google/gwt/user/server/rpc/CoreJavaTestServiceImpl.java
new file mode 100644
index 0000000..9941c96
--- /dev/null
+++ b/user/test/com/google/gwt/user/server/rpc/CoreJavaTestServiceImpl.java
@@ -0,0 +1,50 @@
+/*
+ * 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;
+import java.util.logging.LogRecord;
+
+/**
+ * Remote service implementation for serialization of GWT core.java emulations.
+ */
+public class CoreJavaTestServiceImpl extends HybridServiceServlet implements CoreJavaTestService {
+
+ public LogRecord echoLogRecord(LogRecord value) throws CoreJavaTestServiceException {
+ /*
+ * 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 CoreJavaTest, and
+ * hence has a different stack trace from the streamed version.
+ */
+ if (!CoreJavaTest.isValid(value, false)) {
+ throw new CoreJavaTestServiceException();
+ }
+
+ return value;
+ }
+
+ public MathContext echoMathContext(MathContext value) throws CoreJavaTestServiceException {
+ if (!CoreJavaTest.isValid(value)) {
+ throw new CoreJavaTestServiceException();
+ }
+
+ return value;
+ }
+}