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 static org.easymock.EasyMock.checkOrder;
018    import static org.easymock.EasyMock.expect;
019    
020    import java.util.Date;
021    import java.util.Locale;
022    
023    import org.apache.tapestry.IMarkupWriter;
024    import org.apache.tapestry.IRequestCycle;
025    import org.apache.tapestry.form.FormComponentContributorContext;
026    import org.apache.tapestry.form.IFormComponent;
027    import org.apache.tapestry.form.TranslatedField;
028    import org.apache.tapestry.form.ValidationMessages;
029    import org.apache.tapestry.form.translator.DateTranslator;
030    import org.apache.tapestry.json.JSONObject;
031    import org.apache.tapestry.valid.ValidationConstraint;
032    import org.apache.tapestry.valid.ValidationStrings;
033    import org.apache.tapestry.valid.ValidatorException;
034    import org.testng.annotations.Test;
035    
036    /**
037     * Tests for {@link org.apache.tapestry.form.validator.MaxDate}
038     * 
039     * @author Howard Lewis Ship
040     * @since 4.0
041     */
042    @Test
043    public class TestMaxDate extends BaseValidatorTestCase
044    {
045        private static final long ONE_DAY = 24 * 60 * 60 * 1000l;
046        
047        public void testOK() throws Exception
048        {
049            long now = System.currentTimeMillis();
050    
051            Date today = new Date(now);
052            Date yesterday = new Date(now - ONE_DAY);
053    
054            IFormComponent field = newField();
055            ValidationMessages message = newMessages();
056    
057            replay();
058    
059            MaxDate v = new MaxDate();
060            v.setMaxDate(today);
061    
062            v.validate(field, message, yesterday);
063    
064            verify();
065        }
066    
067        public void testFail() throws Exception
068        {
069            long now = System.currentTimeMillis();
070    
071            Date today = new Date(now);
072            Date tomorrow = new Date(now + ONE_DAY);
073    
074            IFormComponent field = newField("Fred");
075            ValidationMessages message = newMessages(
076                    null,
077                    ValidationStrings.DATE_TOO_LATE,
078                    new Object[]
079                    { "Fred", today.toString() },
080                    "default message");
081    
082            replay();
083    
084            MaxDate v = new MaxDate();
085            v.setMaxDate(today);
086    
087            try
088            {
089                v.validate(field, message, tomorrow);
090                unreachable();
091            }
092            catch (ValidatorException ex)
093            {
094                assertEquals("default message", ex.getMessage());
095                assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint());
096            }
097    
098            verify();
099        }
100    
101        public void testFailCustomMessage() throws Exception
102        {
103            long now = System.currentTimeMillis();
104    
105            Date today = new Date(now);
106            Date tomorrow = new Date(now + ONE_DAY);
107    
108            IFormComponent field = newField("Fred");
109            ValidationMessages message = newMessages(
110                    "custom",
111                    ValidationStrings.DATE_TOO_LATE,
112                    new Object[]
113                    { "Fred", today.toString() },
114                    "custom message");
115    
116            replay();
117    
118            MaxDate v = new MaxDate("message=custom");
119            v.setMaxDate(today);
120    
121            try
122            {
123                v.validate(field, message, tomorrow);
124                unreachable();
125            }
126            catch (ValidatorException ex)
127            {
128                assertEquals("custom message", ex.getMessage());
129                assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint());
130            }
131    
132            verify();
133        }
134        
135        public void test_Render_Contribution()
136        {
137            IMarkupWriter writer = newWriter();
138            IRequestCycle cycle = newCycle();
139            JSONObject json = new JSONObject();
140            
141            TranslatedField field = newMock(TranslatedField.class);
142            checkOrder(field, false);
143            
144            Date maxDate = new Date(System.currentTimeMillis() + ONE_DAY);
145            DateTranslator translator = new DateTranslator();
146            
147            expect(field.getTranslator()).andReturn(translator);
148            
149            expect(field.getClientId()).andReturn("myfield").anyTimes();
150            
151            expect(field.getDisplayName()).andReturn("My Field");
152            
153            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
154            
155            Locale locale = Locale.ENGLISH;
156            expect(context.getLocale()).andReturn(locale).anyTimes();
157            
158            expect(context.getProfile()).andReturn(json);
159            
160            context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
161            
162            String strMax = translator.format(field, locale, maxDate);
163            
164            trainFormatMessage(context, null, ValidationStrings.DATE_TOO_LATE, 
165                    new Object[] { "My Field", strMax }, "default message");
166            
167            replay();
168            
169            new MaxDate("maxDate="+strMax).renderContribution(writer, cycle, context, field);
170            
171            verify();
172            
173            assertEquals(json.toString(),"{\"constraints\":{\"myfield\":[["
174                    + "tapestry.form.datetime.isValidDate,{max:\""
175                    + strMax + "\",datePattern:"
176                    + JSONObject.quote(translator.getPattern()) 
177                    + "}]]},"
178                    +"\"myfield\":{\"constraints\":[\"default message\"]}}");
179        }
180        
181        public void test_Render_Contribution_Custom_Message()
182        {
183            IMarkupWriter writer = newWriter();
184            IRequestCycle cycle = newCycle();
185            JSONObject json = new JSONObject();
186            
187            TranslatedField field = newMock(TranslatedField.class);
188            checkOrder(field, false);
189            
190            Date maxDate = new Date(System.currentTimeMillis() + ONE_DAY);
191            DateTranslator translator = new DateTranslator();
192            
193            expect(field.getTranslator()).andReturn(translator);
194            
195            expect(field.getClientId()).andReturn("myfield").anyTimes();
196            
197            expect(field.getDisplayName()).andReturn("My Field");
198            
199            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
200            
201            Locale locale = Locale.ENGLISH;
202            expect(context.getLocale()).andReturn(locale).anyTimes();
203            
204            expect(context.getProfile()).andReturn(json);
205            
206            context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
207            
208            String strMax = translator.format(field, locale, maxDate);
209            
210            trainFormatMessage(context, "custom", ValidationStrings.DATE_TOO_LATE, 
211                    new Object[] { "My Field", strMax }, 
212                    "custom\\message");
213            
214            replay();
215            
216            new MaxDate("maxDate=" + strMax + ",message=custom").renderContribution(writer, cycle, context, field);
217            
218            verify();
219            
220            assertEquals(json.toString(), "{\"constraints\":{\"myfield\":[["
221                    + "tapestry.form.datetime.isValidDate,{max:\""
222                    + strMax + "\",datePattern:"
223                    + JSONObject.quote(translator.getPattern()) 
224                    + "}]]},"
225                    + "\"myfield\":{\"constraints\":[\"custom\\\\message\"]}}");
226        }
227    }