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