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.validator;
016    
017    import org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.form.FormComponentContributorContext;
020    import org.apache.tapestry.form.IFormComponent;
021    import org.apache.tapestry.form.ValidationMessages;
022    import org.apache.tapestry.json.JSONObject;
023    import org.apache.tapestry.valid.ValidationConstraint;
024    import org.apache.tapestry.valid.ValidationStrings;
025    import org.apache.tapestry.valid.ValidatorException;
026    import static org.easymock.EasyMock.expect;
027    import org.testng.annotations.Test;
028    
029    import java.text.DecimalFormatSymbols;
030    import java.util.Locale;
031    
032    /**
033     * Tests for {@link org.apache.tapestry.form.validator.Min}.
034     * 
035     * @author Howard Lewis Ship
036     * @since 4.0
037     */
038    @Test
039    public class TestMin extends BaseValidatorTestCase
040    {
041        public void test_OK() throws Exception
042        {
043            IFormComponent field = newField();
044            ValidationMessages messages = newMessages();
045    
046            Integer object = new Integer(10);
047    
048            replay();
049    
050            new Min("min=5").validate(field, messages, object);
051    
052            verify();
053        }
054    
055        public void test_Fail()
056        {
057            IFormComponent field = newField("My Field");
058            ValidationMessages messages = newMessages(
059                    null,
060                    ValidationStrings.VALUE_TOO_SMALL,
061                    new Object[]
062                    { "My Field", String.valueOf(new Double(10).doubleValue()) },
063                    "Exception!");
064    
065            expect(messages.getLocale()).andReturn(Locale.getDefault()).atLeastOnce();
066    
067            replay();
068    
069            try
070            {
071                new Min("min=10").validate(field, messages, new Integer(3));
072            }
073            catch (ValidatorException ex)
074            {
075                assertEquals("Exception!", ex.getMessage());
076                assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
077            }
078        }
079    
080        public void test_Fail_Custom_Message()
081        {
082            IFormComponent field = newField("My Field");
083            ValidationMessages messages = newMessages(
084                    "custom",
085                    ValidationStrings.VALUE_TOO_SMALL,
086                    new Object[]
087                    { "My Field", String.valueOf(new Double(10).doubleValue()) },
088                    "custom message");
089    
090            expect(messages.getLocale()).andReturn(Locale.getDefault()).atLeastOnce();
091    
092            replay();
093    
094            try
095            {
096                new Min("min=10,message=custom").validate(field, messages, new Integer(3));
097            }
098            catch (ValidatorException ex)
099            {
100                assertEquals(ex.getMessage(), "custom message");
101                assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
102            }
103        }
104    
105        public void test_Render_Contribution()
106        {
107            IMarkupWriter writer = newWriter();
108            IRequestCycle cycle = newCycle();
109            JSONObject json = new JSONObject();
110            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
111            IFormComponent field = newField("My Field", "myfield");
112    
113            Locale locale = Locale.FRANCE;
114            DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
115            
116            expect(context.getLocale()).andReturn(locale).atLeastOnce();
117            expect(context.getProfile()).andReturn(json);
118            
119            trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_SMALL, new Object[]
120            { "My Field", String.valueOf(new Double(20).doubleValue())}, "default message");
121            
122            replay();
123            
124            new Min("min=20").renderContribution(writer, cycle, context, field);
125            
126            verify();
127            
128            assertEquals(json.toString(), 
129                    "{\"constraints\":{\"myfield\":" +
130                    "[[tapestry.form.validation.greaterThanOrEqual,\"20.0\",{decimal:\",\"}]]}," +
131                    "\"myfield\":{\"constraints\":[\"default message\"]}}");
132        }
133        
134        public void test_Render_Contribution_Custom_Message()
135        {
136            IMarkupWriter writer = newWriter();
137            IRequestCycle cycle = newCycle();
138            JSONObject json = new JSONObject();
139            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
140            IFormComponent field = newField("My Field", "myfield");
141    
142            Locale locale = Locale.FRANCE;
143            DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
144            
145            expect(context.getLocale()).andReturn(locale).atLeastOnce();
146            expect(context.getProfile()).andReturn(json);
147            
148            trainFormatMessage(
149                    context,
150                    "custom",
151                    ValidationStrings.VALUE_TOO_SMALL,
152                    new Object[]
153                    { "My Field", String.valueOf(new Double(20).doubleValue()) },
154                    "custom\\message");
155            
156            replay();
157            
158            new Min("min=20,message=custom").renderContribution(writer, cycle, context, field);
159            
160            verify();
161            
162            assertEquals(json.toString(),
163                    "{\"constraints\":{\"myfield\":" +
164                    "[[tapestry.form.validation.greaterThanOrEqual,\"20.0\",{decimal:\",\"}]]}," +
165                    "\"myfield\":{\"constraints\":[\"custom\\\\message\"]}}");
166        }
167        
168    }