001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif;
017
018import junit.framework.Assert;
019
020import org.junit.Before;
021import org.junit.Test;
022import org.kuali.rice.core.api.util.ConcreteKeyValue;
023import org.kuali.rice.core.api.util.KeyValue;
024import org.kuali.rice.krad.uif.component.Component;
025import org.kuali.rice.krad.uif.control.RadioGroupControl;
026import org.kuali.rice.krad.uif.element.Link;
027import org.kuali.rice.krad.uif.element.Message;
028import org.kuali.rice.krad.uif.field.InputField;
029import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
030import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
031import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
032import org.kuali.rice.krad.uif.util.ComponentFactory;
033import org.kuali.rice.krad.uif.util.KeyMessage;
034import org.kuali.rice.krad.uif.view.FormView;
035import org.kuali.rice.krad.uif.util.LifecycleElement;
036import org.kuali.rice.krad.uif.view.View;
037import org.kuali.rice.krad.util.KRADConstants;
038import org.kuali.rice.krad.web.form.UifFormBase;
039import org.kuali.rice.krad.test.KRADTestCase;
040
041import java.util.ArrayList;
042import java.util.List;
043
044/**
045 * Test rich message generation and parsing functionality
046 *
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 */
049public class RichMessageTest extends KRADTestCase {
050
051    /**
052     * SimpleForm for testing purposes
053     */
054    public class SampleForm extends UifFormBase {
055        public String field1;
056        public String field2;
057        public boolean renderField;
058    }
059
060    View view = new FormView();
061    Message message;
062    SampleForm model;
063
064    /**
065     * @see org.kuali.rice.test.BaselineTestCase#setUp()
066     * @throws Exception
067     */
068    @Override
069    @Before
070    public void setUp() throws Exception {
071        super.setUp();
072        view.setViewHelperServiceClass(ViewHelperServiceImpl.class);
073    }
074
075    /**
076     * Test html content generation in a message
077     */
078    @Test
079    public void testHtmlContentGeneration() {
080        model = new SampleForm();
081        List<Component> components;
082
083        //single tag
084        generateAndSetMessage("[b]Message Content[/b]");
085        components = message.getMessageComponentStructure();
086        Assert.assertNotNull(components);
087        Assert.assertEquals(1, components.size());
088        Assert.assertTrue(components.get(0) instanceof Message);
089        Assert.assertFalse(((Message) components.get(0)).isRenderWrapperTag());
090        Assert.assertEquals("<b>Message Content</b>", ((Message) components.get(0)).getMessageText());
091
092        //nested tags
093        generateAndSetMessage("[span][b]Message Content[/b][/span]");
094        components = message.getMessageComponentStructure();
095        Assert.assertEquals(1, components.size());
096        Assert.assertEquals("<span><b>Message Content</b></span>", ((Message) components.get(0)).getMessageText());
097
098        //multiple tags
099        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&nbsp;<i>Message</i>&nbsp;Message</p>",
103                ((Message) components.get(0)).getMessageText());
104
105        //multiple tags with properties
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&nbsp;<a href='http://www.kuali.org'>Message</a>&nbsp;Message</p>",
112                ((Message) components.get(0)).getMessageText());
113    }
114
115    /**
116     * Test that the escape characters are properly replaced
117     */
118    @Test
119    public void testEscapeCharacter() {
120        model = new SampleForm();
121        List<Component> components;
122
123        //escape character
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     * Test link tag generation in a message
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&nbsp;<a href='http://www.kuali.org' target='_blank'>Link</a>&nbsp;text", messageText);
161    }
162
163    /**
164     * Test action link generation in a message
165     */
166    @Test
167    public void testActionGeneration() {
168        model = new SampleForm();
169        List<Component> components;
170
171        //methodToCall
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&nbsp;<a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,true,true,null); return false;\">action</a>&nbsp;text",
180                messageText);
181
182        //Other options
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&nbsp;<a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,false,true,null); return false;\">action</a>&nbsp;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&nbsp;<a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,false,true,null); return false;\">action</a>&nbsp;text",
201                messageText);
202
203        //ajax submit off
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&nbsp;<a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,true,false,null); return false;\">action</a>&nbsp;text",
212                messageText);
213
214        //ajax callback defined
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&nbsp;<a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',null,false,true,function(){console.log('success');}); return false;\">action</a>&nbsp;text",
224                messageText);
225
226        //data ajax
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&nbsp;<a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',{something: 'value', something2: 'value2'},true,true,null); return false;\">action</a>&nbsp;text",
236                messageText);
237
238        //data non-ajax
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&nbsp;<a href=\"javascript:void(null)\" onclick=\"submitForm('methodToCall',{something: 'value', something2: 'value2'},true,false,null); return false;\">action</a>&nbsp;text",
248                messageText);
249    }
250
251    /**
252     * Test color tag generation in a message
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&nbsp;<span style='color: blue;'>Color</span>&nbsp;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&nbsp;<span style='color: #FFFFFF;'>Color</span>&nbsp;text", messageText);
274    }
275
276    /**
277     * Test css tag generation in a message
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&nbsp;<span class='c1'>sample</span>&nbsp;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     * Test the ability to put components defined on the message inline by index number.
305     */
306    @Test
307    public void testInlineComponentGeneration() {
308        List<Component> components;
309
310        //One inline component
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&nbsp;", ((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("&nbsp;Message text", ((Message) components.get(2)).getMessageText());
325
326        //Two inline components with html content
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&nbsp;", ((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("&nbsp;Message&nbsp;<b>text&nbsp;", ((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("&nbsp;other</b>&nbsp;text</p>", ((Message) components.get(4)).getMessageText());
349
350        //inline components with changed properties
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&nbsp;", ((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("&nbsp;Message&nbsp;<b>text&nbsp;", ((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("&nbsp;other</b>&nbsp;text</p>", ((Message) components.get(4)).getMessageText());
376    }
377
378    /**
379     * Test the ability to put components inline by id
380     */
381    @Test
382    public void testIdComponentGeneration() {
383        List<Component> components;
384
385        //One inline component and id component
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&nbsp;", ((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("&nbsp;Message text&nbsp;", ((Message) components.get(2)).getMessageText());
400        Assert.assertTrue(components.get(3) instanceof Link);
401
402        //One inline component and id components
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&nbsp;", ((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("&nbsp;Message text&nbsp;", ((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     * Test a complex message which combines all the above tested functionality
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&nbsp;<a href='http://www.kuali.org' target='_blank'>link</a></span>&nbsp;",
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("&nbsp;<a href=\"javascript:void(null)\" "
452                + "onclick=\"submitForm('methodToCall',{key: 'value'},false,true,null); return false;\">"
453                + "action text</a>&nbsp;<span style='color: green;'>text&nbsp;", ((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("&nbsp;<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     * Test rich message options available on MultiValueControls
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&nbsp;<b>1</b></span>", ((Message) (richOptions.get(0)
495                .getMessage().getMessageComponentStructure().get(0))).getMessageText());
496        Assert.assertEquals("Option 2&nbsp;<a href='http://www.kuali.org' target='_blank'>link</a>",
497                ((Message) (richOptions.get(1).getMessage().getMessageComponentStructure().get(0))).getMessageText());
498        Assert.assertEquals("Other:&nbsp;", ((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:&nbsp;", ((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     * Test SPEL richMessages
510     */
511    @Test
512    public void testSPELRichMessages() {
513        /*  TODO Cannot figure out how to simulate correctly
514        model = new SampleForm();
515        view.setFormClass(SampleForm.class);
516        List<Component> components;
517        model.field1 = "value";
518        model.field2 = "[link='http://www.kuali.org']value2[/link]";
519        model.renderField = false;
520
521        //simple
522        generateAndSetMessage("Message @{field1} text");
523        components = message.getMessageComponentStructure();
524        Assert.assertNull(components);
525        Assert.assertTrue(message.isRenderWrapperTag());
526        Assert.assertEquals("Message value text", message.getMessageText());
527
528        //rich message wrapping
529        generateAndSetMessage("Message [b]@{field1}[/b] text");
530        components = message.getMessageComponentStructure();
531        Assert.assertNotNull(components);
532        Assert.assertEquals(1, components.size());
533        Assert.assertTrue(components.get(0) instanceof Message);
534        Assert.assertFalse(((Message) components.get(0)).isRenderWrapperTag());
535        Assert.assertEquals("Message <b>value</b> text", ((Message) components.get(0)).getMessageText());
536
537        //spel value contains rich content
538        generateAndSetMessage("Message @{field2} text");
539        components = message.getMessageComponentStructure();
540        Assert.assertNotNull(components);
541        Assert.assertEquals(1, components.size());
542        Assert.assertTrue(components.get(0) instanceof Message);
543        Assert.assertFalse(((Message) components.get(0)).isRenderWrapperTag());
544        Assert.assertEquals("Message <a href='http://www.kuali.org' target='_blank'>value2</a> text",
545                ((Message) components.get(0)).getMessageText());
546
547        //spel value setting richMessage component value
548        generateAndSetMessage("Message text [id=Uif-InputField render=@{renderField} propertyName=@{field1}]");
549        components = message.getMessageComponentStructure();
550        Assert.assertNotNull(components);
551        Assert.assertEquals(2, components.size());
552        Assert.assertTrue(components.get(0) instanceof Message);
553        Assert.assertFalse(((Message) components.get(0)).isRenderWrapperTag());
554        Assert.assertEquals("Message text ", ((Message) components.get(0)).getMessageText());
555        Assert.assertTrue(components.get(1) instanceof InputField);
556        Assert.assertFalse(((InputField) components.get(1)).isRender());
557        Assert.assertEquals("value", ((InputField) components.get(1)).getPropertyName());*/
558    }
559
560    /**
561     * Test basic message with no rich functionality
562     */
563    @Test
564    public void testBasicMessage() {
565        model = new SampleForm();
566        List<Component> components;
567
568        //single tag
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     * Peform a simulated lifecycle on the component passed in
578     *
579     * @param element
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     * Generate and setup a message to be used in testing
597     *
598     * @param messageText
599     */
600    private void generateAndSetMessage(String messageText) {
601        message = new Message();
602        message.setMessageText(messageText);
603        performSimulatedLifecycle(message);
604    }
605
606    /**
607     * Generate and setup a message to be used in testing which contains inline components
608     *
609     * @param messageText
610     * @param inlineComponents
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}