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