2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.usecases.systest;
19  
20  import com.gargoylesoftware.htmlunit.html.HtmlElement;
21  import com.gargoylesoftware.htmlunit.html.HtmlForm;
22  import com.gargoylesoftware.htmlunit.html.HtmlHiddenInput;
23  import com.gargoylesoftware.htmlunit.html.HtmlPage;
24  import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
25  import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Locale;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  import org.apache.shale.test.cargo.CargoTestSetup;
33  
34  /***
35   * <p>Test case for the {@link Token} ViewController implementation.</p>
36   */
37  public class TokenTestCase extends AbstractTestCase {
38  
39  
40      // ------------------------------------------------------------ Constructors
41  
42  
43      // Construct a new instance of this test case.
44      public TokenTestCase(String name) {
45          super(name);
46      }
47  
48  
49      // ---------------------------------------------------- Overall Test Methods
50  
51  
52      // Set up instance variables required by this test case.
53      protected void setUp() throws Exception {
54  
55          super.setUp();
56          page("/token/test.faces");
57  
58      }
59  
60      // Return the tests included in this test case.
61      public static Test suite() {
62  
63          return new CargoTestSetup(new TestSuite(TokenTestCase.class));
64  
65      }
66  
67  
68      // Tear down instance variables required by this test case.
69      protected void tearDown() throws Exception {
70  
71          super.tearDown();
72  
73      }
74  
75  
76      // ------------------------------------------------------------ Test Methods
77  
78  
79      /***
80       * <p>Test that a double submit causes a validation error.</p>
81       */
82      public void testDouble() throws Exception {
83  
84          // Save the current page so we can resubmit it
85          HtmlPage save = this.page;
86          HtmlElement message = null;
87          String text = null;
88  
89          // setUp() should have put us on the page
90          assertEquals("Shale Token Test", title());
91  
92          // Fill in the required value and submit the form
93          HtmlTextInput value = (HtmlTextInput) element("form:value");
94          value.setValueAttribute("Value");
95          HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
96          submit(submit);
97  
98          // Restore the saved page and submit it again
99          this.page = save;
100         value = (HtmlTextInput) element("form:value");
101         value.setValueAttribute("Value");
102         submit = (HtmlSubmitInput) element("form:submit");
103         submit(submit);
104 
105         // Verify we stayed on the same page with appropriate error messages
106         assertEquals("Shale Token Test", title());
107         HtmlElement messages = element("form:messages");
108         assertNotNull(messages);
109         text = messages.asText();
110         assertTrue(text.indexOf("Invalid resubmit (summary)") != -1);
111         message = element("form:valueMessage");
112         assertNull(message);
113         message = element("form:tokenMessage");
114         assertNotNull(message);
115         assertTrue(message.asText().indexOf("Invalid resubmit (detail)") != -1);
116 
117     }
118 
119 
120     /***
121      * <p>Test the "normal" flow of submitting a correctly entered form.</p>
122      */
123     public void testNormal() throws Exception {
124 
125         // setUp() should have put us on the page
126         assertEquals("Shale Token Test", title());
127 
128         // Fill in the required value and submit the form
129         HtmlTextInput value = (HtmlTextInput) element("form:value");
130         value.setValueAttribute("Value");
131         HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
132         submit(submit);
133 
134         // Validate that we have returned to the main page
135         assertEquals("Shale Framework Use Cases", title());
136 
137     }
138 
139 
140     /***
141      * <p>Validate pristine instance of the "Token" test page.</p>
142      */
143     public void testPristine() throws Exception {
144 
145         HtmlForm form = null;
146         HtmlElement message = null;
147         HtmlElement messages = null;
148         HtmlHiddenInput token = null;
149         HtmlSubmitInput submit = null;
150         HtmlTextInput value = null;
151 
152         // setUp() should have put us on the page
153         assertEquals("Shale Token Test", title());
154         form = (HtmlForm) element("form");
155         assertNotNull(form);
156         messages = element("form:messages");
157         // MyFaces 1.1 and the JSF RI 1.1 are inconsistent about whether the
158         // "messages" element is actually created when there are no messages
159 //        assertNull(messages);
160         value = (HtmlTextInput) element("form:value");
161         assertNotNull(value);
162         assertEquals("", value.getValueAttribute());
163         message = element("form:valueMessage");
164         assertNull(message);
165         token = (HtmlHiddenInput) element("form:token");
166         assertNotNull(token);
167         assertNotNull(token.getValueAttribute());
168         message = element("form:tokenMessage");
169         assertNull(message);
170         submit = (HtmlSubmitInput) element("form:submit");
171         assertNotNull(submit);
172 
173     }
174 
175 
176     /***
177      * <p>Test that a validation error correctly stays on the same page
178      */
179     public void testValidate() throws Exception {
180 
181         // Save the current page so we can resubmit it
182         HtmlPage save = this.page;
183         HtmlElement message = null;
184         String text = null;
185 
186         // setUp() should have put us on the page
187         assertEquals("Shale Token Test", title());
188 
189         // Submit the form without setting the value
190         HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
191         submit(submit);
192 
193         // Verify we stayed on the same page with appropriate error messages
194         assertEquals("Shale Token Test", title());
195         HtmlElement messages = element("form:messages");
196         assertNotNull(messages);
197         // Cannot accurately test for implementation-defined message
198         message = element("form:valueMessage");
199         assertNotNull(message);
200         // Cannot accurately test for implementation-defined message
201         message = element("form:tokenMessage");
202         assertNull(message);
203 
204     }
205 
206 
207     /***
208      * <p>Test that fixing a validation error correctly allows the form
209      * to be submitted, but not resubmitted.</p>
210      */
211     public void testValidateRetry() throws Exception {
212 
213         // Reproduce the steps that testValidate() performs
214         testValidate();
215 
216         // Fix the validation error and submit the form
217         HtmlPage save = this.page;
218         HtmlTextInput value = (HtmlTextInput) element("form:value");
219         value.setValueAttribute("Value");
220         HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
221         submit(submit);
222 
223         // Verify that we were returned to the main menu
224         assertEquals("Shale Framework Use Cases", title());
225 
226         // Restore the saved page and submit it again
227         this.page = save;
228         value = (HtmlTextInput) element("form:value");
229         value.setValueAttribute("Value");
230         submit = (HtmlSubmitInput) element("form:submit");
231         submit(submit);
232 
233         // Verify we stayed on the same page with appropriate error messages
234         assertEquals("Shale Token Test", title());
235         HtmlElement messages = element("form:messages");
236         assertNotNull(messages);
237         String text = messages.asText();
238         assertTrue(text.indexOf("Invalid resubmit (summary)") != -1);
239         HtmlElement message = element("form:valueMessage");
240         assertNull(message);
241         message = element("form:tokenMessage");
242         assertNotNull(message);
243         assertTrue(message.asText().indexOf("Invalid resubmit (detail)") != -1);
244 
245     }
246 
247 
248 }