KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > TestScriptParser


1 // Copyright 2004, 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.test;
16
17 import java.io.PrintWriter JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.ClassResolver;
22 import org.apache.hivemind.Resource;
23 import org.apache.hivemind.impl.DefaultClassResolver;
24 import org.apache.hivemind.util.ClasspathResource;
25 import org.apache.tapestry.junit.TapestryTestCase;
26 import org.apache.tapestry.test.mock.MockContext;
27 import org.apache.tapestry.test.mock.MockRequest;
28 import org.apache.tapestry.test.mock.MockResponse;
29 import org.apache.tapestry.util.xml.DocumentParseException;
30
31 /**
32  * Tests {@link org.apache.tapestry.test.IntegrationTestScriptParser}.
33  *
34  * @author Howard Lewis Ship
35  * @since 4.0
36  */

37 public class TestScriptParser extends TapestryTestCase
38 {
39     private ClassResolver _resolver = new DefaultClassResolver();
40
41     private ScriptDescriptor parse(String JavaDoc file) throws Exception JavaDoc
42     {
43         String JavaDoc basePath = "/" + getClass().getName().replace('.', '/');
44         Resource baseResource = new ClasspathResource(_resolver, basePath);
45
46         Resource fileResource = baseResource.getRelativeResource(file);
47
48         return new IntegrationTestScriptParser().parse(fileResource);
49     }
50
51     /**
52      * Test a script that contains just the <test-script> element and its attributes.
53      */

54     public void testEmpty() throws Exception JavaDoc
55     {
56         ScriptDescriptor sd = parse("Empty.xml");
57
58         assertNotNull(sd.getLocation());
59         assertEquals("test-context", sd.getContextName());
60         assertEquals("root", sd.getRootDirectory());
61     }
62
63     public void testEmptyMissingAttribute() throws Exception JavaDoc
64     {
65         try
66         {
67             parse("EmptyMissingAttribute.xml");
68             unreachable();
69         }
70         catch (DocumentParseException ex)
71         {
72             checkException(
73                     ex,
74                     "Required attribute 'context' is not supplied for element test-script.");
75         }
76     }
77
78     public void testEmptyUnknownAttribute() throws Exception JavaDoc
79     {
80         try
81         {
82             parse("EmptyUnknownAttribute.xml");
83             unreachable();
84         }
85         catch (DocumentParseException ex)
86         {
87             checkException(ex, "Unexpected attribute 'cntext' (in element test-script).");
88         }
89     }
90
91     public void testServlet() throws Exception JavaDoc
92     {
93         ScriptDescriptor sd = parse("Servlet.xml");
94
95         ServletDescriptor d = sd.getServletDescriptor("default");
96         assertEquals("default", d.getName());
97         assertEquals("MyClass", d.getClassName());
98
99         d = sd.getServletDescriptor("other");
100         assertEquals("other", d.getName());
101         assertEquals("org.apache.tapestry.ApplicationServlet", d.getClassName());
102
103         assertNull(sd.getServletDescriptor("unknown"));
104     }
105
106     public void testServletDuplicateName() throws Exception JavaDoc
107     {
108         try
109         {
110             parse("ServletDuplicateName.xml");
111             unreachable();
112         }
113         catch (ApplicationRuntimeException ex)
114         {
115             checkException(ex, "Servlet descriptor 'default'");
116             checkException(ex, "conflicts with prior instance");
117         }
118     }
119
120     public void testInitParameter() throws Exception JavaDoc
121     {
122         ScriptDescriptor sd = parse("InitParameter.xml");
123
124         ServletDescriptor s1 = sd.getServletDescriptor("default");
125
126         assertEquals("v1", s1.getInitParameter("p1"));
127         assertEquals("v2", s1.getInitParameter("p2"));
128
129         ServletDescriptor s2 = sd.getServletDescriptor("other");
130
131         assertEquals("v3", s2.getInitParameter("p3"));
132         assertNull(s2.getInitParameter("foo"));
133     }
134
135     public void testRequest() throws Exception JavaDoc
136     {
137         ScriptDescriptor sd = parse("Request.xml");
138
139         List JavaDoc l = sd.getRequestDescriptors();
140
141         assertEquals(2, l.size());
142
143         RequestDescriptor rd = (RequestDescriptor) l.get(0);
144
145         assertEquals("alpha", rd.getServletName());
146         assertEquals("/app", rd.getServletPath());
147
148         rd = (RequestDescriptor) l.get(1);
149
150         assertEquals("beta", rd.getServletName());
151         assertEquals("/beta", rd.getServletPath());
152     }
153
154     public void testRequestParameters() throws Exception JavaDoc
155     {
156         ScriptDescriptor sd = parse("RequestParameters.xml");
157
158         List JavaDoc l = sd.getRequestDescriptors();
159
160         assertEquals(2, l.size());
161
162         RequestDescriptor rd = (RequestDescriptor) l.get(0);
163
164         assertEquals("alpha", rd.getServletName());
165
166         assertListsEqual(new String JavaDoc[]
167         { "manchu" }, rd.getParameterValues("foo"));
168         assertListsEqual(new String JavaDoc[]
169         { "fred", "wilma" }, rd.getParameterValues("flintstone"));
170
171         rd = (RequestDescriptor) l.get(1);
172
173         assertNull(rd.getServletName());
174         assertListsEqual(new String JavaDoc[]
175         { "pebbles" }, rd.getParameterValues("flintstone"));
176     }
177
178     public void testAssertOutput() throws Exception JavaDoc
179     {
180         ScriptDescriptor sd = parse("AssertOutput.xml");
181
182         ScriptedTestSession ss = createSession();
183
184         RequestDescriptor rd = (RequestDescriptor) sd.getRequestDescriptors().get(0);
185
186         try
187         {
188             rd.executeAssertions(ss);
189             unreachable();
190         }
191         catch (ApplicationRuntimeException ex)
192         {
193             assertRegexp(
194                     "Expected text \\(\"<title>Away</title>\", at .*?\\) was not found in the response\\.",
195                     ex.getMessage());
196             assertNotNull(ex.getLocation());
197         }
198     }
199
200     public void testAssertRegexp() throws Exception JavaDoc
201     {
202         ScriptDescriptor sd = parse("AssertRegexp.xml");
203
204         ScriptedTestSession ss = createSession();
205
206         RequestDescriptor rd = (RequestDescriptor) sd.getRequestDescriptors().get(0);
207
208         try
209         {
210             rd.executeAssertions(ss);
211             unreachable();
212         }
213         catch (ApplicationRuntimeException ex)
214         {
215             assertRegexp(
216                     "Expected regular expression \\(\"<body>.*</body>\", at .*?\\) was not found in the response\\.",
217                     ex.getMessage());
218             assertNotNull(ex.getLocation());
219         }
220     }
221
222     public void testAssertRegexpMatch() throws Exception JavaDoc
223     {
224         ScriptDescriptor sd = parse("AssertRegexpMatch.xml");
225
226         ScriptedTestSession ss = createSession();
227
228         RequestDescriptor rd = (RequestDescriptor) sd.getRequestDescriptors().get(0);
229
230         try
231         {
232             rd.executeAssertions(ss);
233             unreachable();
234         }
235         catch (ApplicationRuntimeException ex)
236         {
237             assertRegexp(
238                     "Regular expression '<\\(\\.\\*\\?\\)>' \\(at .*?\\) should have generated 4 matches, but generated 2 instead\\.",
239                     ex.getMessage());
240             assertNotNull(ex.getLocation());
241         }
242     }
243
244     /**
245      * Kludges together a context, request and response as well as some simple text to run
246      * assertions against.
247      */

248     static ScriptedTestSession createSession() throws Exception JavaDoc
249     {
250         ScriptedTestSession result = new ScriptedTestSession(null);
251
252         MockContext context = new MockContext();
253         context.setServletContextName("context");
254         MockRequest request = new MockRequest(context, "/app");
255
256         MockResponse r = new MockResponse(request);
257         result.setResponse(r);
258
259         PrintWriter JavaDoc p = result.getResponse().getWriter();
260
261         p.println("Now is the time for all good men to come to the aid of their country.");
262         p.println("Here comes that text: <title>Test</title>");
263         p.println("That wasn't so bad?");
264
265         p.close();
266
267         return result;
268     }
269 }
Popular Tags