KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > describe > TestLocationRenderStrategy


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.describe;
16
17 import java.net.URL JavaDoc;
18
19 import org.apache.hivemind.Location;
20 import org.apache.hivemind.Resource;
21 import org.apache.hivemind.impl.LocationImpl;
22 import org.apache.hivemind.util.URLResource;
23 import org.apache.tapestry.IMarkupWriter;
24 import org.apache.tapestry.IRequestCycle;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.describe.LocationRenderStrategy}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestLocationRenderStrategy extends BaseDescribeTestCase
34 {
35     private Resource newResource(URL JavaDoc url)
36     {
37         MockControl control = newControl(Resource.class);
38         Resource resource = (Resource) control.getMock();
39
40         resource.getResourceURL();
41         control.setReturnValue(url);
42
43         return resource;
44     }
45
46     private Location newLocation(String JavaDoc file, int lineNumber)
47     {
48         URL JavaDoc url = getClass().getResource(file);
49
50         Resource resource = new URLResource(url);
51
52         return new LocationImpl(resource, lineNumber);
53     }
54
55     private void train(IMarkupWriter writer, int startLine, int lineNumber, String JavaDoc[] lines)
56     {
57         writer.beginEmpty("br");
58         writer.begin("table");
59         writer.attribute("class", "location-content");
60
61         for (int i = 0; i < lines.length; i++)
62         {
63             int currentLine = startLine + i;
64
65             writer.begin("tr");
66
67             if (currentLine == lineNumber)
68                 writer.attribute("class", "target-line");
69
70             writer.begin("td");
71             writer.attribute("class", "line-number");
72             writer.print(currentLine);
73             writer.end();
74
75             writer.begin("td");
76             writer.print(lines[i]);
77             writer.end("tr");
78             writer.println();
79         }
80
81         writer.end("table");
82     }
83
84     public void testNoLineNumber()
85     {
86         IMarkupWriter writer = newWriter();
87         IRequestCycle cycle = newCycle();
88         MockControl lc = newControl(Location.class);
89         Location l = (Location) lc.getMock();
90
91         l.getLineNumber();
92         lc.setReturnValue(0);
93
94         writer.print(l.toString());
95
96         replayControls();
97
98         new LocationRenderStrategy().renderObject(l, writer, cycle);
99
100         verifyControls();
101     }
102
103     public void testNoURL()
104     {
105         IMarkupWriter writer = newWriter();
106         IRequestCycle cycle = newCycle();
107         Resource resource = newResource(null);
108         MockControl lc = newControl(Location.class);
109         Location l = (Location) lc.getMock();
110
111         l.getLineNumber();
112         lc.setReturnValue(99);
113
114         l.getResource();
115         lc.setReturnValue(resource);
116
117         writer.print(l.toString());
118
119         replayControls();
120
121         new LocationRenderStrategy().renderObject(l, writer, cycle);
122
123         verifyControls();
124     }
125
126     /**
127      * Test when the highlight line is close to the end of the document.
128      */

129     public void testShortContent()
130     {
131         IMarkupWriter writer = newWriter();
132         IRequestCycle cycle = newCycle();
133         Location l = newLocation("Short.txt", 7);
134
135         writer.print(l.toString());
136
137         train(writer, 2, 7, new String JavaDoc[]
138         { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" });
139
140         replayControls();
141
142         new LocationRenderStrategy().renderObject(l, writer, cycle);
143
144         verifyControls();
145     }
146
147     /**
148      * Test when the highlight line is close to the end of the document.
149      */

150     public void testLongContent()
151     {
152         IMarkupWriter writer = newWriter();
153         IRequestCycle cycle = newCycle();
154         Location l = newLocation("Long.txt", 3);
155
156         writer.print(l.toString());
157
158         train(writer, 1, 3, new String JavaDoc[]
159         { "Line One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight" });
160
161         replayControls();
162
163         new LocationRenderStrategy().renderObject(l, writer, cycle);
164
165         verifyControls();
166     }
167 }
Popular Tags