Bertrand Delacretaz created SLING-5040:
------------------------------------------

             Summary: Simplified server-side tests with ServerSideTestRule
                 Key: SLING-5040
                 URL: https://issues.apache.org/jira/browse/SLING-5040
             Project: Sling
          Issue Type: Improvement
          Components: Testing
            Reporter: Bertrand Delacretaz
            Assignee: Bertrand Delacretaz
            Priority: Minor


I've been working on prototype at [1] that makes it much easier to create 
server-side tests. 

See below for an example test using the new {{ServerSideTestRule}} to indicate 
that it must run server-side, and  the {{OSGiService}} rule to access services 
or the {{BundleContext}}. Such a test can be kept alongside other (unit or 
integration) tests if that's convenient, and you don't need to build and 
install a specific bundle to run it, that all happens behind the scenes.

The {{ServerSideTestRule}} creates a bundle on the fly, with the appropriate 
{{Sling-Test-Regexp}} header to make the test available to the existing JUnit 
servlet from the junit.core module, installs the bundle, waits for the test to 
be available, executes it and removes the bundle.

The server-side test then reports results as usual in an IDE or in a Maven 
build. Debugging the test requires setting up a remote debugging session before 
executing it, but both can be quickly done from an IDE.

There's a number of things missing, notably a dynamic selection of the test 
server as well as embedding of additional required local classes, but for now 
this works against a Sling test server launched under {{launchpad/testing}} 
with 

{code}
 mvn clean install -Dlaunchpad.keep.running=true -Dhttp.port=8080 -Ddebug
{code}

after building the latest {{junit.core}} snapshot.

As a next step I'll test this on an actual bundle, using 
https://github.com/fizzed/maven-plugins to quickly update the bundle when it's 
modified.

Test example:

{code}
public class OsgiServicesTest {

    @Rule
    public final ServerSideTestRule t = new ServerSideTestRule(getClass());
    
    @Rule
    public OSGiService<ConfigurationAdmin> configAdminService = 
OSGiService.ofClass(ConfigurationAdmin.class);
    
    @Rule
    public OSGiService<BundleContext> bundleContextService = 
OSGiService.ofClass(BundleContext.class);
    
    @Test
    public void testConfigAdmin() throws Exception {
        assertNotNull(
                "Expecting ConfigurationAdmin to be injected by Sling test 
runner", 
                configAdminService.get());
        
        final String pid = "TEST_" + getClass().getName() + 
System.currentTimeMillis();
        assertNotNull("Expecting config " + pid + " to be created",
                configAdminService.get().getConfiguration(pid));
    }
    
    @Test
    public void testBundleContext() throws Exception {
        assertTrue(bundleContextService.get().getBundles().length > 1);
    }
}
{code}

[1] https://svn.apache.org/repos/asf/sling/whiteboard/bdelacretaz/test-rules



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

Reply via email to