[OLINGO-979]Changed exception message accessing from getMessage() to 
getLocalizedMessage() and added a test

Signed-off-by: Christian Amend <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/7d698645
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/7d698645
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/7d698645

Branch: refs/heads/master
Commit: 7d698645e0769b648312d833f484d32e3221e99c
Parents: 7742338
Author: Morten Riedel <[email protected]>
Authored: Thu Jul 14 09:52:34 2016 +0200
Committer: Christian Amend <[email protected]>
Committed: Thu Jul 14 10:46:31 2016 +0200

----------------------------------------------------------------------
 .../server/core/ODataExceptionHelper.java       |  2 +-
 .../server/core/ODataHandlerImplTest.java       | 32 +++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7d698645/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
 
b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
index 47896c9..e5117cd 100644
--- 
a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
+++ 
b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
@@ -143,7 +143,7 @@ public class ODataExceptionHelper {
   }
 
   private static ODataServerError basicServerError(final Exception e) {
-    ODataServerError serverError = new 
ODataServerError().setException(e).setMessage(e.getMessage());
+    ODataServerError serverError = new 
ODataServerError().setException(e).setMessage(e.getLocalizedMessage());
     if (serverError.getMessage() == null) {
       serverError.setMessage("OData Library: An exception without message text 
was thrown.");
     }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7d698645/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
----------------------------------------------------------------------
diff --git 
a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
 
b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
index 7b1a05b..2ed5250 100644
--- 
a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
+++ 
b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.olingo.server.core;
 
+import java.io.InputStream;
+import java.nio.charset.Charset;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -266,7 +268,35 @@ public class ODataHandlerImplTest {
     assertEquals("application/json;odata.metadata=minimal",
         response.getHeader(HttpHeader.CONTENT_TYPE));
   }
-
+  
+  @Test
+  public void applicationExceptionInProcessorMessage() throws Exception {
+    final String ODATA_ERRORCODE = "425";
+    final String ORIGINAL_MESSAGE = "original message";
+    final String LOCALIZED_MESSAGE = "localized message";
+    MetadataProcessor processor = mock(MetadataProcessor.class);
+    
+    ODataApplicationException oDataApplicationException = new 
ODataApplicationException(ORIGINAL_MESSAGE, 425, Locale.ENGLISH, 
ODATA_ERRORCODE) {
+      @Override
+      public String getLocalizedMessage() {
+          return LOCALIZED_MESSAGE;
+      }
+    };
+    
+    doThrow(oDataApplicationException).when(processor).readMetadata(
+        any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), 
any(ContentType.class));
+    
+    final ODataResponse response = dispatch(HttpMethod.GET, "$metadata", 
processor);
+    InputStream contentStream = response.getContent();
+    String responseContent = IOUtils.toString(contentStream, 
Charset.forName("UTF-8"));
+    // does the response contain the localized message and the status code?
+    boolean isMessage = responseContent.contains(LOCALIZED_MESSAGE) && 
responseContent.contains(ODATA_ERRORCODE);
+    // test if message is localized
+    assertEquals(true, isMessage);
+    // test if the original is hold
+    assertEquals(ORIGINAL_MESSAGE, oDataApplicationException.getMessage());
+  }
+  
   @Test
   public void applicationExceptionInProcessor() throws Exception {
     MetadataProcessor processor = mock(MetadataProcessor.class);

Reply via email to