google-code-export / gwt-test-utils

Automatically exported from code.google.com/p/gwt-test-utils
1 stars 0 forks source link

Attempt to create a JsArray, but receive a JsArrayString (ClassCastException) #97

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When I attempt to create a "JsArray" in my test cases, I receive a 
"JsArrayString" and I cannot cast this array.

You can easily reproduce this problem with this code :
    JsArray<JavaScriptObject> array = JavaScriptObject.createArray().cast();
Or either :
    JsArray<JavaScriptObject> array = (JsArray<JavaScriptObject>) JavaScriptObject.createArray();

An "ClassCastException" is thrown every time, you can view the complete stack 
trace at the end of this message.

When i browse the sources, we can see a "JsArrayString" is returned on a call 
on the "createArray" method.
http://code.google.com/p/gwt-test-utils/source/browse/src/framework/trunk/gwt-te
st-utils/src/main/java/com/octo/gwt/test/internal/patchers/JavaScriptObjectPatch
er.java
Is there a way to create a objects array like this ?

I'm using the 0.34 version and GWT 2.4 on Windows XP.

Thanks a lot for your help ;)

-----------------------------

java.lang.ClassCastException: com.google.gwt.core.client.JsArrayString cannot 
be cast to com.google.gwt.core.client.JsArray
    at my.package.client.MyTest.createJavaScriptArray(MyTest.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at com.octo.gwt.test.internal.runner.AbstractGwtRunner.run(AbstractGwtRunner.java:40)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Original issue reported on code.google.com by external...@oxylane.com on 28 Nov 2011 at 4:47

GoogleCodeExporter commented 9 years ago
This issue is a tricky one, and I'm afraid to tell it would be complicated to 
fix.

GWT comes with something called Overlay Types 
(http://code.google.com/p/google-web-toolkit/wiki/OverlayTypes), basically 
JavaScriptObject subclasses, which are possible to cast from one type to 
another when it would have been impossible in Java because its strong typing 
nature.

Supporting Overlay Type and JavaScriptObject.cast() instruction would be 
possible, but will involves a lot of (very) low-level bytecode manipultation, 
which Javassist (the current bytecode manipultation API used in gwt-test-utils) 
will probably not be able to do.
We will have a lot of refactoring to do to introduce a lower-level API, such as 
ASM, which is used in real GWT to do the Overlay types magic.

Such a refactor is in the public roadmap, but is not the priority, since users 
are still waiting for Cell, RequestFactory and Editors support...

My advice would be to go back to GWTTestCase for such low-level GWT / 
JavaScript tests, since you reach one of the current technical limitation of 
gwt-test-utils, I'm so sorry :-(

Original comment by gael.laz...@gmail.com on 28 Nov 2011 at 7:18

GoogleCodeExporter commented 9 years ago
HI there,

the lastest 0.40-SNAPSHOT now provides support for overlay types !

The following test pass well now :

  @Test
  public void push() {
    // Arrange
    JavaScriptObject jso = JavaScriptObject.createObject();
    JsArray<JavaScriptObject> array = JavaScriptObject.createArray().cast();

    // Act
    array.push(jso);

    // Assert
    assertThat(array.length()).isEqualTo(1);
    assertThat(array.get(0)).isEqualTo(jso);
  }

Before updating, you should read this page carefully : 
http://code.google.com/p/gwt-test-utils/wiki/MigrationTo040

Could you please give it a try and post some feedback ? Thanks !

Original comment by gael.laz...@gmail.com on 8 Jul 2012 at 1:10