From 3f2251ba645c5b6b720766c8a4dfb3c8e773f6ec Mon Sep 17 00:00:00 2001 From: Xu Cang Date: Tue, 24 Jul 2018 11:21:29 -0700 Subject: [PATCH] HBASE-20782 Fix duplication of TestServletFilter.access and other check-style issues --- .../hbase/http/HttpServerFunctionalTest.java | 54 +++++++++++++++++----- .../apache/hadoop/hbase/http/TestGlobalFilter.java | 34 +++----------- .../apache/hadoop/hbase/http/TestPathFilter.java | 28 ++--------- .../hadoop/hbase/http/TestServletFilter.java | 24 ---------- 4 files changed, 51 insertions(+), 89 deletions(-) diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java index 69972a2e12..02fae2f2ab 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java @@ -18,19 +18,24 @@ package org.apache.hadoop.hbase.http; -import org.apache.hadoop.net.NetUtils; -import org.apache.hadoop.security.authorize.AccessControlList; -import org.junit.Assert; -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.http.HttpServer.Builder; - +import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; import java.net.ServerSocket; import java.net.URI; import java.net.URL; -import java.net.MalformedURLException; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.http.HttpServer.Builder; +import org.apache.hadoop.net.NetUtils; +import org.apache.hadoop.security.authorize.AccessControlList; +import org.junit.Assert; + + /** * This is a base class for functional tests of the {@link HttpServer}. @@ -114,9 +119,9 @@ public class HttpServerFunctionalTest extends Assert { File testWebappDir = new File(webapps + File.separatorChar + TEST); try { - if (!testWebappDir.exists()) { - fail("Test webapp dir " + testWebappDir.getCanonicalPath() + " missing"); - } + if (!testWebappDir.exists()) { + fail("Test webapp dir " + testWebappDir.getCanonicalPath() + " missing"); + } } catch (IOException e) { } @@ -158,7 +163,8 @@ public class HttpServerFunctionalTest extends Assert { return localServerBuilder(webapp).setFindPort(true).setConf(conf).build(); } - public static HttpServer createServer(String webapp, Configuration conf, AccessControlList adminsAcl) + public static HttpServer createServer(String webapp, Configuration conf, + AccessControlList adminsAcl) throws IOException { return localServerBuilder(webapp).setFindPort(true).setConf(conf).setACL(adminsAcl).build(); } @@ -178,7 +184,8 @@ public class HttpServerFunctionalTest extends Assert { */ public static HttpServer createServer(String webapp, Configuration conf, String[] pathSpecs) throws IOException { - return localServerBuilder(webapp).setFindPort(true).setConf(conf).setPathSpec(pathSpecs).build(); + return localServerBuilder(webapp).setFindPort(true).setConf(conf).setPathSpec(pathSpecs) + .build(); } /** @@ -269,4 +276,27 @@ public class HttpServerFunctionalTest extends Assert { } } } + + /** + * access a url, ignoring some IOException such as the page does not exist + */ + public static void access(String urlstring) throws IOException { + URL url = new URL(urlstring); + + URLConnection connection = url.openConnection(); + connection.connect(); + + try { + BufferedReader in = new BufferedReader(new InputStreamReader( + connection.getInputStream(), StandardCharsets.UTF_8)); + try { + for(; in.readLine() != null; ) { + continue; + } + } finally { + in.close(); + } + } catch(IOException ioe) { + } + } } diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java index 81902901f8..d761ef7f9b 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java @@ -17,11 +17,7 @@ */ package org.apache.hadoop.hbase.http; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; import java.util.Set; import java.util.TreeSet; import javax.servlet.Filter; @@ -42,6 +38,8 @@ import org.junit.experimental.categories.Category; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + + @Category({MiscTests.class, SmallTests.class}) public class TestGlobalFilter extends HttpServerFunctionalTest { @@ -69,8 +67,9 @@ public class TestGlobalFilter extends HttpServerFunctionalTest { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if (filterConfig == null) - return; + if (filterConfig == null) { + return; + } String uri = ((HttpServletRequest)request).getRequestURI(); LOG.info("filtering " + uri); @@ -89,27 +88,6 @@ public class TestGlobalFilter extends HttpServerFunctionalTest { } } - - /** access a url, ignoring some IOException such as the page does not exist */ - static void access(String urlstring) throws IOException { - LOG.warn("access " + urlstring); - URL url = new URL(urlstring); - URLConnection connection = url.openConnection(); - connection.connect(); - - try { - BufferedReader in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - try { - for(; in.readLine() != null; ); - } finally { - in.close(); - } - } catch(IOException ioe) { - LOG.warn("urlstring=" + urlstring, ioe); - } - } - @Test public void testServletFilter() throws Exception { Configuration conf = new Configuration(); @@ -132,7 +110,7 @@ public class TestGlobalFilter extends HttpServerFunctionalTest { final String logURL = "/logs/a.log"; final String[] urls = {fsckURL, stacksURL, ajspURL, listPathsURL, - dataURL, streamFile, rootURL, allURL, outURL, logURL}; + dataURL, streamFile, rootURL, allURL, outURL, logURL}; //access the urls final String prefix = "http://" diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java index 47e6ea26f1..f954f3cb14 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java @@ -17,11 +17,7 @@ */ package org.apache.hadoop.hbase.http; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; import java.util.Set; import java.util.TreeSet; import javax.servlet.Filter; @@ -69,8 +65,9 @@ public class TestPathFilter extends HttpServerFunctionalTest { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if (filterConfig == null) - return; + if (filterConfig == null) { + return; + } String uri = ((HttpServletRequest)request).getRequestURI(); LOG.info("filtering " + uri); @@ -90,26 +87,7 @@ public class TestPathFilter extends HttpServerFunctionalTest { } - /** access a url, ignoring some IOException such as the page does not exist */ - static void access(String urlstring) throws IOException { - LOG.warn("access " + urlstring); - URL url = new URL(urlstring); - - URLConnection connection = url.openConnection(); - connection.connect(); - try { - BufferedReader in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - try { - for(; in.readLine() != null; ); - } finally { - in.close(); - } - } catch(IOException ioe) { - LOG.warn("urlstring=" + urlstring, ioe); - } - } @Test public void testPathSpecFilters() throws Exception { diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java index 6c70dbc9d8..2aa971a980 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java @@ -17,11 +17,7 @@ */ package org.apache.hadoop.hbase.http; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; import java.util.Random; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -97,26 +93,6 @@ public class TestServletFilter extends HttpServerFunctionalTest { + StringUtils.stringifyException(t), msg.contains(string)); } - /** access a url, ignoring some IOException such as the page does not exist */ - static void access(String urlstring) throws IOException { - LOG.warn("access " + urlstring); - URL url = new URL(urlstring); - URLConnection connection = url.openConnection(); - connection.connect(); - - try { - BufferedReader in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - try { - for(; in.readLine() != null; ); - } finally { - in.close(); - } - } catch(IOException ioe) { - LOG.warn("urlstring=" + urlstring, ioe); - } - } - @Test @Ignore //From stack -- 2.14.3 (Apple Git-98)