View Javadoc

1   /*
2    * $Id: ContentTypeHandlerManagerTest.java 676195 2008-07-12 15:55:58Z mrdon $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.rest;
23  
24  import com.mockobjects.dynamic.C;
25  import com.mockobjects.dynamic.Mock;
26  import com.opensymphony.xwork2.ActionContext;
27  import com.opensymphony.xwork2.config.entities.ActionConfig;
28  import com.opensymphony.xwork2.inject.Container;
29  import junit.framework.TestCase;
30  import org.apache.struts2.ServletActionContext;
31  import org.apache.struts2.rest.handler.ContentTypeHandler;
32  import org.apache.struts2.rest.handler.FormUrlEncodedHandler;
33  import org.springframework.mock.web.MockHttpServletRequest;
34  import org.springframework.mock.web.MockHttpServletResponse;
35  
36  import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
37  import static javax.servlet.http.HttpServletResponse.SC_OK;
38  import java.io.IOException;
39  import java.io.Reader;
40  import java.io.Writer;
41  import java.util.Arrays;
42  import java.util.HashMap;
43  import java.util.HashSet;
44  import java.util.Map;
45  
46  public class ContentTypeHandlerManagerTest extends TestCase {
47  
48      private DefaultContentTypeHandlerManager mgr;
49      private MockHttpServletResponse mockResponse;
50      private MockHttpServletRequest mockRequest;
51  
52      @Override
53      public void setUp() {
54          mgr = new DefaultContentTypeHandlerManager();
55          mockResponse = new MockHttpServletResponse();
56          mockRequest = new MockHttpServletRequest();
57          mockRequest.setMethod("GET");
58          ActionContext.setContext(new ActionContext(new HashMap()));
59          ServletActionContext.setRequest(mockRequest);
60          ServletActionContext.setResponse(mockResponse);
61      }
62  
63      @Override
64      public void tearDown() {
65          mockRequest = null;
66          mockRequest = null;
67          mgr = null;
68      }
69  
70      public void testHandleResultOK() throws IOException {
71  
72          String obj = "mystring";
73          ContentTypeHandler handler = new ContentTypeHandler() {
74              public void toObject(Reader in, Object target) {}
75              public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
76                  stream.write(obj.toString());
77                  return resultCode;
78              }
79              public String getContentType() { return "foo"; }
80              public String getExtension() { return "foo"; }
81          };
82          mgr.handlersByExtension.put("xml", handler);
83          mgr.defaultExtension = "xml";
84          mgr.handleResult(new ActionConfig.Builder("", "", "").build(), new DefaultHttpHeaders().withStatus(SC_OK), obj);
85  
86          assertEquals(obj.getBytes().length, mockResponse.getContentLength());
87      }
88  
89      public void testHandleResultNotModified() throws IOException {
90  
91          Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
92          mockHandlerXml.matchAndReturn("getExtension", "xml");
93          mgr.handlersByExtension.put("xml", (ContentTypeHandler) mockHandlerXml.proxy());
94          mgr.handleResult(null, new DefaultHttpHeaders().withStatus(SC_NOT_MODIFIED), new Object());
95  
96          assertEquals(0, mockResponse.getContentLength());
97      }
98  
99      public void testHandlerOverride() {
100         Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
101         mockHandlerXml.matchAndReturn("getExtension", "xml");
102         mockHandlerXml.matchAndReturn("getContentType", "application/xml");
103         mockHandlerXml.matchAndReturn("toString", "xml");
104         Mock mockHandlerJson = new Mock(ContentTypeHandler.class);
105         mockHandlerJson.matchAndReturn("getExtension", "json");
106         mockHandlerJson.matchAndReturn("getContentType", "application/javascript");
107         mockHandlerJson.matchAndReturn("toString", "json");
108         Mock mockHandlerXmlOverride = new Mock(ContentTypeHandler.class);
109         mockHandlerXmlOverride.matchAndReturn("getExtension", "xml");
110         mockHandlerXmlOverride.matchAndReturn("toString", "xmlOverride");
111         mockHandlerXmlOverride.matchAndReturn("getContentType", "application/xml");
112 
113         Mock mockContainer = new Mock(Container.class);
114         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("xmlOverride")), mockHandlerXmlOverride.proxy());
115         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("xml")), mockHandlerXml.proxy());
116         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("json")), mockHandlerJson.proxy());
117         mockContainer.expectAndReturn("getInstanceNames", C.args(C.eq(ContentTypeHandler.class)), new HashSet(Arrays.asList("xml", "xmlOverride", "json")));
118 
119         mockContainer.matchAndReturn("getInstance", C.args(C.eq(String.class),
120                 C.eq(ContentTypeHandlerManager.STRUTS_REST_HANDLER_OVERRIDE_PREFIX+"xml")), "xmlOverride");
121         mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class),
122                 C.eq(ContentTypeHandlerManager.STRUTS_REST_HANDLER_OVERRIDE_PREFIX+"json")), null);
123         
124         DefaultContentTypeHandlerManager mgr = new DefaultContentTypeHandlerManager();
125         mgr.setContainer((Container) mockContainer.proxy());
126 
127         Map<String,ContentTypeHandler> handlers = mgr.handlersByExtension;
128         assertNotNull(handlers);
129         assertEquals(2, handlers.size());
130         assertEquals(mockHandlerXmlOverride.proxy(), handlers.get("xml"));
131         assertEquals(mockHandlerJson.proxy(), handlers.get("json"));
132     }
133 
134     /*** Assert that the request content-type and differ from the response content type */
135     public void HandleRequestContentType() throws IOException {
136 
137         Mock mockHandlerForm = new Mock(ContentTypeHandler.class);
138         mockHandlerForm.matchAndReturn("getExtension", null);
139         mockHandlerForm.matchAndReturn("getContentType", "application/x-www-form-urlencoded");
140         mockHandlerForm.matchAndReturn("toString", "x-www-form-urlencoded");
141 
142         Mock mockHandlerJson = new Mock(ContentTypeHandler.class);
143         mockHandlerJson.matchAndReturn("getExtension", "json");
144         mockHandlerJson.matchAndReturn("getContentType", "application/javascript");
145         mockHandlerJson.matchAndReturn("toString", "json");
146 
147         Mock mockContainer = new Mock(Container.class);
148         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("x-www-form-urlencoded")), mockHandlerForm.proxy());
149         mockContainer.expectAndReturn("getInstanceNames", C.args(C.eq(ContentTypeHandler.class)), new HashSet(Arrays.asList("x-www-form-urlencoded", "json")));
150 
151         mockRequest.setContentType(FormUrlEncodedHandler.CONTENT_TYPE);
152         mockRequest.setContent("a=1&b=2".getBytes("UTF-8"));
153         ContentTypeHandler handler = mgr.getHandlerForRequest(mockRequest);
154 
155         assertEquals("x-www-form-urlencoded", toString());
156     }
157 }