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.myfaces.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       * Test method for 'org.apache.myfaces.custom.stylesheet.StylesheetRenderer.encodeEnd(FacesContext, UIComponent)'
66       */
67      public void testInline() throws IOException
68      {
69          stylesheet.setInline(true);
70          stylesheet.setPath("/styles/test.css");
71          stylesheet.encodeEnd(facesContext);
72          facesContext.renderResponse();
73  
74          String output = writer.getWriter().toString();
75          
76          assertTrue("looking for a <style>", output.trim().startsWith("<style"));
77      }
78  
79      /*
80       * Test method for 'org.apache.myfaces.custom.stylesheet.StylesheetRenderer.encodeEnd(FacesContext, UIComponent)'
81       */
82      public void testInlineInvalid() throws IOException
83      {
84          stylesheet.setInline(true);
85          try
86          {
87              stylesheet.encodeEnd(facesContext);
88              fail("No exception was thrown for an invalid stylesheet path");
89          }
90          catch(FacesException e)
91          {
92              // expected
93          }
94      }
95  
96      public void testLink() throws IOException
97      {
98  
99          stylesheet.encodeEnd(facesContext);
100         facesContext.renderResponse();
101 
102         String output = writer.getWriter().toString();
103         
104         assertTrue("looking for a <link>", output.trim().startsWith("<link"));
105     }
106 
107 }