PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet.test/org/restlet/test/jaxrs/util/UtilTests.java

https://bitbucket.org/haris_peco/debrief
Java | 209 lines | 129 code | 30 blank | 50 comment | 6 complexity | c8d45ec24eb81d38a981e4a8d973d380 MD5 | raw file
  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.test.jaxrs.util;
  31. import javax.ws.rs.core.HttpHeaders;
  32. import javax.ws.rs.core.MultivaluedMap;
  33. import junit.framework.TestCase;
  34. import org.restlet.data.CharacterSet;
  35. import org.restlet.data.MediaType;
  36. import org.restlet.ext.jaxrs.internal.core.MultivaluedMapImpl;
  37. import org.restlet.ext.jaxrs.internal.exceptions.IllegalPathException;
  38. import org.restlet.ext.jaxrs.internal.util.Converter;
  39. import org.restlet.ext.jaxrs.internal.util.PathRegExp;
  40. import org.restlet.ext.jaxrs.internal.util.Util;
  41. /**
  42. * @author Stephan Koops
  43. * @see PathRegExp
  44. */
  45. @SuppressWarnings("all")
  46. public class UtilTests extends TestCase {
  47. /** test interface for test of {@link Util#doesImplement(Class, Class)}. */
  48. private static class C1 {
  49. }
  50. /** test class for test of {@link Util#doesImplement(Class, Class)}. */
  51. private static class C2 extends C1 implements I1 {
  52. }
  53. /** test class for test of {@link Util#doesImplement(Class, Class)}. */
  54. private static class C3 implements I1 {
  55. }
  56. /** test class for test of {@link Util#doesImplement(Class, Class)}. */
  57. private static class C4 extends C3 {
  58. }
  59. /** test class for test of {@link Util#doesImplement(Class, Class)}. */
  60. private static class C5 extends C3 implements I2 {
  61. }
  62. /** test interface for test of {@link Util#doesImplement(Class, Class)}. */
  63. private static interface I1 {
  64. }
  65. /** test interface for test of {@link Util#doesImplement(Class, Class)}. */
  66. private static interface I2 extends I1 {
  67. }
  68. private MultivaluedMap<String, Object> httpHeaders;
  69. private void checkPathTemplateWithoutRegExp(String expectedOut, String in)
  70. throws IllegalPathException {
  71. assertEquals(expectedOut, Util.getPathTemplateWithoutRegExps(in, null));
  72. }
  73. private void checkPathTemplateWithoutRegExpIllegal(String in) {
  74. try {
  75. Util.getPathTemplateWithoutRegExps(in, null);
  76. fail("\"" + in + "\" must not be allowed");
  77. } catch (IllegalPathException e) {
  78. // wonderful
  79. }
  80. }
  81. /**
  82. * @return the {@link CharacterSet} as String
  83. */
  84. private String getCss() {
  85. return Util.getCharsetName(this.httpHeaders, null);
  86. }
  87. /**
  88. * @return the {@link MediaType} without any parameter
  89. */
  90. private MediaType getMt() {
  91. final MediaType mediaType = Util.getMediaType(this.httpHeaders);
  92. if (mediaType == null) {
  93. return null;
  94. }
  95. return Converter.getMediaTypeWithoutParams(mediaType);
  96. }
  97. /**
  98. * @return the {@link MediaType} without any parameter as String
  99. */
  100. private String getMts() {
  101. final MediaType mediaType = getMt();
  102. if (mediaType == null) {
  103. return null;
  104. }
  105. return mediaType.toString();
  106. }
  107. private void setContentType(MediaType mediaType, CharacterSet characterSet) {
  108. if (characterSet != null) {
  109. mediaType.getParameters().add("charset", characterSet.getName());
  110. }
  111. setContentType(mediaType.toString());
  112. }
  113. private void setContentType(String contentType) {
  114. this.httpHeaders.add(HttpHeaders.CONTENT_TYPE, contentType);
  115. }
  116. public void setUp() {
  117. this.httpHeaders = new MultivaluedMapImpl<String, Object>();
  118. }
  119. @Override
  120. protected void tearDown() throws Exception {
  121. this.httpHeaders = null;
  122. super.tearDown();
  123. }
  124. public void testDoesImplements() {
  125. assertTrue(Util.doesImplement(String.class, CharSequence.class));
  126. assertFalse(Util.doesImplement(CharSequence.class, String.class));
  127. assertFalse(Util.doesImplement(Object.class, CharSequence.class));
  128. assertTrue(Util.doesImplement(Integer.class, Comparable.class));
  129. assertFalse(Util.doesImplement(C1.class, I1.class));
  130. assertFalse(Util.doesImplement(C1.class, I2.class));
  131. assertTrue(Util.doesImplement(C2.class, I1.class));
  132. assertFalse(Util.doesImplement(C2.class, I2.class));
  133. assertTrue(Util.doesImplement(C3.class, I1.class));
  134. assertFalse(Util.doesImplement(C3.class, I2.class));
  135. assertTrue(Util.doesImplement(C4.class, I1.class));
  136. assertFalse(Util.doesImplement(C4.class, I2.class));
  137. assertTrue(Util.doesImplement(C5.class, I1.class));
  138. assertTrue(Util.doesImplement(C5.class, I2.class));
  139. }
  140. public void testGetOfContentType0() {
  141. assertEquals(null, getCss());
  142. assertEquals(null, getMts());
  143. }
  144. public void testGetOfContentType1() {
  145. setContentType("a/b;charset=CS");
  146. assertEquals("CS", getCss());
  147. assertEquals("a/b", getMts());
  148. }
  149. public void testGetOfContentType2() {
  150. setContentType(MediaType.TEXT_HTML, null);
  151. assertEquals(null, getCss());
  152. assertEquals(MediaType.TEXT_HTML, getMt());
  153. }
  154. public void testGetOfContentType3() {
  155. setContentType("a/b ;charset=CS");
  156. assertEquals("CS", getCss());
  157. assertEquals("a/b", getMts());
  158. }
  159. public void testGetOfContentType4() {
  160. setContentType("a/b;d=g;charset=CS");
  161. assertEquals("CS", getCss());
  162. assertEquals("a/b", getMts());
  163. }
  164. public void testGetPathTemplateWithoutRegExp() throws IllegalPathException {
  165. checkPathTemplateWithoutRegExp("abc", "abc");
  166. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{de}fg");
  167. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{de:sd}fg");
  168. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{ de}fg");
  169. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{ de }fg");
  170. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{de }fg");
  171. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{de :}fg");
  172. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{de : }fg");
  173. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{de : yx}fg");
  174. checkPathTemplateWithoutRegExp("abc{de}fg", "abc{de : yx }fg");
  175. checkPathTemplateWithoutRegExpIllegal("abc{}hjk");
  176. checkPathTemplateWithoutRegExpIllegal("abc{:}hjk");
  177. checkPathTemplateWithoutRegExpIllegal("abc{:sdf}hjk");
  178. }
  179. }