View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.test;
20  
21  import java.io.File;
22  import java.io.StringWriter;
23  
24  import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
25  import org.apache.myfaces.test.base.AbstractViewControllerTestCase;
26  import org.apache.myfaces.test.mock.MockResponseWriter;
27  import org.apache.myfaces.test.mock.resource.MockResourceHandler;
28  import org.apache.myfaces.test.utils.TestUtils;
29  
30  /**
31   * Abstract Shale Test base class, which sets up the JSF environment.  If the test
32   * overrides <code>setUp()</code> and/or <code>tearDown()</code>, then those
33   * methods but call the overwitten method to insure a valid test environment.
34   */
35  public class AbstractTomahawkViewControllerTestCase extends AbstractViewControllerTestCase
36  {
37      /** Response Writer */
38      private MockResponseWriter writer;
39      
40      //private MockResourceHandler resourceHandler;
41  
42      /**
43       * Construct a new instance of the test.
44       * 
45       * @param name Name of the test.
46       */
47      public AbstractTomahawkViewControllerTestCase(String name)
48      {
49          super(name);
50      }
51  
52      /**
53       *  Setup the test environment, including the following:
54       *  <ul>
55       *  <li>Set the Application Map.</li>
56       *  <li>Set a response writer</li>
57       *  <li>Add Tomahawk's renderers to the faces context.</li>
58       *  </ul> 
59       */
60      protected void setUp() throws Exception
61      {
62          super.setUp();
63          // additional setup not provided automatically by the shale mock stuff
64          facesContext.getExternalContext().getApplicationMap().put(
65                  MyfacesConfig.class.getName(), new MyfacesConfig());
66          writer = new MockResponseWriter(new StringWriter(), null, null);
67          facesContext.setResponseWriter(writer);
68          
69          //resourceHandler = new MockResourceHandler(new File(this.getClass().getName().replace('.', '/')));
70          //facesContext.getApplication().setResourceHandler(resourceHandler);
71          
72          TestUtils.addDefaultRenderers(facesContext);
73      }
74  
75      /**
76       * Tear down the test environment.
77       */
78      protected void tearDown() throws Exception
79      {
80          //resourceHandler = null;
81          writer = null;
82          super.tearDown();
83      }
84  
85      /**
86       * Verify the following:
87       * <ul>
88       * <li>id is not null</li>
89       * <li>Response is complete</li>
90       * <li>Responce contains the id</li>
91       * </ul>
92       * 
93       * @param id ID to verify
94       */
95      protected void assertIdExists(String id)
96      {
97          assertNotNull("ID is not null", id);
98          assertTrue("Response Complete", facesContext.getResponseComplete());
99          String output = writer.getWriter().toString();
100 //        System.out.println("Output = '" + output + "'");
101         assertNotNull("Has output", output);
102         assertTrue("Contains id '" + id + "'", output.indexOf(id) != -1);
103     }
104 
105 }