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 java.util.List;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.Location;
023    import org.apache.tapestry.IBinding;
024    import org.apache.tapestry.IComponent;
025    import org.apache.tapestry.binding.BindingTestCase;
026    import org.apache.tapestry.coerce.ValueConverter;
027    import org.testng.annotations.Test;
028    
029    /**
030     * Tests for {@link org.apache.tapestry.form.validator.ValidatorsBinding} and
031     * {@link org.apache.tapestry.form.validator.ValidatorsBindingFactory}.
032     * 
033     * @author Howard Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class TestValidatorsBinding extends BindingTestCase
038    {
039        public void testSuccess()
040        {
041            IComponent component = newComponent();
042            Location l = newLocation();
043            List validators = newMock(List.class);
044            ValueConverter vc = newValueConverter();
045            
046            ValidatorFactory vf = newMock(ValidatorFactory.class);
047    
048            expect(vf.constructValidatorList(component, "required")).andReturn(validators);
049    
050            replay();
051    
052            ValidatorsBindingFactory factory = new ValidatorsBindingFactory();
053            factory.setValueConverter(vc);
054            factory.setValidatorFactory(vf);
055    
056            IBinding binding = factory.createBinding(component, "my desc", "required", l);
057    
058            assertSame(validators, binding.getObject());
059            assertSame(l, binding.getLocation());
060            assertEquals("my desc", binding.getDescription());
061    
062            verify();
063        }
064    
065        public void testFailure()
066        {
067            IComponent component = newComponent();
068            Throwable t = new RuntimeException("Boom!");
069            Location l = newLocation();
070    
071            ValueConverter vc = newValueConverter();
072    
073            ValidatorFactory vf = newMock(ValidatorFactory.class);
074    
075            expect(vf.constructValidatorList(component, "required")).andThrow(t);
076    
077            replay();
078    
079            ValidatorsBindingFactory factory = new ValidatorsBindingFactory();
080            factory.setValueConverter(vc);
081            factory.setValidatorFactory(vf);
082    
083            try
084            {
085                factory.createBinding(component, "my desc", "required", l);
086                unreachable();
087            }
088            catch (ApplicationRuntimeException ex)
089            {
090                assertEquals("Boom!", ex.getMessage());
091                assertSame(t, ex.getRootCause());
092                assertSame(l, ex.getLocation());
093            }
094    
095            verify();
096        }
097    }