24
24
import static org .junit .Assert .assertNull ;
25
25
import static org .junit .Assert .assertSame ;
26
26
import static org .junit .Assert .assertTrue ;
27
+ import static org .junit .Assert .fail ;
27
28
28
29
import java .lang .reflect .Method ;
29
30
import java .util .Arrays ;
34
35
import org .springframework .mock .web .MockHttpServletRequest ;
35
36
import org .springframework .ui .Model ;
36
37
import org .springframework .validation .BindingResult ;
38
+ import org .springframework .web .HttpSessionRequiredException ;
37
39
import org .springframework .web .bind .WebDataBinder ;
38
40
import org .springframework .web .bind .annotation .ModelAttribute ;
39
41
import org .springframework .web .bind .annotation .SessionAttributes ;
@@ -59,6 +61,8 @@ public class ModelFactoryTests {
59
61
60
62
private InvocableHandlerMethod handleMethod ;
61
63
64
+ private InvocableHandlerMethod handleSessionAttrMethod ;
65
+
62
66
private SessionAttributesHandler handlerSessionAttributeStore ;
63
67
64
68
private SessionAttributeStore sessionAttributeStore ;
@@ -69,67 +73,77 @@ public class ModelFactoryTests {
69
73
70
74
@ Before
71
75
public void setUp () throws Exception {
72
- handleMethod = new InvocableHandlerMethod (handler , handler .getClass ().getDeclaredMethod ("handle" ));
76
+ Class <?> handlerType = handler .getClass ();
77
+ handleMethod = new InvocableHandlerMethod (handler , handlerType .getDeclaredMethod ("handle" ));
78
+ Method method = handlerType .getDeclaredMethod ("handleSessionAttr" , String .class );
79
+ handleSessionAttrMethod = new InvocableHandlerMethod (handler , method );
73
80
sessionAttributeStore = new DefaultSessionAttributeStore ();
74
- handlerSessionAttributeStore = new SessionAttributesHandler (handler . getClass () , sessionAttributeStore );
81
+ handlerSessionAttributeStore = new SessionAttributesHandler (handlerType , sessionAttributeStore );
75
82
mavContainer = new ModelAndViewContainer ();
76
83
webRequest = new ServletWebRequest (new MockHttpServletRequest ());
77
84
}
78
85
79
86
@ Test
80
- public void createModel () throws Exception {
81
- ModelFactory modelFactory = createModelFactory ("model " , Model .class );
87
+ public void addAttributeToModel () throws Exception {
88
+ ModelFactory modelFactory = createModelFactory ("modelAttr " , Model .class );
82
89
modelFactory .initModel (webRequest , mavContainer , handleMethod );
83
90
84
- assertEquals (Boolean .TRUE , mavContainer .getAttribute ("model " ));
91
+ assertEquals (Boolean .TRUE , mavContainer .getAttribute ("modelAttr " ));
85
92
}
86
93
87
94
@ Test
88
- public void createModelWithName () throws Exception {
89
- ModelFactory modelFactory = createModelFactory ("modelWithName " );
95
+ public void returnAttributeWithName () throws Exception {
96
+ ModelFactory modelFactory = createModelFactory ("modelAttrWithName " );
90
97
modelFactory .initModel (webRequest , mavContainer , handleMethod );
91
98
92
99
assertEquals (Boolean .TRUE , mavContainer .getAttribute ("name" ));
93
100
}
94
101
95
102
@ Test
96
- public void createModelWithDefaultName () throws Exception {
97
- ModelFactory modelFactory = createModelFactory ("modelWithDefaultName " );
103
+ public void returnAttributeWithNameByConvention () throws Exception {
104
+ ModelFactory modelFactory = createModelFactory ("modelAttrConvention " );
98
105
modelFactory .initModel (webRequest , mavContainer , handleMethod );
99
106
100
107
assertEquals (Boolean .TRUE , mavContainer .getAttribute ("boolean" ));
101
108
}
102
109
103
110
@ Test
104
- public void createModelWithExistingName () throws Exception {
105
- ModelFactory modelFactory = createModelFactory ("modelWithName" );
106
- modelFactory .initModel (webRequest , mavContainer , handleMethod );
107
-
108
- assertEquals (Boolean .TRUE , mavContainer .getAttribute ("name" ));
109
- }
110
-
111
- @ Test
112
- public void createModelWithNullAttribute () throws Exception {
113
- ModelFactory modelFactory = createModelFactory ("modelWithNullAttribute" );
111
+ public void returnNullAttributeValue () throws Exception {
112
+ ModelFactory modelFactory = createModelFactory ("nullModelAttr" );
114
113
modelFactory .initModel (webRequest , mavContainer , handleMethod );
115
114
116
115
assertTrue (mavContainer .containsAttribute ("name" ));
117
116
assertNull (mavContainer .getAttribute ("name" ));
118
117
}
119
118
120
119
@ Test
121
- public void createModelExistingSessionAttributes () throws Exception {
122
- sessionAttributeStore .storeAttribute (webRequest , "sessAttr " , "sessAttrValue " );
120
+ public void retrieveAttributeFromSession () throws Exception {
121
+ sessionAttributeStore .storeAttribute (webRequest , "sessionAttr " , "sessionAttrValue " );
123
122
124
123
// Resolve successfully handler session attribute once
125
- assertTrue (handlerSessionAttributeStore .isHandlerSessionAttribute ("sessAttr " , null ));
124
+ assertTrue (handlerSessionAttributeStore .isHandlerSessionAttribute ("sessionAttr " , null ));
126
125
127
- ModelFactory modelFactory = createModelFactory ("model " , Model .class );
126
+ ModelFactory modelFactory = createModelFactory ("modelAttr " , Model .class );
128
127
modelFactory .initModel (webRequest , mavContainer , handleMethod );
129
128
130
- assertEquals ("sessAttrValue " , mavContainer .getAttribute ("sessAttr " ));
129
+ assertEquals ("sessionAttrValue " , mavContainer .getAttribute ("sessionAttr " ));
131
130
}
132
-
131
+
132
+ @ Test
133
+ public void requiredSessionAttribute () throws Exception {
134
+ ModelFactory modelFactory = new ModelFactory (null , null , handlerSessionAttributeStore );
135
+
136
+ try {
137
+ modelFactory .initModel (webRequest , mavContainer , handleSessionAttrMethod );
138
+ fail ("Expected HttpSessionRequiredException" );
139
+ } catch (HttpSessionRequiredException e ) { }
140
+
141
+ sessionAttributeStore .storeAttribute (webRequest , "sessionAttr" , "sessionAttrValue" );
142
+ modelFactory .initModel (webRequest , mavContainer , handleSessionAttrMethod );
143
+
144
+ assertEquals ("sessionAttrValue" , mavContainer .getAttribute ("sessionAttr" ));
145
+ }
146
+
133
147
@ Test
134
148
public void updateBindingResult () throws Exception {
135
149
String attrName = "attr1" ;
@@ -170,35 +184,33 @@ private ModelFactory createModelFactory(String methodName, Class<?>... parameter
170
184
return new ModelFactory (Arrays .asList (handlerMethod ), null , handlerSessionAttributeStore );
171
185
}
172
186
173
- @ SessionAttributes ("sessAttr " )
187
+ @ SessionAttributes ("sessionAttr" ) @ SuppressWarnings ( "unused " )
174
188
private static class ModelHandler {
175
189
176
- @ SuppressWarnings ("unused" )
177
190
@ ModelAttribute
178
- public void model (Model model ) {
179
- model .addAttribute ("model " , Boolean .TRUE );
191
+ public void modelAttr (Model model ) {
192
+ model .addAttribute ("modelAttr " , Boolean .TRUE );
180
193
}
181
194
182
- @ SuppressWarnings ("unused" )
183
195
@ ModelAttribute ("name" )
184
- public Boolean modelWithName () {
196
+ public Boolean modelAttrWithName () {
185
197
return Boolean .TRUE ;
186
198
}
187
199
188
- @ SuppressWarnings ("unused" )
189
200
@ ModelAttribute
190
- public Boolean modelWithDefaultName () {
201
+ public Boolean modelAttrConvention () {
191
202
return Boolean .TRUE ;
192
203
}
193
204
194
- @ SuppressWarnings ("unused" )
195
205
@ ModelAttribute ("name" )
196
- public Boolean modelWithNullAttribute () {
206
+ public Boolean nullModelAttr () {
197
207
return null ;
198
208
}
199
209
200
- @ SuppressWarnings ("unused" )
201
210
public void handle () {
202
211
}
212
+
213
+ public void handleSessionAttr (@ ModelAttribute ("sessionAttr" ) String sessionAttr ) {
214
+ }
203
215
}
204
216
}
0 commit comments