Java tutorial
/* Copyright 2006 - 2011 Under Dusken 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 no.dusken.common.plugin.control; import junit.framework.Assert; import no.dusken.common.exception.PageNotFoundException; import no.dusken.common.plugin.control.web.PluginResourceController; import org.junit.Before; import org.junit.Test; import org.springframework.mock.web.MockHttpServletResponse; import java.io.IOException; import java.util.Arrays; import static org.junit.Assert.assertTrue; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class PluginResourceControllerTest { private PluginResourceController pluginResourceController; private MockHttpServletResponse response; @Before public void setUp() throws Exception { pluginResourceController = new PluginResourceController(); pluginResourceController.setAllowedFileTypes(Arrays.asList(".css", ".png", ".jpg", ".jpeg", ".gif")); response = new MockHttpServletResponse(); } @Test public void testGetCss() throws IOException, PageNotFoundException { pluginResourceController.get("testplugin/css/style.css", response); assertTrue("Content had length 0", response.getContentAsString().length() > 0); } @Test(expected = PageNotFoundException.class) public void PageNotFoundWhenNotExisting() throws PageNotFoundException { pluginResourceController.get("testplugin/css/dfstyle.css", response); } @Test(expected = PageNotFoundException.class) public void PageNotFoundWhenNotAllowedFileType() throws PageNotFoundException { pluginResourceController.get("testplugin/config.conf", response); } @Test(expected = PageNotFoundException.class) public void PageNotFoundWhenOutOfScope() throws PageNotFoundException { pluginResourceController.get("../unreachable.css", response); } @Test public void contentlengthSet() throws PageNotFoundException { pluginResourceController.get("testplugin/css/style.css", response); assertTrue("Content had length 0", response.getContentLength() > 0); } @Test public void cssHasRightContentType() throws PageNotFoundException { pluginResourceController.get("testplugin/css/style.css", response); Assert.assertEquals("Wrong contenttype", "text/css", response.getContentType()); } @Test public void jpgHasRightContentType() throws PageNotFoundException { pluginResourceController.get("testplugin/images/tumblr500.jpg", response); Assert.assertEquals("Wrong contenttype", "image/jpeg", response.getContentType()); } @Test public void jpegHasRightContentType() throws PageNotFoundException { pluginResourceController.get("testplugin/images/moo.jpeg", response); Assert.assertEquals("Wrong contenttype", "image/jpeg", response.getContentType()); } @Test public void pngHasRightContentType() throws PageNotFoundException { pluginResourceController.get("testplugin/images/tumblr400.png", response); Assert.assertEquals("Wrong contenttype", "image/png", response.getContentType()); } @Test public void gifHasRightContentType() throws PageNotFoundException { pluginResourceController.get("testplugin/images/rawr.gif", response); Assert.assertEquals("Wrong contenttype", "image/gif", response.getContentType()); } @Test public void cachingSet() throws PageNotFoundException { pluginResourceController.get("testplugin/images/rawr.gif", response); Assert.assertEquals("Wrong header", "public, max-age=2505600", response.getHeader("Cache-Control")); } }