de.perdian.commons.servlet.TestServletUtils.java Source code

Java tutorial

Introduction

Here is the source code for de.perdian.commons.servlet.TestServletUtils.java

Source

/*
 * Copyright 2014 Christian Robert
 *
 * 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 de.perdian.commons.servlet;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.springframework.mock.web.MockHttpServletRequest;

import de.perdian.commons.servlet.ServletUtils;

public class TestServletUtils {

    @Test
    public void testExtractParameterString() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("bar");

        Assert.assertEquals("bar", ServletUtils.extractParameterString(httpRequest, "foo", "default"));
        Assert.assertEquals("bar", ServletUtils.extractParameterString(httpRequest, "foo"));

    }

    @Test
    public void testExtractParameterStringValueContainsWhitespaces() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("  bar  ");

        Assert.assertEquals("bar", ServletUtils.extractParameterString(httpRequest, "foo", "default"));
        Assert.assertEquals("bar", ServletUtils.extractParameterString(httpRequest, "foo"));

    }

    @Test
    public void testExtractParameterStringValueNotFound() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("  bar  ");

        Assert.assertEquals("default", ServletUtils.extractParameterString(httpRequest, "foo2", "default"));
        Assert.assertNull(ServletUtils.extractParameterString(httpRequest, "foo2"));

    }

    @Test
    public void testExtractParameterInt() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("42");

        Assert.assertEquals(42, ServletUtils.extractParameterInt(httpRequest, "foo", 4711));

    }

    @Test
    public void testExtractParameterIntValueNotFound() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("42");

        Assert.assertEquals(4711, ServletUtils.extractParameterInt(httpRequest, "foo2", 4711));

    }

    @Test
    public void testExtractParameterBooleanTrue() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("true");

        Assert.assertTrue(ServletUtils.extractParameterBoolean(httpRequest, "foo"));

    }

    @Test
    public void testExtractParameterBooleanOn() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("on");

        Assert.assertTrue(ServletUtils.extractParameterBoolean(httpRequest, "foo"));

    }

    @Test
    public void testExtractParameterBoolean1() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("1");

        Assert.assertTrue(ServletUtils.extractParameterBoolean(httpRequest, "foo"));

    }

    @Test
    public void testExtractParameterBooleanValueNotFound() {

        HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpRequest.getParameter(Matchers.eq("foo"))).thenReturn("42");

        Assert.assertFalse(ServletUtils.extractParameterBoolean(httpRequest, "foo2"));
        Assert.assertTrue(ServletUtils.extractParameterBoolean(httpRequest, "foo2", true));

    }

    @Test
    public void testExtractAcceptedLocales() {

        MockHttpServletRequest httpRequest = new MockHttpServletRequest();
        httpRequest.addHeader("accept-language", "de_DE, en_US");

        List<Locale> acceptedLocales = ServletUtils.extractAcceptedLocales(httpRequest, null);
        Assert.assertEquals(2, acceptedLocales.size());
        Assert.assertEquals("de", acceptedLocales.get(0).getLanguage());
        Assert.assertEquals("DE", acceptedLocales.get(0).getCountry());
        Assert.assertEquals("en", acceptedLocales.get(1).getLanguage());
        Assert.assertEquals("US", acceptedLocales.get(1).getCountry());

    }

    @Test
    public void testExtractAcceptedLocalesWithQValues() {

        MockHttpServletRequest httpRequest = new MockHttpServletRequest();
        httpRequest.addHeader("accept-language", "de_DE, en_US;q=0.5, en_GB;q=0.8");

        List<Locale> acceptedLocales = ServletUtils.extractAcceptedLocales(httpRequest, null);
        Assert.assertEquals(3, acceptedLocales.size());
        Assert.assertEquals("de", acceptedLocales.get(0).getLanguage());
        Assert.assertEquals("DE", acceptedLocales.get(0).getCountry());
        Assert.assertEquals("en", acceptedLocales.get(1).getLanguage());
        Assert.assertEquals("GB", acceptedLocales.get(1).getCountry());
        Assert.assertEquals("en", acceptedLocales.get(2).getLanguage());
        Assert.assertEquals("US", acceptedLocales.get(2).getCountry());

    }

    @Test
    public void testExtractAcceptedLocalesWithQValuesInvalid() {

        MockHttpServletRequest httpRequest = new MockHttpServletRequest();
        httpRequest.addHeader("accept-language", "de_DE, en_GB;q=0.8, en_US;q=INVALID");

        List<Locale> acceptedLocales = ServletUtils.extractAcceptedLocales(httpRequest, null);
        Assert.assertEquals(3, acceptedLocales.size());
        Assert.assertEquals("de", acceptedLocales.get(0).getLanguage());
        Assert.assertEquals("DE", acceptedLocales.get(0).getCountry());
        Assert.assertEquals("en", acceptedLocales.get(1).getLanguage());
        Assert.assertEquals("US", acceptedLocales.get(1).getCountry());
        Assert.assertEquals("en", acceptedLocales.get(2).getLanguage());
        Assert.assertEquals("GB", acceptedLocales.get(2).getCountry());

    }

    @Test
    public void testExtractAcceptedLocalesNoHeaderFound() {

        MockHttpServletRequest httpRequest = new MockHttpServletRequest();

        List<Locale> acceptedLocales = ServletUtils.extractAcceptedLocales(httpRequest,
                Arrays.asList(Locale.GERMANY));
        Assert.assertEquals(1, acceptedLocales.size());
        Assert.assertTrue(acceptedLocales.contains(Locale.GERMANY));

    }

    @Test
    public void testExtractAcceptedLocalesNoHeaderFoundAndNoDefault() {

        MockHttpServletRequest httpRequest = new MockHttpServletRequest();

        List<Locale> acceptedLocales = ServletUtils.extractAcceptedLocales(httpRequest, null);
        Assert.assertEquals(0, acceptedLocales.size());

    }

}