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  
20  package org.apache.myfaces.custom.stylesheet;
21  
22  import java.io.IOException;
23  import java.io.StringWriter;
24  
25  import javax.faces.FacesException;
26  
27  import junit.framework.Test;
28  
29  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
30  import org.apache.shale.test.mock.MockResponseWriter;
31  
32  public class StylesheetRendererTest extends AbstractTomahawkViewControllerTestCase
33  {
34  
35      private MockResponseWriter writer ;
36      private Stylesheet stylesheet ;
37      
38      public StylesheetRendererTest(String name)
39      {
40          super(name);
41      }
42      
43      protected void setUp() throws Exception
44      {
45          super.setUp();
46          stylesheet = new Stylesheet();
47          stylesheet.setPath("test.css");
48          stylesheet.setMedia("printer");
49          writer = new MockResponseWriter(new StringWriter(), null, null);
50          facesContext.setResponseWriter(writer);
51  //        facesContext.getRenderKit().addRenderer(
52  //                stylesheet.getFamily(), 
53  //                stylesheet.getRendererType(), 
54  //                new StylesheetRenderer());
55      }
56  
57      protected void tearDown() throws Exception
58      {
59          super.tearDown();
60          writer = null;
61          stylesheet = null;
62      }
63  
64      /*
65      public static Test suite()
66      {
67          return null; // keep this method or maven won't run it
68      }*/
69  
70      /*
71       * Test method for 'org.apache.myfaces.custom.stylesheet.StylesheetRenderer.encodeEnd(FacesContext, UIComponent)'
72       */
73      public void testInline() throws IOException
74      {
75          stylesheet.setInline(true);
76          stylesheet.setPath("/styles/test.css");
77          stylesheet.encodeEnd(facesContext);
78          facesContext.renderResponse();
79  
80          String output = writer.getWriter().toString();
81          
82          assertTrue("looking for a <style>", output.trim().startsWith("<style"));
83      }
84  
85      /*
86       * Test method for 'org.apache.myfaces.custom.stylesheet.StylesheetRenderer.encodeEnd(FacesContext, UIComponent)'
87       */
88      public void testInlineInvalid() throws IOException
89      {
90          stylesheet.setInline(true);
91          try
92          {
93              stylesheet.encodeEnd(facesContext);
94              fail("No exception was thrown for an invalid stylesheet path");
95          }
96          catch(FacesException e)
97          {
98              // expected
99          }
100     }
101 
102     public void testLink() throws IOException
103     {
104 
105         stylesheet.encodeEnd(facesContext);
106         facesContext.renderResponse();
107 
108         String output = writer.getWriter().toString();
109         
110         assertTrue("looking for a <link>", output.trim().startsWith("<link"));
111     }
112 
113 }