RestAssured groovy.json.JsonException - json

I am trying to create a test to validate the response of a JSON Post is as expected.
I am trying to test the POST of a JSON message body to a URL which is turn then sends a text message and if successful it sends a response that it was successful again in JSON format.
My test is as follows
public void simpleTest() {
JSONObject jsonObj = new JSONObject();
jsonObj.put("phoneNumber","353837986524");
jsonObj.put("messageContent","test");
given()
.port(31111) // port number
.contentType("application/json")
.body(jsonObj.toString())
.when()
.post("/testing/services/text/send")
.then()
.assertThat()
.body("message", equalTo("{\"resultMessage\":\"Message accepted\", \"messageID\":\"0123456\"}"));
}
but i'm getting the following exception
groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading 'R', no possible valid JSON value or punctuation could be recognized.
at groovy.json.JsonLexer.nextToken(JsonLexer.java:85)
at groovy.json.JsonLexer$nextToken.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper.parse(ConfigurableJsonSlurper.groovy:96)
at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper$parse$0.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper.parseText(ConfigurableJsonSlurper.groovy:82)
at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper$parseText.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at com.jayway.restassured.internal.ContentParser.parse(ContentParser.groovy:41)
at com.jayway.restassured.internal.ContentParser$parse.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:583)
at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at com.jayway.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:760)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at com.jayway.restassured.internal.ResponseSpecificationImpl.content(ResponseSpecificationImpl.groovy:251)
at com.jayway.restassured.specification.ResponseSpecification$content$0.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:190)
at com.jayway.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:234)
at com.jayway.restassured.internal.ValidatableResponseOptionsImpl.body(ValidatableResponseOptionsImpl.java:264)
at ie.david.text.flow.Test1.simpleTest(NoNumberSpecifiedTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Which is weird because the first character is not "R"
I originally didn't use JSONObject and had the code as
String myJson = "{\"phoneNumber\":\"353837986524\", \"messageContent\":\"test\", \"clientApp\":\"30711\", \"transactionId\":\"20100101-DL123-353875213464\"}";
But was still getting the same exception

Based on the error code, it looks like that your JSON is corrupt, use a JSON validator to check it. If not, it is likely a Restassured issue. What happens if you just use .body(jsonObj) instead of the .toString()? I think it will solve the issue, but I am not sure.

Related

How to avoid SAXParseException; The reference to entity "CONFORT" must end with the ';' delimiter

I'm having this SAXParseException when I try to parse an xml file.
Error trace:
[Fatal Error] :1:3107: The reference to entity "CONFORT" must end with the ';' delimiter.
Nuxeo-Importer-1 17:05:26,140 ERROR [EPJSourceNode] java.io.IOException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 3107; The reference to entity "CONFORT" must end with the ';' delimiter.
Nuxeo-Importer-1 17:05:26,146 ERROR [SolocalDocumentFactory] Erreur pendant l'import de EPJ code '52694992', cause : Impossible de creer l'etablissement, le nom est null
Content
Dump size :0
java.lang.AssertionError:
Expected :97
Actual :0
<Click to see difference>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:555)
at org.junit.Assert.assertEquals(Assert.java:542)
at com.solocal.importer.test.TestImport.shouldImport(TestImport.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.nuxeo.runtime.test.runner.FeaturesRunner$15.evaluate(FeaturesRunner.java:472)
at org.nuxeo.runtime.test.runner.FeaturesRunner$BeforeMethodRunStatement.evaluate(FeaturesRunner.java:351)
at org.nuxeo.runtime.test.runner.FeaturesRunner$BeforeSetupStatement.evaluate(FeaturesRunner.java:367)
at org.nuxeo.runtime.test.runner.FeaturesRunner$AfterMethodRunStatement.evaluate(FeaturesRunner.java:398)
at org.nuxeo.runtime.test.runner.FeaturesRunner$AfterTeardownStatement.evaluate(FeaturesRunner.java:417)
at org.nuxeo.runtime.test.runner.FeaturesRunner$RulesFactory$1.evaluate(FeaturesRunner.java:530)
at org.nuxeo.runtime.test.runner.RuntimeDeployment$DeploymentStatement.evaluate(RuntimeDeployment.java:265)
at org.nuxeo.runtime.test.runner.RuntimeFeature$2$1.evaluate(RuntimeFeature.java:124)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.nuxeo.runtime.test.runner.FeaturesRunner$AfterClassStatement.evaluate(FeaturesRunner.java:290)
at org.nuxeo.runtime.test.runner.FeaturesRunner$RulesFactory$1.evaluate(FeaturesRunner.java:530)
at org.nuxeo.runtime.test.runner.FeaturesRunner$BeforeClassStatement.evaluate(FeaturesRunner.java:273)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)
My error message brought up other posts about XML errors and escaping the & and the proposed solutions consist of using '&amp ;' instead of & but in my case, I'm using already &
I use a basic SAXParser with default configuration and a UTF-8 encoding.
The line bellow is throwing above exception.
<IN_RSE_LI_NOMEN>DESIGN &CONFORT</IN_RSE_LI_NOMEN>
Thanks in advance for your help

Could not parse response: org.fusesource.restygwt.client.ResponseFormatException: Response was NOT a valid JSON document

Could not parse response: org.fusesource.restygwt.client.ResponseFormatException: Response was NOT a valid JSON document
I am getting the following errors in my code .
I have added Defaults.setDateFormat(null); in onModuleload function.
14:16:42.441 [ERROR] [HelloWorld] Could not parse response: org.fusesource.restygwt.client.ResponseFormatException: Response was NOT a valid JSON document
org.fusesource.restygwt.client.ResponseFormatException: Response was NOT a valid JSON document
at org.com.hello.shared.LoginInterface_Generated_RestServiceProxy_$1.parseResult(LoginInterface_Generated_RestServiceProxy_.java:34)
at org.com.hello.shared.LoginInterface_Generated_RestServiceProxy_$1.parseResult(LoginInterface_Generated_RestServiceProxy_.java:1)
at org.fusesource.restygwt.client.AbstractRequestCallback.onResponseReceived(AbstractRequestCallback.java:62)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:259)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:347)
at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.fusesource.restygwt.client.JsonEncoderDecoder$DecodingException: Expected a json string, but was given: {"email":"******#gmail.com", "password":"****"}
at org.fusesource.restygwt.client.AbstractJsonEncoderDecoder$9.decode(AbstractJsonEncoderDecoder.java:187)
at org.fusesource.restygwt.client.AbstractJsonEncoderDecoder$9.decode(AbstractJsonEncoderDecoder.java:1)
at org.com.hello.shared.LoginInterface_Generated_RestServiceProxy_$1.parseResult(LoginInterface_Generated_RestServiceProxy_.java:32)
at org.com.hello.shared.LoginInterface_Generated_RestServiceProxy_$1.parseResult(LoginInterface_Generated_RestServiceProxy_.java:1)
at org.fusesource.restygwt.client.AbstractRequestCallback.onResponseReceived(AbstractRequestCallback.java:62)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:259)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:347)
at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Thread.java:745)

jUnit Test fails although expected exception was thrown

this is how a test looks like in my application:
#Test (expected = UniqueFieldValueConstraintViolationException.class)
public void testName() {
Branch b1 = new Branch("tetsname","Test 234","06:00-17:00","098144658");
Branch b2 = new Branch("tetsname","Test 234","06:00-17:00","098144658");
db.store(b1);
db.store(b2);
db.commit();
}
This test fails although the exception is thrown:
com.db4o.constraints.UniqueFieldValueConstraintViolationException: class: dataModel.Branch field: pNumber at
com.db4o.constraints.UniqueFieldValueConstraint$1.ensureSingleOccurence(UniqueFieldValueConstraint.java:66)
at
com.db4o.constraints.UniqueFieldValueConstraint$1.onEvent(UniqueFieldValueConstraint.java:97)
at com.db4o.internal.events.Event4Impl.trigger(Event4Impl.java:78)
at
com.db4o.internal.events.EventRegistryImpl$4.run(EventRegistryImpl.java:123)
at com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)
at com.db4o.internal.InCallback.run(InCallback.java:24) at
com.db4o.internal.events.EventRegistryImpl.withExceptionHandlingInCallback(EventRegistryImpl.java:279)
at
com.db4o.internal.events.EventRegistryImpl.commitOnStarted(EventRegistryImpl.java:121)
at
com.db4o.internal.LocalTransaction.dispatchCommittingCallback(LocalTransaction.java:89)
at
com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:66)
at
com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:59)
at
com.db4o.internal.LocalObjectContainer.commitTransaction(LocalObjectContainer.java:689)
at
com.db4o.internal.LocalObjectContainer.close2(LocalObjectContainer.java:94)
at
com.db4o.internal.ObjectContainerBase.close1(ObjectContainerBase.java:361)
at
com.db4o.internal.ObjectContainerBase.close(ObjectContainerBase.java:344)
at tests.BranchTests.cleanUp(BranchTests.java:127) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at
org.junit.runners.ParentRunner.run(ParentRunner.java:309) at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)
How can i avoid this behavior?
From the stack trace, you can see this exception isn't being thrown in the testName() method: it's being thrown by tests.BranchTests.cleanUp, which is presumably a separate method run before or after your real test.
JUnit's expected won't cover exceptions thrown during these phases. Either ensure these failures don't happen here, or catch and ignore them.

Ceylon cannot apply some functions to tuples?

What's wrong with this code?
value x = [[1, Color(0)]];
//Exception thrown here
value y = x.map(([Integer, Color] elem) => "``elem[0]`` -> ``elem[1]``");
... the last line could be replaced with this:
value y = { for(elem in x) "``elem[0]`` -> ``elem[1]``" };
... I believe these should run fine, but in both cases I get this error:
Ceylon run: java.awt.Color cannot be cast to com.redhat.ceylon.compiler.java.
runtime.model.ReifiedType
java.lang.ClassCastException: java.awt.Color cannot be cast to com.redhat.ceylon.compiler.java.runtime.model.ReifiedType
at ceylon.language.Tuple.$getElementType(Tuple.java:286)
at ceylon.language.Tuple.$getUnionOfAllType(Tuple.java:279)
at ceylon.language.Tuple.$getType(Tuple.java:266)
at ceylon.language.Tuple.$getType$(Tuple.java:257)
at ceylon.language.Tuple.$getElementType(Tuple.java:287)
at ceylon.language.Tuple.$getUnionOfAllType(Tuple.java:279)
at ceylon.language.Tuple.$getReifiedElement$(Tuple.java:102)
at ceylon.language.ArraySequence.map(ArraySequence.java:612)
at firstModule.run_.run(run.ceylon:11)
at firstModule.run_.main(run.ceylon)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at ceylon.modules.api.runtime.SecurityActions.invokeRunInternal(SecurityActions.java:61)
at ceylon.modules.api.runtime.SecurityActions.invokeRun(SecurityActions.java:51)
at ceylon.modules.api.runtime.AbstractRuntime.invokeRun(AbstractRuntime.java:89)
at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:141)
at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:126)
at ceylon.modules.Main.execute(Main.java:69)
at ceylon.modules.Main.main(Main.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.jboss.modules.Module.run(Module.java:270)
at org.jboss.modules.Main.main(Main.java:294)
at ceylon.modules.bootstrap.CeylonRunTool.run(CeylonRunTool.java:208)
at com.redhat.ceylon.common.tools.CeylonTool.run(CeylonTool.java:343)
at com.redhat.ceylon.common.tools.CeylonTool.execute(CeylonTool.java:283)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.redhat.ceylon.launcher.Launcher.run(Launcher.java:89)
at com.redhat.ceylon.launcher.Launcher.main(Launcher.java:21)
Please report this as a bug against the Ceylon language module, here:
https://github.com/ceylon/ceylon.language/issues
The bug is indeed a result of the fact that Color is a Java class, not a Ceylon class.

Why there is an "IllegalStateException: Attempt to mutate in notification" exception when using mutual binding with converter?

Maybe I've missed something in understanding Griffon's converter & reverseConverter. What is wrong and how should I fix this?
Thanks to #tim_yates for the edit, but I find out that I also got the same error not only in Griffon but also in plain Groovy like this:
import groovy.beans.Bindable
import groovy.swing.SwingBuilder
class Model {
#Bindable String name
#Bindable Integer score
}
Model m = new Model()
new SwingBuilder().edt {
frame(title: 'Untitled', pack: true, show: true) {
flowLayout()
label("Name: ")
textField(columns: 10, text: bind("name", source: m, mutual: true))
label("Score: ")
textField(columns: 3, text: bind("score", source: m,
reverseConverter: { it.isEmpty()? 0: Integer.parseInt(it) },
converter: { String.valueOf(it) },
mutual: true))
button("Save", actionPerformed: {
println "You're saving ${m.name} with score ${m.score}"
})
button("Reset Input To Default", actionPerformed: {
edt {
m.name = "defaultName"
m.score = 50
}
})
}
}
The stacktrace is more verbose:
Result: groovy.swing.SwingBuilder#ae20b6Exception in thread "AWT-EventQueue-0"
java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1338)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:659)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718)
at sun.reflect.GeneratedMethodAccessor276.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2404)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3341)
at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:196)
at groovy.beans.DefaultPropertyWriter.write(DefaultPropertyWriter.java:28)
at groovy.beans.DefaultPropertyAccessor.write(DefaultPropertyAccessor.java:30)
at org.codehaus.groovy.binding.PropertyBinding.setBeanProperty(PropertyBinding.java:236)
at org.codehaus.groovy.binding.PropertyBinding.access$000(PropertyBinding.java:51)
at org.codehaus.groovy.binding.PropertyBinding$2.run(PropertyBinding.java:189)
at org.codehaus.groovy.binding.PropertyBinding.updateTargetValue(PropertyBinding.java:196)
at org.codehaus.groovy.binding.AbstractFullBinding.fireBinding(AbstractFullBinding.java:51)
at org.codehaus.groovy.binding.AbstractFullBinding.update(AbstractFullBinding.java:55)
at org.codehaus.groovy.binding.PropertyBinding$PropertyFullBinding.propertyChange(PropertyBinding.java:279)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:328)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:283)
at java_beans_PropertyChangeSupport$firePropertyChange$0.call(Unknown Source)
at Model.firePropertyChange(test.groovy)
at Model$firePropertyChange.callCurrent(Unknown Source)
at Model.setScore(test.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2404)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3341)
at Model.setProperty(test.groovy)
at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:192)
at groovy.beans.DefaultPropertyWriter.write(DefaultPropertyWriter.java:28)
at groovy.beans.DefaultPropertyAccessor.write(DefaultPropertyAccessor.java:30)
at org.codehaus.groovy.binding.PropertyBinding.setBeanProperty(PropertyBinding.java:236)
at org.codehaus.groovy.binding.PropertyBinding.access$000(PropertyBinding.java:51)
at org.codehaus.groovy.binding.PropertyBinding$2.run(PropertyBinding.java:189)
at org.codehaus.groovy.binding.PropertyBinding.updateTargetValue(PropertyBinding.java:220)
at org.codehaus.groovy.binding.AbstractFullBinding.fireBinding(AbstractFullBinding.java:51)
at org.codehaus.groovy.binding.AbstractFullBinding.update(AbstractFullBinding.java:55)
at groovy.swing.binding.JTextComponentTextBinding.removeUpdate(JTextComponentProperties.java:85)
at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:260)
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:623)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:591)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:667)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718)
at sun.reflect.GeneratedMethodAccessor276.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2404)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3341)
at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:196)
at groovy.beans.DefaultPropertyWriter.write(DefaultPropertyWriter.java:28)
at groovy.beans.DefaultPropertyAccessor.write(DefaultPropertyAccessor.java:30)
at org.codehaus.groovy.binding.PropertyBinding.setBeanProperty(PropertyBinding.java:236)
at org.codehaus.groovy.binding.PropertyBinding.access$000(PropertyBinding.java:51)
at org.codehaus.groovy.binding.PropertyBinding$2.run(PropertyBinding.java:189)
at org.codehaus.groovy.binding.PropertyBinding.updateTargetValue(PropertyBinding.java:196)
at org.codehaus.groovy.binding.AbstractFullBinding.fireBinding(AbstractFullBinding.java:51)
at org.codehaus.groovy.binding.AbstractFullBinding.update(AbstractFullBinding.java:55)
at org.codehaus.groovy.binding.PropertyBinding$PropertyFullBinding.propertyChange(PropertyBinding.java:279)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:328)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:283)
at java_beans_PropertyChangeSupport$firePropertyChange$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at java_beans_PropertyChangeSupport$firePropertyChange.call(Unknown Source)
at Model.firePropertyChange(test.groovy)
at Model$firePropertyChange.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at Model$firePropertyChange.callCurrent(Unknown Source)
at Model.setScore(test.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2404)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3341)
at Model.setProperty(test.groovy)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setGroovyObjectProperty(ScriptBytecodeAdapter.java:528)
at test$_run_closure1_closure2_closure6_closure7.doCall(test.groovy:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:906)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at groovy.swing.SwingBuilder.edt(SwingBuilder.groovy:330)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:361)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:906)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at test$_run_closure1_closure2_closure6.doCall(test.groovy:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:906)
at groovy.lang.Closure.call(Closure.java:412)
at org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.java:51)
at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:82)
at $Proxy11.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
I believe this is a bug in the binding caused by the converters, as they transform the value in such a way that the binding believes it's changing while triggering. If no converters are registered (thus enforcing the same type on both ends) then no error occurs; which makes me think it's the converter's fault.
Sounds to me a ticket should be raised at http://jira.codehaus.org/browse/groovy
From the answers above, I also agree maybe this is a bug and should be reported.
For temporary, I will use this workaround to prevent Exception from showing:
textField(columns: 3, text: bind("score", target: m,
converter: { it.isEmpty()? 50: Integer.parseInt(it) },
reverseConverter: { String.valueOf(it) },
mutual: true))
converter will need to convert empty String to the same default value in model, in this example is 50. This will satisfy the following guard in org.codehaus.groovy.binding.PropertyBinding:
public void updateTargetValue(final Object newValue) {
Runnable runnable = new Runnable() {
public void run() {
...
if ((sourceValue==null && newValue==null) ||
DefaultTypeTransformation.compareEqual(sourceValue, newValue)) {
// not a change, don't fire it
return;
}
}
}
}