001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.servlet;
015
016import ch.qos.logback.access.dummy.DummyResponse;
017import ch.qos.logback.access.dummy.DummyServletOutputStream;
018import org.junit.Test;
019import org.junit.runner.RunWith;
020import org.junit.runners.Parameterized;
021
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.io.PrintWriter;
025import java.util.Arrays;
026import java.util.Collection;
027
028import static org.junit.Assert.assertArrayEquals;
029
030@RunWith(Parameterized.class)
031public class TeeHttpServletResponseTest {
032
033    String characterEncoding;
034    String testString;
035    byte[] expectedBytes;
036
037    public TeeHttpServletResponseTest(String characterEncoding, String testString, byte[] expectedBytes) {
038        this.characterEncoding = characterEncoding;
039        this.testString = testString;
040        this.expectedBytes = expectedBytes;
041    }
042
043    @Parameterized.Parameters
044    public static Collection<?> inputValues() {
045        return Arrays.asList(new Object[][] {
046                { "utf-8", "Gülcü", new byte[] { (byte) 0x47, (byte) 0xC3, (byte) 0xBC, (byte) 0x6C, (byte) 0x63, (byte) 0xC3, (byte) 0xBC } },
047                { "iso-8859-1", "Gülcü", new byte[] { (byte) 0x47, (byte) 0xFC, (byte) 0x6C, (byte) 0x63, (byte) 0xFC } } });
048    }
049
050    @Test
051    public void testWriterEncoding() throws IOException {
052        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
053
054        DummyResponse dummyResponse = new DummyResponse();
055        dummyResponse.setCharacterEncoding(characterEncoding);
056        dummyResponse.setOutputStream(new DummyServletOutputStream(byteArrayOutputStream));
057
058        TeeHttpServletResponse teeServletResponse = new TeeHttpServletResponse(dummyResponse);
059
060        PrintWriter writer = teeServletResponse.getWriter();
061        writer.write(testString);
062        writer.flush();
063
064        assertArrayEquals(expectedBytes, byteArrayOutputStream.toByteArray());
065    }
066
067}