Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-2092: Unit Test for NutchServer contributedy by Sujen Shah <[email protected]> this closes #54.
Chris Mattmann315d7006261f
diff --git a/CHANGES.txt b/CHANGES.txtindex 14adbae4a..2bc86582f 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT+* NUTCH-2092: Unit Test for NutchServer (Sujen Shah via mattmann)+* NUTCH-2096 Explicitly indicate broswer binary to use when selectingselenium remote option in config (Kim Whitehall via mattmann)diff --git a/ivy/ivy.xml b/ivy/ivy.xmlindex 4892dcbd8..c18d233ab 100644--- a/ivy/ivy.xml+++ b/ivy/ivy.xml@@ -71,14 +71,15 @@<dependency org="com.github.crawler-commons" name="crawler-commons" rev="0.6" />- <dependency org="org.apache.cxf" name="cxf" rev="3.0.4"/>- <dependency org="org.apache.cxf" name="cxf-rt-frontend-jaxws" rev="3.0.4"/>- <dependency org="org.apache.cxf" name="cxf-rt-frontend-jaxrs" rev="3.0.4"/>- <dependency org="org.apache.cxf" name="cxf-rt-transports-http" rev="3.0.4"/>- <dependency org="org.apache.cxf" name="cxf-rt-transports-http-jetty" rev="3.0.4"/>- <dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.5.1" />- <dependency org="com.fasterxml.jackson.dataformat" name="jackson-dataformat-cbor" rev="2.5.1" />- <dependency org="com.fasterxml.jackson.jaxrs" name="jackson-jaxrs-json-provider" rev="2.5.1" />+ <dependency org="org.apache.cxf" name="cxf" rev="3.0.4" conf="*->default"/>+ <dependency org="org.apache.cxf" name="cxf-rt-frontend-jaxws" rev="3.0.4" conf="*->default"/>+ <dependency org="org.apache.cxf" name="cxf-rt-frontend-jaxrs" rev="3.0.4" conf="*->default"/>+ <dependency org="org.apache.cxf" name="cxf-rt-transports-http" rev="3.0.4" conf="*->default"/>+ <dependency org="org.apache.cxf" name="cxf-rt-transports-http-jetty" rev="3.0.4" conf="*->default"/>+ <dependency org="org.apache.cxf" name="cxf-rt-rs-client" rev="3.0.4" conf="test->default"/>+ <dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.5.1" conf="*->default"/>+ <dependency org="com.fasterxml.jackson.dataformat" name="jackson-dataformat-cbor" rev="2.5.1" conf="*->default"/>+ <dependency org="com.fasterxml.jackson.jaxrs" name="jackson-jaxrs-json-provider" rev="2.5.1" conf="*->default"/><!--artifacts needed for testing --><dependency org="junit" name="junit" rev="4.11" conf="test->default" />diff --git a/src/java/org/apache/nutch/service/NutchServer.java b/src/java/org/apache/nutch/service/NutchServer.javaindex 9f7b4fb77..16929ace2 100644--- a/src/java/org/apache/nutch/service/NutchServer.java+++ b/src/java/org/apache/nutch/service/NutchServer.java@@ -107,7 +107,7 @@ public class NutchServer {return server;}- private static void startServer() {+ protected static void startServer() {server.start();}@@ -211,6 +211,10 @@ public class NutchServer {return jobs.isEmpty();}+ protected static void setPort(int port) {+ NutchServer.port = port;+ }+public int getPort() {return port;}diff --git a/src/test/org/apache/nutch/service/TestNutchServer.java b/src/test/org/apache/nutch/service/TestNutchServer.javanew file mode 100644index 000000000..ba2ffb6ea--- /dev/null+++ b/src/test/org/apache/nutch/service/TestNutchServer.java@@ -0,0 +1,65 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.nutch.service;++import javax.ws.rs.core.Response;++import org.apache.cxf.jaxrs.client.WebClient;+import org.junit.Assert;+import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class TestNutchServer {++ private static final Logger LOG = LoggerFactory.getLogger(TestNutchServer.class);+ NutchServer server = NutchServer.getInstance();++ private int port[] = {8081, 9999, 9100, 8900};+ private final String ENDPOINT_ADDRESS = "http://localhost:";++ @Test+ public void testNutchServerStartup() {+ boolean isRunning = false;+ for(int i=0;i<port.length; i++) {+ try {+ startServer(port[i]);+ isRunning = true;+ break;+ }catch(Exception e) {+ LOG.info("Could not start server on port: {}. Tries remaining {}",port[i],port.length-i);+ }+ }+ if(!isRunning) {+ LOG.info("Could not start server, all ports in use");+ }+ else {+ LOG.info("Testing admin endpoint");+ WebClient client = WebClient.create(ENDPOINT_ADDRESS + server.getPort());+ Response response = client.path("admin").get();+ Assert.assertTrue(response.readEntity(String.class).contains("startDate"));+ response = client.path("stop").get();+ Assert.assertTrue(response.readEntity(String.class).contains("Stopping"));+ }+ }++ private void startServer(int port) throws Exception{+ NutchServer.setPort(port);+ NutchServer.startServer();+ }+}