/* * Copyright (c) 2001-2015, Inversoft Inc., All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific * language governing permissions and limitations under the License. */ package org.primeframework.mvc.action.result; import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayInputStream; import java.io.IOException; import java.lang.annotation.Annotation; import org.easymock.EasyMock; import org.primeframework.mock.servlet.MockServletOutputStream; import org.primeframework.mvc.action.ActionInvocation; import org.primeframework.mvc.action.ActionInvocationStore; import org.primeframework.mvc.action.ExecuteMethodConfiguration; import org.primeframework.mvc.action.result.annotation.Stream; import org.primeframework.mvc.parameter.el.ExpressionEvaluator; import org.primeframework.mvc.servlet.HTTPMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static org.easymock.EasyMock.*; import static org.testng.Assert.*; /** * This class tests the stream result. * * @author Brian Pontarelli */ public class StreamResultTest { @DataProvider(name= "httMethod") public Object[][] httpMethod() { return new Object[][] {{HTTPMethod.GET}, {HTTPMethod.HEAD}}; } @Test(dataProvider = "httpMethod") public void explicit(HTTPMethod httpMethod) throws IOException, ServletException { Object action = new Object(); ExpressionEvaluator ee = EasyMock.createStrictMock(ExpressionEvaluator.class); EasyMock.expect(ee.expand("10", action, false)).andReturn("10"); EasyMock.expect(ee.expand("foo.zip", action, true)).andReturn("foo.zip"); EasyMock.expect(ee.expand("application/octet-stream", action, false)).andReturn("application/octet-stream"); EasyMock.expect(ee.getValue("stream", action)).andReturn(new ByteArrayInputStream("test".getBytes())); EasyMock.replay(ee); MockServletOutputStream sos = new MockServletOutputStream(); HttpServletResponse response = EasyMock.createStrictMock(HttpServletResponse.class); response.setContentType("application/octet-stream"); response.setContentLength(10); response.setHeader("Content-Disposition", "attachment; filename=\"foo.zip\""); EasyMock.expect(response.getOutputStream()).andReturn(sos); EasyMock.replay(response); ActionInvocationStore store = createStrictMock(ActionInvocationStore.class); expect(store.getCurrent()).andReturn(new ActionInvocation(action, new ExecuteMethodConfiguration(httpMethod, null, null), "/foo", "", null)); replay(store); Stream stream = new StreamImpl("success", "foo.zip", "10", "application/octet-stream", "stream"); StreamResult streamResult = new StreamResult(ee, response, store); streamResult.execute(stream); if (httpMethod == HTTPMethod.GET) { assertEquals(sos.toString(), "test"); EasyMock.verify(ee, response); } else { assertEquals(sos.toString(), ""); } } public class StreamImpl implements Stream { private final String code; private final String name; private final String length; private final String type; private final String property; public StreamImpl(String code, String name, String length, String type, String property) { this.code = code; this.name = name; this.length = length; this.type = type; this.property = property; } public String code() { return code; } public String property() { return property; } public String type() { return type; } public String length() { return length; } public String name() { return name; } public Class<? extends Annotation> annotationType() { return Stream.class; } } }