1616
1717package org .springframework .messaging .handler .annotation .support ;
1818
19+ import static org .junit .Assert .*;
20+
1921import java .lang .reflect .Method ;
22+ import java .util .Locale ;
2023
2124import org .junit .Before ;
25+ import org .junit .Rule ;
2226import org .junit .Test ;
27+ import org .junit .rules .ExpectedException ;
28+
2329import org .springframework .core .LocalVariableTableParameterNameDiscoverer ;
2430import org .springframework .core .MethodParameter ;
2531import org .springframework .messaging .Message ;
32+ import org .springframework .messaging .converter .StringMessageConverter ;
2633import org .springframework .messaging .handler .annotation .Payload ;
2734import org .springframework .messaging .support .MessageBuilder ;
28- import org .springframework .messaging .converter .StringMessageConverter ;
29- import org .springframework .util .StringUtils ;
35+ import org .springframework .util .Assert ;
3036import org .springframework .validation .Errors ;
3137import org .springframework .validation .Validator ;
3238import org .springframework .validation .annotation .Validated ;
3339
34- import static org .junit .Assert .*;
35-
3640/**
3741 * Test fixture for {@link PayloadArgumentResolver}.
3842 *
3943 * @author Rossen Stoyanchev
4044 * @author Brian Clozel
45+ * @author Stephane Nicoll
4146 */
4247public class PayloadArgumentResolverTests {
4348
49+ @ Rule
50+ public final ExpectedException thrown = ExpectedException .none ();
51+
4452 private PayloadArgumentResolver resolver ;
4553
46- private MethodParameter param ;
47- private MethodParameter paramNotRequired ;
48- private MethodParameter paramWithSpelExpression ;
54+ private Method payloadMethod ;
55+ private Method simpleMethod ;
4956 private MethodParameter paramValidated ;
5057
51-
5258 @ Before
5359 public void setup () throws Exception {
54-
5560 this .resolver = new PayloadArgumentResolver (new StringMessageConverter (), testValidator ());
5661
57- Method method = PayloadArgumentResolverTests .class .getDeclaredMethod ("handleMessage" ,
58- String .class , String .class , String .class , String .class );
62+ payloadMethod = PayloadArgumentResolverTests .class .getDeclaredMethod ("handleMessage" ,
63+ String .class , String .class , Locale .class , String .class , String .class );
64+ simpleMethod = PayloadArgumentResolverTests .class .getDeclaredMethod ("handleAnotherMessage" ,
65+ String .class , String .class );
5966
60- this .param = new MethodParameter (method , 0 );
61- this .paramNotRequired = new MethodParameter (method , 1 );
62- this .paramWithSpelExpression = new MethodParameter (method , 2 );
63- this .paramValidated = new MethodParameter (method , 3 );
67+ this .paramValidated = getMethodParameter (payloadMethod , 4 );
6468 this .paramValidated .initParameterNameDiscovery (new LocalVariableTableParameterNameDiscoverer ());
6569 }
6670
6771
6872 @ Test
6973 public void resolveRequired () throws Exception {
7074 Message <?> message = MessageBuilder .withPayload ("ABC" .getBytes ()).build ();
71- Object actual = this .resolver .resolveArgument (this . param , message );
75+ Object actual = this .resolver .resolveArgument (getMethodParameter ( payloadMethod , 0 ) , message );
7276
7377 assertEquals ("ABC" , actual );
7478 }
7579
80+ @ Test
81+ public void resolveRequiredEmpty () throws Exception {
82+ Message <?> message = MessageBuilder .withPayload ("" ).build ();
83+
84+ thrown .expect (MethodArgumentNotValidException .class ); // Required but empty
85+ this .resolver .resolveArgument (getMethodParameter (payloadMethod , 0 ), message );
86+ }
87+
7688 @ Test
7789 public void resolveNotRequired () throws Exception {
90+ MethodParameter paramNotRequired = getMethodParameter (payloadMethod , 1 );
7891
7992 Message <?> emptyByteArrayMessage = MessageBuilder .withPayload (new byte [0 ]).build ();
80- assertNull (this .resolver .resolveArgument (this .paramNotRequired , emptyByteArrayMessage ));
93+ assertNull (this .resolver .resolveArgument (paramNotRequired , emptyByteArrayMessage ));
94+
95+ Message <?> emptyStringMessage = MessageBuilder .withPayload ("" ).build ();
96+ assertNull (this .resolver .resolveArgument (paramNotRequired , emptyStringMessage ));
8197
8298 Message <?> notEmptyMessage = MessageBuilder .withPayload ("ABC" .getBytes ()).build ();
83- assertEquals ("ABC" , this .resolver .resolveArgument (this . paramNotRequired , notEmptyMessage ));
99+ assertEquals ("ABC" , this .resolver .resolveArgument (paramNotRequired , notEmptyMessage ));
84100 }
85101
86- @ Test (expected =IllegalStateException .class )
102+ @ Test
103+ public void resolveNonConvertibleParam () throws Exception {
104+ Message <?> notEmptyMessage = MessageBuilder .withPayload (123 ).build ();
105+
106+ // Could not convert from int to Locale so will be "empty" after conversion
107+ thrown .expect (MethodArgumentNotValidException .class );
108+ thrown .expectMessage (Locale .class .getName ()); // reference to the type that could not be converted
109+ this .resolver .resolveArgument (getMethodParameter (payloadMethod , 2 ), notEmptyMessage );
110+ }
111+
112+ @ Test
87113 public void resolveSpelExpressionNotSupported () throws Exception {
88114 Message <?> message = MessageBuilder .withPayload ("ABC" .getBytes ()).build ();
89- this .resolver .resolveArgument (this .paramWithSpelExpression , message );
115+
116+ thrown .expect (IllegalStateException .class );
117+ this .resolver .resolveArgument (getMethodParameter (payloadMethod , 3 ), message );
90118 }
91119
92120 @ Test
@@ -95,12 +123,46 @@ public void resolveValidation() throws Exception {
95123 this .resolver .resolveArgument (this .paramValidated , message );
96124 }
97125
98- @ Test ( expected = MethodArgumentNotValidException . class )
126+ @ Test
99127 public void resolveFailValidation () throws Exception {
100- Message <?> message = MessageBuilder .withPayload ("" .getBytes ()).build ();
128+ // See testValidator()
129+ Message <?> message = MessageBuilder .withPayload ("invalidValue" .getBytes ()).build ();
130+
131+ thrown .expect (MethodArgumentNotValidException .class );
101132 this .resolver .resolveArgument (this .paramValidated , message );
102133 }
103134
135+ @ Test
136+ public void resolveFailValidationNoConversionNecessary () throws Exception {
137+ Message <?> message = MessageBuilder .withPayload ("invalidValue" ).build ();
138+
139+ thrown .expect (MethodArgumentNotValidException .class );
140+ this .resolver .resolveArgument (this .paramValidated , message );
141+ }
142+
143+ @ Test
144+ public void resolveNonAnnotatedParameter () throws Exception {
145+ MethodParameter paramNotRequired = getMethodParameter (simpleMethod , 0 );
146+
147+ Message <?> emptyByteArrayMessage = MessageBuilder .withPayload (new byte [0 ]).build ();
148+ assertEquals ("" , this .resolver .resolveArgument (paramNotRequired , emptyByteArrayMessage ));
149+
150+ Message <?> emptyStringMessage = MessageBuilder .withPayload ("" ).build ();
151+ assertEquals ("" , this .resolver .resolveArgument (paramNotRequired , emptyStringMessage ));
152+
153+
154+ Message <?> notEmptyMessage = MessageBuilder .withPayload ("ABC" .getBytes ()).build ();
155+ assertEquals ("ABC" , this .resolver .resolveArgument (paramNotRequired , notEmptyMessage ));
156+ }
157+
158+ @ Test
159+ public void resolveNonAnnotatedParameterFailValidation () throws Exception {
160+ // See testValidator()
161+ Message <?> message = MessageBuilder .withPayload ("invalidValue" .getBytes ()).build ();
162+
163+ assertEquals ("invalidValue" , this .resolver .resolveArgument (getMethodParameter (simpleMethod , 1 ), message ));
164+ }
165+
104166 private Validator testValidator () {
105167
106168 return new Validator () {
@@ -111,19 +173,31 @@ public boolean supports(Class<?> clazz) {
111173 @ Override
112174 public void validate (Object target , Errors errors ) {
113175 String value = (String ) target ;
114- if (StringUtils . isEmpty (value . toString () )) {
115- errors .reject ("empty value" );
176+ if ("invalidValue" . equals (value )) {
177+ errors .reject ("invalid value" );
116178 }
117179 }
118180 };
119181 }
120182
183+ private MethodParameter getMethodParameter (Method method , int index ) {
184+ Assert .notNull (method , "Method must be set" );
185+ return new MethodParameter (method , index );
186+ }
187+
121188 @ SuppressWarnings ("unused" )
122189 private void handleMessage (
123190 @ Payload String param ,
124191 @ Payload (required =false ) String paramNotRequired ,
192+ @ Payload (required =true ) Locale nonConvertibleRequiredParam ,
125193 @ Payload ("foo.bar" ) String paramWithSpelExpression ,
126194 @ Validated @ Payload String validParam ) {
127195 }
128196
197+ @ SuppressWarnings ("unused" )
198+ private void handleAnotherMessage (
199+ String param ,
200+ @ Validated String validParam ) {
201+ }
202+
129203}
0 commit comments