http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletRequest.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletRequest.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletRequest.java deleted file mode 100644 index 82ebe3d..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletRequest.java +++ /dev/null @@ -1,410 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import javax.servlet.AsyncContext; -import javax.servlet.DispatcherType; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpUpgradeHandler; -import javax.servlet.http.Part; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.security.Principal; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Locale; -import java.util.Map; - -public class MockHttpServletRequest implements HttpServletRequest { - - private String queryString; - private String contentType; - private String characterEncoding; - private ServletInputStream inputStream; - private String method = "GET"; - - @Override - public String getAuthType() { - return null; - } - - @Override - public Cookie[] getCookies() { - return new Cookie[ 0 ]; - } - - @Override - public long getDateHeader( String s ) { - return 0; - } - - @Override - public String getHeader( String s ) { - return null; - } - - @Override - public Enumeration<String> getHeaders( String s ) { - return null; - } - - @Override - public Enumeration<String> getHeaderNames() { - return null; - } - - @Override - public int getIntHeader( String s ) { - return 0; - } - - @Override - public String getMethod() { - return method; - } - - public void setMethod( String method ) { - this.method = method; - } - - @Override - public String getPathInfo() { - return null; - } - - @Override - public String getPathTranslated() { - return null; - } - - @Override - public String getContextPath() { - return null; - } - - @Override - public String getQueryString() { - return queryString; - } - - public void setQueryString( String queryString ) { - this.queryString = queryString; - } - - @Override - public String getRemoteUser() { - return null; - } - - @Override - public boolean isUserInRole( String s ) { - return false; - } - - @Override - public Principal getUserPrincipal() { - return null; - } - - @Override - public String getRequestedSessionId() { - return null; - } - - @Override - public String getRequestURI() { - return null; - } - - @Override - public StringBuffer getRequestURL() { - return null; - } - - @Override - public String getServletPath() { - return null; - } - - @Override - public HttpSession getSession( boolean b ) { - return null; - } - - @Override - public HttpSession getSession() { - return null; - } - - @Override - public String changeSessionId() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isRequestedSessionIdValid() { - return false; - } - - @Override - public boolean isRequestedSessionIdFromCookie() { - return false; - } - - @Override - public boolean isRequestedSessionIdFromURL() { - return false; - } - - @Override - @SuppressWarnings("deprecation") - public boolean isRequestedSessionIdFromUrl() { - return false; - } - - @Override - public boolean authenticate( HttpServletResponse httpServletResponse ) throws IOException, ServletException { - return false; - } - - @Override - public void login( String s, String s1 ) throws ServletException { - } - - @Override - public void logout() throws ServletException { - } - - @Override - public Collection<Part> getParts() throws IOException, ServletException { - return null; - } - - @Override - public Part getPart( String s ) throws IOException, ServletException { - return null; - } - - @Override - public <T extends HttpUpgradeHandler> T upgrade( Class<T> aClass ) throws IOException, ServletException { - throw new UnsupportedOperationException(); - } - - @Override - public Object getAttribute( String s ) { - return null; - } - - @Override - public Enumeration<String> getAttributeNames() { - return null; - } - - @Override - public String getCharacterEncoding() { - return characterEncoding; - } - - @Override - public void setCharacterEncoding( String characterEncoding ) throws UnsupportedEncodingException { - this.characterEncoding = characterEncoding; - } - - @Override - public int getContentLength() { - return 0; - } - - @Override - public long getContentLengthLong() { - return 0; - } - - @Override - public String getContentType() { - return contentType; - } - - public void setContentType( String contentType ) { - this.contentType = contentType; - } - - @Override - public ServletInputStream getInputStream() throws IOException { - return inputStream; - } - - public void setInputStream( ServletInputStream intputStream ) { - this.inputStream = intputStream; - } - - @Override - public String getParameter( String s ) { - return null; - } - - @Override - public Enumeration<String> getParameterNames() { - return null; - } - - @Override - public String[] getParameterValues( String s ) { - return new String[ 0 ]; - } - - @Override - public Map<String, String[]> getParameterMap() { - return null; - } - - @Override - public String getProtocol() { - return null; - } - - @Override - public String getScheme() { - return null; - } - - @Override - public String getServerName() { - return null; - } - - @Override - public int getServerPort() { - return 0; - } - - @Override - public BufferedReader getReader() throws IOException { - return null; - } - - @Override - public String getRemoteAddr() { - return null; - } - - @Override - public String getRemoteHost() { - return null; - } - - @Override - public void setAttribute( String s, Object o ) { - } - - @Override - public void removeAttribute( String s ) { - } - - @Override - public Locale getLocale() { - return null; - } - - @Override - public Enumeration<Locale> getLocales() { - return null; - } - - @Override - public boolean isSecure() { - return false; - } - - @Override - public RequestDispatcher getRequestDispatcher( String s ) { - return null; - } - - @Override - @SuppressWarnings("deprecation") - public String getRealPath( String s ) { - return null; - } - - @Override - public int getRemotePort() { - return 0; - } - - @Override - public String getLocalName() { - return null; - } - - @Override - public String getLocalAddr() { - return null; - } - - @Override - public int getLocalPort() { - return 0; - } - - @Override - public ServletContext getServletContext() { - return null; - } - - @Override - public AsyncContext startAsync() throws IllegalStateException { - return null; - } - - @Override - public AsyncContext startAsync( ServletRequest servletRequest, ServletResponse servletResponse ) throws IllegalStateException { - return null; - } - - @Override - public boolean isAsyncStarted() { - return false; - } - - @Override - public boolean isAsyncSupported() { - return false; - } - - @Override - public AsyncContext getAsyncContext() { - return null; - } - - @Override - public DispatcherType getDispatcherType() { - return null; - } - -}
http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletResponse.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletResponse.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletResponse.java deleted file mode 100644 index 9d20d17..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockHttpServletResponse.java +++ /dev/null @@ -1,195 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Collection; -import java.util.Locale; - -public class MockHttpServletResponse implements HttpServletResponse { - - @Override - public void addCookie( Cookie cookie ) { - } - - @Override - public boolean containsHeader( String s ) { - return false; - } - - @Override - public String encodeURL( String s ) { - return null; - } - - @Override - public String encodeRedirectURL( String s ) { - return null; - } - - @Override - @SuppressWarnings("deprecation") - public String encodeUrl( String s ) { - return null; - } - - @Override - public String encodeRedirectUrl( String s ) { - return null; - } - - @Override - public void sendError( int i, String s ) throws IOException { - } - - @Override - public void sendError( int i ) throws IOException { - } - - @Override - public void sendRedirect( String s ) throws IOException { - } - - @Override - public void setDateHeader( String s, long l ) { - } - - @Override - public void addDateHeader( String s, long l ) { - } - - @Override - public void setHeader( String s, String s1 ) { - } - - @Override - public void addHeader( String s, String s1 ) { - } - - @Override - public void setIntHeader( String s, int i ) { - } - - @Override - public void addIntHeader( String s, int i ) { - } - - @Override - public void setStatus( int i ) { - } - - @Override - @SuppressWarnings("deprecation") - public void setStatus( int i, String s ) { - } - - @Override - public int getStatus() { - return 0; - } - - @Override - public String getHeader( String s ) { - return null; - } - - @Override - public Collection<String> getHeaders( String s ) { - return null; - } - - @Override - public Collection<String> getHeaderNames() { - return null; - } - - @Override - public String getCharacterEncoding() { - return null; - } - - @Override - public String getContentType() { - return null; - } - - @Override - public ServletOutputStream getOutputStream() throws IOException { - return null; - } - - @Override - public PrintWriter getWriter() throws IOException { - return null; - } - - @Override - public void setCharacterEncoding( String s ) { - } - - @Override - public void setContentLength( int i ) { - } - - @Override - public void setContentLengthLong( long l ) { - } - - @Override - public void setContentType( String s ) { - } - - @Override - public void setBufferSize( int i ) { - } - - @Override - public int getBufferSize() { - return 0; - } - - @Override - public void flushBuffer() throws IOException { - } - - @Override - public void resetBuffer() { - } - - @Override - public boolean isCommitted() { - return false; - } - - @Override - public void reset() { - } - - @Override - public void setLocale( Locale locale ) { - } - - @Override - public Locale getLocale() { - return null; - } -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockInteraction.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockInteraction.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockInteraction.java deleted file mode 100644 index 1e30d38..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockInteraction.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * 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.hadoop.test.mock; - -public class MockInteraction { - - private MockResponseProvider response = new MockResponseProvider(); - private MockRequestMatcher request = new MockRequestMatcher( response ); - - public MockRequestMatcher expect() { - return request; - } - - public MockResponseProvider respond() { - return response; - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java deleted file mode 100644 index e107e6f..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java +++ /dev/null @@ -1,330 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.http.NameValuePair; -import org.apache.http.client.utils.URLEncodedUtils; -import org.apache.http.message.BasicNameValuePair; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; - -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalToIgnoringCase; -import static org.xmlmatchers.XmlMatchers.isEquivalentTo; -import static org.xmlmatchers.transform.XmlConverters.the; -import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs; - -public class MockRequestMatcher { - - private static final Charset UTF8 = Charset.forName( "UTF-8" ); - - private String from; - private MockResponseProvider response; - private Set<String> methods = null; - private String pathInfo = null; - private String requestURL = null; - Map<String,Matcher> headers = null; - Set<Cookie> cookies = null; - private Map<String,Object> attributes = null; - private Map<String,String> queryParams = null; - private String contentType = null; - private String characterEncoding = null; - private Integer contentLength = null; - private byte[] entity = null; - private Map<String,String[]> formParams = null; - - public MockRequestMatcher( MockResponseProvider response ) { - this.response = response; - } - - public MockResponseProvider respond() { - return response; - } - - public MockRequestMatcher from( String from ) { - this.from = from; - return this; - } - - public MockRequestMatcher method( String... methods ) { - if( this.methods == null ) { - this.methods = new HashSet<>(); - } - if( methods != null ) { - for( String method: methods ) { - this.methods.add( method ); - } - } - return this; - } - - public MockRequestMatcher pathInfo( String pathInfo ) { - this.pathInfo = pathInfo; - return this; - } - - public MockRequestMatcher requestUrl( String requestUrl ) { - this.requestURL = requestUrl; - return this; - } - - public MockRequestMatcher header( String name, String value ) { - if( headers == null ) { - headers = new HashMap<>(); - } - headers.put( name, Matchers.is(value) ); - return this; - } - - public MockRequestMatcher header( String name, Matcher matcher ) { - if( headers == null ) { - headers = new HashMap<>(); - } - headers.put( name, matcher ); - return this; - } - - public MockRequestMatcher cookie( Cookie cookie ) { - if( cookies == null ) { - cookies = new HashSet<>(); - } - cookies.add( cookie ); - return this; - } - - public MockRequestMatcher attribute( String name, Object value ) { - if( this.attributes == null ) { - this.attributes = new HashMap<>(); - } - attributes.put( name, value ); - return this; - } - - public MockRequestMatcher queryParam( String name, String value ) { - if( this.queryParams == null ) { - this.queryParams = new HashMap<>(); - } - queryParams.put( name, value ); - return this; - } - - public MockRequestMatcher formParam( String name, String... values ) { - if( entity != null ) { - throw new IllegalStateException( "Entity already specified." ); - } - if( formParams == null ) { - formParams = new HashMap<>(); - } - String[] currentValues = formParams.get( name ); - if( currentValues == null ) { - currentValues = values; - } else if ( values != null ) { - currentValues = ArrayUtils.addAll( currentValues, values ); - } - formParams.put( name, currentValues ); - return this; - } - - public MockRequestMatcher content( String string, Charset charset ) { - content( string.getBytes( charset ) ); - return this; - } - - public MockRequestMatcher content( byte[] entity ) { - if( formParams != null ) { - throw new IllegalStateException( "Form params already specified." ); - } - this.entity = entity; - return this; - } - - public MockRequestMatcher content( URL url ) throws IOException { - content( url.openStream() ); - return this; - } - - public MockRequestMatcher content( InputStream stream ) throws IOException { - content( IOUtils.toByteArray( stream ) ); - return this; - } - - public MockRequestMatcher contentType( String contentType ) { - this.contentType = contentType; - return this; - } - - public MockRequestMatcher contentLength( int length ) { - this.contentLength = length; - return this; - } - - public MockRequestMatcher characterEncoding( String charset ) { - this.characterEncoding = charset; - return this; - } - - public void match( HttpServletRequest request ) throws IOException { - if( methods != null ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " is not using one of the expected HTTP methods", - methods, hasItem( request.getMethod() ) ); - } - if( pathInfo != null ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " does not have the expected pathInfo", - request.getPathInfo(), is( pathInfo ) ); - } - if( requestURL != null ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " does not have the expected requestURL", - request.getRequestURL().toString(), is( requestURL ) ); - } - if( headers != null ) { - for( Entry<String, Matcher> entry : headers.entrySet() ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " does not have the expected value for header " + entry.getKey(), - request.getHeader( entry.getKey() ), entry.getValue() ); - } - } - if( cookies != null ) { - List<Cookie> requestCookies = Arrays.asList( request.getCookies() ); - for( Cookie cookie: cookies ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " does not have the expected cookie " + cookie, - requestCookies, hasItem( cookie ) ); - } - } - if( contentType != null ) { - String[] requestContentType = request.getContentType().split(";",2); - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " does not have the expected content type", - requestContentType[ 0 ], is( contentType ) ); - } - if( characterEncoding != null ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " does not have the expected character encoding", - request.getCharacterEncoding(), equalToIgnoringCase( characterEncoding ) ); - } - if( contentLength != null ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " does not have the expected content length", - request.getContentLength(), is( contentLength ) ); - } - if( attributes != null ) { - for( String name: attributes.keySet() ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " is missing attribute '" + name + "'", - request.getAttribute( name ), notNullValue() ); - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " has wrong value for attribute '" + name + "'", - request.getAttribute( name ), is( request.getAttribute( name ) ) ); - } - } - // Note: Cannot use any of the expect.getParameter*() methods because they will read the - // body and we don't want that to happen. - if( queryParams != null ) { - String queryString = request.getQueryString(); - List<NameValuePair> requestParams = parseQueryString( queryString == null ? "" : queryString ); - for( Entry<String, String> entry : queryParams.entrySet() ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " query string " + queryString + " is missing parameter '" + entry.getKey() + "'", - requestParams, hasItem( new BasicNameValuePair(entry.getKey(), entry.getValue())) ); - } - } - if( formParams != null ) { - String paramString = IOUtils.toString( request.getInputStream(), request.getCharacterEncoding() ); - List<NameValuePair> requestParams = parseQueryString( paramString == null ? "" : paramString ); - for( Entry<String, String[]> entry : formParams.entrySet() ) { - String[] expectedValues = entry.getValue(); - for( String expectedValue : expectedValues ) { - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " form params " + paramString + " is missing a value " + expectedValue + " for parameter '" + entry.getKey() + "'", - requestParams, hasItem( new BasicNameValuePair(entry.getKey(), expectedValue ) )); - } - } - } - if( entity != null ) { - if( contentType != null && contentType.endsWith( "/xml" ) ) { - String expectEncoding = characterEncoding; - String expect = new String( entity, ( expectEncoding == null ? UTF8.name() : expectEncoding ) ); - String actualEncoding = request.getCharacterEncoding(); - String actual = IOUtils.toString( request.getInputStream(), actualEncoding == null ? UTF8.name() : actualEncoding ); - assertThat( the( actual ), isEquivalentTo( the( expect ) ) ); - } else if ( contentType != null && contentType.endsWith( "/json" ) ) { - String expectEncoding = characterEncoding; - String expect = new String( entity, ( expectEncoding == null ? UTF8.name() : expectEncoding ) ); - String actualEncoding = request.getCharacterEncoding(); - String actual = IOUtils.toString( request.getInputStream(), actualEncoding == null ? UTF8.name() : actualEncoding ); -// System.out.println( "EXPECT=" + expect ); -// System.out.println( "ACTUAL=" + actual ); - assertThat( actual, sameJSONAs( expect ) ); - } else if( characterEncoding == null || request.getCharacterEncoding() == null ) { - byte[] bytes = IOUtils.toByteArray( request.getInputStream() ); - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " content does not match the expected content", - bytes, is( entity ) ); - } else { - String expect = new String( entity, characterEncoding ); - String actual = IOUtils.toString( request.getInputStream(), request.getCharacterEncoding() ); - assertThat( - "Request " + request.getMethod() + " " + request.getRequestURL() + - " content does not match the expected content", - actual, is( expect ) ); - } - } - } - - public String toString() { - return "from=" + from + ", pathInfo=" + pathInfo; - } - - private static List<NameValuePair> parseQueryString( String queryString ) { - return URLEncodedUtils.parse(queryString, Charset.defaultCharset()); - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockResponseProvider.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockResponseProvider.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockResponseProvider.java deleted file mode 100644 index b1b1178..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockResponseProvider.java +++ /dev/null @@ -1,158 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import org.apache.commons.io.IOUtils; - -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Locale; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -public class MockResponseProvider { - - Integer errorCode = null; - String errorMsg = null; - Integer statusCode = null; - String redirectUrl = null; - Map<String,String> headers = null; - Set<Cookie> cookies = null; - byte[] entity = null; - String contentType = null; - String characterEncoding = null; - Integer contentLength = null; - - public MockResponseProvider status( int statusCode ) { - this.statusCode = statusCode; - return this; - } - - public MockResponseProvider error( int code, String message ) { - errorCode = code; - errorMsg = message; - return this; - } - - public MockResponseProvider redirect( String location ) { - redirectUrl = location; - return this; - } - - public MockResponseProvider header( String name, String value ) { - if( headers == null ) { - headers = new HashMap<>(); - } - headers.put( name, value ); - return this; - } - - public MockResponseProvider cookie( Cookie cookie ) { - if( cookies == null ) { - cookies = new HashSet<>(); - } - cookies.add( cookie ); - return this; - } - - public MockResponseProvider content( byte[] entity ) { - this.entity = entity; - return this; - } - - public MockResponseProvider content( String string, Charset charset ) { - this.entity = string.getBytes( charset ); - return this; - } - - public MockResponseProvider content( URL url ) throws IOException { - content( url.openStream() ); - return this; - } - - public MockResponseProvider content( InputStream stream ) throws IOException { - content( IOUtils.toByteArray( stream ) ); - return this; - } - - public MockResponseProvider contentType( String contentType ) { - this.contentType = contentType; - return this; - } - - public MockResponseProvider contentLength( int contentLength ) { - this.contentLength = contentLength; - return this; - } - - public MockResponseProvider characterEncoding( String charset ) { - this.characterEncoding = charset; - return this; - } - - public void apply( HttpServletResponse response ) throws IOException { - if( statusCode != null ) { - response.setStatus( statusCode ); - } else { - response.setStatus( HttpServletResponse.SC_OK ); - } - if( errorCode != null ) { - if( errorMsg != null ) { - response.sendError( errorCode, errorMsg ); - } else { - response.sendError( errorCode ); - } - } - if( redirectUrl != null ) { - response.sendRedirect( redirectUrl ); - } - if( headers != null ) { - for( Entry<String, String> entry : headers.entrySet() ) { - response.addHeader( entry.getKey(), entry.getValue() ); - } - } - if( cookies != null ) { - for( Cookie cookie: cookies ) { - response.addCookie( cookie ); - } - } - if( contentType != null ) { - response.setContentType( contentType ); - } - if( characterEncoding != null ) { - response.setCharacterEncoding( characterEncoding ); - } - if( contentLength != null ) { - response.setContentLength( contentLength ); - } - response.flushBuffer(); - if( entity != null ) { - response.getOutputStream().write( entity ); - //KNOX-685: response.getOutputStream().flush(); - response.getOutputStream().close(); - } - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServer.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServer.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServer.java deleted file mode 100644 index 5d95ce6..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServer.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.servlet.Servlet; -import java.util.LinkedList; -import java.util.Queue; - -/** - * An embedded Jetty server with a single servlet deployed on "/*". - * It is used by populating a queue of "interactions". - * Each interaction is an expected request and a resulting response. - * These interactions are added to a queue in a fluent API style. - * So in most of the tests like GatewayBasicFuncTest.testBasicJsonUseCase you will see calls like - * driver.getMock( "WEBHDFS" ).expect()....respond()...; - * This adds a single interaction to the mock server which is returned via the driver.getMock( "WEBHDFS" ) above. - * Any number of interactions may be added. - * When the request comes in it will check the request against the expected request. - * If it matches return the response otherwise it will return a 500 error. - * Typically at the end of a test you should check to make sure the interaction queue is consumed by calling isEmpty(). - * The reset() method can be used to ensure everything is cleaned up so that the mock server can be reused beteween tests. - * The whole idea was modeled after how the REST testing framework REST-assured and aims to be a server side equivalent. - */ -public class MockServer { - - private Logger log = LoggerFactory.getLogger( this.getClass() ); - - private String name; - private Server jetty; - - private Queue<MockInteraction> interactions = new LinkedList<MockInteraction>(); - - public MockServer( String name ) { - this.name = name; - } - - public MockServer( String name, boolean start ) throws Exception { - this.name = name; - if( start ) { - start(); - } - } - - public String getName() { - return name; - } - - public void start() throws Exception { - Handler context = createHandler(); - jetty = new Server(0); - jetty.setHandler( context ); - jetty.start(); - log.info( "Mock server started on port " + getPort() ); - } - - public void stop() throws Exception { - jetty.stop(); - jetty.join(); - } - - private ServletContextHandler createHandler() { - Servlet servlet = new MockServlet( getName(), interactions ); - ServletHolder holder = new ServletHolder( servlet ); - ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS ); - context.setContextPath( "/" ); - context.addServlet( holder, "/*" ); - return context; - } - - public int getPort() { - return jetty.getURI().getPort(); - } - - public MockRequestMatcher expect() { - MockInteraction interaction = new MockInteraction(); - interactions.add( interaction ); - return interaction.expect(); - } - - public MockResponseProvider respond() { - MockInteraction interaction = new MockInteraction(); - interactions.add( interaction ); - return interaction.respond(); - } - - public int getCount() { - return interactions.size(); - } - - public boolean isEmpty() { - return interactions.isEmpty(); - } - - public void reset() { - interactions.clear(); - } -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServlet.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServlet.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServlet.java deleted file mode 100644 index ca4692c..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServlet.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Queue; - -import org.apache.log4j.Logger; - -import static org.junit.Assert.fail; - -public class MockServlet extends HttpServlet { - - private static final Logger LOG = Logger.getLogger(MockServlet.class.getName()); - - public String name; - public Queue<MockInteraction> interactions; - - public MockServlet( String name, Queue<MockInteraction> interactions ) { - this.name = name; - this.interactions = interactions; - } - - @Override - protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { - LOG.debug( "service: request=" + request.getMethod() + " " + request.getRequestURL() + "?" + request.getQueryString() ); - try { - if( interactions.isEmpty() ) { - fail( "Mock servlet " + name + " received a request but the expected interaction queue is empty." ); - } - MockInteraction interaction = interactions.remove(); - interaction.expect().match( request ); - interaction.respond().apply( response ); - LOG.debug( "service: response=" + response.getStatus() ); - } catch( AssertionError e ) { - LOG.debug( "service: exception=" + e.getMessage() ); - e.printStackTrace(); // I18N not required. - throw new ServletException( e ); - } - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletContext.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletContext.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletContext.java deleted file mode 100644 index 0df84c3..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletContext.java +++ /dev/null @@ -1,293 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import javax.servlet.Filter; -import javax.servlet.FilterRegistration; -import javax.servlet.RequestDispatcher; -import javax.servlet.Servlet; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; -import javax.servlet.SessionCookieConfig; -import javax.servlet.SessionTrackingMode; -import javax.servlet.descriptor.JspConfigDescriptor; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Enumeration; -import java.util.EventListener; -import java.util.Map; -import java.util.Set; - -public class MockServletContext implements ServletContext { - - @Override - public String getContextPath() { - return null; - } - - @Override - public ServletContext getContext( String s ) { - return null; - } - - @Override - public int getMajorVersion() { - return 0; - } - - @Override - public int getMinorVersion() { - return 0; - } - - @Override - public int getEffectiveMajorVersion() { - return 0; - } - - @Override - public int getEffectiveMinorVersion() { - return 0; - } - - @Override - public String getMimeType( String s ) { - return null; - } - - @Override - public Set<String> getResourcePaths( String s ) { - return null; - } - - @Override - public URL getResource( String s ) throws MalformedURLException { - return null; - } - - @Override - public InputStream getResourceAsStream( String s ) { - return null; - } - - @Override - public RequestDispatcher getRequestDispatcher( String s ) { - return null; - } - - @Override - public RequestDispatcher getNamedDispatcher( String s ) { - return null; - } - - @Override - @SuppressWarnings("deprecation") - public Servlet getServlet( String s ) throws ServletException { - return null; - } - - @Override - @SuppressWarnings("deprecation") - public Enumeration<Servlet> getServlets() { - return null; - } - - @Override - @SuppressWarnings("deprecation") - public Enumeration<String> getServletNames() { - return null; - } - - @Override - public void log( String s ) { - } - - @Override - @SuppressWarnings("deprecation") - public void log( Exception e, String s ) { - } - - @Override - public void log( String s, Throwable throwable ) { - } - - @Override - public String getRealPath( String s ) { - return null; - } - - @Override - public String getServerInfo() { - return null; - } - - @Override - public String getInitParameter( String s ) { - return null; - } - - @Override - public Enumeration<String> getInitParameterNames() { - return null; - } - - @Override - public boolean setInitParameter( String s, String s1 ) { - return false; - } - - @Override - public Object getAttribute( String s ) { - return null; - } - - @Override - public Enumeration<String> getAttributeNames() { - return null; - } - - @Override - public void setAttribute( String s, Object o ) { - } - - @Override - public void removeAttribute( String s ) { - } - - @Override - public String getServletContextName() { - return null; - } - - @Override - public ServletRegistration.Dynamic addServlet( String s, String s1 ) { - return null; - } - - @Override - public ServletRegistration.Dynamic addServlet( String s, Servlet servlet ) { - return null; - } - - @Override - public ServletRegistration.Dynamic addServlet( String s, Class<? extends Servlet> aClass ) { - return null; - } - - @Override - public <T extends Servlet> T createServlet( Class<T> tClass ) throws ServletException { - return null; - } - - @Override - public ServletRegistration getServletRegistration( String s ) { - return null; - } - - @Override - public Map<String, ? extends ServletRegistration> getServletRegistrations() { - return null; - } - - @Override - public FilterRegistration.Dynamic addFilter( String s, String s1 ) { - return null; - } - - @Override - public FilterRegistration.Dynamic addFilter( String s, Filter filter ) { - return null; - } - - @Override - public FilterRegistration.Dynamic addFilter( String s, Class<? extends Filter> aClass ) { - return null; - } - - @Override - public <T extends Filter> T createFilter( Class<T> tClass ) throws ServletException { - return null; - } - - @Override - public FilterRegistration getFilterRegistration( String s ) { - return null; - } - - @Override - public Map<String, ? extends FilterRegistration> getFilterRegistrations() { - return null; - } - - @Override - public SessionCookieConfig getSessionCookieConfig() { - return null; - } - - @Override - public void setSessionTrackingModes( Set<SessionTrackingMode> sessionTrackingModes ) { - } - - @Override - public Set<SessionTrackingMode> getDefaultSessionTrackingModes() { - return null; - } - - @Override - public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() { - return null; - } - - @Override - public void addListener( String s ) { - } - - @Override - public <T extends EventListener> void addListener( T t ) { - } - - @Override - public void addListener( Class<? extends EventListener> aClass ) { - } - - @Override - public <T extends EventListener> T createListener( Class<T> tClass ) throws ServletException { - return null; - } - - @Override - public JspConfigDescriptor getJspConfigDescriptor() { - return null; - } - - @Override - public ClassLoader getClassLoader() { - return null; - } - - @Override - public void declareRoles( String... strings ) { - } - - @Override - public String getVirtualServerName() { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletInputStream.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletInputStream.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletInputStream.java deleted file mode 100644 index 227dc1c..0000000 --- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockServletInputStream.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * 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.hadoop.test.mock; - -import java.io.IOException; -import java.io.InputStream; - -import javax.servlet.ReadListener; -import javax.servlet.ServletInputStream; - -public class MockServletInputStream extends ServletInputStream { - - private InputStream stream; - - public MockServletInputStream( InputStream stream ) { - this.stream = stream; - } - - @Override - public int read() throws IOException { - return stream.read(); - } - - @Override - public boolean isFinished() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isReady() { - throw new UnsupportedOperationException(); - } - - @Override - public void setReadListener( ReadListener readListener ) { - throw new UnsupportedOperationException(); - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/Console.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/Console.java b/gateway-test-utils/src/main/java/org/apache/knox/test/Console.java new file mode 100644 index 0000000..0965748 --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/Console.java @@ -0,0 +1,57 @@ +/** + * 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.knox.test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +public class Console { + + PrintStream oldOut, newOut; + PrintStream oldErr, newErr; + ByteArrayOutputStream newOutBuf, newErrBuf; + + public void capture() { + oldErr = System.err; + newErrBuf = new ByteArrayOutputStream(); + newErr = new PrintStream( newErrBuf ); + + oldOut = System.out; // I18N not required. + newOutBuf = new ByteArrayOutputStream(); + newOut = new PrintStream( newOutBuf ); + + System.setErr( newErr ); + System.setOut( newOut ); + } + + public byte[] getOut() { + return newOutBuf.toByteArray(); + } + + public byte[] getErr() { + return newErrBuf.toByteArray(); + } + + public void release() { + System.setErr( oldErr ); + System.setOut( oldOut ); + newErr.close(); + newOut.close(); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java b/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java new file mode 100644 index 0000000..5437ce1 --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java @@ -0,0 +1,216 @@ +/** + * 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.knox.test; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringWriter; +import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.URL; +import java.nio.ByteBuffer; +import java.util.Properties; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.log4j.Logger; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.runtime.RuntimeConstants; +import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; +import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.servlet.ServletTester; + +public class TestUtils { + + private static Logger LOG = Logger.getLogger(TestUtils.class); + + public static final long SHORT_TIMEOUT = 1000L; + public static final long MEDIUM_TIMEOUT = 20 * 1000L; + public static final long LONG_TIMEOUT = 60 * 1000L; + + public static String getResourceName( Class clazz, String name ) { + name = clazz.getName().replaceAll( "\\.", "/" ) + "/" + name; + return name; + } + + public static URL getResourceUrl( Class clazz, String name ) throws FileNotFoundException { + name = getResourceName( clazz, name ); + URL url = ClassLoader.getSystemResource( name ); + if( url == null ) { + throw new FileNotFoundException( name ); + } + return url; + } + + public static URL getResourceUrl( String name ) throws FileNotFoundException { + URL url = ClassLoader.getSystemResource( name ); + if( url == null ) { + throw new FileNotFoundException( name ); + } + return url; + } + + public static InputStream getResourceStream( String name ) throws IOException { + URL url = ClassLoader.getSystemResource( name ); + InputStream stream = url.openStream(); + return stream; + } + + public static InputStream getResourceStream( Class clazz, String name ) throws IOException { + URL url = getResourceUrl( clazz, name ); + InputStream stream = url.openStream(); + return stream; + } + + public static Reader getResourceReader( String name, String charset ) throws IOException { + return new InputStreamReader( getResourceStream( name ), charset ); + } + + public static Reader getResourceReader( Class clazz, String name, String charset ) throws IOException { + return new InputStreamReader( getResourceStream( clazz, name ), charset ); + } + + public static String getResourceString( Class clazz, String name, String charset ) throws IOException { + return IOUtils.toString( getResourceReader( clazz, name, charset ) ); + } + + public static File createTempDir( String prefix ) throws IOException { + File targetDir = new File( System.getProperty( "user.dir" ), "target" ); + File tempDir = new File( targetDir, prefix + UUID.randomUUID() ); + FileUtils.forceMkdir( tempDir ); + return tempDir; + } + + public static void LOG_ENTER() { + StackTraceElement caller = Thread.currentThread().getStackTrace()[2]; + System.out.flush(); + System.out.println( String.format( "Running %s#%s", caller.getClassName(), caller.getMethodName() ) ); + System.out.flush(); + } + + public static void LOG_EXIT() { + StackTraceElement caller = Thread.currentThread().getStackTrace()[2]; + System.out.flush(); + System.out.println( String.format( "Exiting %s#%s", caller.getClassName(), caller.getMethodName() ) ); + System.out.flush(); + } + + public static void awaitPortOpen( InetSocketAddress address, int timeout, int delay ) throws InterruptedException { + long maxTime = System.currentTimeMillis() + timeout; + do { + try { + Socket socket = new Socket(); + socket.connect( address, delay ); + socket.close(); + return; + } catch ( IOException e ) { + //e.printStackTrace(); + } + } while( System.currentTimeMillis() < maxTime ); + throw new IllegalStateException( "Timed out " + timeout + " waiting for port " + address ); + } + + public static void awaitNon404HttpStatus( URL url, int timeout, int delay ) throws InterruptedException { + long maxTime = System.currentTimeMillis() + timeout; + do { + Thread.sleep( delay ); + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection)url.openConnection(); + conn.getInputStream().close(); + return; + } catch ( IOException e ) { + //e.printStackTrace(); + try { + if( conn != null && conn.getResponseCode() != 404 ) { + return; + } + } catch ( IOException ee ) { + //ee.printStackTrace(); + } + } + } while( System.currentTimeMillis() < maxTime ); + throw new IllegalStateException( "Timed out " + timeout + " waiting for URL " + url ); + } + + public static String merge( String resource, Properties properties ) { + ClasspathResourceLoader loader = new ClasspathResourceLoader(); + loader.getResourceStream( resource ); + + VelocityEngine engine = new VelocityEngine(); + Properties config = new Properties(); + config.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem" ); + config.setProperty( RuntimeConstants.RESOURCE_LOADER, "classpath" ); + config.setProperty( "classpath.resource.loader.class", ClasspathResourceLoader.class.getName() ); + engine.init( config ); + + VelocityContext context = new VelocityContext( properties ); + Template template = engine.getTemplate( resource ); + StringWriter writer = new StringWriter(); + template.merge( context, writer ); + return writer.toString(); + } + + public static String merge( Class base, String resource, Properties properties ) { + String baseResource = base.getName().replaceAll( "\\.", "/" ); + String fullResource = baseResource + "/" + resource; + return merge( fullResource, properties ); + } + + public static int findFreePort() throws IOException { + ServerSocket socket = new ServerSocket(0); + int port = socket.getLocalPort(); + socket.close(); + return port; + } + + public static void waitUntilNextSecond() { + long before = System.currentTimeMillis(); + long wait; + while( ( wait = ( 1000 - ( System.currentTimeMillis() - before ) ) ) > 0 ) { + try { + Thread.sleep( wait ); + } catch( InterruptedException e ) { + // Ignore. + } + } + } + + public static HttpTester.Response execute( ServletTester server, HttpTester.Request request ) throws Exception { + LOG.debug( "execute: request=" + request ); + ByteBuffer requestBuffer = request.generate(); + LOG.trace( "execute: requestBuffer=[" + new String(requestBuffer.array(),0,requestBuffer.limit()) + "]" ); + ByteBuffer responseBuffer = server.getResponses( requestBuffer, 30, TimeUnit.SECONDS ); + HttpTester.Response response = HttpTester.parseResponse( responseBuffer ); + LOG.trace( "execute: responseBuffer=[" + new String(responseBuffer.array(),0,responseBuffer.limit()) + "]" ); + LOG.debug( "execute: reponse=" + response ); + return response; + } + + +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/category/FastTests.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/category/FastTests.java b/gateway-test-utils/src/main/java/org/apache/knox/test/category/FastTests.java new file mode 100644 index 0000000..2360c17 --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/category/FastTests.java @@ -0,0 +1,21 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.knox.test.category; + +public interface FastTests { +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/category/ManualTests.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/category/ManualTests.java b/gateway-test-utils/src/main/java/org/apache/knox/test/category/ManualTests.java new file mode 100644 index 0000000..0065357 --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/category/ManualTests.java @@ -0,0 +1,21 @@ +/** + * 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.knox.test.category; + +public interface ManualTests { +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/category/MediumTests.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/category/MediumTests.java b/gateway-test-utils/src/main/java/org/apache/knox/test/category/MediumTests.java new file mode 100644 index 0000000..f5d354b --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/category/MediumTests.java @@ -0,0 +1,21 @@ +/** + * 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.knox.test.category; + +public interface MediumTests { +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/category/ReleaseTest.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/category/ReleaseTest.java b/gateway-test-utils/src/main/java/org/apache/knox/test/category/ReleaseTest.java new file mode 100644 index 0000000..6e2279e --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/category/ReleaseTest.java @@ -0,0 +1,21 @@ +/** + * 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.knox.test.category; + +public interface ReleaseTest { +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/category/SlowTests.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/category/SlowTests.java b/gateway-test-utils/src/main/java/org/apache/knox/test/category/SlowTests.java new file mode 100644 index 0000000..3f0b50c --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/category/SlowTests.java @@ -0,0 +1,21 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.knox.test.category; + +public interface SlowTests { +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/category/UnitTests.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/category/UnitTests.java b/gateway-test-utils/src/main/java/org/apache/knox/test/category/UnitTests.java new file mode 100644 index 0000000..0d91e00 --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/category/UnitTests.java @@ -0,0 +1,21 @@ +/** + * 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.knox.test.category; + +public interface UnitTests { +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/category/VerifyTest.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/category/VerifyTest.java b/gateway-test-utils/src/main/java/org/apache/knox/test/category/VerifyTest.java new file mode 100644 index 0000000..825c08f --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/category/VerifyTest.java @@ -0,0 +1,21 @@ +/** + * 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.knox.test.category; + +public interface VerifyTest { +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/log/CollectAppender.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/log/CollectAppender.java b/gateway-test-utils/src/main/java/org/apache/knox/test/log/CollectAppender.java new file mode 100644 index 0000000..3ab0c93 --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/log/CollectAppender.java @@ -0,0 +1,51 @@ +/** + * 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.knox.test.log; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.log4j.AppenderSkeleton; +import org.apache.log4j.spi.LoggingEvent; + +public class CollectAppender extends AppenderSkeleton { + + public CollectAppender() { + super(); + } + + public static BlockingQueue<LoggingEvent> queue = new LinkedBlockingQueue<LoggingEvent>(); + public static boolean closed = false; + + @Override + protected void append( LoggingEvent event ) { + event.getProperties(); + queue.add( event ); + } + + @Override + public void close() { + closed = true; + } + + @Override + public boolean requiresLayout() { + return false; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpAppender.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpAppender.java b/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpAppender.java new file mode 100644 index 0000000..80a7fce --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpAppender.java @@ -0,0 +1,98 @@ +/** + * 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.knox.test.log; + +import org.apache.log4j.Appender; +import org.apache.log4j.Layout; +import org.apache.log4j.Logger; +import org.apache.log4j.spi.ErrorHandler; +import org.apache.log4j.spi.Filter; +import org.apache.log4j.spi.LoggingEvent; + +import java.util.Enumeration; + +public class NoOpAppender implements Appender { + + public static Enumeration<Appender> setUp() { + Enumeration<Appender> appenders = (Enumeration<Appender>)Logger.getRootLogger().getAllAppenders(); + Logger.getRootLogger().removeAllAppenders(); + Logger.getRootLogger().addAppender( new NoOpAppender() ); + return appenders; + } + + public static void tearDown( Enumeration<Appender> appenders ) { + if( appenders != null ) { + while( appenders.hasMoreElements() ) { + Logger.getRootLogger().addAppender( appenders.nextElement() ); + } + } + } + + @Override + public void addFilter( Filter newFilter ) { + } + + @Override + public Filter getFilter() { + return null; + } + + @Override + public void clearFilters() { + } + + @Override + public void close() { + } + + @Override + public void doAppend( LoggingEvent event ) { + } + + @Override + public String getName() { + return this.getClass().getName(); + } + + @Override + public void setErrorHandler( ErrorHandler errorHandler ) { + } + + @Override + public ErrorHandler getErrorHandler() { + return null; + } + + @Override + public void setLayout( Layout layout ) { + } + + @Override + public Layout getLayout() { + return null; + } + + @Override + public void setName( String name ) { + } + + @Override + public boolean requiresLayout() { + return false; + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpLogger.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpLogger.java b/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpLogger.java new file mode 100644 index 0000000..2c6763f --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/log/NoOpLogger.java @@ -0,0 +1,87 @@ +/** + * 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.knox.test.log; + +import org.eclipse.jetty.util.log.Logger; + +public class NoOpLogger implements Logger { + + @Override + public String getName() { + return ""; + } + + @Override + public void warn( String msg, Object... args ) { + } + + @Override + public void warn( Throwable thrown ) { + } + + @Override + public void warn( String msg, Throwable thrown ) { + } + + @Override + public void info( String msg, Object... args ) { + } + + @Override + public void info( Throwable thrown ) { + } + + @Override + public void info( String msg, Throwable thrown ) { + } + + @Override + public boolean isDebugEnabled() { + return false; + } + + @Override + public void setDebugEnabled( boolean enabled ) { + } + + @Override + public void debug( String msg, Object... args ) { + } + + @Override + public void debug( String msg, long arg ) { + } + + @Override + public void debug( Throwable thrown ) { + } + + @Override + public void debug( String msg, Throwable thrown ) { + } + + @Override + public Logger getLogger( String name ) { + return this; + } + + @Override + public void ignore( Throwable ignored ) { + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/1451428f/gateway-test-utils/src/main/java/org/apache/knox/test/mock/MockFilterConfig.java ---------------------------------------------------------------------- diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/mock/MockFilterConfig.java b/gateway-test-utils/src/main/java/org/apache/knox/test/mock/MockFilterConfig.java new file mode 100644 index 0000000..39a3625 --- /dev/null +++ b/gateway-test-utils/src/main/java/org/apache/knox/test/mock/MockFilterConfig.java @@ -0,0 +1,46 @@ +/** + * 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.knox.test.mock; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import java.util.Enumeration; + +public class MockFilterConfig implements FilterConfig { + + @Override + public String getFilterName() { + return null; + } + + @Override + public ServletContext getServletContext() { + return null; + } + + @Override + public String getInitParameter( String s ) { + return null; + } + + @Override + public Enumeration<String> getInitParameterNames() { + return null; + } + +}
