View Javadoc

1   /* Licensed to the Apache Software Foundation (ASF) under one or more
2    * contributor license agreements.  See the NOTICE file distributed with
3    * this work for additional information regarding copyright ownership.
4    * The ASF licenses this file to you under the Apache License, Version 2.0
5    * (the "License"); you may not use this file except in compliance with
6    * the License.  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15  */
16  package org.apache.myfaces.context;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.io.PrintWriter;
22  
23  import javax.faces.FactoryFinder;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  import javax.servlet.ServletOutputStream;
27  import javax.servlet.ServletResponse;
28  import javax.servlet.ServletResponseWrapper;
29  
30  import org.apache.myfaces.context.servlet.FacesContextImpl;
31  import org.apache.myfaces.test.base.AbstractJsfTestCase;
32  import org.apache.myfaces.test.mock.MockResponseWriter;
33  
34  /**
35   * Testcase for the response switching
36   *
37   * @author Werner Punz(latest modification by $Author: bommel $)
38   * @version $Revision: 1187701 $ $Date: 2011-10-22 07:21:54 -0500 (Sat, 22 Oct 2011) $
39   */
40  public class ResponseWrapperSwitchTest extends AbstractJsfTestCase {
41  
42      public ResponseWrapperSwitchTest() {
43          super("ResponseWrapperSwitchTest");
44      }
45  
46      @Override
47      protected void setUp() throws Exception {
48          super.setUp();
49          FactoryFinder.setFactory (FactoryFinder.EXCEPTION_HANDLER_FACTORY,
50          "org.apache.myfaces.context.ExceptionHandlerFactoryImpl");
51      }
52  
53      @Override
54      protected void tearDown() throws Exception {
55          super.tearDown();
56      }
57  
58      /**
59       * we define our own response class to be able to test the output suppression!
60       */
61      class NewStreamingMockResponse extends ServletResponseWrapper {
62  
63          PrintWriter _writer;
64          ServletOutputStream _strm;
65          ServletResponse _response;
66  
67          public NewStreamingMockResponse(ServletResponse response, ServletOutputStream strm, PrintWriter writer) {
68              super(response);
69              _strm = strm;
70              _writer = writer;
71              _response = response;
72          }
73  
74          @Override
75          public ServletResponse getResponse() {
76              return _response;
77          }
78  
79          @Override
80          public ServletOutputStream getOutputStream() throws IOException {
81              return _strm;
82          }
83  
84          @Override
85          public PrintWriter getWriter() throws IOException {
86              return _writer;
87          }
88      }
89  
90      /**
91       * we need to define our own mockup for the output stream
92       * so that we can simulate a servlet one
93       */
94      class ServletOutputStreamMock extends ServletOutputStream {
95  
96          private OutputStream _bos = null;
97  
98          ServletOutputStreamMock(OutputStream ostr) {
99              _bos = ostr;
100         }
101 
102         public void write(byte[] arg0) throws IOException {
103             _bos.write(arg0);
104         }
105 
106         public void flush() throws IOException {
107             _bos.flush();
108         }
109 
110         public String toString() {
111             return _bos.toString();
112         }
113 
114         public void close() throws IOException {
115             _bos.close();
116         }
117 
118         /**
119          * @return the _bos
120          */
121         public OutputStream getBos() {
122             return _bos;
123         }
124 
125         /**
126          * @param bos the _bos to set
127          */
128         public void setBos(OutputStream bos) {
129             this._bos = bos;
130         }
131 
132         @Override
133         public void write(int arg0) throws IOException {
134             _bos.write(arg0);
135         }
136     }
137 
138     /**
139      * testing the off switch for the
140      * response
141      */
142     public void testSwitchOnWriter() {
143 
144         ByteArrayOutputStream ostr = new ByteArrayOutputStream();
145         ByteArrayOutputStream ostrWriter = new ByteArrayOutputStream();
146         PrintWriter writer = new PrintWriter(ostrWriter);
147         ServletOutputStreamMock sOstr = new ServletOutputStreamMock(ostr);
148         NewStreamingMockResponse resp = new NewStreamingMockResponse(response, sOstr, writer);
149 
150         FacesContext context = new FacesContextImpl(servletContext, request, resp);
151 
152 
153         ResponseWriter responseWriter = context.getResponseWriter();
154         if (responseWriter == null) {
155             try {
156                 responseWriter = new MockResponseWriter(((ServletResponse) context.getExternalContext().getResponse()).getWriter(), null, null);
157             } catch (IOException ex) {
158                 super.fail(ex.getMessage());
159             }
160             context.setResponseWriter(responseWriter);
161         }
162 
163 
164 
165         assertTrue("responsewriter not null", responseWriter != null);
166 
167         try {
168             responseWriter.append("hello world");
169             responseWriter.flush();
170             responseWriter.close();
171 
172         } catch (IOException ex) {
173             super.fail(ex.getMessage());
174         }
175 
176         assertTrue(ostrWriter.toString().trim().equals("hello world"));
177 
178     }
179 
180     /**
181      * Test switch off on the writer api
182      * 
183      * FIXME: enableResponseWriting no longer exists.
184      *
185     public void testSwitchOffWriter() {
186         ByteArrayOutputStream ostr = new ByteArrayOutputStream();
187         ByteArrayOutputStream ostrWriter = new ByteArrayOutputStream();
188         PrintWriter writer = new PrintWriter(ostrWriter);
189         ServletOutputStreamMock sOstr = new ServletOutputStreamMock(ostr);
190         NewStreamingMockResponse resp = new NewStreamingMockResponse(response, sOstr, writer);
191 
192         FacesContext context = new FacesContextImpl(servletContext, request, resp);
193 
194 
195         ResponseWriter responseWriter = context.getResponseWriter();
196         if (responseWriter == null) {
197             try {
198                 responseWriter = new MockResponseWriter(((ServletResponse) context.getExternalContext().getResponse()).getWriter(), null, null);
199             } catch (IOException ex) {
200                 super.fail(ex.getMessage());
201             }
202             context.setResponseWriter(responseWriter);
203         }
204 
205 
206 
207         assertTrue("responsewriter not null", responseWriter != null);
208         context.getPartialViewContext().enableResponseWriting(false);
209 
210         try {
211             responseWriter.append("hello world");
212             responseWriter.flush();
213             responseWriter.close();
214 
215         } catch (IOException ex) {
216             super.fail(ex.getMessage());
217         }
218 
219 
220         assertTrue(ostrWriter.toString().trim().equals(""));
221 
222 
223     }*/
224 
225     /**
226      * 
227      * FIXME: enableResponseWriting no longer exists.
228      * 
229      * test switch off on the stream api
230      * if this works then the stream switch
231      * shoud work on the facesContext should work as well!
232      * @throws java.io.IOException
233      *
234     public void testSwitchOffOstr() {
235         ByteArrayOutputStream ostr = new ByteArrayOutputStream();
236         ByteArrayOutputStream ostrWriter = new ByteArrayOutputStream();
237         PrintWriter writer = new PrintWriter(ostrWriter);
238         ServletOutputStreamMock sOstr = new ServletOutputStreamMock(ostr);
239         NewStreamingMockResponse resp = new NewStreamingMockResponse(response, sOstr, writer);
240 
241         FacesContext context = new FacesContextImpl(servletContext, request, resp);
242         context.getPartialViewContext().enableResponseWriting(false);
243         try {
244             OutputStream finalOstr = (OutputStream) ((ServletResponse) context.getExternalContext().getResponse()).getOutputStream();
245             PrintWriter finalWriter = new PrintWriter(finalOstr);
246            
247             finalWriter.write("hello world");
248             finalOstr.write('a');
249             finalOstr.flush();
250             finalOstr.close();
251 
252         } catch (IOException ex) {
253             super.fail(ex.getMessage());
254         }
255 
256 
257         assertTrue(ostr.toString().trim().equals(""));
258 
259 
260     }*/
261 }