|
21 | 21 |
|
22 | 22 | import java.io.IOException; |
23 | 23 | import java.io.InputStream; |
| 24 | +import java.io.OutputStream; |
24 | 25 | import java.lang.annotation.Annotation; |
25 | 26 | import java.lang.reflect.Field; |
26 | 27 | import java.lang.reflect.Type; |
27 | 28 | import java.util.HashMap; |
28 | 29 | import java.util.Set; |
29 | 30 |
|
| 31 | +import javax.ws.rs.Consumes; |
30 | 32 | import javax.ws.rs.Produces; |
| 33 | +import javax.ws.rs.WebApplicationException; |
31 | 34 | import javax.ws.rs.core.MediaType; |
32 | 35 | import javax.ws.rs.core.MultivaluedMap; |
33 | 36 | import javax.ws.rs.ext.MessageBodyReader; |
| 37 | +import javax.ws.rs.ext.MessageBodyWriter; |
34 | 38 | import javax.ws.rs.ext.Provider; |
35 | 39 |
|
36 | 40 | import junit.framework.TestCase; |
@@ -110,6 +114,86 @@ public void testOverrideSystemProvider() throws Exception { |
110 | 114 | // make there is only one provider in the list to conform to JAX-RS 4.1 first sentence |
111 | 115 | assertEquals(1, readers.size()); |
112 | 116 | } |
| 117 | + |
| 118 | + /** |
| 119 | + * Tests that a structured syntax suffix is handled correctly for determining a writer. |
| 120 | + * |
| 121 | + * @throws Exception |
| 122 | + */ |
| 123 | + public void testProvidesStructuredSyntaxSuffixHandledOk() throws Exception { |
| 124 | + ProvidersRegistry providersRegistry = |
| 125 | + new ProvidersRegistry(new LifecycleManagersRegistry(), new ApplicationValidator()); |
| 126 | + providersRegistry.addProvider(GenericProvider.class); |
| 127 | + providersRegistry.addProvider(SpecificProvider.class); |
| 128 | + |
| 129 | + MediaType mediaType; |
| 130 | + MessageBodyWriter<String> writer; |
| 131 | + |
| 132 | + mediaType = MediaType.valueOf("application/json"); // use generic provider |
| 133 | + |
| 134 | + writer = providersRegistry.getMessageBodyWriter(String.class, null, null, mediaType, null); |
| 135 | + assertTrue(writer instanceof GenericProvider); |
| 136 | + |
| 137 | + mediaType = MediaType.valueOf("application/vnd.other+json"); // use generic provider |
| 138 | + |
| 139 | + writer = providersRegistry.getMessageBodyWriter(String.class, null, null, mediaType, null); |
| 140 | + assertTrue(writer instanceof GenericProvider); |
| 141 | + |
| 142 | + mediaType = MediaType.valueOf("application/subschema+json"); // use specific provider |
| 143 | + |
| 144 | + writer = providersRegistry.getMessageBodyWriter(String.class, null, null, mediaType, null); |
| 145 | + assertTrue(writer instanceof SpecificProvider); |
| 146 | + |
| 147 | + mediaType = MediaType.valueOf("application/subschema"); // cannot use specific provider, nor generic |
| 148 | + |
| 149 | + writer = providersRegistry.getMessageBodyWriter(String.class, null, null, mediaType, null); |
| 150 | + assertNull(writer); |
| 151 | + |
| 152 | + mediaType = MediaType.valueOf("text/json"); // cannot use specific provider, nor generic |
| 153 | + |
| 154 | + writer = providersRegistry.getMessageBodyWriter(String.class, null, null, mediaType, null); |
| 155 | + assertNull(writer); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Tests that a structured syntax suffix is handled correctly for determining a reader. |
| 160 | + * |
| 161 | + * @throws Exception |
| 162 | + */ |
| 163 | + public void testConsumesStructuredSyntaxSuffixHandledOk() throws Exception { |
| 164 | + ProvidersRegistry providersRegistry = |
| 165 | + new ProvidersRegistry(new LifecycleManagersRegistry(), new ApplicationValidator()); |
| 166 | + providersRegistry.addProvider(GenericProvider.class); |
| 167 | + providersRegistry.addProvider(SpecificProvider.class); |
| 168 | + |
| 169 | + MediaType mediaType; |
| 170 | + MessageBodyReader<String> reader; |
| 171 | + |
| 172 | + mediaType = MediaType.valueOf("application/json"); // use generic provider |
| 173 | + |
| 174 | + reader = providersRegistry.getMessageBodyReader(String.class, null, null, mediaType, null); |
| 175 | + assertTrue(reader instanceof GenericProvider); |
| 176 | + |
| 177 | + mediaType = MediaType.valueOf("application/vnd.other+json"); // use generic provider |
| 178 | + |
| 179 | + reader = providersRegistry.getMessageBodyReader(String.class, null, null, mediaType, null); |
| 180 | + assertTrue(reader instanceof GenericProvider); |
| 181 | + |
| 182 | + mediaType = MediaType.valueOf("application/subschema+json"); // use specific provider |
| 183 | + |
| 184 | + reader = providersRegistry.getMessageBodyReader(String.class, null, null, mediaType, null); |
| 185 | + assertTrue(reader instanceof SpecificProvider); |
| 186 | + |
| 187 | + mediaType = MediaType.valueOf("application/subschema"); // cannot use specific provider, nor generic |
| 188 | + |
| 189 | + reader = providersRegistry.getMessageBodyReader(String.class, null, null, mediaType, null); |
| 190 | + assertNull(reader); |
| 191 | + |
| 192 | + mediaType = MediaType.valueOf("text/json"); // cannot use specific provider, nor generic |
| 193 | + |
| 194 | + reader = providersRegistry.getMessageBodyReader(String.class, null, null, mediaType, null); |
| 195 | + assertNull(reader); |
| 196 | + } |
113 | 197 |
|
114 | 198 | // TODO: perhaps future tests should be added to actually exercise the providersCache code, but it would be an involved, |
115 | 199 | // multi-threaded test that dynamically adds providers at just the right time to ensure no problems with |
@@ -140,4 +224,69 @@ public String readFrom(Class<String> type, Type genericType, |
140 | 224 | } |
141 | 225 | } |
142 | 226 |
|
| 227 | + @Provider |
| 228 | + @Produces({"application/subschema+json"}) |
| 229 | + @Consumes({"application/subschema+json"}) |
| 230 | + public static class SpecificProvider implements MessageBodyReader<String>, MessageBodyWriter<String> { |
| 231 | + public boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) |
| 232 | + { |
| 233 | + return String.class.isAssignableFrom(type); |
| 234 | + } |
| 235 | + |
| 236 | + public long getSize( String t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) |
| 237 | + { |
| 238 | + return -1L; |
| 239 | + } |
| 240 | + |
| 241 | + public void writeTo( String t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
| 242 | + MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream ) throws IOException, |
| 243 | + WebApplicationException |
| 244 | + { |
| 245 | + } |
| 246 | + |
| 247 | + public boolean isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) |
| 248 | + { |
| 249 | + return String.class.isAssignableFrom(type); |
| 250 | + } |
| 251 | + |
| 252 | + public String readFrom( Class<String> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
| 253 | + MultivaluedMap<String, String> httpHeaders, InputStream entityStream ) throws IOException, |
| 254 | + WebApplicationException |
| 255 | + { |
| 256 | + return null; |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + @Provider |
| 261 | + @Produces({"application/json"}) |
| 262 | + @Consumes({"application/json"}) |
| 263 | + public static class GenericProvider implements MessageBodyReader<String>, MessageBodyWriter<String> { |
| 264 | + public boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) |
| 265 | + { |
| 266 | + return String.class.isAssignableFrom(type); |
| 267 | + } |
| 268 | + |
| 269 | + public long getSize( String t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) |
| 270 | + { |
| 271 | + return -1L; |
| 272 | + } |
| 273 | + |
| 274 | + public void writeTo( String t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
| 275 | + MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream ) throws IOException, |
| 276 | + WebApplicationException |
| 277 | + { |
| 278 | + } |
| 279 | + |
| 280 | + public boolean isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) |
| 281 | + { |
| 282 | + return String.class.isAssignableFrom(type); |
| 283 | + } |
| 284 | + |
| 285 | + public String readFrom( Class<String> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
| 286 | + MultivaluedMap<String, String> httpHeaders, InputStream entityStream ) throws IOException, |
| 287 | + WebApplicationException |
| 288 | + { |
| 289 | + return null; |
| 290 | + } |
| 291 | + } |
143 | 292 | } |
0 commit comments