Hello,

I'm stuck trying to use classes from a test helper/utility jar within the
(junit) @Test methods in my pax exam integration test.  Tests that don't
use any external modules work perfectly so I think the general setup is ok.

The utility jar is included in the integration test's POM as a dependency
with scope=test, but this results in a NoClassDefFoundError.  I've a
similar utility jar for common pax-exam options etc and this works fine
(but these are only used in the test setup, so I'm guessing outside of the
scope of the probe bundle).

I initially assumed it might be a dependency on junit, so I embedded it and
when that didn't work I removed the dependency, replacing Assert.*s with
AssertionError, so it now has zero dependencies but that hasn't helped.

As these utility jars are valid bundles I tried deploying the test utility
one, hoping the probe would pick it up via dynamic-import or some other
mechanism, but still the same NoClassDefFoundError.  After this I tried
using the maven-dependency-plugin:unpack to add the test utility jar's
contents to target/classes but this messes up how pax exam handles the test
classes (i think).

(I'm now using the labs-paxexam-karaf with version 2.3.0.M1 of pax, but was
originally using vanilla pax-exam 2.3.0.M1 and had similar issue)


Clearly I'm going down the wrong path(s) here, but I'm at a complete loss
as to what - can anyone point me in the right direction?

(Code snips at the end of this email, the poms' are very hierarchic but I
can strip down to a simple test case if anybody requests this, otherwise
key dependencies are as in
https://github.com/openengsb/labs-paxexam-karaf/wiki)


Also how can I inspect the probe bundle?  The /tmp/tb/ directory is only
populated with the tinybundles my code creates and I can't see anything
relevant in the module's target directory.


thanks,
Caspar



Test class :
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.company.product.test;

import static
org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.configureConsole;
import static
org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static
org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.logLevel;
import static
org.openengsb.labs.paxexam.karaf.options.LogLevelOption.LogLevel.INFO;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.*;
import static org.ops4j.pax.exam.OptionUtils.*;
import static org.ops4j.pax.exam.spi.PaxExamRuntime.createContainer;
import static org.ops4j.pax.exam.spi.PaxExamRuntime.createTestSystem;

import java.io.File;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.options.MavenUrlReference;
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;

import com.company.test.util.web.HttpAssertion;


@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class JunitHelperIntegrationTest {


    //@formatter:off

    public static void main(String[] args) throws Exception
    {
        HttpAssertion.httpAssertion("http://google.co.uk
").expectResponseCodes(200).expectContentMatches(".*google.*").assertExpectations();
        createContainer(createTestSystem(
                combine(
                        new JunitHelperIntegrationTest().config(),
                        configureConsole().startLocalConsole()

                )
            )).start();
    }


    @Test
    public void test() throws Exception
    {
        HttpAssertion.httpAssertion("http://google.co.uk
").expectResponseCodes(200).expectContentMatches(".*google.*").assertExpectations();
        Assert.assertTrue(true);
    }


    @Configuration
    public Option[] config()
    {
        return new Option[]
            {
                customKarafDistribution(),
                logLevel(INFO),//logLevel(TRACE),
                junitBundles()//,
//                debug()
            };
    }


    private Option customKarafDistribution()
    {
        return karafDistributionConfiguration()
                .frameworkUrl(referenceCustomKaraf())
                .karafVersion("2.2.4")
                .name("MIM Karaf")
                .unpackDirectory(new File("target/paxexam/unpack/"));
    }


    private MavenUrlReference referenceCustomKaraf()
    {
        return maven().groupId("com.matterhorn.karaf")
                    .artifactId("com.matterhorn.karaf.distribution")
                    .classifier("caspar")
                    .type("tar.gz")
                    .versionAsInProject();
    }



}



Test Utility class (from separate project):
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


package com.company.test.util.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;


public final class HttpAssertion {

    private URL url;
    private int[] codes;
    private Pattern pattern;
    private String[] contentTypes;
    private Map<String, String> requestProperties = new HashMap<String,
String>();

    private HttpAssertion() {}


    public static HttpAssertion httpAssertion(String url) throws
MalformedURLException
    {
        HttpAssertion ha = new HttpAssertion();
        ha.url = new URL(url);
        return ha;
    }

    public static HttpAssertion httpAssertion(URL url)
    {
        HttpAssertion ha = new HttpAssertion();
        ha.url = url;
        return ha;
    }


    public HttpAssertion expectResponseCodes(int ... codes)
    {
        this.codes = codes;
        return this;
    }


    public HttpAssertion expectContentTypes(String[] contentTypes)
    {
        this.contentTypes = contentTypes;
        return this;
    }


    public HttpAssertion expectContentMatches(Pattern pattern)
    {
        this.pattern = pattern;
        return this;
    }

    public HttpAssertion expectContentMatches(String pattern)
    {
        this.pattern = Pattern.compile(pattern);
        return this;
    }


    public HttpAssertion setRequestProperty(String property, String value)
    {
        requestProperties.put(property, value);
        return this;
    }


    public void assertExpectations() throws Exception
    {
        HttpURLConnection connection = connection();
        String contents = contents(connection);

        assertContentTypes(connection);
        assertResponseCodes(connection);
        assertContentMatches(contents);
    }


    private String contents(HttpURLConnection connection) throws IOException
    {
        BufferedReader in = null;
        StringBuilder sb = new StringBuilder();
        InputStream response = connection.getInputStream();
        try {
            in = new BufferedReader(new InputStreamReader(response));
            String str;
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
        } finally {
            if(in != null) {
                in.close();
            }
        }
        return sb.toString();
    }


    private HttpURLConnection connection() throws IOException
    {
        HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
        for(Map.Entry<String, String> e : requestProperties.entrySet()) {
            connection.setRequestProperty(e.getKey(), e.getValue());
        }
        return connection;
    }


    private void assertContentTypes(HttpURLConnection connection)
    {
        if(contentTypes != null) {
            boolean found = false;
            for(String contentType : contentTypes) {
                if(contentType.equals(connection.getContentType())) {
                    found = true;
                    break;
                }
            }
            if(!found) {
                throw new AssertionError("content-type not found");
            }
        }
    }


    private void assertResponseCodes(HttpURLConnection connection) throws
Exception
    {
        if(codes != null) {
            boolean found = false;
            for(int code : codes) {
                if(code == connection.getResponseCode()) {
                    found = true;
                    break;
                }
            }
            if(!found) {
                throw new AssertionError("response-code not found");
            }
        }
    }


    private void assertContentMatches(String contents)
    {
        if(!pattern.matcher(contents).matches()) {
            throw new AssertionError("content pattern not matched");
        }
    }

}
_______________________________________________
general mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/general

Reply via email to