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.expect;
018    
019    import org.apache.tapestry.IMarkupWriter;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.form.FormComponentContributorContext;
022    import org.apache.tapestry.form.IFormComponent;
023    import org.apache.tapestry.form.ValidationMessages;
024    import org.apache.tapestry.json.JSONObject;
025    import org.apache.tapestry.valid.ValidationConstraint;
026    import org.apache.tapestry.valid.ValidationStrings;
027    import org.apache.tapestry.valid.ValidatorException;
028    import org.testng.annotations.Test;
029    
030    @Test
031    public class TestMaxLength extends BaseValidatorTestCase
032    {
033    
034        public void testOK() throws Exception
035        {
036            IFormComponent field = newField();
037            ValidationMessages messages = newMessages();
038    
039            String object = "short and sweet";
040    
041            replay();
042    
043            new MaxLength("maxLength=50").validate(field, messages, object);
044    
045            verify();
046        }
047    
048        public void testFail()
049        {
050            IFormComponent field = newField("My Field");
051            ValidationMessages messages = newMessages(
052                    null,
053                    ValidationStrings.VALUE_TOO_LONG,
054                    new Object[]
055                    { new Integer(10), "My Field" },
056                    "Exception!");
057    
058            replay();
059    
060            try
061            {
062                new MaxLength("maxLength=10")
063                        .validate(field, messages, "brevity is the essence of wit");
064            }
065            catch (ValidatorException ex)
066            {
067                assertEquals("Exception!", ex.getMessage());
068                assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint());
069            }
070        }
071    
072        public void testFailCustomMessage()
073        {
074            IFormComponent field = newField("My Field");
075            ValidationMessages messages = newMessages(
076                    "Too Long",
077                    ValidationStrings.VALUE_TOO_LONG,
078                    new Object[]
079                    { new Integer(10), "My Field" },
080                    "Exception!");
081    
082            replay();
083    
084            try
085            {
086                new MaxLength("maxLength=10,message=Too Long").validate(
087                        field,
088                        messages,
089                        "this should be more than ten characters");
090            }
091            catch (ValidatorException ex)
092            {
093                assertEquals("Exception!", ex.getMessage());
094                assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint());
095            }
096        }
097    
098        public void test_Render_Contribution()
099        {
100            IMarkupWriter writer = newWriter();
101            IRequestCycle cycle = newCycle();
102            JSONObject json = new JSONObject();
103            
104            IFormComponent field = newField("My Field", "myfield");
105            
106            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
107            
108            expect(context.getProfile()).andReturn(json);
109            
110            trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_LONG, 
111                    new Object[] { new Integer(20), "My Field" }, "default\\message");
112            
113            replay();
114            
115            new MaxLength("maxLength=20").renderContribution(writer, cycle, context, field);
116            
117            verify();
118            
119            assertEquals("{\"constraints\":{\"myfield\":[[tapestry.form.validation.isText,{maxlength:20}]]},"
120                    +"\"myfield\":{\"constraints\":[\"default\\\\message\"]}}",
121                    json.toString());
122        }
123    }