KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > faces > sysclient > SimpleTestCase


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

16
17 package org.apache.struts.faces.sysclient;
18
19 import java.util.Iterator JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import com.gargoylesoftware.htmlunit.html.HtmlBase;
25 import com.gargoylesoftware.htmlunit.html.HtmlElement;
26 import com.gargoylesoftware.htmlunit.html.HtmlLink;
27 import com.gargoylesoftware.htmlunit.html.HtmlSpan;
28
29
30 /**
31  * <p>Test case for a simple Struts-Faces page that is statically examined
32  * to ensure that all of the expected elements have been rendered correctly.</p>
33  *
34  * @version $Rev: 165410 $ $Date: 2005-04-30 17:25:14 +0100 (Sat, 30 Apr 2005) $
35  */

36
37 public class SimpleTestCase extends AbstractTestCase {
38
39
40     // ------------------------------------------------------------ Constructors
41

42
43     /**
44      * <p>Construct a new instance of this test case.</p>
45      *
46      * @param name Name of the new test case
47      */

48     public SimpleTestCase(String JavaDoc name) {
49
50         super(name);
51
52     }
53
54
55     // ------------------------------------------------------ Instance Variables
56

57
58     // ------------------------------------------------------ Test Setup Methods
59

60
61     /**
62      * <p>Set up the instance variables required for this test case.</p>
63      */

64     public void setUp() throws Exception JavaDoc {
65
66         super.setUp();
67         page("/simple.faces");
68
69     }
70
71
72     /**
73      * <p>Return the set of tests included in this test suite.</p>
74      */

75     public static Test suite() {
76
77         return (new TestSuite(SimpleTestCase.class));
78
79     }
80
81
82     /**
83      * <p>Tear down instance variables required by this test case.</p>
84      */

85     public void tearDown() {
86
87         super.tearDown();
88
89     }
90
91
92
93     // ------------------------------------------------- Individual Test Methods
94

95
96     /**
97      * <p>Verify the presence and contents of a base element.</p>
98      */

99     public void testBase() throws Exception JavaDoc {
100
101         HtmlBase base = null;
102         Iterator JavaDoc kids = head().getChildIterator();
103         while (kids.hasNext()) {
104             HtmlElement kid = (HtmlElement) kids.next();
105             if (kid instanceof HtmlBase) {
106                 assertNull("Only one base element present", base);
107                 base = (HtmlBase) kid;
108             }
109         }
110         assertNotNull("Exactly one base element present", base);
111         assertEquals("base", base.getTagName());
112         assertEquals(url("/simple.jsp").toString(), base.getHrefAttribute());
113         assertEquals("", base.getTargetAttribute());
114
115     }
116
117
118     /**
119      * <p>Verify the presence and contents of an html element.</p>
120      */

121     public void testHtml() throws Exception JavaDoc {
122
123         HtmlElement html = page.getDocumentElement();
124         assertEquals("html", html.getTagName());
125         assertEquals("http://www.w3.org/1999/xhtml", html.getAttributeValue("xmlns"));
126         // TODO: verify the "lang" attribute
127
// TODO: verify the "xml:lang" attribute
128

129     }
130
131
132     /**
133      * <p>Verify that the loadMessages tag properly exposes a Struts
134      * MessageResources instance as a Map.</p>
135      */

136     public void testLoadMessages() throws Exception JavaDoc {
137
138         HtmlSpan span = null;
139
140         span = (HtmlSpan) element("lookup-simple");
141         assertNotNull(span);
142         assertEquals("Resource Simple Text", span.asText());
143
144         span = (HtmlSpan) element("lookup-filtered");
145         assertNotNull(span);
146         assertEquals("Resource <b>Filtered</b> Text", span.asText());
147
148         span = (HtmlSpan) element("lookup-unfiltered");
149         assertNotNull(span);
150         assertEquals("Resource Unfiltered Text", span.asText());
151
152     }
153
154
155     /**
156      * <p>Verify the presence and contents of several message components.</p>
157      */

158     public void testMessage() throws Exception JavaDoc {
159
160         HtmlSpan span = null;
161
162         span = (HtmlSpan) element("message-direct");
163         assertNotNull(span);
164         assertEquals("Resource Simple Text", span.asText());
165
166         span = (HtmlSpan) element("message-indirect");
167         assertNotNull(span);
168         assertEquals("Resource Simple Text", span.asText());
169
170         span = (HtmlSpan) element("message-filtered");
171         assertNotNull(span);
172         assertEquals("Resource <b>Filtered</b> Text", span.asText());
173
174         span = (HtmlSpan) element("message-unfiltered");
175         assertNotNull(span);
176         assertEquals("Resource Unfiltered Text", span.asText());
177
178         span = (HtmlSpan) element("message-substitute");
179         assertNotNull(span);
180         assertEquals("From Here to Eternity", span.asText());
181
182     }
183
184
185     /**
186      * <p>Verify the presence and contents of a stylesheet element.</p>
187      */

188     public void testStylesheet() throws Exception JavaDoc {
189
190         HtmlLink link = null;
191         Iterator JavaDoc kids = head().getChildElementsIterator();
192         while (kids.hasNext()) {
193             HtmlElement kid = (HtmlElement) kids.next();
194             if (kid instanceof HtmlLink) {
195                 assertNull("Only one stylesheet element present", link);
196                 link = (HtmlLink) kid;
197             }
198         }
199         assertNotNull("Exactly one stylesheet element present", link);
200         assertEquals("link", link.getTagName());
201         assertEquals("", link.getCharsetAttribute());
202         String JavaDoc url = this.url.toString();
203         url = url.substring(0, url.length() - 1);
204         url = url.substring(url.lastIndexOf('/'));
205         assertEquals(url + "/stylesheet.css", link.getHrefAttribute());
206         assertEquals("", link.getHrefLangAttribute());
207         assertEquals("", link.getMediaAttribute());
208         assertEquals("stylesheet", link.getRelAttribute());
209         assertEquals("", link.getRevAttribute());
210         assertEquals("", link.getTargetAttribute());
211         assertEquals("text/css", link.getTypeAttribute());
212         
213     }
214
215
216     /**
217      * <p>Verify the proper operation of clientId in a subview.</p.
218      */

219     public void testSubview() throws Exception JavaDoc {
220
221         HtmlSpan span = null;
222
223         span = (HtmlSpan) element("subview:write1");
224         assertNotNull(span);
225         assertEquals("subview:write1", span.asText());
226
227     }
228
229
230     /**
231      * <p>Verify the title of the returned page.</p>
232      */

233     public void testTitle() throws Exception JavaDoc {
234
235         assertEquals("simple", title());
236
237     }
238
239
240     /**
241      * <p>Verify the presence and contents of several write components.</p>
242      */

243     public void testWrite() throws Exception JavaDoc {
244
245         HtmlSpan span = null;
246
247         span = (HtmlSpan) element("write-literal");
248         assertNotNull(span);
249         assertEquals("Literal Write Content", span.asText());
250
251         span = (HtmlSpan) element("write-filtered");
252         assertNotNull(span);
253         assertEquals("Literal <b>Filtered</b> Content", span.asText());
254
255         span = (HtmlSpan) element("write-unfiltered");
256         assertNotNull(span);
257         assertEquals("Literal Unfiltered Content", span.asText());
258
259         span = (HtmlSpan) element("retrieved-literal");
260         assertNotNull(span);
261         assertEquals("Retrieved Simple Content", span.asText());
262
263         span = (HtmlSpan) element("retrieved-filtered");
264         assertNotNull(span);
265         assertEquals("Retrieved <b>Filtered</b> Content", span.asText());
266
267         span = (HtmlSpan) element("retrieved-unfiltered");
268         assertNotNull(span);
269         assertEquals("Retrieved Unfiltered Content", span.asText());
270
271     }
272
273
274 }
275
Popular Tags