001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form;
016    
017    import static org.easymock.EasyMock.checkOrder;
018    import static org.easymock.EasyMock.expect;
019    
020    import java.util.Locale;
021    
022    import org.apache.hivemind.Messages;
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.apache.tapestry.IComponent;
025    import org.apache.tapestry.valid.ValidationStrings;
026    import org.testng.annotations.Test;
027    
028    /**
029     * Tests for {@link org.apache.tapestry.form.ValidationMessagesImpl}.
030     * 
031     * @author Howard Lewis Ship
032     * @since 4.0
033     */
034    @Test
035    public class TestValidationMessages extends BaseComponentTestCase
036    {
037        private IFormComponent newField()
038        {
039            return newMock(IFormComponent.class);
040        }
041    
042        public void testMessageOverrideNull()
043        {
044            IFormComponent field = newField();
045    
046            ValidationMessages m = new ValidationMessagesImpl(field, Locale.ENGLISH);
047    
048            replay();
049    
050            assertEquals("You must enter a value for My Field.", m.formatValidationMessage(
051                    null,
052                    ValidationStrings.REQUIRED_FIELD,
053                    new Object[]
054                    { "My Field" }));
055    
056            m = new ValidationMessagesImpl(field, new Locale("es"));
057    
058            assertEquals("Tiene que ingresar un valor para My Field.", m.formatValidationMessage(
059                    null,
060                    ValidationStrings.REQUIRED_FIELD,
061                    new Object[]
062                    { "My Field" }));
063    
064            verify();
065        }
066    
067        public void testGetLocale()
068        {
069            IFormComponent field = newField();
070    
071            ValidationMessages m = new ValidationMessagesImpl(field, Locale.ENGLISH);
072    
073            replay();
074    
075            assertSame(Locale.ENGLISH, m.getLocale());
076    
077            verify();
078        }
079    
080        public void testMessageOverride()
081        {
082            IFormComponent field = newField();
083    
084            ValidationMessages m = new ValidationMessagesImpl(field, Locale.ENGLISH);
085    
086            replay();
087    
088            assertEquals("Gimme data for My Field.", m.formatValidationMessage(
089                    "Gimme data for {0}.",
090                    ValidationStrings.REQUIRED_FIELD,
091                    new Object[]
092                    { "My Field" }));
093    
094            verify();
095        }
096    
097        /**
098         * Test the use of the '%key' construct as the message.
099         */
100    
101        public void testMessageOverrideAsReference()
102        {
103            Messages messages = newMessage("myfield-required", "Yo Dawg! Gimme a piece of {0}.");
104            
105            IComponent container = newComponent(messages);
106            
107            IFormComponent field = newField(container);
108            
109            ValidationMessages m = new ValidationMessagesImpl(field, Locale.ENGLISH);
110            
111            replay();
112    
113            assertEquals("Yo Dawg! Gimme a piece of My Field.", m.formatValidationMessage(
114                    "%myfield-required",
115                    ValidationStrings.REQUIRED_FIELD,
116                    new Object[]
117                    { "My Field" }));
118    
119            verify();
120        }
121    
122        private IFormComponent newField(IComponent container)
123        {
124            IFormComponent field = newMock(IFormComponent.class);
125    
126            expect(field.getContainer()).andReturn(container);
127    
128            return field;
129        }
130    
131        private IComponent newComponent(Messages messages)
132        {
133            IComponent component = newComponent();
134    
135            expect(component.getMessages()).andReturn(messages);
136    
137            return component;
138        }
139    
140        private Messages newMessage(String key, String message)
141        {
142            Messages messages = newMock(Messages.class);
143            checkOrder(messages, false);
144            
145            expect(messages.getMessage(key)).andReturn(message);
146    
147            return messages;
148        }
149    }