diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/debug/DebugSupport.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/debug/DebugSupport.java index b3721e64a..34527b961 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/debug/DebugSupport.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/debug/DebugSupport.java @@ -32,26 +32,25 @@ public interface DebugSupport { public static final String ODATA_DEBUG_DOWNLOAD = "download"; /** - * Initializes the debug support implementation. Is called before {@link #isUserAuthorized()} and - * {@link #createDebugResponse(String, DebugInformation)} + * Initializes the debug support implementation. + * Is called before {@link #isUserAuthorized()} and + * {@link #createDebugResponse(String, DebugInformation)}. * @param odata */ void init(OData odata); /** - * This method is called to make sure that the user that requested the debug output is authorized to see this output. + * Ensures that the user that requested the debug output is authorized to see this output. * @return true if the current user is authorized */ boolean isUserAuthorized(); /** - * This method should create a debug response and deliver it back to the Olingo library. This method MUST NEVER throw - * an exception. - * @param debugFormat which is requested via the odata-debug query parameter - * @param request object which was send to the server - * @param response object which was filled by the application - * @param exception which has been thrown. Might be null in case there was no exception - * @return a new debug response which will be send to the client + * Creates a debug response and delivers it back to the Olingo library. + * This method MUST NEVER throw an exception. + * @param debugFormat the value of the odata-debug query parameter + * @param debugInfo all necessary information to construct debug output + * @return a new debug response which will be sent to the client */ ODataResponse createDebugResponse(String debugFormat, DebugInformation debugInfo); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java index 5dedc116a..12f2dfd99 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java @@ -74,7 +74,7 @@ public class ODataHandler { public ODataResponse process(final ODataRequest request) { ODataResponse response = new ODataResponse(); - int measurementHandel = debugger.startRuntimeMeasurement("ODataHandler", "processInternal"); + final int responseHandle = debugger.startRuntimeMeasurement("ODataHandler", "process"); try { processInternal(request, response); } catch (final UriValidationException e) { @@ -111,29 +111,51 @@ public class ODataHandler { ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e); handleException(request, response, serverError, e); } - debugger.stopRuntimeMeasurement(measurementHandel); + debugger.stopRuntimeMeasurement(responseHandle); return response; } private void processInternal(final ODataRequest request, final ODataResponse response) throws ODataApplicationException, ODataLibraryException { + final int measurementHandle = debugger.startRuntimeMeasurement("ODataHandler", "processInternal"); + response.setHeader(HttpHeader.ODATA_VERSION, ODataServiceVersion.V40.toString()); + try { + validateODataVersion(request); + } catch (final ODataHandlerException e) { + debugger.stopRuntimeMeasurement(measurementHandle); + throw e; + } - validateODataVersion(request); - - int measurementUriParser = debugger.startRuntimeMeasurement("UriParser", "parseUri"); - uriInfo = new Parser().parseUri(request.getRawODataPath(), request.getRawQueryPath(), null, - serviceMetadata.getEdm()); + final int measurementUriParser = debugger.startRuntimeMeasurement("UriParser", "parseUri"); + try { + uriInfo = new Parser().parseUri(request.getRawODataPath(), request.getRawQueryPath(), null, + serviceMetadata.getEdm()); + } catch (final ODataLibraryException e) { + debugger.stopRuntimeMeasurement(measurementUriParser); + debugger.stopRuntimeMeasurement(measurementHandle); + throw e; + } debugger.stopRuntimeMeasurement(measurementUriParser); - int measurementUriValidator = debugger.startRuntimeMeasurement("UriValidator", "validate"); + final int measurementUriValidator = debugger.startRuntimeMeasurement("UriValidator", "validate"); final HttpMethod method = request.getMethod(); - new UriValidator().validate(uriInfo, method); + try { + new UriValidator().validate(uriInfo, method); + } catch (final UriValidationException e) { + debugger.stopRuntimeMeasurement(measurementUriValidator); + debugger.stopRuntimeMeasurement(measurementHandle); + throw e; + } debugger.stopRuntimeMeasurement(measurementUriValidator); - int measurementDispatcher = debugger.startRuntimeMeasurement("Dispatcher", "dispatch"); - new ODataDispatcher(uriInfo, this).dispatch(request, response); - debugger.stopRuntimeMeasurement(measurementDispatcher); + final int measurementDispatcher = debugger.startRuntimeMeasurement("ODataDispatcher", "dispatch"); + try { + new ODataDispatcher(uriInfo, this).dispatch(request, response); + } finally { + debugger.stopRuntimeMeasurement(measurementDispatcher); + debugger.stopRuntimeMeasurement(measurementHandle); + } } public void handleException(final ODataRequest request, final ODataResponse response, @@ -154,13 +176,12 @@ public class ODataHandler { } catch (final ContentNegotiatorException e) { requestedContentType = ContentType.JSON; } - int measurementHandle = debugger.startRuntimeMeasurement("ErrorProcessor", "processError"); + final int measurementHandle = debugger.startRuntimeMeasurement("ErrorProcessor", "processError"); exceptionProcessor.processError(request, response, serverError, requestedContentType); debugger.stopRuntimeMeasurement(measurementHandle); } - private void validateODataVersion(final ODataRequest request) - throws ODataHandlerException { + private void validateODataVersion(final ODataRequest request) throws ODataHandlerException { final String maxVersion = request.getHeader(HttpHeader.ODATA_MAX_VERSION); if (maxVersion != null && ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersion)) { throw new ODataHandlerException("ODataVersion not supported: " + maxVersion, diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java index 33b62c3f0..75563b18d 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java @@ -45,10 +45,10 @@ import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.ODataServerError; import org.apache.olingo.server.api.ServiceMetadata; import org.apache.olingo.server.api.debug.DebugSupport; +import org.apache.olingo.server.api.deserializer.DeserializerException; import org.apache.olingo.server.api.etag.CustomETagSupport; import org.apache.olingo.server.api.processor.Processor; import org.apache.olingo.server.api.serializer.CustomContentTypeSupport; -import org.apache.olingo.server.api.serializer.SerializerException; import org.apache.olingo.server.core.debug.ServerCoreDebugger; public class ODataHttpHandlerImpl implements ODataHttpHandler { @@ -72,21 +72,17 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler { ODataResponse odResponse; debugger.resolveDebugMode(request); - int processMethodHandel = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "process"); + final int processMethodHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "process"); try { - int requestHandel = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest"); fillODataRequest(odRequest, request, split); - debugger.stopRuntimeMeasurement(requestHandel); - int responseHandel = debugger.startRuntimeMeasurement("ODataHandler", "process"); odResponse = handler.process(odRequest); - debugger.stopRuntimeMeasurement(responseHandel); // ALL future methods after process must not throw exceptions! } catch (Exception e) { exception = e; odResponse = handleException(odRequest, e); } - debugger.stopRuntimeMeasurement(processMethodHandel); + debugger.stopRuntimeMeasurement(processMethodHandle); if (debugger.isDebugMode()) { Map serverEnvironmentVariables = createEnvironmentVariablesMap(request); @@ -95,7 +91,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler { exception = handler.getLastThrownException(); } odResponse = - debugger.createDebugResponse(request, exception, odRequest, odResponse, handler.getUriInfo(), + debugger.createDebugResponse(odRequest, odResponse, exception, handler.getUriInfo(), serverEnvironmentVariables); } @@ -103,7 +99,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler { } private Map createEnvironmentVariablesMap(HttpServletRequest request) { - LinkedHashMap environment = new LinkedHashMap(); + Map environment = new LinkedHashMap(); environment.put("authType", request.getAuthType()); environment.put("localAddr", request.getLocalAddr()); environment.put("localName", request.getLocalName()); @@ -186,9 +182,9 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler { } } - private ODataRequest fillODataRequest(final ODataRequest odRequest, final HttpServletRequest httpRequest, - final int split) - throws ODataLibraryException { + private ODataRequest fillODataRequest(ODataRequest odRequest, final HttpServletRequest httpRequest, + final int split) throws ODataLibraryException { + final int requestHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest"); try { odRequest.setBody(httpRequest.getInputStream()); odRequest.setProtocol(httpRequest.getProtocol()); @@ -198,13 +194,14 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler { return odRequest; } catch (final IOException e) { - throw new SerializerException("An I/O exception occurred.", e, - SerializerException.MessageKeys.IO_EXCEPTION); + throw new DeserializerException("An I/O exception occurred.", e, + DeserializerException.MessageKeys.IO_EXCEPTION); + } finally { + debugger.stopRuntimeMeasurement(requestHandle); } } - static HttpMethod extractMethod(final HttpServletRequest httpRequest) - throws ODataLibraryException { + static HttpMethod extractMethod(final HttpServletRequest httpRequest) throws ODataLibraryException { try { HttpMethod httpRequestMethod = HttpMethod.valueOf(httpRequest.getMethod()); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java index 88a39ca38..4cc17e686 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java @@ -65,7 +65,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper { } @Override - public ODataResponse createDebugResponse(DebugInformation debugInfo) { + public ODataResponse createDebugResponse(final DebugInformation debugInfo) { try { final List parts = createParts(debugInfo); @@ -100,7 +100,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper { } } - private List createParts(DebugInformation debugInfo) { + private List createParts(final DebugInformation debugInfo) { List parts = new ArrayList(); // request @@ -182,20 +182,18 @@ public class DebugResponseHelperImpl implements DebugResponseHelper { } /** - * Get version field information for response. - * Result is never null. - * + * Gets version field information for response. Result is never null. * @return version field information */ - private String getVersion() { - final String version = DebugResponseHelperImpl.class.getPackage().getImplementationVersion(); - if (version == null) { - return "Olingo"; - } - return "Olingo " + version; + protected static String getVersion() { + final Package pack = DebugResponseHelperImpl.class.getPackage(); + final String name = pack.getImplementationTitle(); + final String version = pack.getImplementationVersion(); + return (name == null ? "Olingo" : name) + + (version == null ? "" : " Version " + version); } - private InputStream wrapInHtml(final List parts, String title) throws IOException { + private InputStream wrapInHtml(final List parts, final String title) throws IOException { StringWriter writer = new StringWriter(); writer.append("", ">"); } - protected static void appendJsonTable(final JsonGenerator gen, final Map entries) + protected static void appendJsonTable(JsonGenerator gen, final Map entries) throws IOException { - gen.writeStartObject(); - - for (Map.Entry entry : entries.entrySet()) { - if (entry.getValue() != null) { - gen.writeStringField(entry.getKey(), entry.getValue()); - } else { - gen.writeNullField(entry.getKey()); + if (entries == null || entries.isEmpty()) { + gen.writeNull(); + } else { + gen.writeStartObject(); + for (Map.Entry entry : entries.entrySet()) { + gen.writeFieldName(entry.getKey()); + if (entry.getValue() == null) { + gen.writeNull(); + } else { + gen.writeString(entry.getValue()); + } } + gen.writeEndObject(); } - gen.writeEndObject(); } - protected static void appendHtmlTable(final Writer writer, final Map entries) throws IOException { + protected static void appendHtmlTable(Writer writer, final Map entries) throws IOException { writer.append("\n\n") .append("\n") .append("\n\n"); - for (final Entry entry : entries.entrySet()) { - writer.append("") - .append("") + .append("\n"); } - writer.append("\n"); } writer.append("\n
NameValue
").append(entry.getKey()).append(""); - if (entry.getValue() != null) { - writer.append(escapeHtml(entry.getValue())); - } else { - writer.append("null"); + if (entries != null && !entries.isEmpty()) { + for (final Entry entry : entries.entrySet()) { + writer.append("
").append(entry.getKey()).append(""); + if (entry.getValue() != null) { + writer.append(escapeHtml(entry.getValue())); + } else { + writer.append("null"); + } + writer.append("
\n"); } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java index 8af0e435e..36c1d05e4 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java @@ -106,12 +106,12 @@ public class DebugTabBody implements DebugTab { case XML: writer.append("
\n");
       writer.append(DebugResponseHelperImpl.escapeHtml(body));
-      writer.append("
\n"); + writer.append("\n\n"); break; case JSON: writer.append("
\n");
       writer.append(DebugResponseHelperImpl.escapeHtml(body));
-      writer.append("
\n"); + writer.append("\n\n"); break; case IMAGE: writer.append("\n"); writer.append(DebugResponseHelperImpl.escapeHtml(body)); - writer.append("\n"); + writer.append("\n\n"); break; } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java index 68cd58a57..12ffc00ea 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java @@ -85,7 +85,7 @@ public class DebugTabResponse implements DebugTab { private Map map(Map> headers) { Map result = new HashMap(); for (Map.Entry> entry : headers.entrySet()) { - if(entry.getValue().size() == 1) { + if (entry.getValue().size() == 1) { result.put(entry.getKey(), entry.getValue().get(0)); } else { result.put(entry.getKey(), entry.getValue().toString()); @@ -105,7 +105,7 @@ public class DebugTabResponse implements DebugTab { if (response != null && response.getContent() != null) { new DebugTabBody(response).appendHtml(writer); } else { - writer.append("

ODataLibrary: no response body

"); + writer.append("

ODataLibrary: no response body

\n"); } } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRuntime.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRuntime.java index c7657908e..757a8b38d 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRuntime.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRuntime.java @@ -89,7 +89,7 @@ public class DebugTabRuntime implements DebugTab { private void appendRuntimeNode(final RuntimeNode node, final String draw, final boolean isLast, final Writer writer) throws IOException { if (node.className != null) { - writer.append("
  • ") + writer.append("
  • \n") .append("") .append("").append(draw) .append(isLast ? "└" : "├").append("─ ") diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java index e8fd90fc0..1601075ba 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java @@ -42,22 +42,14 @@ public class DebugTabServer implements DebugTab { @Override public void appendJson(JsonGenerator gen) throws IOException { - if (serverEnvironmentVariables != null && !serverEnvironmentVariables.isEmpty()) { - DebugResponseHelperImpl.appendJsonTable(gen, serverEnvironmentVariables); - } else { - gen.writeNull(); - } + DebugResponseHelperImpl.appendJsonTable(gen, serverEnvironmentVariables); } @Override public void appendHtml(Writer writer) throws IOException { - final Package pack = DebugResponseHelperImpl.class.getPackage(); writer.append("

    Library Version

    \n") - .append("

    ").append(pack.getImplementationTitle()) - .append(" Version ").append(pack.getImplementationVersion()).append("

    \n") + .append("

    ").append(DebugResponseHelperImpl.getVersion()).append("

    \n") .append("

    Server Environment

    \n"); - if (serverEnvironmentVariables != null && !serverEnvironmentVariables.isEmpty()) { - DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVariables); - } + DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVariables); } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java index 716b65ed2..f67f02dc8 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java @@ -19,12 +19,9 @@ package org.apache.olingo.server.core.debug; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.io.Writer; import java.util.List; -import org.apache.commons.io.IOUtils; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.api.uri.UriResource; import org.apache.olingo.server.api.uri.queryoption.CountOption; @@ -38,9 +35,7 @@ import org.apache.olingo.server.api.uri.queryoption.SelectOption; import org.apache.olingo.server.api.uri.queryoption.SkipOption; import org.apache.olingo.server.api.uri.queryoption.TopOption; import org.apache.olingo.server.api.uri.queryoption.expression.Expression; -import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer; -import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; @@ -63,11 +58,6 @@ public class DebugTabUri implements DebugTab { @Override public void appendJson(JsonGenerator gen) throws IOException { - if (uriInfo == null) { - gen.writeNull(); - return; - } - gen.writeStartObject(); if (uriInfo.getFormatOption() != null) { @@ -233,7 +223,7 @@ public class DebugTabUri implements DebugTab { boolean first = true; for (UriResource resourcePart : selectItem.getResourcePath().getUriResourceParts()) { if (!first) { - selectedProperty = selectedProperty + "/"; + selectedProperty += "/"; } selectedProperty = resourcePart.toString(); first = false; @@ -245,62 +235,12 @@ public class DebugTabUri implements DebugTab { @Override public void appendHtml(final Writer writer) throws IOException { - if (uriInfo == null) { - return; - } - writer.append("

    Uri Information

    \n") - .append("
    • "); - writer.append(getJsonString()); - writer.append("
    \n"); - } - - private String getJsonString() throws IOException { - CircleStreamBuffer csb = new CircleStreamBuffer(); - IOException cachedException = null; - OutputStream outputStream = csb.getOutputStream(); - try { - JsonGenerator json = - new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8) - .setPrettyPrinter(new DefaultPrettyPrinter()); - appendJson(json); - json.close(); - outputStream.close(); - } catch (IOException e) { - throw e; - } finally { - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - if (cachedException != null) { - throw cachedException; - } else { - throw e; - } - } - } - } - - InputStream inputStream = csb.getInputStream(); - try { - String jsonString = IOUtils.toString(inputStream); - inputStream.close(); - return jsonString; - } catch (IOException e) { - throw e; - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - if (cachedException != null) { - throw cachedException; - } else { - throw e; - } - } - } - } + .append("
      \n
    • \n"); + JsonGenerator json = new JsonFactory().createGenerator(writer) + .setPrettyPrinter(new DefaultPrettyPrinter()); + appendJson(json); + json.close(); + writer.append("\n
    • \n
    \n"); } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java index d52e2d497..09ef98d9c 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java @@ -61,17 +61,16 @@ public class ServerCoreDebugger { } } - public ODataResponse createDebugResponse(final HttpServletRequest request, final Exception exception, - final ODataRequest odRequest, final ODataResponse odResponse, UriInfo uriInfo, - Map serverEnvironmentVariables) { + public ODataResponse createDebugResponse(final ODataRequest request, final ODataResponse response, + final Exception exception, final UriInfo uriInfo, final Map serverEnvironmentVariables) { //Failsafe so we do not generate unauthorized debug messages - if(!isDebugMode){ - return odResponse; + if (!isDebugMode) { + return response; } - + try { DebugInformation debugInfo = - createDebugInformation(request, exception, odRequest, odResponse, uriInfo, serverEnvironmentVariables); + createDebugInformation(request, response, exception, uriInfo, serverEnvironmentVariables); return debugSupport.createDebugResponse(debugFormat, debugInfo); } catch (Exception e) { @@ -88,16 +87,15 @@ public class ServerCoreDebugger { return odResponse; } - private DebugInformation createDebugInformation(final HttpServletRequest request, final Exception exception, - final ODataRequest odRequest, final ODataResponse odResponse, UriInfo uriInfo, - Map serverEnvironmentVaribles) { + private DebugInformation createDebugInformation(final ODataRequest request, final ODataResponse response, + final Exception exception, final UriInfo uriInfo, final Map serverEnvironmentVariables) { DebugInformation debugInfo = new DebugInformation(); - debugInfo.setRequest(odRequest); - debugInfo.setApplicationResponse(odResponse); + debugInfo.setRequest(request); + debugInfo.setApplicationResponse(response); debugInfo.setException(exception); - debugInfo.setServerEnvironmentVariables(serverEnvironmentVaribles); + debugInfo.setServerEnvironmentVariables(serverEnvironmentVariables); debugInfo.setUriInfo(uriInfo); diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java index 43b1aaaaf..13a690fdd 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java @@ -18,41 +18,28 @@ */ package org.apache.olingo.server.core.debug; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; -import org.apache.commons.io.IOUtils; -import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer; - -import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; public abstract class AbstractDebugTabTest { - protected String createHtml(DebugTab tab) throws Exception { + protected String createHtml(DebugTab tab) throws IOException { StringWriter writer = new StringWriter(); tab.appendHtml(writer); writer.flush(); - byte[] bytes = writer.toString().getBytes("UTF-8"); - return IOUtils.toString(new ByteArrayInputStream(bytes)); + return writer.toString(); } - protected String createJson(DebugTab requestTab) throws IOException { - CircleStreamBuffer csb = new CircleStreamBuffer(); - JsonGenerator gen = new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8); - requestTab.appendJson(gen); + protected String createJson(DebugTab tab) throws IOException { + StringWriter writer = new StringWriter(); + JsonGenerator gen = new JsonFactory().createGenerator(writer); + tab.appendJson(gen); gen.flush(); gen.close(); - csb.closeWrite(); - return IOUtils.toString(csb.getInputStream()); + writer.flush(); + return writer.toString(); } - - protected void print(DebugTab tab) throws Exception{ - System.out.println(createJson(tab)); - System.out.println("---------------------------------------------------------"); - System.out.println(createHtml(tab)); - } - } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java index b77679fe5..16f0e1517 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java @@ -28,10 +28,7 @@ public class DebugTabBodyTest extends AbstractDebugTabTest { public void nullResponseMustNotLeadToException() throws Exception { DebugTabBody tab = new DebugTabBody(null); - String expectedHtml = "
    \n"
    -        + "ODataLibrary: No body.
    \n"; - assertEquals("null", createJson(tab)); - assertEquals(expectedHtml, createHtml(tab)); + assertEquals("
    \nODataLibrary: No body.\n
    \n", createHtml(tab)); } } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java index af109c2b3..8ca530628 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java @@ -42,7 +42,7 @@ public class DebugTabResponseTest extends AbstractDebugTabTest { + "\n" + "\n" + "

    Response Body

    \n" - + "

    ODataLibrary: no response body

    "; + + "

    ODataLibrary: no response body

    \n"; assertEquals(expectedJson, createJson(tab)); assertEquals(expectedHtml, createHtml(tab)); @@ -71,7 +71,7 @@ public class DebugTabResponseTest extends AbstractDebugTabTest { + "\n" + "\n" + "

    Response Body

    \n" - + "

    ODataLibrary: no response body

    "; + + "

    ODataLibrary: no response body

    \n"; assertEquals(expectedJson, createJson(tab)); assertEquals(expectedHtml, createHtml(tab)); } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java index dc4748da3..fa54c3d68 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java @@ -18,7 +18,8 @@ */ package org.apache.olingo.server.core.debug; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.Collections; import java.util.LinkedHashMap; @@ -35,18 +36,17 @@ public class DebugTabServerTest extends AbstractDebugTabTest { assertEquals("null", createJson(serverTab)); String html = createHtml(serverTab); assertTrue(html.startsWith("

    Library Version

    ")); - assertTrue(html.endsWith("

    Server Environment

    \n")); + assertTrue(html.contains("

    Server Environment

    \n")); } @Test public void initialServerInformationMustNotleadToException() throws Exception { - Map env = Collections.emptyMap(); - DebugTabServer serverTab = new DebugTabServer(env); + DebugTabServer serverTab = new DebugTabServer(Collections. emptyMap()); assertEquals("null", createJson(serverTab)); String html = createHtml(serverTab); assertTrue(html.startsWith("

    Library Version

    ")); - assertTrue(html.endsWith("

    Server Environment

    \n")); + assertTrue(html.contains("

    Server Environment

    \n")); } @Test diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java index 0ec0fceaf..3d994a9ac 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java @@ -18,9 +18,10 @@ */ package org.apache.olingo.server.core.debug; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -30,6 +31,7 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.IOUtils; import org.apache.olingo.commons.api.ex.ODataRuntimeException; +import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.debug.DebugInformation; @@ -44,11 +46,15 @@ public class ServerCoreDebuggerTest { @Before public void setupDebugger() { debugger = new ServerCoreDebugger(OData.newInstance()); - debugger.setDebugSupportProcessor(new LocalDebugProcessor()); + DebugSupport processor = mock(DebugSupport.class); + when(processor.isUserAuthorized()).thenReturn(true); + when(processor.createDebugResponse(anyString(), any(DebugInformation.class))) + .thenThrow(new ODataRuntimeException("Test")); + debugger.setDebugSupportProcessor(processor); } @Test - public void standardIsDebugModeIsFlase() { + public void standardIsDebugModeIsFalse() { assertFalse(debugger.isDebugMode()); } @@ -86,36 +92,20 @@ public class ServerCoreDebuggerTest { } @Test - public void testFailResponse() throws IOException { + public void failResponse() throws IOException { HttpServletRequest request = mock(HttpServletRequest.class); when(request.getParameter(DebugSupport.ODATA_DEBUG_QUERY_PARAMETER)).thenReturn(DebugSupport.ODATA_DEBUG_JSON); debugger.resolveDebugMode(request); - ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, null, null, null); - assertEquals(500, debugResponse.getStatusCode()); + ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, null, null); + assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), debugResponse.getStatusCode()); assertEquals("ODataLibrary: Could not assemble debug response.", IOUtils.toString(debugResponse.getContent())); } @Test public void noDebugModeCreateDebugResponseCallMustDoNothing() { ODataResponse odResponse = new ODataResponse(); - ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, odResponse, null, null); + ODataResponse debugResponse = debugger.createDebugResponse(null, odResponse, null, null, null); - assertTrue(odResponse == debugResponse); - } - - public class LocalDebugProcessor implements DebugSupport { - - @Override - public void init(OData odata) {} - - @Override - public boolean isUserAuthorized() { - return true; - } - - @Override - public ODataResponse createDebugResponse(String debugFormat, DebugInformation debugInfo) { - throw new ODataRuntimeException("Test"); - } + assertEquals(odResponse, debugResponse); } }