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