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.describe;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.Resource;
019    import org.apache.hivemind.impl.LocationImpl;
020    import org.apache.hivemind.util.URLResource;
021    import org.apache.tapestry.IMarkupWriter;
022    import org.apache.tapestry.IRequestCycle;
023    import static org.easymock.EasyMock.checkOrder;
024    import static org.easymock.EasyMock.expect;
025    import org.testng.annotations.Test;
026    
027    import java.net.URL;
028    
029    /**
030     * Tests for {@link org.apache.tapestry.describe.LocationRenderStrategy}.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    @Test
036    public class TestLocationRenderStrategy extends BaseDescribeTestCase
037    {
038        private Resource newResource(URL url)
039        {
040            Resource resource = newMock(Resource.class);
041            checkOrder(resource, false);
042            
043            expect(resource.getResourceURL()).andReturn(url);
044    
045            return resource;
046        }
047    
048        private Location newLocation(String file, int lineNumber)
049        {
050            URL url = getClass().getResource(file);
051    
052            Resource resource = new URLResource(url);
053    
054            return new LocationImpl(resource, lineNumber);
055        }
056    
057        private void train(IMarkupWriter writer, int startLine, int lineNumber, String[] lines)
058        {
059            writer.beginEmpty("br");
060            writer.begin("table");
061            writer.attribute("class", "location-content");
062            writer.attribute("cellspacing", "0");
063            writer.attribute("cellpadding", "0");
064    
065            for (int i = 0; i < lines.length; i++)
066            {
067                int currentLine = startLine + i;
068    
069                writer.begin("tr");
070    
071                if (currentLine == lineNumber)
072                    writer.attribute("class", "target-line");
073    
074                writer.begin("td");
075                writer.attribute("class", "line-number");
076                writer.print(currentLine);
077                writer.end();
078    
079                writer.begin("td");
080                writer.print(lines[i]);
081                writer.end("tr");
082                writer.println();
083            }
084    
085            writer.end("table");
086        }
087    
088        public void testNoLineNumber()
089        {
090            IMarkupWriter writer = newWriter();
091            IRequestCycle cycle = newCycle();
092            Location l = newLocation();
093    
094            writer.print(l.toString());
095            
096            expect(l.getLineNumber()).andReturn(0);
097            
098            replay();
099    
100            new LocationRenderStrategy().renderObject(l, writer, cycle);
101    
102            verify();
103        }
104    
105        public void testNoURL()
106        {
107            IMarkupWriter writer = newWriter();
108            IRequestCycle cycle = newCycle();
109            Resource resource = newResource(null);
110            Location l = fabricateLocation(99);
111    
112            expect(l.getResource()).andReturn(resource);
113    
114            writer.print(l.toString());
115    
116            replay();
117    
118            new LocationRenderStrategy().renderObject(l, writer, cycle);
119    
120            verify();
121        }
122    
123        /**
124         * Test when the highlight line is close to the end of the document.
125         */
126        public void testShortContent()
127        {
128            IMarkupWriter writer = newWriter();
129            IRequestCycle cycle = newCycle();
130            Location l = newLocation("Short.txt", 7);
131    
132            writer.print(l.toString());
133    
134            train(writer, 2, 7, new String[]
135            { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" });
136    
137            replay();
138    
139            new LocationRenderStrategy().renderObject(l, writer, cycle);
140    
141            verify();
142        }
143    
144        /**
145         * Test when the highlight line is close to the end of the document.
146         */
147        public void testLongContent()
148        {
149            IMarkupWriter writer = newWriter();
150            IRequestCycle cycle = newCycle();
151            Location l = newLocation("Long.txt", 3);
152    
153            writer.print(l.toString());
154    
155            train(writer, 1, 3, new String[]
156            { "Line One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight" });
157    
158            replay();
159    
160            new LocationRenderStrategy().renderObject(l, writer, cycle);
161    
162            verify();
163        }
164    }