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.html;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.tapestry.BaseComponentTestCase;
022    import org.apache.tapestry.IMarkupWriter;
023    import org.apache.tapestry.IRequestCycle;
024    import org.testng.annotations.Test;
025    
026    /**
027     * Tests for the {@link org.apache.tapestry.html.Relation}  component.
028     * 
029     * @author Andreas Andreou
030     * @since 4.1.1
031     */
032    @Test(sequential=true)
033    public class TestRelation extends BaseComponentTestCase
034    {
035        
036        /**
037         * Test that Relation does nothing when the entire page is rewinding
038         */
039    
040        public void testRewinding()
041        {
042            IMarkupWriter writer = newWriter();
043            IRequestCycle cycle = newCycle(true);
044            
045            Relation relation = newInstance(Relation.class, null);
046            
047            expect(cycle.renderStackPush(relation)).andReturn(relation);
048            
049            expect(cycle.renderStackPop()).andReturn(relation);
050            
051            replay();
052    
053            relation.render(writer, cycle);
054    
055            verify();
056        }
057        
058        /**
059         * Test that exception is thrown when Shell is missing
060         */    
061        public void testShellMissing()
062        {
063            IMarkupWriter writer = newWriter();
064            IRequestCycle cycle = newCycle(false);
065            Location componentLocation = newMock(Location.class);
066            
067            Relation relation = newInstance(Relation.class, 
068                    new Object[] {"location", componentLocation});
069            
070            expect(cycle.renderStackPush(relation)).andReturn(relation);
071            
072            trainGetShellFromCycle(cycle, null);
073    
074            expect(cycle.renderStackPop()).andReturn(relation);
075            
076            replay();
077            
078            try {
079                
080                relation.render(writer, cycle);
081                unreachable();
082                
083            } catch (ApplicationRuntimeException ex) {
084                
085                assertEquals(ex.getLocation(), componentLocation);
086            }
087    
088            verify();        
089        }
090        
091        /**
092         * Test that exception is thrown for invalid href parameter
093         */      
094        public void testInvalidHrefParameter()
095        {
096            IMarkupWriter writer = newWriter();
097            IRequestCycle cycle = newCycle(false);
098            Location componentLocation = newMock(Location.class);
099            
100            Relation relation = newInstance(Relation.class, 
101                    new Object[] {"location", componentLocation, "href", null});
102            
103            Shell shell = newInstance(Shell.class, null);
104            
105            expect(cycle.renderStackPush(relation)).andReturn(relation);
106            
107            trainGetShellFromCycle(cycle, shell);
108    
109            expect(cycle.renderStackPop()).andReturn(relation);
110            
111            replay();
112            
113            try {
114                
115                relation.render(writer, cycle);
116                unreachable();
117                
118            } catch (ApplicationRuntimeException ex) {
119                
120                assertEquals(ex.getLocation(), componentLocation);
121            }
122    
123            verify();         
124        }    
125        
126        protected void trainGetShellFromCycle(IRequestCycle cycle, Shell shell)
127        {
128            expect(cycle.getAttribute(Shell.SHELL_ATTRIBUTE)).andReturn(shell);
129        }    
130    }