org.ppwcode.vernacular.l10n_III.dojo.DojoDjConfigFilterTest.java Source code

Java tutorial

Introduction

Here is the source code for org.ppwcode.vernacular.l10n_III.dojo.DojoDjConfigFilterTest.java

Source

/*<license>
Copyright 2009 - $Date: 2008-10-15 18:22:22 +0200 (Wed, 15 Oct 2008) $ by PeopleWare n.v..
    
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.
</license>*/
package org.ppwcode.vernacular.l10n_III.dojo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Test;
import org.ppwcode.vernacular.l10n_III.LocaleHelpers;
import org.ppwcode.vernacular.l10n_III.util.SimpleServlet;
import org.ppwcode.vernacular.l10n_III.web.HttpRequestLocaleFilter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.PassThroughFilterChain;

/**
 * This class contains unit tests for DojoDjConfigFilter.  The purpose of these tests is
 * to verify that the algorithm used in DojoDjConfigFilter is correct.
 *
 * @author Ruben Vandeginste
 * @author PeopleWare n.v.
 */
public class DojoDjConfigFilterTest {

    private static final Log LOG = LogFactory.getLog(DojoDjConfigFilterTest.class);

    //
    //  test setups
    //

    public final static String INPUT_1 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'nl-be'\">\n"
            + "</script>";

    public final static String OUTPUT_1 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'fr-be'\">\n"
            + "</script>";

    public final static String INPUT_2 = "<script type=\"text/javascript\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'nl-be'\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\">\n" + "</script>";

    public final static String OUTPUT_2 = "<script type=\"text/javascript\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'fr-be'\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\">\n" + "</script>";

    public final static String INPUT_3 = "<script type='text/javascript'\n"
            + "  djConfig='parseOnLoad: true, modulePaths: {org: \"../../org\", contracts: \"../../../contracts_i18n\", i18n: \"../../../i18n\"}, isDebug: false, locale:\"nl-be\"'\n"
            + "  src=\"js/dojo-1.1.1/dojo/dojo.js\">\n" + "</script>";

    public final static String OUTPUT_3 = "<script type='text/javascript'\n"
            + "  djConfig='parseOnLoad: true, modulePaths: {org: \"../../org\", contracts: \"../../../contracts_i18n\", i18n: \"../../../i18n\"}, isDebug: false, locale:\"fr-be\"'\n"
            + "  src=\"js/dojo-1.1.1/dojo/dojo.js\">\n" + "</script>";

    public final static String INPUT_4 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'nl'\">\n"
            + "</script>";

    public final static String OUTPUT_4 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'fr-be'\">\n"
            + "</script>";

    public final static String INPUT_5 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:''\">\n"
            + "</script>";

    public final static String OUTPUT_5 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'fr-be'\">\n"
            + "</script>";

    public final static String INPUT_6 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, 'locale':''\">\n"
            + "</script>";

    public final static String OUTPUT_6 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, 'locale':'fr-be'\">\n"
            + "</script>";

    public final static String INPUT_7 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false\">\n"
            + "</script>";

    public final static String OUTPUT_7 = "<script type=\"text/javascript\"\n"
            + "src=\"js/dojo-1.1.1/dojo/dojo.js\"\n"
            + "djConfig=\"parseOnLoad: true, modulePaths: {org: '../../org', contracts: '../../../contracts_i18n', i18n: '../../../i18n'}, isDebug: false, locale:'fr-be'\">\n"
            + "</script>";

    public final static String INPUT_8 = "<script type='text/javascript'\n"
            + "  djConfig='parseOnLoad: true, modulePaths: {org: \"../../org\", contracts: \"../../../contracts_i18n\", i18n: \"../../../i18n\"}, isDebug: false'\n"
            + "  src=\"js/dojo-1.1.1/dojo/dojo.js\">\n" + "</script>";

    public final static String OUTPUT_8 = "<script type='text/javascript'\n"
            + "  djConfig='parseOnLoad: true, modulePaths: {org: \"../../org\", contracts: \"../../../contracts_i18n\", i18n: \"../../../i18n\"}, isDebug: false, locale:\"fr-be\"'\n"
            + "  src=\"js/dojo-1.1.1/dojo/dojo.js\">\n" + "</script>";

    //  @Before
    //  public void setUp() {
    //  }
    //
    //  @After
    //  public void tearDown() {
    //  }

    //
    // FILTER TEST
    //

    @Test
    public void testDojoDjConfig1() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_1, OUTPUT_1, "fr-be");
    }

    @Test
    public void testDojoDjConfig2() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_2, OUTPUT_2, "fr-be");
    }

    @Test
    public void testDojoDjConfig3() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_3, OUTPUT_3, "fr-be");
    }

    @Test
    public void testDojoDjConfig4() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_4, OUTPUT_4, "fr-be");
    }

    @Test
    public void testDojoDjConfig5() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_5, OUTPUT_5, "fr-be");
    }

    @Test
    public void testDojoDjConfig6() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_6, OUTPUT_6, "fr-be");
    }

    @Test
    public void testDojoDjConfig7() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_7, OUTPUT_7, "fr-be");
    }

    @Test
    public void testDojoDjConfig8() throws UnsupportedEncodingException, IOException, ServletException {
        testDojoDjConfigFilterHelper("text/html", INPUT_8, OUTPUT_8, "fr-be");
    }

    private void testDojoDjConfigFilterHelper(String type, String input, String output, String bestLocale)
            throws UnsupportedEncodingException, IOException, ServletException {
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
        MockHttpServletResponse response = new MockHttpServletResponse();

        Servlet testServlet = new SimpleServlet(type, input);
        PassThroughFilterChain chain = new PassThroughFilterChain(testServlet);

        HttpSession session = request.getSession();
        session.setAttribute(HttpRequestLocaleFilter.ATTRIBUTE_PREFERRED_LOCALE,
                LocaleHelpers.constructLocaleFromString(bestLocale));

        Filter djConfigFilter = new DojoDjConfigFilter();
        djConfigFilter.doFilter(request, response, chain);

        LOG.debug("input\n" + input);
        LOG.debug("output\n" + response.getContentAsString());

        Assert.assertEquals(output, response.getContentAsString());
        Assert.assertEquals(output.length(), response.getContentLength());
    }

    //
    // PERFORMANCE
    //

    @Test(timeout = 300)
    public void testPerformance1()
            throws FileNotFoundException, IOException, UnsupportedEncodingException, ServletException {
        testPerformanceHelper("sample1.html", "sample1-nl.html", "nl");
    }

    @Test(timeout = 300)
    public void testPerformance2()
            throws FileNotFoundException, IOException, UnsupportedEncodingException, ServletException {
        testPerformanceHelper("sample2.html", "sample2-fr.html", "fr");
    }

    @Test(timeout = 300)
    public void testPerformance3()
            throws FileNotFoundException, IOException, UnsupportedEncodingException, ServletException {
        testPerformanceHelper("sample3.html", "sample3-en.html", "en");
    }

    private void testPerformanceHelper(String inputFileName, String outputFileName, String bestLocale)
            throws FileNotFoundException, IOException, ServletException {
        String input = readFileAsString(inputFileName);
        String output = readFileAsString(outputFileName);
        long start = System.currentTimeMillis();
        testDojoDjConfigFilterHelper("text/html", input, output, bestLocale);
        long end = System.currentTimeMillis();
        LOG.debug("DojoDjConfigFilter needed " + (end - start) + " milliseconds.");
    }

    private String readFileAsString(String fileName) throws FileNotFoundException, IOException {
        String basedir = System.getProperty("basedir");
        String testFileName = basedir + File.separator + "src" + File.separator + "test" + File.separator
                + "resources" + File.separator + "samples" + File.separator + fileName;
        BufferedReader reader = new BufferedReader(new FileReader(testFileName));
        StringBuffer buffer = new StringBuffer(1024);
        char[] charBuffer = new char[1024];
        int numChars = reader.read(charBuffer);
        while (numChars != -1) {
            buffer.append(charBuffer, 0, numChars);
            numChars = reader.read(charBuffer);
        }
        reader.close();
        return buffer.toString();
    }

}