1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif;
17
18 import junit.framework.Assert;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.kuali.rice.core.api.util.ConcreteKeyValue;
23 import org.kuali.rice.core.api.util.KeyValue;
24 import org.kuali.rice.krad.uif.component.Component;
25 import org.kuali.rice.krad.uif.control.RadioGroupControl;
26 import org.kuali.rice.krad.uif.element.Link;
27 import org.kuali.rice.krad.uif.element.Message;
28 import org.kuali.rice.krad.uif.field.InputField;
29 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
30 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
31 import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
32 import org.kuali.rice.krad.uif.util.ComponentFactory;
33 import org.kuali.rice.krad.uif.util.KeyMessage;
34 import org.kuali.rice.krad.uif.view.FormView;
35 import org.kuali.rice.krad.uif.util.LifecycleElement;
36 import org.kuali.rice.krad.uif.view.View;
37 import org.kuali.rice.krad.util.KRADConstants;
38 import org.kuali.rice.krad.web.form.UifFormBase;
39 import org.kuali.rice.krad.test.KRADTestCase;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44
45
46
47
48
49 public class RichMessageTest extends KRADTestCase {
50
51
52
53
54 public class SampleForm extends UifFormBase {
55 public String field1;
56 public String field2;
57 public boolean renderField;
58 }
59
60 View view = new FormView();
61 Message message;
62 SampleForm model;
63
64
65
66
67
68 @Override
69 @Before
70 public void setUp() throws Exception {
71 super.setUp();
72 view.setViewHelperServiceClass(ViewHelperServiceImpl.class);
73 }
74
75
76
77
78 @Test
79 public void testHtmlContentGeneration() {
80 model = new SampleForm();
81 List<Component> components;
82
83
84 generateAndSetMessage("[b]Message Content[/b]");
85 components = message.getMessageComponentStructure();
86 Assert.assertNotNull(components);
87 Assert.assertEquals(1, components.size());
88 Assert.assertTrue(components.get(0) instanceof Message);
89 Assert.assertFalse(((Message) components.get(0)).isRenderWrapperTag());
90 Assert.assertEquals("<b>Message Content</b>", ((Message) components.get(0)).getMessageText());
91
92
93 generateAndSetMessage("[span][b]Message Content[/b][/span]");
94 components = message.getMessageComponentStructure();
95 Assert.assertEquals(1, components.size());
96 Assert.assertEquals("<span><b>Message Content</b></span>", ((Message) components.get(0)).getMessageText());
97
98
99 generateAndSetMessage("[div][b]Message Content[/b][/div][p]Message [i]Message[/i] Message[/p]");
100 components = message.getMessageComponentStructure();
101 Assert.assertEquals(1, components.size());
102 Assert.assertEquals("<div><b>Message Content</b></div><p>Message <i>Message</i> Message</p>",
103 ((Message) components.get(0)).getMessageText());
104
105
106 generateAndSetMessage(
107 "[div class='cssClass'][b]Message Content[/b][/div][p]Message [a href='http://www.kuali.org']Message[/a] Message[/p]");
108 components = message.getMessageComponentStructure();
109 Assert.assertEquals(1, components.size());
110 Assert.assertEquals(
111 "<div class='cssClass'><b>Message Content</b></div><p>Message <a href='http://www.kuali.org'>Message</a> Message</p>",
112 ((Message) components.get(0)).getMessageText());
113 }
114
115
116
117
118 @Test
119 public void testEscapeCharacter() {
120 model = new SampleForm();
121 List<Component> components;
122
123
124 generateAndSetMessage("\\[ \\] Message \\[content\\]\\[/content\\]");
125 components = message.getMessageComponentStructure();
126 Assert.assertNotNull(components);
127 Assert.assertEquals(1, components.size());
128 Assert.assertTrue(components.get(0) instanceof Message);
129 String messageText = ((Message) components.get(0)).getMessageText();
130 Assert.assertEquals(KRADConstants.MessageParsing.LEFT_BRACKET
131 + " "
132 + KRADConstants.MessageParsing.RIGHT_BRACKET
133 + " Message "
134 + KRADConstants.MessageParsing.LEFT_BRACKET
135 + "content"
136 + KRADConstants.MessageParsing.RIGHT_BRACKET
137 + ""
138 + KRADConstants.MessageParsing.LEFT_BRACKET
139 + "/content"
140 + KRADConstants.MessageParsing.RIGHT_BRACKET, messageText);
141 messageText = messageText.replace(KRADConstants.MessageParsing.LEFT_BRACKET, "[");
142 messageText = messageText.replace(KRADConstants.MessageParsing.RIGHT_BRACKET, "]");
143 Assert.assertEquals("[ ] Message [content][/content]", messageText);
144 }
145
146
147
148
149 @Test
150 public void testLinkGeneration() {
151 model = new SampleForm();
152 List<Component> components;
153
154 generateAndSetMessage("Link here [link='http://www.kuali.org']Link[/link] text");
155 components = message.getMessageComponentStructure();
156 Assert.assertNotNull(components);
157 Assert.assertEquals(1, components.size());
158 Assert.assertTrue(components.get(0) instanceof Message);
159 String messageText = ((Message) components.get(0)).getMessageText();
160 Assert.assertEquals("Link here <a href='http://www.kuali.org' target='_blank'>Link</a> text", messageText);
161 }
162
163
164
165
166 @Test
167 public void testActionGeneration() {
168 model = new SampleForm();
169 List<Component> components;
170
171
172 generateAndSetMessage("Action here [action=methodToCall]action[/action] text");
173 components = message.getMessageComponentStructure();
174 Assert.assertNotNull(components);
175 Assert.assertEquals(1, components.size());
176 Assert.assertTrue(components.get(0) instanceof Message);
177 String messageText = ((Message) components.get(0)).getMessageText();
178 Assert.assertEquals(
179 "Action here <a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,true,true,null); return false;\">action</a> text",
180 messageText);
181
182
183 generateAndSetMessage("Action here [action=methodToCall,false]action[/action] text");
184 components = message.getMessageComponentStructure();
185 Assert.assertNotNull(components);
186 Assert.assertEquals(1, components.size());
187 Assert.assertTrue(components.get(0) instanceof Message);
188 messageText = ((Message) components.get(0)).getMessageText();
189 Assert.assertEquals(
190 "Action here <a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,false,true,null); return false;\">action</a> text",
191 messageText);
192
193 generateAndSetMessage("Action here [action=methodToCall,false,true]action[/action] text");
194 components = message.getMessageComponentStructure();
195 Assert.assertNotNull(components);
196 Assert.assertEquals(1, components.size());
197 Assert.assertTrue(components.get(0) instanceof Message);
198 messageText = ((Message) components.get(0)).getMessageText();
199 Assert.assertEquals(
200 "Action here <a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,false,true,null); return false;\">action</a> text",
201 messageText);
202
203
204 generateAndSetMessage("Action here [action=methodToCall,true,false]action[/action] text");
205 components = message.getMessageComponentStructure();
206 Assert.assertNotNull(components);
207 Assert.assertEquals(1, components.size());
208 Assert.assertTrue(components.get(0) instanceof Message);
209 messageText = ((Message) components.get(0)).getMessageText();
210 Assert.assertEquals(
211 "Action here <a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,true,false,null); return false;\">action</a> text",
212 messageText);
213
214
215 generateAndSetMessage(
216 "Action here [action=methodToCall,false,true,function(){console.log('success');}]action[/action] text");
217 components = message.getMessageComponentStructure();
218 Assert.assertNotNull(components);
219 Assert.assertEquals(1, components.size());
220 Assert.assertTrue(components.get(0) instanceof Message);
221 messageText = ((Message) components.get(0)).getMessageText();
222 Assert.assertEquals(
223 "Action here <a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,false,true,function(){console.log('success');}); return false;\">action</a> text",
224 messageText);
225
226
227 generateAndSetMessage(
228 "Action here [action=methodToCall data={something: 'value', something2: 'value2'}]action[/action] text");
229 components = message.getMessageComponentStructure();
230 Assert.assertNotNull(components);
231 Assert.assertEquals(1, components.size());
232 Assert.assertTrue(components.get(0) instanceof Message);
233 messageText = ((Message) components.get(0)).getMessageText();
234 Assert.assertEquals(
235 "Action here <a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',{something: 'value', something2: 'value2'},true,true,null); return false;\">action</a> text",
236 messageText);
237
238
239 generateAndSetMessage(
240 "Action here [action=methodToCall,true,false data={something: 'value', something2: 'value2'}]action[/action] text");
241 components = message.getMessageComponentStructure();
242 Assert.assertNotNull(components);
243 Assert.assertEquals(1, components.size());
244 Assert.assertTrue(components.get(0) instanceof Message);
245 messageText = ((Message) components.get(0)).getMessageText();
246 Assert.assertEquals(
247 "Action here <a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',{something: 'value', something2: 'value2'},true,false,null); return false;\">action</a> text",
248 messageText);
249 }
250
251
252
253
254 @Test
255 public void testColorGeneration() {
256 model = new SampleForm();
257 List<Component> components;
258
259 generateAndSetMessage("color here [color='blue']Color[/color] text");
260 components = message.getMessageComponentStructure();
261 Assert.assertNotNull(components);
262 Assert.assertEquals(1, components.size());
263 Assert.assertTrue(components.get(0) instanceof Message);
264 String messageText = ((Message) components.get(0)).getMessageText();
265 Assert.assertEquals("color here <span style='color: blue;'>Color</span> text", messageText);
266
267 generateAndSetMessage("color here [color=#FFFFFF]Color[/color] text");
268 components = message.getMessageComponentStructure();
269 Assert.assertNotNull(components);
270 Assert.assertEquals(1, components.size());
271 Assert.assertTrue(components.get(0) instanceof Message);
272 messageText = ((Message) components.get(0)).getMessageText();
273 Assert.assertEquals("color here <span style='color: #FFFFFF;'>Color</span> text", messageText);
274 }
275
276
277
278
279 @Test
280 public void testCssClassGeneration() {
281 model = new SampleForm();
282 List<Component> components;
283
284 generateAndSetMessage("css here [css='c1']sample[/css] text");
285 components = message.getMessageComponentStructure();
286 Assert.assertNotNull(components);
287 Assert.assertEquals(1, components.size());
288 Assert.assertTrue(components.get(0) instanceof Message);
289 String messageText = ((Message) components.get(0)).getMessageText();
290 Assert.assertEquals("css here <span class='c1'>sample</span> text", messageText);
291
292 generateAndSetMessage("css here \\[[css='c1 c2']sample[/css]\\] text");
293 components = message.getMessageComponentStructure();
294 Assert.assertNotNull(components);
295 Assert.assertEquals(1, components.size());
296 Assert.assertTrue(components.get(0) instanceof Message);
297 messageText = ((Message) components.get(0)).getMessageText();
298 Assert.assertEquals("css here " + KRADConstants.MessageParsing.LEFT_BRACKET +
299 "<span class='c1 c2'>sample</span>" + KRADConstants.MessageParsing.RIGHT_BRACKET +
300 " text", messageText);
301 }
302
303
304
305
306 @Test
307 public void testInlineComponentGeneration() {
308 List<Component> components;
309
310
311 InputField inputField1 = ComponentFactory.getInputField();
312 inputField1.setPropertyName("field1");
313 List<Component> inline = new ArrayList<Component>();
314 inline.add(inputField1);
315 generateAndSetMessage("Message text [0] Message text", inline);
316 components = message.getMessageComponentStructure();
317 Assert.assertNotNull(components);
318 Assert.assertEquals(3, components.size());
319 Assert.assertTrue(components.get(0) instanceof Message);
320 Assert.assertEquals("Message text ", ((Message) components.get(0)).getMessageText());
321 Assert.assertTrue(components.get(1) instanceof InputField);
322 Assert.assertEquals("field1", ((InputField) components.get(1)).getPropertyName());
323 Assert.assertTrue(components.get(2) instanceof Message);
324 Assert.assertEquals(" Message text", ((Message) components.get(2)).getMessageText());
325
326
327 inputField1 = ComponentFactory.getInputField();
328 inputField1.setPropertyName("field1");
329 InputField inputField2 = ComponentFactory.getInputField();
330 inputField2.setPropertyName("field2");
331 inline = new ArrayList<Component>();
332 inline.add(inputField1);
333 inline.add(inputField2);
334 generateAndSetMessage("[p class='cssClass']Message text [0] Message [b]text [1] other[/b] text[/p]", inline);
335 components = message.getMessageComponentStructure();
336 Assert.assertNotNull(components);
337 Assert.assertEquals(5, components.size());
338 Assert.assertTrue(components.get(0) instanceof Message);
339 Assert.assertEquals("<p class='cssClass'>Message text ", ((Message) components.get(0)).getMessageText());
340 Assert.assertTrue(components.get(1) instanceof InputField);
341 Assert.assertEquals("field1", ((InputField) components.get(1)).getPropertyName());
342 Assert.assertTrue(components.get(2) instanceof Message);
343 Assert.assertEquals(" Message <b>text ", ((Message) components.get(2)).getMessageText());
344 Assert.assertFalse(((Message) components.get(2)).isRenderWrapperTag());
345 Assert.assertTrue(components.get(3) instanceof InputField);
346 Assert.assertEquals("field2", ((InputField) components.get(3)).getPropertyName());
347 Assert.assertTrue(components.get(4) instanceof Message);
348 Assert.assertEquals(" other</b> text</p>", ((Message) components.get(4)).getMessageText());
349
350
351 inputField1 = ComponentFactory.getInputField();
352 inputField1.setPropertyName("field1");
353 inputField2 = ComponentFactory.getInputField();
354 inputField2.setPropertyName("field2");
355 inline = new ArrayList<Component>();
356 inline.add(inputField1);
357 inline.add(inputField2);
358 generateAndSetMessage(
359 "[p class='cssClass']Message text [0 propertyName='field20'] Message [b]text [1 cssClasses='c1 c2' required=true] other[/b] text[/p]",
360 inline);
361 components = message.getMessageComponentStructure();
362 Assert.assertNotNull(components);
363 Assert.assertEquals(5, components.size());
364 Assert.assertTrue(components.get(0) instanceof Message);
365 Assert.assertEquals("<p class='cssClass'>Message text ", ((Message) components.get(0)).getMessageText());
366 Assert.assertTrue(components.get(1) instanceof InputField);
367 Assert.assertEquals("field20", ((InputField) components.get(1)).getPropertyName());
368 Assert.assertTrue(components.get(2) instanceof Message);
369 Assert.assertEquals(" Message <b>text ", ((Message) components.get(2)).getMessageText());
370 Assert.assertTrue(components.get(3) instanceof InputField);
371 Assert.assertEquals("field2", ((InputField) components.get(3)).getPropertyName());
372 Assert.assertTrue(((InputField) components.get(3)).getRequired());
373 Assert.assertTrue(((InputField) components.get(3)).getCssClasses().contains("c1 c2"));
374 Assert.assertTrue(components.get(4) instanceof Message);
375 Assert.assertEquals(" other</b> text</p>", ((Message) components.get(4)).getMessageText());
376 }
377
378
379
380
381 @Test
382 public void testIdComponentGeneration() {
383 List<Component> components;
384
385
386 InputField inputField1 = ComponentFactory.getInputField();
387 inputField1.setPropertyName("field1");
388 List<Component> inline = new ArrayList<Component>();
389 inline.add(inputField1);
390 generateAndSetMessage("Message text [0] Message text [id=Uif-Link]", inline);
391 components = message.getMessageComponentStructure();
392 Assert.assertNotNull(components);
393 Assert.assertEquals(4, components.size());
394 Assert.assertTrue(components.get(0) instanceof Message);
395 Assert.assertEquals("Message text ", ((Message) components.get(0)).getMessageText());
396 Assert.assertTrue(components.get(1) instanceof InputField);
397 Assert.assertEquals("field1", ((InputField) components.get(1)).getPropertyName());
398 Assert.assertTrue(components.get(2) instanceof Message);
399 Assert.assertEquals(" Message text ", ((Message) components.get(2)).getMessageText());
400 Assert.assertTrue(components.get(3) instanceof Link);
401
402
403 inputField1 = ComponentFactory.getInputField();
404 inputField1.setPropertyName("field1");
405 inline = new ArrayList<Component>();
406 inline.add(inputField1);
407 generateAndSetMessage(
408 "Message text [0] Message text [id=Uif-InputField propertyName=field2][id=Uif-InputField propertyName=field3]",
409 inline);
410 components = message.getMessageComponentStructure();
411 Assert.assertNotNull(components);
412 Assert.assertEquals(5, components.size());
413 Assert.assertTrue(components.get(0) instanceof Message);
414 Assert.assertEquals("Message text ", ((Message) components.get(0)).getMessageText());
415 Assert.assertTrue(components.get(1) instanceof InputField);
416 Assert.assertEquals("field1", ((InputField) components.get(1)).getPropertyName());
417 Assert.assertTrue(components.get(2) instanceof Message);
418 Assert.assertEquals(" Message text ", ((Message) components.get(2)).getMessageText());
419 Assert.assertTrue(components.get(3) instanceof InputField);
420 Assert.assertEquals("field2", ((InputField) components.get(3)).getPropertyName());
421 Assert.assertTrue(components.get(4) instanceof InputField);
422 Assert.assertEquals("field3", ((InputField) components.get(4)).getPropertyName());
423
424 }
425
426
427
428
429 @Test
430 public void testComplexMessageGeneration() {
431 List<Component> components;
432
433 InputField inputField1 = ComponentFactory.getInputField();
434 inputField1.setPropertyName("field1");
435 List<Component> inline = new ArrayList<Component>();
436 inline.add(inputField1);
437 generateAndSetMessage("[p][css=class]Message [link=http://www.kuali.org]link[/link][/css] [0]"
438 + " [action=methodToCall,false data={key: 'value'}]action text[/action]"
439 + " [color=green]text [id=Uif-Link href='http://www.google.com' linkText=Linky]"
440 + " [b]more text[/b][/color]\\[0\\][/p]", inline);
441 components = message.getMessageComponentStructure();
442 Assert.assertNotNull(components);
443 Assert.assertEquals(5, components.size());
444 Assert.assertTrue(components.get(0) instanceof Message);
445 Assert.assertEquals(
446 "<p><span class='class'>Message <a href='http://www.kuali.org' target='_blank'>link</a></span> ",
447 ((Message) components.get(0)).getMessageText());
448 Assert.assertTrue(components.get(1) instanceof InputField);
449 Assert.assertEquals("field1", ((InputField) components.get(1)).getPropertyName());
450 Assert.assertTrue(components.get(2) instanceof Message);
451 Assert.assertEquals(" <a href=\"javascript:void(null)\" "
452 + "onclick=\"submitForm('methodToCall',{key: 'value'},false,true,null); return false;\">"
453 + "action text</a> <span style='color: green;'>text ", ((Message) components.get(2)).getMessageText());
454 Assert.assertTrue(components.get(3) instanceof Link);
455 Assert.assertEquals("http://www.google.com", ((Link) components.get(3)).getHref());
456 Assert.assertEquals("Linky", ((Link) components.get(3)).getLinkText());
457 Assert.assertTrue(components.get(4) instanceof Message);
458 Assert.assertEquals(" <b>more text</b></span>"
459 + KRADConstants.MessageParsing.LEFT_BRACKET
460 + "0"
461 + KRADConstants.MessageParsing.RIGHT_BRACKET
462 + "</p>", ((Message) components.get(4)).getMessageText());
463
464 }
465
466
467
468
469 @Test
470 public void testRichMultiValueOptions() {
471
472 List<KeyValue> options = new ArrayList<KeyValue>();
473 options.add(new ConcreteKeyValue("1", "[color=green]Option [b]1[/b][/color]"));
474 options.add(new ConcreteKeyValue("2", "Option 2 [link='http://www.kuali.org']link[/link]"));
475 options.add(new ConcreteKeyValue("3", "Other: [id=Uif-InputField propertyName=field1]"));
476 options.add(new ConcreteKeyValue("4", "Other 2: [0]"));
477 RadioGroupControl radioGroupControl = ComponentFactory.getRadioGroupControl();
478
479 List<Component> inline = new ArrayList<Component>();
480 InputField field2 = ComponentFactory.getInputField();
481 field2.setPropertyName("field2");
482 inline.add(field2);
483 radioGroupControl.setInlineComponents(inline);
484
485 radioGroupControl.setOptions(options);
486 performSimulatedLifecycle(radioGroupControl);
487 for (LifecycleElement component : ViewLifecycleUtils.getElementsForLifecycle(radioGroupControl).values()) {
488 if (component instanceof Component) {
489 performSimulatedLifecycle((Component) component);
490 }
491 }
492
493 List<KeyMessage> richOptions = radioGroupControl.getRichOptions();
494 Assert.assertEquals("<span style='color: green;'>Option <b>1</b></span>", ((Message) (richOptions.get(0)
495 .getMessage().getMessageComponentStructure().get(0))).getMessageText());
496 Assert.assertEquals("Option 2 <a href='http://www.kuali.org' target='_blank'>link</a>",
497 ((Message) (richOptions.get(1).getMessage().getMessageComponentStructure().get(0))).getMessageText());
498 Assert.assertEquals("Other: ", ((Message) (richOptions.get(2).getMessage().getMessageComponentStructure().get(
499 0))).getMessageText());
500 Assert.assertEquals("field1", ((InputField) (richOptions.get(2).getMessage().getMessageComponentStructure().get(
501 1))).getPropertyName());
502 Assert.assertEquals("Other 2: ", ((Message) (richOptions.get(3).getMessage().getMessageComponentStructure().get(
503 0))).getMessageText());
504 Assert.assertEquals("field2", ((InputField) (richOptions.get(3).getMessage().getMessageComponentStructure().get(
505 1))).getPropertyName());
506 }
507
508
509
510
511 @Test
512 public void testSPELRichMessages() {
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 }
559
560
561
562
563 @Test
564 public void testBasicMessage() {
565 model = new SampleForm();
566 List<Component> components;
567
568
569 generateAndSetMessage("Message Content");
570 components = message.getMessageComponentStructure();
571 Assert.assertNull(components);
572 Assert.assertTrue(message.isRenderWrapperTag());
573 Assert.assertEquals("Message Content", message.getMessageText());
574 }
575
576
577
578
579
580
581 private void performSimulatedLifecycle(final LifecycleElement element) {
582 if (model == null) {
583 model = new SampleForm();
584 }
585
586 ViewLifecycle.encapsulateLifecycle(view, model, null, new Runnable(){
587 @Override
588 public void run() {
589 element.performInitialization(model);
590 element.performApplyModel(model, view);
591 element.performFinalize(model, view);
592 }});
593 }
594
595
596
597
598
599
600 private void generateAndSetMessage(String messageText) {
601 message = new Message();
602 message.setMessageText(messageText);
603 performSimulatedLifecycle(message);
604 }
605
606
607
608
609
610
611
612 private void generateAndSetMessage(String messageText, List<Component> inlineComponents) {
613 message = new Message();
614 message.setInlineComponents(inlineComponents);
615 message.setMessageText(messageText);
616 performSimulatedLifecycle(message);
617 }
618 }