org.apache.wink.common.internal.utils.FileLoader.loadFileUsingClassLoaders中的java.lang.NullPointerException

时间:2014-09-30 13:24:17

标签: java nullpointerexception jetty rest-client apache-wink

我正在尝试通过嵌入式jetty服务器和apache wink REST Client在我的scim restful服务器实现上运行集成测试,但无法运行测试。这是我的EmbeddedServerBase类

package org.picketlink.test.scim;

import java.net.URL;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
import org.junit.After;
import org.junit.Before;
import org.picketlink.scim.PicketLinkSCIMApplication;

public class EmbeddedWebServerBase {
protected Server server = null;

@Before
public void setUp() throws Exception {
    // Start the Jetty embedded container
    server = new Server();

    server.setConnectors(getConnectors());

    this.establishUserApps();

    server.start();
}

@After
public void tearDown() throws Exception {
    if (server != null) {
        server.stop();
        try {
            server.destroy();
        } catch (Exception e) {
            //Don't bother
        }
        server = null;
    }
}

/**
 * Return the connectors that need to be configured on the server. Subclasses can create as many connectors as they want
 *
 * @return
 */
protected Connector[] getConnectors() {
    Connector connector = new SocketConnector();
    connector.setPort(11080);
    return new Connector[] { connector };
}

/**
 * Establish the user applications - context, servlets etc
 */
protected void establishUserApps() {
    ClassLoader tcl = Thread.currentThread().getContextClassLoader();
    if (tcl == null) {
        tcl = getClass().getClassLoader();
    }

    final String WEBAPPDIR = "scim";

    final String CONTEXTPATH = "/*";

    // for localhost:port/admin/index.html and whatever else is in the webapp directory
    final URL warUrl = tcl.getResource(WEBAPPDIR);
    final String warUrlString = warUrl.toExternalForm();

    WebAppContext context = createWebApp(CONTEXTPATH, warUrlString);
    context.setClassLoader(getClass().getClassLoader());
    context.setExtraClasspath(warUrlString + "/..");

    context.setConfigurationClasses(new String[] { "org.eclipse.jetty.webapp.WebInfConfiguration",
            "org.eclipse.jetty.webapp.WebXmlConfiguration", "org.eclipse.jetty.webapp.MetaInfConfiguration",
            "org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration",
            //"org.eclipse.jetty.plus.webapp.PlusConfiguration",
            "org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.webapp.TagLibConfiguration" });

    context.setContextPath("/");

    ServletHolder servletHolder = new ServletHolder(new HttpServletDispatcher());
    servletHolder.setInitParameter("javax.ws.rs.Application", PicketLinkSCIMApplication.class.getName());
    context.addServlet(servletHolder, "/*");

    server.setHandler(context);
}

protected FilterMapping createFilterMapping(String pathSpec, FilterHolder filterHolder)      {
    FilterMapping filterMapping = new FilterMapping();
    filterMapping.setPathSpec(pathSpec);
    filterMapping.setFilterName(filterHolder.getName());
    return filterMapping;
}

protected WebAppContext createWebApp(String contextPath, String warURLString) {
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath(contextPath);
    webapp.setWar(warURLString);

    Thread.currentThread().setContextClassLoader(webapp.getClassLoader());
    return webapp;
}
}

异常很可能是在" webapp.getClassLoader()"在Thread.currentThread()。setContextClassLoader(webapp.getClassLoader());

以下是CrudTest.java的片段

package org.picketlink.scim.restful.client.test;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.picketlink.scim.core.entities.ObjectFactory;
import org.picketlink.scim.restful.client.Client;
import org.picketlink.scim.restful.client.Tests;
import org.picketlink.test.scim.EmbeddedWebServerBase;

public class CrudTest extends EmbeddedWebServerBase {
private Client client = null;
private ObjectFactory factory = null;

@Before
public void onBefore() {
    // create a client to send the user/group crud requests
    client = new Client("http://localhost:11080/scim", "matt", "password");

    // create an object factory to create the user/group objects
    factory = new ObjectFactory();
}

@Test
public void testBasicCrud() {
    // test the create user functionality
    String gid = Tests.createGroupTest(client, factory);
}
}

以下是Client.java的片段

import javax.ws.rs.core.MediaType; 
import javax.xml.bind.JAXBElement;

import org.picketlink.scim.core.entities.Group;
import org.picketlink.scim.core.entities.ObjectFactory;
import org.picketlink.scim.core.entities.User;

import org.apache.wink.client.ClientConfig;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;

public class Client {
private ObjectFactory factory = null;
private ClientConfig config = null;
private RestClient client = null;
private String url = null;

public Client(String url, String username, String password) {
    // setup the object factory
    factory = new ObjectFactory();

    // setup the client config
    config = new ClientConfig();

    // setup the rest client
    client = new RestClient(config);

    // remove any trailing slash
    url = url.replaceAll("/$", "");

    // store the url for later
    this.url = url;
}
//Other methods
}

异常最有可能被抛出:

client = new RestClient(config);

Stacktrace

java.lang.NullPointerException
        at org.apache.wink.common.internal.utils.FileLoader.loadFileUsingClassLoaders(FileLoader.java:130)
        at org.apache.wink.common.internal.utils.FileLoader.loadFileAsStream(FileLoader.java:96)
        at org.apache.wink.common.internal.application.ApplicationFileLoader.<init>(ApplicationFileLoader.java:97)
        at org.apache.wink.common.internal.application.ApplicationFileLoader.<init>(ApplicationFileLoader.java:66)
        at org.apache.wink.client.ClientConfig$1.run(ClientConfig.java:112)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.wink.client.ClientConfig.initDefaultApplication(ClientConfig.java:109)
        at org.apache.wink.client.ClientConfig.clone(ClientConfig.java:408)
        at org.apache.wink.client.RestClient.<init>(RestClient.java:81)
        at org.picketlink.scim.restful.client.Client.<init>(Client.java:60)
        at org.picketlink.scim.restful.client.test.CrudTest.onBefore(CrudTest.java:35)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.apache.maven.surefire.junitcore.pc.Scheduler$1.run(Scheduler.java:318)
        at org.apache.maven.surefire.junitcore.pc.InvokerStrategy.schedule(InvokerStrategy.java:41)
        at org.apache.maven.surefire.junitcore.pc.Scheduler.schedule(Scheduler.java:274)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:24)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.apache.maven.surefire.junitcore.pc.Scheduler$1.run(Scheduler.java:318)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

以下是完成log

的链接

0 个答案:

没有答案