From 7c90a0fb960ebbeff6e102d151726b5ae3ce6513 Mon Sep 17 00:00:00 2001 From: fjy Date: Tue, 8 Apr 2014 15:01:31 -0700 Subject: [PATCH] fix web supplier test --- examples/pom.xml | 24 ++++++++++++------- .../druid/examples/web/WebJsonSupplier.java | 11 +++++---- .../examples/web/WebJsonSupplierTest.java | 5 ++-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/examples/pom.xml b/examples/pom.xml index 6edb8429b73..dbe8d814f31 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -18,7 +18,8 @@ ~ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --> - + 4.0.0 io.druid.extensions druid-examples @@ -58,6 +59,11 @@ twitter4j-stream 3.0.3 + + commons-validator + commons-validator + 1.4.0 + @@ -82,14 +88,14 @@ ${project.build.directory}/${project.artifactId}-${project.version}-selfcontained.jar - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + diff --git a/examples/src/main/java/io/druid/examples/web/WebJsonSupplier.java b/examples/src/main/java/io/druid/examples/web/WebJsonSupplier.java index cafb39c3214..9372f721145 100644 --- a/examples/src/main/java/io/druid/examples/web/WebJsonSupplier.java +++ b/examples/src/main/java/io/druid/examples/web/WebJsonSupplier.java @@ -19,8 +19,11 @@ package io.druid.examples.web; +import com.google.api.client.repackaged.com.google.common.base.Throwables; +import com.google.common.base.Preconditions; import com.google.common.io.InputSupplier; import com.metamx.emitter.EmittingLogger; +import org.apache.commons.validator.routines.UrlValidator; import java.io.BufferedReader; import java.io.IOException; @@ -31,25 +34,25 @@ import java.net.URLConnection; public class WebJsonSupplier implements InputSupplier { private static final EmittingLogger log = new EmittingLogger(WebJsonSupplier.class); + private static final UrlValidator urlValidator = new UrlValidator(); - private String urlString; private URL url; public WebJsonSupplier(String urlString) { - this.urlString = urlString; + Preconditions.checkState(urlValidator.isValid(urlString)); + try { this.url = new URL(urlString); } catch (Exception e) { - log.error(e,"Malformed url"); + throw Throwables.propagate(e); } } @Override public BufferedReader getInput() throws IOException { - URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.setDoInput(true); return new BufferedReader(new InputStreamReader(url.openStream())); diff --git a/examples/src/test/java/io/druid/examples/web/WebJsonSupplierTest.java b/examples/src/test/java/io/druid/examples/web/WebJsonSupplierTest.java index ca181427c82..436304b7614 100644 --- a/examples/src/test/java/io/druid/examples/web/WebJsonSupplierTest.java +++ b/examples/src/test/java/io/druid/examples/web/WebJsonSupplierTest.java @@ -22,15 +22,14 @@ package io.druid.examples.web; import org.junit.Test; import java.io.IOException; +import java.net.MalformedURLException; public class WebJsonSupplierTest { - @Test(expected = IOException.class) + @Test(expected = IllegalStateException.class) public void checkInvalidUrl() throws Exception { - String invalidURL = "http://invalid.url."; WebJsonSupplier supplier = new WebJsonSupplier(invalidURL); - supplier.getInput(); } }