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;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.tapestry.spec.IContainedComponent;
019    import org.apache.tapestry.test.Creator;
020    import org.testng.annotations.Test;
021    
022    /**
023     * Tests a few new features of {@link org.apache.tapestry.AbstractComponent} added in release
024     * 4.0.
025     * 
026     * @author Howard M. Lewis Ship
027     * @since 4.0
028     */
029    @Test
030    public class TestAbstractComponent extends BaseComponentTestCase
031    {
032        private static class ConcreteComponent extends AbstractComponent
033        {
034    
035            protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
036            {
037            }
038            
039            public void setClientId(String id)
040            {
041            }
042            
043            public String getClientId()
044            {
045                return null;
046            }
047        }
048        
049        public void testUnimplementedMethods()
050        {
051            IComponent component = new ConcreteComponent();
052    
053            try
054            {
055                component.getMessages();
056                unreachable();
057            }
058            catch (IllegalStateException ex)
059            {
060                assertEquals(
061                        "Method getMessages() is not implemented. An implementation of this method should be provided via runtime class enhancement.",
062                        ex.getMessage());
063            }
064    
065            try
066            {
067                component.getSpecification();
068                unreachable();
069            }
070            catch (IllegalStateException ex)
071            {
072                assertEquals(TapestryMessages.providedByEnhancement("getSpecification"), ex
073                        .getMessage());
074            }
075        }
076        
077        public void testContainedComponent()
078        {
079            Creator creator = new Creator();
080    
081            IContainedComponent cc = newContainedComponent();
082    
083            replay();
084    
085            IComponent component = (IComponent) creator.newInstance(BaseComponent.class);
086    
087            component.setContainedComponent(cc);
088    
089            assertSame(cc, component.getContainedComponent());
090    
091            verify();
092        }
093        
094        public void testContainedComponentConflict()
095        {
096            Creator creator = new Creator();
097    
098            IContainedComponent cc1 = newContainedComponent();
099            IContainedComponent cc2 = newContainedComponent();
100    
101            IPage page = newPage("Fred");
102    
103            trainGetIdPath(page, null);
104            
105            replay();
106            
107            IComponent component = (IComponent) creator.newInstance(BaseComponent.class, new Object[]
108            { "page", page, "container", page, "id", "barney" });
109            
110            component.setContainedComponent(cc1);
111    
112            try
113            {
114                component.setContainedComponent(cc2);
115                unreachable();
116            }
117            catch (ApplicationRuntimeException ex)
118            {
119                assertEquals(
120                        "Attempt to change containedComponent property of component Fred/barney, which is not allowed.",
121                        ex.getMessage());
122            }
123    
124            verify();
125        }
126    
127        private IContainedComponent newContainedComponent()
128        {
129            return newMock(IContainedComponent.class);
130        }
131    }