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

Konrad Windszus commented on SLING-5719:
----------------------------------------

{{SlingContext.resourceResolver}} will always return the same administrative 
resource resolver even for multiple calls. So your code should never close 
that, otherwise you might run into other issues when calling 
{{SlingContext.resourceResolver}} a 2nd time.
In general, if you stick to the rule: The method/class creating a Closeable 
should also be responsible for closing it, you should be fine.

Having a method closing a resource resolver which it gets as parameter is 
definitely unexpected and should be fixed in your code.

IMHO the SlingContext behaves correct here and should fail with an exception if 
someone else closed the resource resolver being opened by SlingContext.

> 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)

Reply via email to