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.testng.annotations.Test;
025    
026    /**
027     * Tests for {@link org.apache.tapestry.form.validator.AbstractValidatorWrapper}
028     * 
029     * @author Howard Lewis Ship
030     * @since 4.0
031     */
032    @Test
033    public class TestValidatorWrapper extends BaseValidatorTestCase
034    {
035        public static class Fixture extends AbstractValidatorWrapper
036        {
037            private final Validator _delegate;
038    
039            public Fixture(Validator delegate)
040            {
041                _delegate = delegate;
042            }
043    
044            protected Validator getDelegate()
045            {
046                return _delegate;
047            }
048        }
049    
050        private Validator newValidator()
051        {
052            return newMock(Validator.class);
053        }
054    
055        public void testValidate() throws Exception
056        {
057            IFormComponent field = newField();
058            ValidationMessages messages = newMessages();
059            Object value = new Object();
060    
061            Validator delegate = newValidator();
062    
063            delegate.validate(field, messages, value);
064    
065            replay();
066    
067            new Fixture(delegate).validate(field, messages, value);
068    
069            verify();
070        }
071    
072        public void testRenderContribution()
073        {
074            IMarkupWriter writer = newWriter();
075            IRequestCycle cycle = newCycle();
076            FormComponentContributorContext context = newContext();
077            IFormComponent field = newField();
078    
079            Validator delegate = newValidator();
080    
081            delegate.renderContribution(writer, cycle, context, field);
082    
083            replay();
084    
085            new Fixture(delegate).renderContribution(writer, cycle, context, field);
086    
087            verify();
088        }
089    
090        public void testGetAcceptsNull()
091        {
092            Validator delegate = newMock(Validator.class);
093    
094            expect(delegate.getAcceptsNull()).andReturn(true);
095    
096            replay();
097    
098            Validator wrapper = new Fixture(delegate);
099    
100            assertEquals(true, wrapper.getAcceptsNull());
101    
102            verify();
103    
104            expect(delegate.getAcceptsNull()).andReturn(false);
105    
106            replay();
107    
108            assertEquals(false, wrapper.getAcceptsNull());
109    
110            verify();
111        }
112    
113        public void testIsRequired()
114        {
115            Validator delegate = newMock(Validator.class);
116    
117            expect(delegate.isRequired()).andReturn(true);
118    
119            replay();
120    
121            Validator wrapper = new Fixture(delegate);
122    
123            assertEquals(true, wrapper.isRequired());
124    
125            verify();
126    
127            expect(delegate.isRequired()).andReturn(false);
128    
129            replay();
130    
131            assertEquals(false, wrapper.isRequired());
132    
133            verify();
134        }
135    }