Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-8312

Generics in OsgiContextBuilder allow compilation of code that cannot run

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • Testing OSGi Mock 2.4.6
    • None
    • Testing
    • None

    Description

      While using the OsgiContextBuilder class, I noticed that e.g. when adding a ContextPlugin, my IDE's code completion created callback methods with an OsgiContextImpl argument. Since I want to initialize an OsgiContext that struck me as odd.

      Suspecting that the generics are not quite right/ideal, I experimented a bit more and noticed that I can write and compile the following code, which leads to a runtime exception (note the SlingContext argument):

      @Rule
      public OsgiContext context = new OsgiContextBuilder()
          .plugin(new AbstractContextPlugin<SlingContext>() {
              @Override
              public void beforeSetUp(@NotNull SlingContext context) throws Exception {
                  // ... initialize context for test ...
              }
          })
          .build();
      

      The exception is:

      java.lang.RuntimeException: Before setup failed (com.foo.OsgiTest$1@5b464ce8): org.apache.sling.testing.mock.sling.junit.SlingContext cannot be cast to org.apache.sling.testing.mock.osgi.junit.OsgiContext
      	at org.apache.sling.testing.mock.osgi.context.ContextPlugins.executeBeforeSetUpCallback(ContextPlugins.java:201)
      	at org.apache.sling.testing.mock.sling.junit.SlingContext$1.before(SlingContext.java:145)
      	at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
      	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
      	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
      	at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
      	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
      	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
      	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
      	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
      	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
      	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
      Caused by: java.lang.ClassCastException: org.apache.sling.testing.mock.sling.junit.SlingContext cannot be cast to org.apache.sling.testing.mock.osgi.junit.OsgiContext
      	at com.foo.OsgiTest$1.beforeSetUp(ResourceCopierTest.java:18)
      	at org.apache.sling.testing.mock.osgi.context.ContextPlugins.executeBeforeSetUpCallback(ContextPlugins.java:198)
      	... 20 more
      

      IMHO it would make sense to address this in a way that

      • the IDE suggests the "correct" *Context class (i.e. OsgiContext for OsgiContextBuilder, SlingContext for SlingContextBuilder
      • a SlingContextBuilder can consume plugins written for an OsgiContext (not currently possible because SlingContext extends OsgiContextImpl (and not OsgiContext
      • only runnable code can be compiled

      cc sseifert@pro-vision.de

      Attachments

        Activity

          People

            Unassigned Unassigned
            jsedding Julian Sedding
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: