You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Jens Lauterbach (JIRA)" <ji...@apache.org> on 2016/05/18 06:40:13 UTC

[jira] [Comment Edited] (SLING-5719) Missing isLive() test before closing ResourceResolver

    [ https://issues.apache.org/jira/browse/SLING-5719?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15288523#comment-15288523 ] 

Jens Lauterbach edited comment on SLING-5719 at 5/18/16 6:40 AM:
-----------------------------------------------------------------

You are of course totally right [~kwindszus]. Normally, only the class/method creating the resource resolver should close it. My example was a bit misleading in that I was trying to use the mocks {{resolver()}} as return value of my {{ResourceResolverFactory}}. Those of my classes that use the RRF were all meant to get their "own" RR. So in fact my classes would have been the owner. The problem of course is that the mock/unit test environment is different in that regard since the `SlingContext` is the owner of the RR and will try to close it after my classes under test did. Resulting in the error. So the better question is: How can I have a mock RRF?


was (Author: jenslauterbach):
You are of course totally right [~kwindszus]. Normally, only the class/method creating the resource resolver should close it. My example was a bit misleading in that I was trying to use the mocks `resolver()` as return value of my `ResourceResolverFactory`. Those of my classes that use the RRF were all meant to get their "own" RR. So in fact my classes would have been the owner. The problem of course is that the mock/unit test environment is different in that regard since the `SlingContext` is the owner of the RR and will try to close it after my classes under test did. Resulting in the error. So the better question is: How can I have a mock RRF?

> Missing isLive() test before closing ResourceResolver
> -----------------------------------------------------
>
>                 Key: SLING-5719
>                 URL: https://issues.apache.org/jira/browse/SLING-5719
>             Project: Sling
>          Issue Type: Bug
>          Components: Testing
>    Affects Versions: Testing Sling Mock 1.6.2
>            Reporter: Jens Lauterbach
>            Priority: Minor
>              Labels: easyfix
>         Attachments: resourceResolverIsLive.patch
>
>
> I am currently writing tests that involve the {{SlingContext}} and its {{ResourceResolver}}:
> {code}
> @Rule
> public final SlingContext context = new SlingContext(ResourceResolverType.JCR_MOCK);
> [... code ...]
> @Test
> public void testSomething() {
>     ResourceResolver resolver = this.context.resourceResolver();
>     someOtherMethod(resolver);
> }
> {code}
> If the resolver is closed in {{someOtherMethod()}} the test will fail because after the test runs the {{SlingContextImpl}} mehtod {{tearDown()}} is called which will {{close()}} the resource resolver if it is not {{null}}.
> The problem is that it won't test if the resource resolver {{isLive()}} which will end in an exception and a failed test:
> {quote}
> java.lang.IllegalStateException: Resource resolver is already closed.
> 	at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.checkClosed(ResourceResolverImpl.java:167)
> 	at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.adaptTo(ResourceResolverImpl.java:718)
> 	at org.apache.sling.testing.mock.sling.context.SlingContextImpl.tearDown(SlingContextImpl.java:145)
> 	at org.apache.sling.testing.mock.sling.junit.SlingContext.access$300(SlingContext.java:32)
> 	at org.apache.sling.testing.mock.sling.junit.SlingContext$1.after(SlingContext.java:120)
> 	at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:50)
> 	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.junit.runners.ParentRunner.run(ParentRunner.java:309)
> 	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
> 	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
> 	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
> 	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
> 	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
> {quote}
> Github link to the line in question:
> https://github.com/apache/sling/blob/4da6392078b73f0f20d3d66e87743cf4e86f4ecb/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java#L161



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)