001    // Copyright Aug 6, 2006 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    package org.apache.tapestry.contrib.palette;
015    
016    import static org.easymock.EasyMock.aryEq;
017    import static org.easymock.EasyMock.eq;
018    import static org.easymock.EasyMock.expect;
019    
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.IMarkupWriter;
022    import org.apache.tapestry.IRequestCycle;
023    import org.apache.tapestry.form.FormComponentContributorContext;
024    import org.apache.tapestry.form.ValidatableFieldExtension;
025    import org.apache.tapestry.form.ValidatableFieldSupport;
026    import org.apache.tapestry.form.validator.Required;
027    import org.apache.tapestry.json.JSONObject;
028    import org.apache.tapestry.valid.ValidationConstants;
029    import org.apache.tapestry.valid.ValidationStrings;
030    import org.testng.annotations.Test;
031    
032    
033    /**
034     * Tests functionality of {@link Palette} and {@link ValidatableFieldExtension} 
035     * contributions.
036     * 
037     * @author jkuhnert
038     */
039    @Test
040    public class PaletteValidationTest extends BaseComponentTestCase
041    {
042    
043        public void test_Palette_Required()
044        {
045            ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class);
046            IRequestCycle cycle = newCycle();
047            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
048            IMarkupWriter writer = newWriter();
049            
050            JSONObject profile = new JSONObject();
051            Required required = new Required("message=Field {0} is required.");
052            
053            Palette component = newInstance(Palette.class, 
054                    new Object[] { 
055                "validatableFieldSupport", vfs, 
056                "clientId", "pal", "displayName", "Pally"});
057            
058            expect(context.getProfile()).andReturn(profile);
059            
060            expect(context.formatValidationMessage(eq(required.getMessage()), eq(ValidationStrings.REQUIRED_FIELD), 
061                    aryEq(new Object[] { "Pally" }) )).andReturn("Pally is required.");
062            
063            replay();
064            
065            assert component.overrideValidator(required, cycle);
066            
067            component.overrideContributions(required, context, writer, cycle);
068            
069            verify();
070            
071            assert profile.has(ValidationConstants.CONSTRAINTS);
072            
073            Object literal = profile.getJSONObject(ValidationConstants.CONSTRAINTS).get(component.getClientId());
074            
075            assert literal != null;
076            
077            assertEquals(literal.toString(),"[[tapestry.form.validation.isPalleteSelected]]");
078        }
079    }