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.StringWriter;
22  
23  import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
24  import org.apache.myfaces.test.utils.TestUtils;
25  import org.apache.shale.test.base.AbstractViewControllerTestCase;
26  import org.apache.shale.test.mock.MockResponseWriter;
27  
28  /**
29   * Abstract Shale Test base class, which sets up the JSF environment.  If the test
30   * overrides <code>setUp()</code> and/or <code>tearDown()</code>, then those
31   * methods but call the overwitten method to insure a valid test environment.
32   */
33  public class AbstractTomahawkViewControllerTestCase extends AbstractViewControllerTestCase
34  {
35      /** Response Writer */
36      private MockResponseWriter writer;
37  
38      /**
39       * Construct a new instance of the test.
40       * 
41       * @param name Name of the test.
42       */
43      public AbstractTomahawkViewControllerTestCase(String name)
44      {
45          super(name);
46      }
47  
48      /**
49       *  Setup the test environment, including the following:
50       *  <ul>
51       *  <li>Set the Application Map.</li>
52       *  <li>Set a response writer</li>
53       *  <li>Add Tomahawk's renderers to the faces context.</li>
54       *  </ul> 
55       */
56      protected void setUp() throws Exception
57      {
58          super.setUp();
59          // additional setup not provided automatically by the shale mock stuff
60          facesContext.getExternalContext().getApplicationMap().put(
61                  MyfacesConfig.class.getName(), new MyfacesConfig());
62          writer = new MockResponseWriter(new StringWriter(), null, null);
63          facesContext.setResponseWriter(writer);
64  
65          TestUtils.addDefaultRenderers(facesContext);
66      }
67  
68      /**
69       * Tear down the test environment.
70       */
71      protected void tearDown() throws Exception
72      {
73          super.tearDown();
74      }
75  
76      /**
77       * Verify the following:
78       * <ul>
79       * <li>id is not null</li>
80       * <li>Response is complete</li>
81       * <li>Responce contains the id</li>
82       * </ul>
83       * 
84       * @param id ID to verify
85       */
86      protected void assertIdExists(String id)
87      {
88          assertNotNull("ID is not null", id);
89          assertTrue("Response Complete", facesContext.getResponseComplete());
90          String output = writer.getWriter().toString();
91  //        System.out.println("Output = '" + output + "'");
92          assertNotNull("Has output", output);
93          assertTrue("Contains id '" + id + "'", output.indexOf(id) != -1);
94      }
95  
96  }