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