1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.application;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33
34 import javax.faces.FactoryFinder;
35 import javax.faces.application.NavigationCase;
36
37 import org.apache.myfaces.config.RuntimeConfig;
38 import org.apache.myfaces.config.element.NavigationRule;
39 import org.apache.myfaces.config.impl.digester.DigesterFacesConfigUnmarshallerImpl;
40 import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
41 import org.junit.Assert;
42 import org.junit.Test;
43 import org.xml.sax.SAXException;
44
45 public class NavigationHandlerImplTest extends AbstractJsfTestCase
46 {
47
48 private DigesterFacesConfigUnmarshallerImpl _digesterFacesConfigUnmarshaller;
49
50 public NavigationHandlerImplTest()
51 {
52 super();
53 }
54
55 @Override
56 public void setUp() throws Exception
57 {
58 super.setUp();
59 _digesterFacesConfigUnmarshaller = new DigesterFacesConfigUnmarshallerImpl(
60 externalContext);
61 }
62
63 @Override
64 protected void setFactories() throws Exception
65 {
66 super.setFactories();
67 FactoryFinder.setFactory(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY,
68 "org.apache.myfaces.context.PartialViewContextFactoryImpl");
69 }
70
71
72 @Override
73 public void tearDown() throws Exception
74 {
75 _digesterFacesConfigUnmarshaller = null;
76 super.tearDown();
77 }
78
79 private void loadTextFacesConfig(String file) throws SAXException,
80 IOException
81 {
82 RuntimeConfig runtimeConfig = RuntimeConfig
83 .getCurrentInstance(externalContext);
84
85 org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl config = _digesterFacesConfigUnmarshaller
86 .getFacesConfig(getClass().getResourceAsStream(file), file);
87
88 for (NavigationRule rule : config.getNavigationRules())
89 {
90 runtimeConfig.addNavigationRule(rule);
91 }
92 }
93
94 @Test
95 public void testGetSimpleExactMatchRule() throws Exception
96 {
97 loadTextFacesConfig("simple-rules-config.xml");
98
99 facesContext.getViewRoot().setViewId("/a.jsp");
100 NavigationHandlerImpl nh = new NavigationHandlerImpl();
101
102 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
103
104 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
105 }
106
107 @Test
108 public void testHandleSimpleExactMatchRule() throws Exception
109 {
110 loadTextFacesConfig("simple-rules-config.xml");
111
112 facesContext.getViewRoot().setViewId("/a.jsp");
113 NavigationHandlerImpl nh = new NavigationHandlerImpl();
114
115 nh.handleNavigation(facesContext, null, "go");
116
117 Assert.assertEquals("/b.jsp", facesContext.getViewRoot().getViewId());
118 }
119
120 @Test
121 public void testGetSimpleGlobalExactMatchRule() throws Exception
122 {
123 loadTextFacesConfig("simple-global-rules-config.xml");
124
125 facesContext.getViewRoot().setViewId("/a.jsp");
126 NavigationHandlerImpl nh = new NavigationHandlerImpl();
127
128 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
129
130 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
131 }
132
133 @Test
134 public void testHandleSimpleGlobalExactMatchRule() throws Exception
135 {
136 loadTextFacesConfig("simple-global-rules-config.xml");
137
138 facesContext.getViewRoot().setViewId("/a.jsp");
139 NavigationHandlerImpl nh = new NavigationHandlerImpl();
140
141 nh.handleNavigation(facesContext, null, "go");
142
143 Assert.assertEquals("/b.jsp", facesContext.getViewRoot().getViewId());
144 }
145
146 @Test
147 public void testGetSimpleMixExactMatchRule() throws Exception
148 {
149 loadTextFacesConfig("simple-mix-rules-config.xml");
150
151 facesContext.getViewRoot().setViewId("/a.jsp");
152 NavigationHandlerImpl nh = new NavigationHandlerImpl();
153
154 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
155
156 Assert.assertEquals("/c.jsp", nc.getToViewId(facesContext));
157
158 facesContext.getViewRoot().setViewId("/z.jsp");
159
160 nc = nh.getNavigationCase(facesContext, null, "go");
161
162 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
163 }
164
165 @Test
166 public void testHandleSimpleMixExactMatchRule() throws Exception
167 {
168 loadTextFacesConfig("simple-mix-rules-config.xml");
169
170 facesContext.getViewRoot().setViewId("/a.jsp");
171 NavigationHandlerImpl nh = new NavigationHandlerImpl();
172
173 nh.handleNavigation(facesContext, null, "go");
174
175 Assert.assertEquals("/c.jsp", facesContext.getViewRoot().getViewId());
176
177 facesContext.getViewRoot().setViewId("/z.jsp");
178
179 nh.handleNavigation(facesContext, null, "go");
180
181 Assert.assertEquals("/b.jsp", facesContext.getViewRoot().getViewId());
182 }
183
184 @Test
185 public void testGetSimplePartialExactMatchRule() throws Exception
186 {
187 loadTextFacesConfig("simple-partial-rules-config.xml");
188
189 facesContext.getViewRoot().setViewId("/a.jsp");
190 NavigationHandlerImpl nh = new NavigationHandlerImpl();
191
192 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
193
194 Assert.assertEquals("/cars/b.jsp", nc.getToViewId(facesContext));
195
196 facesContext.getViewRoot().setViewId("/cars/z.jsp");
197
198 nc = nh.getNavigationCase(facesContext, null, "go");
199
200 Assert.assertEquals("/cars/c.jsp", nc.getToViewId(facesContext));
201 }
202
203 @Test
204 public void testHandleSimplePartialExactMatchRule() throws Exception
205 {
206 loadTextFacesConfig("simple-partial-rules-config.xml");
207
208 facesContext.getViewRoot().setViewId("/a.jsp");
209 NavigationHandlerImpl nh = new NavigationHandlerImpl();
210
211 nh.handleNavigation(facesContext, null, "go");
212
213 Assert.assertEquals("/cars/b.jsp", facesContext.getViewRoot().getViewId());
214
215 facesContext.getViewRoot().setViewId("/cars/z.jsp");
216
217 nh.handleNavigation(facesContext, null, "go");
218
219 Assert.assertEquals("/cars/c.jsp", facesContext.getViewRoot().getViewId());
220 }
221
222 @Test
223 public void testGetSimpleELExactMatchRule() throws Exception
224 {
225 loadTextFacesConfig("simple-el-rules-config.xml");
226
227 facesContext.getViewRoot().setViewId("/a.jsp");
228
229 NavigationHandlerImpl nh = new NavigationHandlerImpl();
230
231 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", "go");
232
233 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
234 }
235
236 @Test
237 public void testGetSimpleELExactMatchRuleFailNullOutcome() throws Exception
238 {
239 loadTextFacesConfig("simple-el-rules-config.xml");
240
241 facesContext.getViewRoot().setViewId("/a.jsp");
242
243 NavigationHandlerImpl nh = new NavigationHandlerImpl();
244
245 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", null);
246
247 Assert.assertNull(nc);
248 }
249
250 public static class TestBean
251 {
252 public boolean isTrue()
253 {
254 return true;
255 }
256
257 public boolean isFalse()
258 {
259 return false;
260 }
261 }
262
263 @Test
264 public void testGetSimpleIfExactMatchRule() throws Exception
265 {
266 loadTextFacesConfig("simple-if-rules-config.xml");
267
268 externalContext.getRequestMap().put("test", new TestBean());
269
270 facesContext.getViewRoot().setViewId("/a.jsp");
271
272 NavigationHandlerImpl nh = new NavigationHandlerImpl();
273
274 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
275
276 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
277 }
278
279 @Test
280 public void testGetSimpleIfPreferMathRule() throws Exception
281 {
282 loadTextFacesConfig("simple-if-rules-config-2.xml");
283
284 externalContext.getRequestMap().put("test", new TestBean());
285
286 facesContext.getViewRoot().setViewId("/a.jsp");
287
288 NavigationHandlerImpl nh = new NavigationHandlerImpl();
289
290 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
291
292 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
293 }
294
295 @Test
296 public void testGetSimpleIfPreferMathRule2() throws Exception
297 {
298 loadTextFacesConfig("simple-if-rules-config-3.xml");
299
300 externalContext.getRequestMap().put("test", new TestBean());
301
302 facesContext.getViewRoot().setViewId("/a.jsp");
303
304 NavigationHandlerImpl nh = new NavigationHandlerImpl();
305
306 NavigationCase nc = nh.getNavigationCase(facesContext, "go", null);
307
308 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
309
310 nc = nh.getNavigationCase(facesContext, "go", "xx");
311
312 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
313 }
314
315 @Test
316 public void testGetSimpleNotIfExactMatchRule() throws Exception
317 {
318 loadTextFacesConfig("simple-if-rules-config.xml");
319
320 externalContext.getRequestMap().put("test", new TestBean());
321
322 facesContext.getViewRoot().setViewId("/b.jsp");
323
324 NavigationHandlerImpl nh = new NavigationHandlerImpl();
325
326 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
327
328 Assert.assertEquals("/d.jsp", nc.getToViewId(facesContext));
329 }
330
331 @Test
332 public void testGetSimplePreemptiveIfExactMatchRule() throws Exception
333 {
334 loadTextFacesConfig("simple-if-rules-config.xml");
335
336 externalContext.getRequestMap().put("test", new TestBean());
337
338 facesContext.getViewRoot().setViewId("/x.jsp");
339
340 NavigationHandlerImpl nh = new NavigationHandlerImpl();
341
342 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
343
344 Assert.assertEquals("/go.jsp", nc.getToViewId(facesContext));
345 }
346
347 @Test
348 public void testGetSimpleGlobalPreemptiveMatchRule() throws Exception
349 {
350 loadTextFacesConfig("simple-global-preemptive-rules-config.xml");
351
352 externalContext.getRequestMap().put("test", new TestBean());
353
354 facesContext.getViewRoot().setViewId("/x.jsp");
355
356 NavigationHandlerImpl nh = new NavigationHandlerImpl();
357
358 NavigationCase nc = nh.getNavigationCase(facesContext, null, "go");
359
360 Assert.assertEquals("/a.jsp", nc.getToViewId(facesContext));
361 }
362
363 @Test
364 public void testGetSimpleELNoCondMatchRule() throws Exception
365 {
366 loadTextFacesConfig("simple-el-nocond-rules-config.xml");
367
368 facesContext.getViewRoot().setViewId("/a.jsp");
369
370 NavigationHandlerImpl nh = new NavigationHandlerImpl();
371
372 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", "go");
373
374 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
375 }
376
377 @Test
378 public void testGetSimpleELNoCondNullOutcomeMatchRule() throws Exception
379 {
380 loadTextFacesConfig("simple-el-nocond-rules-config.xml");
381
382 facesContext.getViewRoot().setViewId("/a.jsp");
383
384 NavigationHandlerImpl nh = new NavigationHandlerImpl();
385
386 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", null);
387
388 Assert.assertNull(nc);
389 }
390
391 @Test
392 public void testActionOutcomePrecendeceMachRule() throws Exception
393 {
394 loadTextFacesConfig("simple-action-outcome-precedence-config.xml");
395
396 for (int i = 0; i < 2; i++)
397 {
398 final int j = i;
399 facesContext.getViewRoot().setViewId("/a.jsp");
400
401 NavigationHandlerImpl nh = new NavigationHandlerImpl(){
402
403 @Override
404 public Map<String, Set<NavigationCase>> getNavigationCases()
405 {
406
407
408 Map<String, Set<NavigationCase>> map = super.getNavigationCases();
409 Map<String, Set<NavigationCase>> map2 = new HashMap<String, Set<NavigationCase>>();
410
411 for (Map.Entry<String, Set<NavigationCase>> entry : map.entrySet())
412 {
413 LinkedHashSet<NavigationCase> set = new LinkedHashSet<NavigationCase>();
414 List<NavigationCase> list = new ArrayList<NavigationCase>();
415 list.addAll(entry.getValue());
416 Collections.rotate(list, j);
417 set.addAll(list);
418 map2.put(entry.getKey(), set);
419 }
420 return map2;
421 }
422 };
423
424 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", "go");
425
426 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
427 }
428 }
429
430 @Test
431 public void testActionOutcomePrecendeceAlternateOutcomeMachRule() throws Exception
432 {
433 loadTextFacesConfig("simple-action-outcome-precedence-config.xml");
434
435 for (int i = 0; i < 2; i++)
436 {
437 final int j = i;
438 facesContext.getViewRoot().setViewId("/a.jsp");
439
440 NavigationHandlerImpl nh = new NavigationHandlerImpl(){
441
442 @Override
443 public Map<String, Set<NavigationCase>> getNavigationCases()
444 {
445
446
447 Map<String, Set<NavigationCase>> map = super.getNavigationCases();
448 Map<String, Set<NavigationCase>> map2 = new HashMap<String, Set<NavigationCase>>();
449
450 for (Map.Entry<String, Set<NavigationCase>> entry : map.entrySet())
451 {
452 LinkedHashSet<NavigationCase> set = new LinkedHashSet<NavigationCase>();
453 List<NavigationCase> list = new ArrayList<NavigationCase>();
454 list.addAll(entry.getValue());
455 Collections.rotate(list, j);
456 set.addAll(list);
457 map2.put(entry.getKey(), set);
458 }
459 return map2;
460 }
461 };
462
463 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", "nogo");
464
465 Assert.assertEquals("/c.jsp", nc.getToViewId(facesContext));
466 }
467 }
468
469 @Test
470 public void testActionOutcomePrecendeceNoOutcomeMachRule() throws Exception
471 {
472 loadTextFacesConfig("simple-action-outcome-precedence-config.xml");
473
474 for (int i = 0; i < 2; i++)
475 {
476 final int j = i;
477 facesContext.getViewRoot().setViewId("/a.jsp");
478
479 NavigationHandlerImpl nh = new NavigationHandlerImpl(){
480
481 @Override
482 public Map<String, Set<NavigationCase>> getNavigationCases()
483 {
484
485
486 Map<String, Set<NavigationCase>> map = super.getNavigationCases();
487 Map<String, Set<NavigationCase>> map2 = new HashMap<String, Set<NavigationCase>>();
488
489 for (Map.Entry<String, Set<NavigationCase>> entry : map.entrySet())
490 {
491 LinkedHashSet<NavigationCase> set = new LinkedHashSet<NavigationCase>();
492 List<NavigationCase> list = new ArrayList<NavigationCase>();
493 list.addAll(entry.getValue());
494 Collections.rotate(list, j);
495 set.addAll(list);
496 map2.put(entry.getKey(), set);
497 }
498 return map2;
499 }
500 };
501
502 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", null);
503
504
505 Assert.assertNull(nc);
506 }
507 }
508
509 @Test
510 public void testActionOutcomePrecendece2MachRule() throws Exception
511 {
512 loadTextFacesConfig("simple-action-outcome-precedence-2-config.xml");
513
514 for (int i = 0; i < 2; i++)
515 {
516 final int j = i;
517 facesContext.getViewRoot().setViewId("/a.jsp");
518
519 NavigationHandlerImpl nh = new NavigationHandlerImpl(){
520
521 @Override
522 public Map<String, Set<NavigationCase>> getNavigationCases()
523 {
524
525
526 Map<String, Set<NavigationCase>> map = super.getNavigationCases();
527 Map<String, Set<NavigationCase>> map2 = new HashMap<String, Set<NavigationCase>>();
528
529 for (Map.Entry<String, Set<NavigationCase>> entry : map.entrySet())
530 {
531 LinkedHashSet<NavigationCase> set = new LinkedHashSet<NavigationCase>();
532 List<NavigationCase> list = new ArrayList<NavigationCase>();
533 list.addAll(entry.getValue());
534 Collections.rotate(list, j);
535 set.addAll(list);
536 map2.put(entry.getKey(), set);
537 }
538 return map2;
539 }
540 };
541
542 NavigationCase nc = nh.getNavigationCase(facesContext, "#{rules.go}", "go");
543
544 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
545 }
546 }
547
548
549
550
551
552 @Test
553 public void testFacesRedirectAddsUrlParameters()
554 {
555 NavigationHandlerImpl nh = new NavigationHandlerImpl();
556
557
558
559 NavigationCase navigationCase = nh.getNavigationCase(facesContext, null,
560 "test.xhtml?faces-redirect=true&a=b&includeViewParams=true&c=d&e=f");
561
562
563 Map<String, List<String>> expected = new HashMap<String, List<String>>();
564 expected.put("a", Arrays.asList("b"));
565 expected.put("c", Arrays.asList("d"));
566 expected.put("e", Arrays.asList("f"));
567
568
569
570 Assert.assertEquals(expected, navigationCase.getParameters());
571 Assert.assertTrue("includeViewParams=true in the query String must "
572 + "set includeViewParams to true.", navigationCase.isIncludeViewParams());
573 Assert.assertTrue("redirect=true in the query String must "
574 + "set redirect to true.", navigationCase.isRedirect());
575 }
576
577
578
579
580
581
582
583 @Test
584 public void testFacesRedirectAddsUrlParametersFacesIncludeViewParams()
585 {
586 NavigationHandlerImpl nh = new NavigationHandlerImpl();
587
588
589
590 NavigationCase navigationCase = nh.getNavigationCase(facesContext, null,
591 "test.xhtml?faces-redirect=true&a=b&faces-include-view-params=true&c=d&e=f");
592
593
594 Map<String, List<String>> expected = new HashMap<String, List<String>>();
595 expected.put("a", Arrays.asList("b"));
596 expected.put("c", Arrays.asList("d"));
597 expected.put("e", Arrays.asList("f"));
598
599
600
601 Assert.assertEquals(expected, navigationCase.getParameters());
602 Assert.assertTrue("faces-include-view-params=true in the query String must "
603 + "set includeViewParams to true.", navigationCase.isIncludeViewParams());
604 Assert.assertTrue("redirect=true in the query String must "
605 + "set redirect to true.", navigationCase.isRedirect());
606 }
607
608
609
610
611 @Test
612 public void testHandleViewExpiredExpcetion() throws Exception {
613 NavigationHandlerImpl underTest = new NavigationHandlerImpl();
614
615 facesContext.setViewRoot(null);
616
617 facesContext.getExternalContext().getRequestMap().put("javax.servlet.include.servlet_path", "/faces/home.xhtml");
618
619
620 underTest.handleNavigation(facesContext, null, "viewExpired");
621
622 assertNotNull(facesContext.getViewRoot());
623 assertEquals("/viewExpired.xhtml", facesContext.getViewRoot().getViewId());
624 }
625
626 @Test
627 public void testHandleViewExpiredExpcetion2() throws Exception {
628 NavigationHandlerImpl underTest = new NavigationHandlerImpl();
629
630 facesContext.setViewRoot(null);
631
632 facesContext.getExternalContext().getRequestMap().put("javax.servlet.include.servlet_path", "/home.jsf");
633
634
635 underTest.handleNavigation(facesContext, null, "viewExpired.xhtml");
636
637 assertNotNull(facesContext.getViewRoot());
638 assertEquals("/viewExpired.xhtml", facesContext.getViewRoot().getViewId());
639
640 }
641
642 @Test
643 public void testHandleViewExpiredExpcetion3() throws Exception {
644 NavigationHandlerImpl underTest = new NavigationHandlerImpl();
645
646 facesContext.setViewRoot(null);
647
648 facesContext.getExternalContext().getRequestMap().put("javax.servlet.include.servlet_path", "/home.jsf");
649
650
651 underTest.handleNavigation(facesContext, null, "viewExpired");
652
653 assertNotNull(facesContext.getViewRoot());
654
655
656
657 assertEquals("/viewExpired.jsf", facesContext.getViewRoot().getViewId());
658
659 }
660
661 @Test
662 public void testHandleViewExpiredExpcetion4() throws Exception {
663 NavigationHandlerImpl underTest = new NavigationHandlerImpl();
664
665 facesContext.setViewRoot(null);
666
667
668 underTest.handleNavigation(facesContext, null, "viewExpired.xhtml");
669
670 assertNotNull(facesContext.getViewRoot());
671 assertEquals("/viewExpired.xhtml", facesContext.getViewRoot().getViewId());
672
673 }
674
675
676
677
678 @Test
679 public void testHandleViewExpiredExpcetionForPartial() throws Exception {
680 NavigationHandlerImpl underTest = new NavigationHandlerImpl();
681
682 facesContext.setViewRoot(null);
683 facesContext.getPartialViewContext().setPartialRequest(true);
684
685 underTest.handleNavigation(facesContext, null, "/viewExpired.xhtml");
686
687 assertNotNull(facesContext.getViewRoot());
688 assertEquals("/viewExpired.xhtml", facesContext.getViewRoot().getViewId());
689 }
690
691 @Test
692 public void testIfDoNotMatchWhenOutcomeNull() throws Exception
693 {
694 loadTextFacesConfig("simple-if-rules-config-4.xml");
695
696 externalContext.getRequestMap().put("test", new TestBean());
697
698 facesContext.getViewRoot().setViewId("/a.jsp");
699
700 NavigationHandlerImpl nh = new NavigationHandlerImpl();
701
702 NavigationCase nc = nh.getNavigationCase(facesContext, null, null);
703
704 Assert.assertNull(nc);
705
706 nc = nh.getNavigationCase(facesContext, null, "go");
707
708 Assert.assertEquals("/b.jsp", nc.getToViewId(facesContext));
709 }
710
711 }