<commit_before>/**
 * Copyright 2011 Asakusa Framework Team.
 *
 * 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 com.asakusafw.bulkloader.common;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

/**
 * Test for {@link StreamRedirectThread}.
 */
public class StreamRedirectThreadTest {

    private static final byte[] BYTES = new byte[65536];
    static {
        for (int i = 0; i < BYTES.length; i++) {
            BYTES[i] = (byte) ((i >> 8) ^ i);
        }
    }

    private final TestInputStream in = new TestInputStream(BYTES);

    private final TestOutputStream out = new TestOutputStream();

    /**
     * Simple testing.
     */
    @Test(timeout = 10000)
    public void redirect() {
        StreamRedirectThread t = new StreamRedirectThread(in, out);
        t.run();
        assertThat(out.toByteArray(), is(BYTES));
        assertThat(in.read(), is(-1));
        assertThat(in.closed, is(false));
        assertThat(out.closed, is(false));
    }

    /**
     * Exit quietly on input error.
     * @throws Exception if occur
     */
    @Test(timeout = 10000)
    public void quietExitOnInputError() throws Exception {
        StreamRedirectThread t = new StreamRedirectThread(new ErroneousInputStream(65534), out);
        t.run();
    }

    /**
     * Exit quietly on output error.
     * @throws Exception if occur
     */
    @Test(timeout = 10000)
    public void quietExitOnOutputError() throws Exception {
        StreamRedirectThread t = new StreamRedirectThread(in, new ErroneousOutputStream(65534));
        t.run();
        assertThat("input stream must be consumed", in.read(), is(-1));
    }

    /**
     * automatically close input.
     */
    @Test(timeout = 10000)
    public void closeInput() {
        StreamRedirectThread t = new StreamRedirectThread(in, out, true, false);
        t.run();<commit_msg>Added a test case for StreamRedirectThread (#47)<commit_after>/**
 * Copyright 2011 Asakusa Framework Team.
 *
 * 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 com.asakusafw.bulkloader.common;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

/**
 * Test for {@link StreamRedirectThread}.
 */
public class StreamRedirectThreadTest {

    private static final byte[] BYTES = new byte[65536];
    static {
        for (int i = 0; i < BYTES.length; i++) {
            BYTES[i] = (byte) ((i >> 8) ^ i);
        }
    }

    private final TestInputStream in = new TestInputStream(BYTES);

    private final TestOutputStream out = new TestOutputStream();

    /**
     * Simple testing.
     */
    @Test(timeout = 10000)
    public void redirect() {
        StreamRedirectThread t = new StreamRedirectThread(in, out);
        t.run();
        assertThat(out.toByteArray(), is(BYTES));
        assertThat(in.read(), is(-1));
        assertThat(in.closed, is(false));
        assertThat(out.closed, is(false));
    }

    /**
     * Exit quietly on input error.
     * @throws Exception if occur
     */
    @Test(timeout = 10000)
    public void quietExitOnInputError() throws Exception {
        StreamRedirectThread t = new StreamRedirectThread(new ErroneousInputStream(65534), out);
        t.run();
    }

    /**
     * Exit quietly on output error.
     * @throws Exception if occur
     */
    @Test(timeout = 10000)
    public void quietExitOnOutputError() throws Exception {
        StreamRedirectThread t = new StreamRedirectThread(in, new ErroneousOutputStream(65534));
        t.run();
        assertThat("input stream must be consumed", in.read(), is(-1));
    }

    /**
     * Exit quietly on output error.
     * @throws Exception if occur
     */
    @Test(timeout = 10000)
    public void consumeInputOnOutputError() throws Exception {
        StreamRedirectThread t = new StreamRedirectThread(in, new ErroneousOutputStream(5));
        t.run();
        assertThat("input stream must be consumed", in.read(), is(-1));
    }

    /**
     * automatically close input.
     */
    @Test(timeout = 10000)
    public void closeInput() {
        StreamRedirectThread t = new StreamRedirectThread(in, out, true, false);
        t.run();
{'fraction_non_alphanumeric': 0.10777797736662476, 'fraction_numerical': 0.018501886114603915, 'mean_word_length': 2.9743040685224837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5222262', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1506, 'n_words': 501}