Sling
  1. Sling
  2. SLING-1963

Support for server-side JUnit tests, injected in a running Sling instance

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None

      Description

      I've been working on a prototype JUnit extension framework [1] that allows JUnit tests to be injected in a Sling instance, and executed via a test runner servlet.

      Tests are not yet OSGi-aware in the first prototype, but they are dynamically injected as exported classes in a bundle that points to them with a Test-Package header. Apart from that they are 100% normal JUnit3 or 4 tests.

      Next step is to use annotations to inject services in the tests, I'm thinking of something like

      @TestReference
      SlingRepository repository

      And maybe

      @Test
      @OptionalTest(requiredService=repository)
      public void someTest()...

      which would ignore the test if the repository service is not present.

      The prototype consists of two bundles: "extension" which is the test detection and runner services, and "testbundle" which provides a few example tests. To play with it, install the two bundles and access the test servlet at /system/sling/junit/

      [1] http://svn.apache.org/repos/asf/sling/whiteboard/bdelacretaz/junit

        Issue Links

          Activity

          Hide
          Bertrand Delacretaz added a comment -

          And BTW if would be easy to access OSGi services from the test bundle, using an Activator that stores the BundleContext and makes it available via a static variable.

          Show
          Bertrand Delacretaz added a comment - And BTW if would be easy to access OSGi services from the test bundle, using an Activator that stores the BundleContext and makes it available via a static variable.
          Hide
          Bertrand Delacretaz added a comment -

          @TestReference annotation implemented in revision 1065993, see
          http://svn.apache.org/repos/asf/sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java
          for an example.

          Also changed the required bundle header that selects test classes to Sling-Test-Regexp, more flexible than a path.

          Show
          Bertrand Delacretaz added a comment - @TestReference annotation implemented in revision 1065993, see http://svn.apache.org/repos/asf/sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java for an example. Also changed the required bundle header that selects test classes to Sling-Test-Regexp, more flexible than a path.
          Hide
          Bertrand Delacretaz added a comment -

          Support for scriptable tests added in revision 1066530.

          To create scriptable tests, create nodes with the sling:Test mixin, and setup Sling so that requesting them with .test.txt generates a text response containing just TEST_PASSED if the test is successful. Empty lines and lines starting with # are ignored in the test output. Other output is reported as failure.

          To execute them open /system/sling/junit and look for "ScriptableTestsProviderTests".

          That servlet needs improvement for navigating and selectively executing tests, of course.

          Show
          Bertrand Delacretaz added a comment - Support for scriptable tests added in revision 1066530. To create scriptable tests, create nodes with the sling:Test mixin, and setup Sling so that requesting them with .test.txt generates a text response containing just TEST_PASSED if the test is successful. Empty lines and lines starting with # are ignored in the test output. Other output is reported as failure. To execute them open /system/sling/junit and look for "ScriptableTestsProviderTests". That servlet needs improvement for navigating and selectively executing tests, of course.
          Hide
          Bertrand Delacretaz added a comment -

          Here's the scenario for playing with this extension, in case someone wants to try it:

          1) Start a Sling launchpad standalone instance:
          cd launchpad/builder
          rm -rf sling (to get rid of old data if needed)
          java -jar target/org.apache.sling.launchpad-6-SNAPSHOT-standalone.jar

          2) Install the junit bundles:
          cd whiteboard/bdelacretaz/junit
          mvn -P autoInstallBundle clean install -Dsling.url=http://localhost:8080/system/console

          3) Go to http://localhost:8080/system/sling/junit
          The servlet should execute tests from the testbundle and explain how to create scriptable tests

          Show
          Bertrand Delacretaz added a comment - Here's the scenario for playing with this extension, in case someone wants to try it: 1) Start a Sling launchpad standalone instance: cd launchpad/builder rm -rf sling (to get rid of old data if needed) java -jar target/org.apache.sling.launchpad-6-SNAPSHOT-standalone.jar 2) Install the junit bundles: cd whiteboard/bdelacretaz/junit mvn -P autoInstallBundle clean install -Dsling.url= http://localhost:8080/system/console 3) Go to http://localhost:8080/system/sling/junit The servlet should execute tests from the testbundle and explain how to create scriptable tests
          Hide
          Bertrand Delacretaz added a comment -

          Here's an example of how to create a scripted test:

          $ curl -F"sling:resourceType=foo" -F "jcr:mixinTypes=sling:Test" http://admin:admin@localhost:8080/content/test/foo

          $ cat > /tmp/foo.txt.esp

          1. Some comments first
            <%
            if(Math.random() > 0.5) out.print("Random fail!");
            %>
            TEST_PASSED
            ^D

          $ curl -X MKCOL http://admin:admin@localhost:8080/apps/foo

          $ curl -T /tmp/foo.txt.esp http://admin:admin@localhost:8080/apps/foo/foo.txt.esp

          Refreshing /system/sling/junit should then show a failure of the content/test/foo.test.txt test about 50% of the time.

          Show
          Bertrand Delacretaz added a comment - Here's an example of how to create a scripted test: $ curl -F"sling:resourceType=foo" -F "jcr:mixinTypes=sling:Test" http://admin:admin@localhost:8080/content/test/foo $ cat > /tmp/foo.txt.esp Some comments first <% if(Math.random() > 0.5) out.print("Random fail!"); %> TEST_PASSED ^D $ curl -X MKCOL http://admin:admin@localhost:8080/apps/foo $ curl -T /tmp/foo.txt.esp http://admin:admin@localhost:8080/apps/foo/foo.txt.esp Refreshing /system/sling/junit should then show a failure of the content/test/foo.test.txt test about 50% of the time.
          Hide
          Bertrand Delacretaz added a comment -

          In revision 1067171 the ScriptableTestsProvider only considers test nodes under /apps or /libs, for security reasons.

          This means that /content/test/foo won't work anymore as a test resource, to create an example test node use for example

          $ curl -F"sling:resourceType=foo" -F "jcr:mixinTypes=sling:Test" http://admin:admin@localhost:8080/apps/foo/fooTestNode

          which also makes the MKCOL request unnecessary in the above example.

          Show
          Bertrand Delacretaz added a comment - In revision 1067171 the ScriptableTestsProvider only considers test nodes under /apps or /libs, for security reasons. This means that /content/test/foo won't work anymore as a test resource, to create an example test node use for example $ curl -F"sling:resourceType=foo" -F "jcr:mixinTypes=sling:Test" http://admin:admin@localhost:8080/apps/foo/fooTestNode which also makes the MKCOL request unnecessary in the above example.
          Hide
          Bertrand Delacretaz added a comment -

          Added basic HTML rendering and test selection in revision 1067200.

          I think tests should only executed on POST, as they might be costly. Right now GET both lists and executes the tests.

          Example URLs for test selection:
          http://localhost:8080/system/sling/junit/
          http://localhost:8080/system/sling/junit/org.apache.sling.junit.scriptable.html
          http://localhost:8080/system/sling/junit/org.apache.sling.junit.scriptable.txt

          Show
          Bertrand Delacretaz added a comment - Added basic HTML rendering and test selection in revision 1067200. I think tests should only executed on POST, as they might be costly. Right now GET both lists and executes the tests. Example URLs for test selection: http://localhost:8080/system/sling/junit/ http://localhost:8080/system/sling/junit/org.apache.sling.junit.scriptable.html http://localhost:8080/system/sling/junit/org.apache.sling.junit.scriptable.txt
          Hide
          Bertrand Delacretaz added a comment -

          As discussed on list, moved the junit modules source to http://svn.apache.org/repos/asf/sling/trunk/testing/junit

          Note also that XML rendering is available, SLING-1973

          Show
          Bertrand Delacretaz added a comment - As discussed on list, moved the junit modules source to http://svn.apache.org/repos/asf/sling/trunk/testing/junit Note also that XML rendering is available, SLING-1973
          Hide
          Bertrand Delacretaz added a comment -

          Marking fixed as the basic infrastructure is in place, we'll create more specific issues as needed.

          Show
          Bertrand Delacretaz added a comment - Marking fixed as the basic infrastructure is in place, we'll create more specific issues as needed.
          Hide
          Bertrand Delacretaz added a comment -
          Show
          Bertrand Delacretaz added a comment - This is now documented at http://sling.apache.org/site/sling-testing-tools.html

            People

            • Assignee:
              Bertrand Delacretaz
              Reporter:
              Bertrand Delacretaz
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development