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.bean;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.testng.annotations.Test;
021    
022    /**
023     * Tests for {@link org.apache.tapestry.bean.LightweightBeanInitializer}.
024     * 
025     * @author Howard M. Lewis Ship
026     * @since 4.0
027     */
028    @Test
029    public class TestLightweightBeanInitializer extends BaseComponentTestCase
030    {
031        public void testSuccess()
032        {
033            String initializer = "required,minLength=10";
034            TargetBean bean = new TargetBean();
035    
036            assertEquals(false, bean.isRequired());
037            assertEquals(0, bean.getMinLength());
038    
039            IBeanInitializer bi = new LightweightBeanInitializer(initializer);
040    
041            assertEquals(initializer, bi.getPropertyName());
042    
043            bi.setBeanProperty(null, bean);
044    
045            assertEquals(true, bean.isRequired());
046            assertEquals(10, bean.getMinLength());
047        }
048    
049        public void testFailure()
050        {
051            Location l = newLocation();
052    
053            IBeanInitializer bi = new LightweightBeanInitializer("zip=zap");
054            bi.setLocation(l);
055    
056            TargetBean bean = new TargetBean();
057    
058            try
059            {
060                bi.setBeanProperty(null, bean);
061                unreachable();
062            }
063            catch (ApplicationRuntimeException ex)
064            {
065                assertEquals("Class org.apache.tapestry.bean.TargetBean "
066                        + "does not contain a property named 'zip'.", ex.getMessage());
067                assertSame(l, ex.getLocation());
068            }
069        }
070    }