1
1
/*
2
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
3
*
4
- * Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
4
+ * Copyright (c) 2012-2016 Oracle and/or its affiliates. All rights reserved.
5
5
*
6
6
* The contents of this file are subject to the terms of either the GNU
7
7
* General Public License Version 2 only ("GPL") or the Common Development
54
54
import javax .ws .rs .Path ;
55
55
import javax .ws .rs .PathParam ;
56
56
import javax .ws .rs .QueryParam ;
57
+ import javax .ws .rs .container .ContainerResponseContext ;
57
58
import javax .ws .rs .core .Cookie ;
58
59
import javax .ws .rs .core .Form ;
59
60
import javax .ws .rs .core .MediaType ;
64
65
import org .glassfish .jersey .server .ContainerResponse ;
65
66
import org .glassfish .jersey .server .ParamException ;
66
67
import org .glassfish .jersey .server .RequestContextBuilder ;
67
-
68
68
import org .junit .Test ;
69
69
import static org .junit .Assert .assertEquals ;
70
+ import static org .junit .Assert .assertTrue ;
70
71
71
72
/**
72
73
* Not sure whether this is relevant anymore.
@@ -81,8 +82,14 @@ public abstract static class BaseExceptionMapper<T extends ParamException> imple
81
82
82
83
public Response toResponse (T exception , String entity ) {
83
84
assertEquals ("x" , exception .getParameterName ());
84
- if (exception .getParameterType () != PathParam .class ) {
85
+
86
+ // path param and form param can be integers in this test, thus different default value
87
+ if (!exception .getParameterType ().equals (PathParam .class )
88
+ && !exception .getParameterType ().equals (FormParam .class )) {
89
+
85
90
assertEquals ("default" , exception .getDefaultStringValue ());
91
+ } else {
92
+ assertTrue (exception .getDefaultStringValue ().equals ("default" ) || exception .getDefaultStringValue ().equals ("1" ));
86
93
}
87
94
return Response .fromResponse (exception .getResponse ()).entity (entity ).build ();
88
95
}
@@ -124,7 +131,7 @@ public Response toResponse(ParamException.QueryParamException exception) {
124
131
}
125
132
126
133
public static class
127
- CookieExceptionMapper extends BaseExceptionMapper <ParamException .CookieParamException > {
134
+ CookieExceptionMapper extends BaseExceptionMapper <ParamException .CookieParamException > {
128
135
129
136
public Response toResponse (ParamException .CookieParamException exception ) {
130
137
return toResponse (exception , "cookie" );
@@ -150,7 +157,7 @@ public static class ParamExceptionMapperResource {
150
157
151
158
@ Path ("path/{x}" )
152
159
@ GET
153
- public String getPath (@ PathParam ("x" ) URI x ) {
160
+ public String getPath (@ DefaultValue ( "1" ) @ PathParam ("x" ) int x ) {
154
161
return "" ;
155
162
}
156
163
@@ -184,19 +191,26 @@ public String getHeader(@DefaultValue("default") @HeaderParam("x") URI x) {
184
191
public String postForm (@ DefaultValue ("default" ) @ FormParam ("x" ) URI x ) {
185
192
return "" ;
186
193
}
194
+
195
+ @ Path ("form-int" )
196
+ @ POST
197
+ @ Consumes (MediaType .APPLICATION_FORM_URLENCODED )
198
+ public String postForm (@ DefaultValue ("1" ) @ FormParam ("x" ) int x ) {
199
+ return "" ;
200
+ }
187
201
}
188
202
189
203
@ Test
190
204
public void testParamException () throws ExecutionException , InterruptedException {
191
205
initiateWebApplication (ParamExceptionMapperResource .class ,
192
- PathExceptionMapper .class ,
193
- MatrixExceptionMapper .class ,
194
- QueryExceptionMapper .class ,
195
- CookieExceptionMapper .class ,
196
- HeaderExceptionMapper .class ,
197
- FormExceptionMapper .class );
198
-
199
- ContainerResponse responseContext = getResponseContext (UriBuilder .fromPath ("/" ).path ("path/ 123 " ).build ().toString ());
206
+ PathExceptionMapper .class ,
207
+ MatrixExceptionMapper .class ,
208
+ QueryExceptionMapper .class ,
209
+ CookieExceptionMapper .class ,
210
+ HeaderExceptionMapper .class ,
211
+ FormExceptionMapper .class );
212
+
213
+ ContainerResponse responseContext = getResponseContext (UriBuilder .fromPath ("/" ).path ("path/ test " ).build ().toString ());
200
214
assertEquals ("path" , responseContext .getEntity ());
201
215
202
216
responseContext = getResponseContext (UriBuilder .fromPath ("/" ).path ("matrix;x= 123" ).build ().toString ());
@@ -226,6 +240,23 @@ public void testParamException() throws ExecutionException, InterruptedException
226
240
assertEquals ("form" , responseContext .getEntity ());
227
241
}
228
242
243
+ @ Test
244
+ public void testFormParamPrimitiveValidation () throws ExecutionException , InterruptedException {
245
+ initiateWebApplication (ParamExceptionMapperResource .class ,
246
+ FormExceptionMapper .class );
247
+
248
+ Form f = new Form ();
249
+ f .param ("x" , "http://oracle.com" );
250
+ ContainerResponseContext responseContext = apply (
251
+ RequestContextBuilder .from ("/form-int" , "POST" )
252
+ .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
253
+ .entity (f )
254
+ .build ()
255
+ );
256
+
257
+ assertEquals ("form" , responseContext .getEntity ());
258
+ }
259
+
229
260
@ Test
230
261
public void testGeneralParamException () throws ExecutionException , InterruptedException {
231
262
initiateWebApplication (ParamExceptionMapperResource .class ,
0 commit comments