View Javadoc

1   /*
2    * $Id: ContentTypeHandlerManagerTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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.springframework.mock.web.MockHttpServletRequest;
33  import org.springframework.mock.web.MockHttpServletResponse;
34  
35  import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
36  import static javax.servlet.http.HttpServletResponse.SC_OK;
37  import java.io.IOException;
38  import java.io.Reader;
39  import java.io.Writer;
40  import java.util.Arrays;
41  import java.util.HashMap;
42  import java.util.HashSet;
43  import java.util.Map;
44  
45  public class ContentTypeHandlerManagerTest extends TestCase {
46  
47      private ContentTypeHandlerManager mgr;
48      private MockHttpServletResponse mockResponse;
49      private MockHttpServletRequest mockRequest;
50  
51      @Override
52      public void setUp() {
53          mgr = new ContentTypeHandlerManager();
54          mockResponse = new MockHttpServletResponse();
55          mockRequest = new MockHttpServletRequest();
56          mockRequest.setMethod("GET");
57          ActionContext.setContext(new ActionContext(new HashMap()));
58          ServletActionContext.setRequest(mockRequest);
59          ServletActionContext.setResponse(mockResponse);
60      }
61  
62      @Override
63      public void tearDown() {
64          mockRequest = null;
65          mockRequest = null;
66          mgr = null;
67      }
68  
69      public void testHandleResultOK() throws IOException {
70  
71          String obj = "mystring";
72          ContentTypeHandler handler = new ContentTypeHandler() {
73              public void toObject(Reader in, Object target) {}
74              public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
75                  stream.write(obj.toString());
76                  return resultCode;
77              }
78              public String getContentType() { return "foo"; }
79              public String getExtension() { return "foo"; }
80          };
81          mgr.handlers.put("xml", handler);
82          mgr.defaultExtension = "xml";
83          mgr.handleResult(new ActionConfig.Builder("", "", "").build(), new DefaultHttpHeaders().withStatus(SC_OK), obj);
84  
85          assertEquals(obj.getBytes().length, mockResponse.getContentLength());
86      }
87  
88      public void testHandleResultNotModified() throws IOException {
89  
90          Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
91          mockHandlerXml.matchAndReturn("getExtension", "xml");
92          mgr.handlers.put("xml", (ContentTypeHandler) mockHandlerXml.proxy());
93          mgr.handleResult(null, new DefaultHttpHeaders().withStatus(SC_NOT_MODIFIED), new Object());
94  
95          assertEquals(0, mockResponse.getContentLength());
96      }
97  
98      public void testHandlerOverride() {
99          Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
100         mockHandlerXml.matchAndReturn("getExtension", "xml");
101         mockHandlerXml.matchAndReturn("toString", "xml");
102         Mock mockHandlerJson = new Mock(ContentTypeHandler.class);
103         mockHandlerJson.matchAndReturn("getExtension", "json");
104         mockHandlerJson.matchAndReturn("toString", "json");
105         Mock mockHandlerXmlOverride = new Mock(ContentTypeHandler.class);
106         mockHandlerXmlOverride.matchAndReturn("getExtension", "xml");
107         mockHandlerXmlOverride.matchAndReturn("toString", "xmlOverride");
108 
109         Mock mockContainer = new Mock(Container.class);
110         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("xmlOverride")), mockHandlerXmlOverride.proxy());
111         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("xml")), mockHandlerXml.proxy());
112         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("json")), mockHandlerJson.proxy());
113         mockContainer.expectAndReturn("getInstanceNames", C.args(C.eq(ContentTypeHandler.class)), new HashSet(Arrays.asList("xml", "xmlOverride", "json")));
114 
115         mockContainer.matchAndReturn("getInstance", C.args(C.eq(String.class),
116                 C.eq(ContentTypeHandlerManager.STRUTS_REST_HANDLER_OVERRIDE_PREFIX+"xml")), "xmlOverride");
117         mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class),
118                 C.eq(ContentTypeHandlerManager.STRUTS_REST_HANDLER_OVERRIDE_PREFIX+"json")), null);
119         
120         ContentTypeHandlerManager mgr = new ContentTypeHandlerManager();
121         mgr.setContainer((Container) mockContainer.proxy());
122 
123         Map<String,ContentTypeHandler> handlers = mgr.handlers;
124         assertNotNull(handlers);
125         assertEquals(2, handlers.size());
126         assertEquals(mockHandlerXmlOverride.proxy(), handlers.get("xml"));
127         assertEquals(mockHandlerJson.proxy(), handlers.get("json"));
128     }
129 }