You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/02/22 19:04:17 UTC

[isis] branch master updated: ISIS-1870 fix MediaType parsing related tests

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 5a574cc  ISIS-1870 fix MediaType parsing related tests
5a574cc is described below

commit 5a574cc60aeeaad0fe3d94429446cd34724f1af5
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Feb 22 20:04:15 2018 +0100

    ISIS-1870 fix MediaType parsing related tests
---
 .../restfulobjects/applib/util/MediaTypes.java     | 25 +++++++++++++++++-----
 .../applib/RepresentationTypeTest_lookup.java      | 21 ++++++++++--------
 .../restfulobjects/applib/util/ParserTest.java     |  7 +++++-
 3 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/MediaTypes.java b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/MediaTypes.java
index 309dea4..7716113 100644
--- a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/MediaTypes.java
+++ b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/MediaTypes.java
@@ -19,6 +19,9 @@
 
 package org.apache.isis.viewer.restfulobjects.applib.util;
 
+import java.util.List;
+import java.util.stream.Collectors;
+
 import javax.ws.rs.core.MediaType;
 
 import org.apache.isis.applib.internal.base._Strings;
@@ -29,7 +32,8 @@ public class MediaTypes {
 	 * Same as {@code MediaType.valueOf(type)}, but with fallback in case {@code MediaType.valueOf(type)}
 	 * throws an IllegalArgumentException.
 	 * <br/><br/>
-	 * The fallback is to retry with String {@code type} cut off at first occurrence of a semicolon (;).
+	 * 
+	 * The fallback is to retry with some special characters replaces in String {@code type}.
 	 * 
 	 * @param type
 	 * @return
@@ -45,10 +49,21 @@ public class MediaTypes {
 			
 		} catch (IllegalArgumentException e) {
 
-			return _Strings.splitThenStream(type, ";")
-			.findFirst()
-			.map(MediaType::valueOf)
-			.orElseThrow(()->e); // could can't be reached, but re-throw the original exception just in case
+			
+			List<String> chunks = _Strings.splitThenStream(type, ";")
+			.collect(Collectors.toList());
+			
+			final StringBuilder sb = new StringBuilder(); 
+			sb.append(chunks.get(0));
+			
+			if(chunks.size()>1) {
+				chunks.stream()
+				.skip(1)
+				.map(chunk->chunk.replace(":", "..").replace("/", "."))
+				.forEach(chunk->sb.append(';').append(chunk));
+			}
+
+			return MediaType.valueOf(sb.toString());
 			
 		}
 		
diff --git a/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationTypeTest_lookup.java b/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationTypeTest_lookup.java
index d10d9b7..379b3dd 100644
--- a/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationTypeTest_lookup.java
+++ b/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationTypeTest_lookup.java
@@ -19,7 +19,6 @@
 package org.apache.isis.viewer.restfulobjects.applib;
 
 import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
 
@@ -32,16 +31,18 @@ public class RepresentationTypeTest_lookup {
 
     @Test
     public void roundtrip() {
-        // overloaded
         for (final RepresentationType repType : RepresentationType.values()) {
-            final MediaType mediaType = repType.getMediaType();
-            final RepresentationType lookup = RepresentationType.lookup(mediaType);
+            final String name = repType.getName();
+            final RepresentationType lookup = RepresentationType.lookup(name);
             assertSame(repType, lookup);
         }
-
+    }
+    
+    @Test
+    public void roundtrip_overloaded() {
         for (final RepresentationType repType : RepresentationType.values()) {
-            final String name = repType.getName();
-            final RepresentationType lookup = RepresentationType.lookup(name);
+            final MediaType mediaType = repType.getJsonMediaType();
+            final RepresentationType lookup = RepresentationType.lookup(mediaType);
             assertSame(repType, lookup);
         }
     }
@@ -61,8 +62,10 @@ public class RepresentationTypeTest_lookup {
 
     @Test
     public void whenDomainObjectWithXRoParameter() {
-        MediaType toLookup = RepresentationType.DOMAIN_OBJECT.getMediaType("x-ro-domain-type", "http://mycompany.com:39393/domain-types/JdkValuedEntities");
-        // ignores the parameter.
+        MediaType toLookup = RepresentationType.DOMAIN_OBJECT
+        		.getMediaType("x-ro-domain-type", "http://mycompany.com:39393/domain-types/JdkValuedEntities");
+        
+        // ignores the parameter ...
         assertThat(
             RepresentationType.lookup(toLookup), is(RepresentationType.DOMAIN_OBJECT));
     }
diff --git a/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/util/ParserTest.java b/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/util/ParserTest.java
index 9e35c63..53f04ce 100644
--- a/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/util/ParserTest.java
+++ b/core/viewer-restfulobjects-applib/src/test/java/org/apache/isis/viewer/restfulobjects/applib/util/ParserTest.java
@@ -164,7 +164,12 @@ public class ParserTest {
     public void forJaxRsMediaType() {
         final Parser<javax.ws.rs.core.MediaType> parser = Parser.forJaxRsMediaType();
 
-        for (final javax.ws.rs.core.MediaType v : new javax.ws.rs.core.MediaType[] { javax.ws.rs.core.MediaType.APPLICATION_ATOM_XML_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE, javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML_TYPE, javax.ws.rs.core.MediaType.valueOf(RestfulMediaType.APPLICATION_JSON_OBJECT) }) {
+        for (final javax.ws.rs.core.MediaType v : new javax.ws.rs.core.MediaType[] { 
+        		javax.ws.rs.core.MediaType.APPLICATION_ATOM_XML_TYPE, 
+        		javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE, 
+        		javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML_TYPE, 
+        		MediaTypes.parse(RestfulMediaType.APPLICATION_JSON_OBJECT) 
+        		}) {
             final String asString = parser.asString(v);
             final javax.ws.rs.core.MediaType valueOf = parser.valueOf(asString);
             assertThat(v, is(equalTo(valueOf)));

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.