PageRenderTime 118ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core-common/src/test/java/org/glassfish/jersey/message/internal/MediaTypesTest.java

https://gitlab.com/jaragan/jersey
Java | 180 lines | 84 code | 30 blank | 66 comment | 1 complexity | 4dfa1605204df47eaf361ebd4fdb9ecf MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of either the GNU
  7. * General Public License Version 2 only ("GPL") or the Common Development
  8. * and Distribution License("CDDL") (collectively, the "License"). You
  9. * may not use this file except in compliance with the License. You can
  10. * obtain a copy of the License at
  11. * http://glassfish.java.net/public/CDDL+GPL_1_1.html
  12. * or packager/legal/LICENSE.txt. See the License for the specific
  13. * language governing permissions and limitations under the License.
  14. *
  15. * When distributing the software, include this License Header Notice in each
  16. * file and include the License file at packager/legal/LICENSE.txt.
  17. *
  18. * GPL Classpath Exception:
  19. * Oracle designates this particular file as subject to the "Classpath"
  20. * exception as provided by Oracle in the GPL Version 2 section of the License
  21. * file that accompanied this code.
  22. *
  23. * Modifications:
  24. * If applicable, add the following below the License Header, with the fields
  25. * enclosed by brackets [] replaced by your own identifying information:
  26. * "Portions Copyright [year] [name of copyright owner]"
  27. *
  28. * Contributor(s):
  29. * If you wish your version of this file to be governed by only the CDDL or
  30. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  31. * elects to include this software in this distribution under the [CDDL or GPL
  32. * Version 2] license." If you don't indicate a single choice of license, a
  33. * recipient has the option to distribute your version of this file under
  34. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  35. * its licensees as provided above. However, if you add GPL Version 2 code
  36. * and therefore, elected the GPL Version 2 license, then the option applies
  37. * only if the new code is made subject to such option by the copyright
  38. * holder.
  39. */
  40. package org.glassfish.jersey.message.internal;
  41. import java.text.ParseException;
  42. import java.util.Collections;
  43. import java.util.List;
  44. import java.util.Map;
  45. import javax.ws.rs.core.MediaType;
  46. import org.junit.Assert;
  47. import org.junit.Test;
  48. import static org.hamcrest.CoreMatchers.is;
  49. import static org.junit.Assert.assertThat;
  50. import jersey.repackaged.com.google.common.collect.Lists;
  51. /**
  52. * MediaTypes utility method tests.
  53. *
  54. * @author Miroslav Fuksa
  55. * @author Marek Potociar (marek.potociar at oralce.com)
  56. */
  57. public class MediaTypesTest {
  58. @Test
  59. public void testConvertToString() {
  60. final List<MediaType> emptyList = Lists.newArrayList();
  61. Assert.assertEquals("", MediaTypes.convertToString(emptyList));
  62. Assert.assertEquals("\"text/plain\"", MediaTypes.convertToString(Lists.newArrayList(
  63. MediaType.TEXT_PLAIN_TYPE)));
  64. Assert.assertEquals("\"text/plain\", \"application/json\"",
  65. MediaTypes.convertToString(Lists.newArrayList(MediaType.TEXT_PLAIN_TYPE,
  66. MediaType.APPLICATION_JSON_TYPE)));
  67. Assert.assertEquals("\"text/plain\", \"application/json\", \"text/html\"",
  68. MediaTypes.convertToString(Lists.newArrayList(MediaType.TEXT_PLAIN_TYPE,
  69. MediaType.APPLICATION_JSON_TYPE,
  70. MediaType.TEXT_HTML_TYPE)));
  71. }
  72. @Test
  73. public void testMostSpecific() {
  74. MediaType m1;
  75. MediaType m2;
  76. /*** wildcard type ***/
  77. m1 = MediaType.WILDCARD_TYPE;
  78. // wildcard type #1 - concrete type wins
  79. m2 = new MediaType("foo", "bar");
  80. _testMostSpecific(m1, m2, m2);
  81. _testMostSpecific(m2, m1, m2);
  82. // wildcard type #2 - wildcard subtype wins
  83. m2 = new MediaType("foo", "*");
  84. _testMostSpecific(m1, m2, m2);
  85. _testMostSpecific(m2, m1, m2);
  86. // wildcard type #3 - first parameter wins
  87. m2 = new MediaType("*", "*");
  88. _testMostSpecific(m1, m2, m1);
  89. _testMostSpecific(m2, m1, m2);
  90. /*** wildcard subtype ***/
  91. m1 = new MediaType("moo", "*");
  92. // wildcard subtype #1 - concrete type wins
  93. m2 = new MediaType("foo", "bar");
  94. _testMostSpecific(m1, m2, m2);
  95. _testMostSpecific(m2, m1, m2);
  96. // wildcard subtype #2 - first parameter in method wins
  97. m2 = new MediaType("foo", "*");
  98. _testMostSpecific(m1, m2, m1);
  99. _testMostSpecific(m2, m1, m2);
  100. /*** concrete types ***/
  101. // concrete types - first parameter in method wins
  102. m1 = new MediaType("moo", "boo");
  103. m2 = new MediaType("foo", "bar");
  104. _testMostSpecific(m1, m2, m1);
  105. _testMostSpecific(m2, m1, m2);
  106. /*** concrete type with parameters ***/
  107. m1 = new MediaType("foo", "bar", asMap("p1=v1;p2=v2"));
  108. // concrete type with parameters #1 - wildcard type looses
  109. m2 = MediaType.WILDCARD_TYPE;
  110. _testMostSpecific(m1, m2, m1);
  111. _testMostSpecific(m2, m1, m1);
  112. // concrete type with parameters #2 - wildcard subtype looses
  113. m2 = new MediaType("foo", "*");
  114. _testMostSpecific(m1, m2, m1);
  115. _testMostSpecific(m2, m1, m1);
  116. // concrete type with parameters #3 - concrete parameter-less type looses
  117. m2 = new MediaType("foo", "baz");
  118. _testMostSpecific(m1, m2, m1);
  119. _testMostSpecific(m2, m1, m1);
  120. // concrete type with parameters #4 - type with less parameters type looses
  121. m2 = new MediaType("foo", "baz", asMap("a1=b1"));
  122. _testMostSpecific(m1, m2, m1);
  123. _testMostSpecific(m2, m1, m1);
  124. // both concrete types with parameters #5 - first parameter in method wins
  125. m2 = new MediaType("foo", "baz", asMap("a1=b1;a2=b2"));
  126. _testMostSpecific(m1, m2, m1);
  127. _testMostSpecific(m2, m1, m2);
  128. }
  129. private static void _testMostSpecific(MediaType m1, MediaType m2, MediaType result) {
  130. assertThat("Unexpected media type selected to be most specific.",
  131. MediaTypes.mostSpecific(m1, m2), is(result));
  132. }
  133. /**
  134. * Creates a map from HTTP header parameter strings.
  135. *
  136. * @param parameters HTTP header parameters string.
  137. * @return HTTP header parameters map.
  138. */
  139. public static Map<String, String> asMap(String parameters) {
  140. HttpHeaderReader reader = HttpHeaderReader.newInstance(";" + parameters);
  141. if (reader.hasNext()) {
  142. try {
  143. return HttpHeaderReader.readParameters(reader);
  144. } catch (ParseException e) {
  145. throw new RuntimeException(e);
  146. }
  147. }
  148. return Collections.emptyMap();
  149. }
  150. }