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    /**
031     * Tests for {@link org.apache.tapestry.form.validator.MinLength}.
032     * 
033     * @author Howard Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class TestMinLength extends BaseValidatorTestCase
038    {
039        public void testOK() throws Exception
040        {
041            IFormComponent field = newField();
042            ValidationMessages messages = newMessages();
043    
044            String object = "a nice long string";
045    
046            replay();
047    
048            new MinLength("minLength=5").validate(field, messages, object);
049    
050            verify();
051        }
052    
053        public void testFail()
054        {
055            IFormComponent field = newField("My Field");
056            ValidationMessages messages = newMessages(
057                    null,
058                    ValidationStrings.VALUE_TOO_SHORT,
059                    new Object[]
060                    { new Integer(10), "My Field" },
061                    "Exception!");
062    
063            replay();
064    
065            try
066            {
067                new MinLength("minLength=10").validate(field, messages, "short");
068            }
069            catch (ValidatorException ex)
070            {
071                assertEquals("Exception!", ex.getMessage());
072                assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
073            }
074        }
075    
076        public void testFailCustomMessage()
077        {
078            IFormComponent field = newField("My Field");
079            ValidationMessages messages = newMessages(
080                    "Too Short",
081                    ValidationStrings.VALUE_TOO_SHORT,
082                    new Object[]
083                    { new Integer(10), "My Field" },
084                    "Exception!");
085    
086            replay();
087    
088            try
089            {
090                new MinLength("minLength=10,message=Too Short").validate(field, messages, "short");
091            }
092            catch (ValidatorException ex)
093            {
094                assertEquals("Exception!", ex.getMessage());
095                assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
096            }
097        }
098    
099        public void test_Render_Contribution()
100        {
101            IMarkupWriter writer = newWriter();
102            IRequestCycle cycle = newCycle();
103            JSONObject json = new JSONObject();
104            
105            IFormComponent field = newField("My Field", "myfield");
106            
107            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
108            
109            expect(context.getProfile()).andReturn(json);
110            
111            trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_SHORT, 
112                    new Object[] { new Integer(25), "My Field" }, "default\\message");
113            
114            replay();
115            
116            new MinLength("minLength=25").renderContribution(writer, cycle, context, field);
117            
118            verify();
119            
120            assertEquals("{\"constraints\":{\"myfield\":[[tapestry.form.validation.isText,{minlength:25}]]},"
121                    +"\"myfield\":{\"constraints\":[\"default\\\\message\"]}}",
122                    json.toString());
123        }
124        
125        public void test_Render_Contribution_Custom_Message()
126        {
127            IMarkupWriter writer = newWriter();
128            IRequestCycle cycle = newCycle();
129            JSONObject json = new JSONObject();
130            
131            IFormComponent field = newField("My Field", "customField");
132            
133            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
134    
135            expect(context.getProfile()).andReturn(json);
136            
137            trainFormatMessage(
138                    context,
139                    "custom",
140                    ValidationStrings.VALUE_TOO_SHORT,
141                    new Object[] { new Integer(25), "My Field" },
142                    "custom\\message");
143            
144            replay();
145            
146            new MinLength("minLength=25,message=custom").renderContribution(
147                    writer,
148                    cycle,
149                    context,
150                    field);
151            
152            verify();
153            
154            assertEquals("{\"constraints\":{\"customField\":[[tapestry.form.validation.isText,{minlength:25}]]},"
155                    + "\"customField\":{\"constraints\":[\"custom\\\\message\"]}}",
156                    json.toString());
157        }
158        
159        public void testNotRequired()
160        {
161            assertEquals(false, new MinLength().isRequired());
162        }
163    }