From 0ffc26d1b26488a030cdaa1ce539fd60898e698c Mon Sep 17 00:00:00 2001 From: Stephan Klevenz Date: Fri, 27 Jun 2014 14:22:23 +0200 Subject: [PATCH] [OLINGO-328] improve AcceptType for multi value support --- .../olingo/commons/api/format/AcceptType.java | 20 ++++---- .../commons/api/format/AcceptTypeTest.java | 46 +++++++++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java index 07a5452bb..844e0c48f 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java @@ -50,7 +50,7 @@ public class AcceptType { private static final String PARAMETER_Q = "q"; private static final Pattern Q_PARAMETER_VALUE_PATTERN = Pattern.compile("1|0|1\\.0{1,3}|0\\.\\d{1,3}"); - public static final AcceptType WILDCARD = create(MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, null, 1F); + public static final AcceptType WILDCARD = create(MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, createParameterMap(), 1F); private final String type; private final String subtype; @@ -66,7 +66,7 @@ public class AcceptType { this.quality = quality; } - private TreeMap createParameterMap() { + private static TreeMap createParameterMap() { return new TreeMap(new Comparator() { @Override public int compare(final String o1, final String o2) { @@ -123,11 +123,15 @@ public class AcceptType { * @return a new AcceptType object * @throws IllegalArgumentException if input string is not parseable */ - public static AcceptType create(final String format) { - if (format == null) { - throw new IllegalArgumentException("Parameter format MUST NOT be NULL."); + public static List create(final String format) { + List result = new ArrayList(); + + String[] values = format.split(","); + for (String value : values) { + result.add(new AcceptType(value.trim())); } - return new AcceptType(format); + + return result; } /** @@ -136,7 +140,7 @@ public class AcceptType { * @param format * @return a new ContentType object */ - public static AcceptType parse(final String format) { + public static List parse(final String format) { try { return AcceptType.create(format); } catch (IllegalArgumentException e) { @@ -222,7 +226,7 @@ public class AcceptType { public static List create(final List acceptTypeStrings) { List acceptTypes = new ArrayList(acceptTypeStrings.size()); for (String contentTypeString : acceptTypeStrings) { - acceptTypes.add(create(contentTypeString)); + acceptTypes.addAll(create(contentTypeString)); } return acceptTypes; } diff --git a/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java new file mode 100644 index 000000000..b3e3a2c9d --- /dev/null +++ b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.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.olingo.commons.api.format; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; + +public class AcceptTypeTest { + + @Test + public void testMultiValueCreate() { + List atl = AcceptType.create("1/1,2/2 , 3/3 "); + + assertEquals(3, atl.size()); + assertEquals("1/1", atl.get(0).toString()); + assertEquals("2/2", atl.get(1).toString()); + assertEquals("3/3", atl.get(2).toString()); + } + + @Test + public void testSingleValueCreate() { + List atl = AcceptType.create(" a/a "); + + assertEquals(1, atl.size()); + assertEquals("a/a", atl.get(0).toString()); + } +}