示例#1
0
  public void setUp(boolean redirection) throws Exception {
    conf = new Configuration();
    conf.addResource("nutch-default.xml");
    conf.addResource("nutch-site-test.xml");

    http = new Http();
    http.setConf(conf);

    server = new Server();

    if (redirection) {
      root = new Context(server, "/redirection", Context.SESSIONS);
      root.setAttribute("newContextURL", "/redirect");
    } else {
      root = new Context(server, "/", Context.SESSIONS);
    }

    ServletHolder sh = new ServletHolder(org.apache.jasper.servlet.JspServlet.class);
    root.addServlet(sh, "*.jsp");
    root.setResourceBase(RES_DIR);
  }
  public void startServer() throws Exception {
    basedir = getBasedir();

    // ----------------------------------------------------------------------------
    // Context Setup
    // ----------------------------------------------------------------------------

    context = new HashMap();

    context.put("basedir", getBasedir());

    customizeContext(new DefaultContext(context));

    boolean hasPlexusHome = context.containsKey("plexus.home");

    if (!hasPlexusHome) {
      File f = getTestFile("target/plexus-home");

      if (!f.isDirectory()) {
        f.mkdir();
      }

      context.put("plexus.home", f.getAbsolutePath());
    }

    // ----------------------------------------------------------------------------
    // Configuration
    // ----------------------------------------------------------------------------

    String config = getCustomConfigurationName();
    InputStream is;

    if (config != null) {
      is = getClass().getClassLoader().getResourceAsStream(config);

      if (is == null) {
        try {
          File configFile = new File(config);

          if (configFile.exists()) {
            is = new FileInputStream(configFile);
          }
        } catch (IOException e) {
          throw new Exception("The custom configuration specified is null: " + config);
        }
      }

    } else {
      config = getConfigurationName(null);

      is = getClass().getClassLoader().getResourceAsStream(config);
    }

    // Look for a configuration associated with this test but return null if we
    // can't find one so the container doesn't look for a configuration that we
    // know doesn't exist. Not all tests have an associated Foo.xml for testing.

    if (is == null) {
      config = null;
    } else {
      is.close();
    }

    // ----------------------------------------------------------------------------
    // Create the container
    // ----------------------------------------------------------------------------

    container = createContainerInstance(context, config);

    // ----------------------------------------------------------------------------
    // Create the DavServerManager
    // ----------------------------------------------------------------------------

    manager = (DavServerManager) container.lookup(DavServerManager.ROLE, getProviderHint());

    // ----------------------------------------------------------------------------
    // Create the jetty server
    // ----------------------------------------------------------------------------

    System.setProperty("DEBUG", "");
    System.setProperty("org.mortbay.log.class", "org.slf4j.impl.SimpleLogger");

    server = new Server(PORT);
    Context root = new Context(server, "/", Context.SESSIONS);
    ServletHandler servletHandler = root.getServletHandler();
    root.setContextPath("/");
    root.setAttribute(PlexusConstants.PLEXUS_KEY, container);

    // ----------------------------------------------------------------------------
    // Configure the webdav servlet
    // ----------------------------------------------------------------------------

    ServletHolder holder =
        servletHandler.addServletWithMapping(BasicWebDavServlet.class, "/projects/*");

    // Initialize server contents directory.
    File serverContentsDir = new File("target/test-server/");

    FileUtils.deleteDirectory(serverContentsDir);
    if (serverContentsDir.exists()) {
      throw new IllegalStateException(
          "Unable to execute test, server contents test directory ["
              + serverContentsDir.getAbsolutePath()
              + "] exists, and cannot be deleted by the test case.");
    }

    if (!serverContentsDir.mkdirs()) {
      throw new IllegalStateException(
          "Unable to execute test, server contents test directory ["
              + serverContentsDir.getAbsolutePath()
              + "] cannot be created.");
    }

    holder.setInitParameter("dav.root", serverContentsDir.getAbsolutePath());

    // ----------------------------------------------------------------------------
    // Start the jetty server
    // ----------------------------------------------------------------------------

    server.start();
  }