1
1
/*
2
- * Copyright 2002-2008 the original author or authors.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
2
+ * Copyright 2002-2012 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
16
17
17
package org .springframework .web .portlet ;
18
18
24
24
import javax .portlet .PortletMode ;
25
25
import javax .portlet .PortletSecurityException ;
26
26
import javax .portlet .PortletSession ;
27
- import javax .portlet .UnavailableException ;
28
27
29
28
import junit .framework .TestCase ;
30
29
35
34
import org .springframework .context .i18n .LocaleContextHolder ;
36
35
import org .springframework .mock .web .portlet .MockActionRequest ;
37
36
import org .springframework .mock .web .portlet .MockActionResponse ;
37
+ import org .springframework .mock .web .portlet .MockEvent ;
38
+ import org .springframework .mock .web .portlet .MockEventRequest ;
39
+ import org .springframework .mock .web .portlet .MockEventResponse ;
38
40
import org .springframework .mock .web .portlet .MockPortletConfig ;
39
41
import org .springframework .mock .web .portlet .MockPortletContext ;
40
42
import org .springframework .mock .web .portlet .MockPortletSession ;
@@ -106,7 +108,7 @@ public void testDispatcherPortlets() {
106
108
(FrameworkPortlet .PORTLET_CONTEXT_PREFIX + "simple" ).equals (simpleDispatcherPortlet .getPortletContextAttributeName ()));
107
109
assertTrue ("Context published" ,
108
110
simpleDispatcherPortlet .getPortletApplicationContext () ==
109
- getPortletContext ().getAttribute (FrameworkPortlet .PORTLET_CONTEXT_PREFIX + "simple" ));
111
+ getPortletContext ().getAttribute (FrameworkPortlet .PORTLET_CONTEXT_PREFIX + "simple" ));
110
112
111
113
assertTrue ("Correct namespace" , "test" .equals (complexDispatcherPortlet .getNamespace ()));
112
114
assertTrue ("Correct attribute" ,
@@ -139,6 +141,56 @@ public void testSimpleInvalidActionRequest() throws Exception {
139
141
assertTrue (exceptionParam .startsWith (NoHandlerFoundException .class .getName ()));
140
142
}
141
143
144
+
145
+ public void testSimpleInvalidActionRequestWithoutHandling () throws Exception {
146
+ MockActionRequest request = new MockActionRequest ();
147
+ MockActionResponse response = new MockActionResponse ();
148
+ request .setParameter ("action" , "invalid" );
149
+ simpleDispatcherPortlet .setForwardActionException (false );
150
+ try {
151
+ simpleDispatcherPortlet .processAction (request , response );
152
+ fail ("Should have thrown a " + NoHandlerFoundException .class );
153
+ }
154
+ catch (NoHandlerFoundException ex ) {
155
+ // expected
156
+ }
157
+ }
158
+
159
+ public void testSimpleValidEventRequest () throws Exception {
160
+ MockEvent event = new MockEvent ("test-event" );
161
+ MockEventRequest request = new MockEventRequest (event );
162
+ MockEventResponse response = new MockEventResponse ();
163
+ request .setParameter ("action" , "form" );
164
+ simpleDispatcherPortlet .processEvent (request , response );
165
+ assertEquals ("test-event" , response .getRenderParameter ("event" ));
166
+ }
167
+
168
+ public void testSimpleInvalidEventRequest () throws Exception {
169
+ MockEvent event = new MockEvent ("test-event" );
170
+ MockEventRequest request = new MockEventRequest (event );
171
+ MockEventResponse response = new MockEventResponse ();
172
+ request .setParameter ("action" , "invalid" );
173
+ try {
174
+ simpleDispatcherPortlet .processEvent (request , response );
175
+ fail ("Should have thrown a " + NoHandlerFoundException .class );
176
+ }
177
+ catch (NoHandlerFoundException ex ) {
178
+ // expected
179
+ }
180
+ }
181
+
182
+ public void testSimpleInvalidEventRequestWithHandling () throws Exception {
183
+ MockEvent event = new MockEvent ("event" );
184
+ MockEventRequest request = new MockEventRequest (event );
185
+ MockEventResponse response = new MockEventResponse ();
186
+ request .setParameter ("action" , "invalid" );
187
+ simpleDispatcherPortlet .setForwardEventException (true );
188
+ simpleDispatcherPortlet .processEvent (request , response );
189
+ String exceptionParam = response .getRenderParameter (DispatcherPortlet .ACTION_EXCEPTION_RENDER_PARAMETER );
190
+ assertNotNull (exceptionParam );
191
+ assertTrue (exceptionParam .startsWith (NoHandlerFoundException .class .getName ()));
192
+ }
193
+
142
194
public void testSimpleFormViewNoBindOnNewForm () throws Exception {
143
195
MockRenderRequest request = new MockRenderRequest ();
144
196
MockRenderResponse response = new MockRenderResponse ();
@@ -202,7 +254,7 @@ public void testSimpleRequiredSessionFormWithoutSession() throws Exception {
202
254
fail ("Should have thrown PortletSessionRequiredException" );
203
255
}
204
256
catch (PortletSessionRequiredException ex ) {
205
- // expected
257
+ // expected
206
258
}
207
259
}
208
260
@@ -247,7 +299,7 @@ public void testSimpleInvalidRenderRequest() throws Exception {
247
299
fail ("Should have thrown UnavailableException" );
248
300
}
249
301
catch (NoHandlerFoundException ex ) {
250
- // expected
302
+ // expected
251
303
}
252
304
}
253
305
@@ -431,7 +483,7 @@ public void testExistingMultipartRequest() throws Exception {
431
483
request .setPortletMode (PortletMode .EDIT );
432
484
ComplexPortletApplicationContext .MockMultipartResolver multipartResolver =
433
485
(ComplexPortletApplicationContext .MockMultipartResolver )
434
- complexDispatcherPortlet .getPortletApplicationContext ().getBean ("portletMultipartResolver" );
486
+ complexDispatcherPortlet .getPortletApplicationContext ().getBean ("portletMultipartResolver" );
435
487
MultipartActionRequest multipartRequest = multipartResolver .resolveMultipart (request );
436
488
complexDispatcherPortlet .processAction (multipartRequest , response );
437
489
multipartResolver .cleanupMultipart (multipartRequest );
@@ -455,7 +507,7 @@ public void testActionRequestHandledEvent() throws Exception {
455
507
complexDispatcherPortlet .processAction (request , response );
456
508
ComplexPortletApplicationContext .TestApplicationListener listener =
457
509
(ComplexPortletApplicationContext .TestApplicationListener )
458
- complexDispatcherPortlet .getPortletApplicationContext ().getBean ("testListener" );
510
+ complexDispatcherPortlet .getPortletApplicationContext ().getBean ("testListener" );
459
511
assertEquals (1 , listener .counter );
460
512
}
461
513
@@ -465,7 +517,7 @@ public void testRenderRequestHandledEvent() throws Exception {
465
517
complexDispatcherPortlet .doDispatch (request , response );
466
518
ComplexPortletApplicationContext .TestApplicationListener listener =
467
519
(ComplexPortletApplicationContext .TestApplicationListener )
468
- complexDispatcherPortlet .getPortletApplicationContext ().getBean ("testListener" );
520
+ complexDispatcherPortlet .getPortletApplicationContext ().getBean ("testListener" );
469
521
assertEquals (1 , listener .counter );
470
522
}
471
523
@@ -477,7 +529,7 @@ public void testPublishEventsOff() throws Exception {
477
529
complexDispatcherPortlet .processAction (request , response );
478
530
ComplexPortletApplicationContext .TestApplicationListener listener =
479
531
(ComplexPortletApplicationContext .TestApplicationListener )
480
- complexDispatcherPortlet .getPortletApplicationContext ().getBean ("testListener" );
532
+ complexDispatcherPortlet .getPortletApplicationContext ().getBean ("testListener" );
481
533
assertEquals (0 , listener .counter );
482
534
}
483
535
@@ -611,7 +663,7 @@ public void testPortletHandlerAdapterActionRequest() throws Exception {
611
663
complexDispatcherPortlet .processAction (request , response );
612
664
assertEquals ("myPortlet action called" , response .getRenderParameter ("result" ));
613
665
ComplexPortletApplicationContext .MyPortlet myPortlet =
614
- (ComplexPortletApplicationContext .MyPortlet ) complexDispatcherPortlet .getPortletApplicationContext ().getBean ("myPortlet" );
666
+ (ComplexPortletApplicationContext .MyPortlet ) complexDispatcherPortlet .getPortletApplicationContext ().getBean ("myPortlet" );
615
667
assertEquals ("complex" , myPortlet .getPortletConfig ().getPortletName ());
616
668
assertEquals (getPortletContext (), myPortlet .getPortletConfig ().getPortletContext ());
617
669
assertEquals (complexDispatcherPortlet .getPortletContext (), myPortlet .getPortletConfig ().getPortletContext ());
@@ -626,12 +678,12 @@ public void testPortletHandlerAdapterRenderRequest() throws Exception {
626
678
complexDispatcherPortlet .doDispatch (request , response );
627
679
assertEquals ("myPortlet was here" , response .getContentAsString ());
628
680
ComplexPortletApplicationContext .MyPortlet myPortlet =
629
- (ComplexPortletApplicationContext .MyPortlet )
630
- complexDispatcherPortlet .getPortletApplicationContext ().getBean ("myPortlet" );
681
+ (ComplexPortletApplicationContext .MyPortlet )
682
+ complexDispatcherPortlet .getPortletApplicationContext ().getBean ("myPortlet" );
631
683
assertEquals ("complex" , myPortlet .getPortletConfig ().getPortletName ());
632
684
assertEquals (getPortletContext (), myPortlet .getPortletConfig ().getPortletContext ());
633
685
assertEquals (complexDispatcherPortlet .getPortletContext (),
634
- myPortlet .getPortletConfig ().getPortletContext ());
686
+ myPortlet .getPortletConfig ().getPortletContext ());
635
687
complexDispatcherPortlet .destroy ();
636
688
assertNull (myPortlet .getPortletConfig ());
637
689
}
@@ -769,18 +821,18 @@ public void testRuntimeExceptionInUnmappedHandler() throws Exception {
769
821
}
770
822
771
823
public void testGetMessage () {
772
- String message = complexDispatcherPortlet .getPortletApplicationContext ().getMessage ("test" , null , Locale .ENGLISH );
824
+ String message = complexDispatcherPortlet .getPortletApplicationContext ().getMessage ("test" , null , Locale .ENGLISH );
773
825
assertEquals ("test message" , message );
774
826
}
775
827
776
828
public void testGetMessageOtherLocale () {
777
- String message = complexDispatcherPortlet .getPortletApplicationContext ().getMessage ("test" , null , Locale .CANADA );
829
+ String message = complexDispatcherPortlet .getPortletApplicationContext ().getMessage ("test" , null , Locale .CANADA );
778
830
assertEquals ("Canadian & test message" , message );
779
831
}
780
832
781
833
public void testGetMessageWithArgs () {
782
834
Object [] args = new String [] {"this" , "that" };
783
- String message = complexDispatcherPortlet .getPortletApplicationContext ().getMessage ("test.args" , args , Locale .ENGLISH );
835
+ String message = complexDispatcherPortlet .getPortletApplicationContext ().getMessage ("test.args" , args , Locale .ENGLISH );
784
836
assertEquals ("test this and that" , message );
785
837
}
786
838
@@ -793,7 +845,7 @@ public void testPortletApplicationContextLookup() {
793
845
fail ("Should have thrown IllegalStateException" );
794
846
}
795
847
catch (IllegalStateException ex ) {
796
- // expected
848
+ // expected
797
849
}
798
850
portletContext .setAttribute (WebApplicationContext .ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE ,
799
851
new StaticWebApplicationContext ());
@@ -813,7 +865,7 @@ public void testValidActionRequestWithExistingThreadLocalRequestContext() throws
813
865
request .setParameter ("action" , "form" );
814
866
request .setParameter ("age" , "29" );
815
867
816
- // see RequestContextListener.requestInitialized()
868
+ // see RequestContextListener.requestInitialized()
817
869
try {
818
870
LocaleContextHolder .setLocale (request .getLocale ());
819
871
RequestContextHolder .setRequestAttributes (new PortletRequestAttributes (request ));
@@ -837,7 +889,7 @@ public void testValidRenderRequestWithExistingThreadLocalRequestContext() throws
837
889
MockRenderResponse response = new MockRenderResponse ();
838
890
request .addPreferredLocale (Locale .GERMAN );
839
891
840
- // see RequestContextListener.requestInitialized()
892
+ // see RequestContextListener.requestInitialized()
841
893
try {
842
894
LocaleContextHolder .setLocale (request .getLocale ());
843
895
RequestContextHolder .setRequestAttributes (new PortletRequestAttributes (request ));
@@ -863,7 +915,7 @@ public void testInvalidActionRequestWithExistingThreadLocalRequestContext() thro
863
915
MockActionResponse response = new MockActionResponse ();
864
916
request .addPreferredLocale (Locale .GERMAN );
865
917
866
- // see RequestContextListener.requestInitialized()
918
+ // see RequestContextListener.requestInitialized()
867
919
try {
868
920
LocaleContextHolder .setLocale (request .getLocale ());
869
921
RequestContextHolder .setRequestAttributes (new PortletRequestAttributes (request ));
@@ -890,7 +942,7 @@ public void testInvalidRenderRequestWithExistingThreadLocalRequestContext() thro
890
942
MockRenderResponse response = new MockRenderResponse ();
891
943
request .addPreferredLocale (Locale .GERMAN );
892
944
893
- // see RequestContextListener.requestInitialized()
945
+ // see RequestContextListener.requestInitialized()
894
946
try {
895
947
LocaleContextHolder .setLocale (request .getLocale ());
896
948
RequestContextHolder .setRequestAttributes (new PortletRequestAttributes (request ));
@@ -903,7 +955,7 @@ public void testInvalidRenderRequestWithExistingThreadLocalRequestContext() thro
903
955
fail ("should have failed to find a handler and raised an NoHandlerFoundExceptionException" );
904
956
}
905
957
catch (NoHandlerFoundException ex ) {
906
- // expected
958
+ // expected
907
959
}
908
960
909
961
assertSame (servletLocaleContext , LocaleContextHolder .getLocaleContext ());
0 commit comments