/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.logging.log4j.io;

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

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.*;

public abstract class AbstractLoggerOutputStreamTest extends AbstractStreamTest {
    protected OutputStream out;
    protected ByteArrayOutputStream wrapped;

    protected abstract ByteArrayOutputStream createOutputStream();

    protected abstract OutputStream createOutputStreamWrapper();

    @Before
    public void createStream() {
        this.wrapped = createOutputStream();
        this.out = createOutputStreamWrapper();
    }

    @Test
    public void testClose_HasRemainingData() throws IOException {
        this.out.write(FIRST.getBytes());
        assertMessages();
        this.out.close();
        assertMessages(FIRST);
        if (this.wrapped != null) {
            assertEquals(FIRST, this.wrapped.toString());
        }
    }

    @Test
    public void testClose_NoRemainingData() throws IOException {
        this.out.close();
        assertMessages();
        if (this.wrapped != null) {
            assertEquals("", this.wrapped.toString());
        }
    }

    @Test
    public void testFlush() throws IOException {
        final OutputStream out = EasyMock.createMock("out", OutputStream.class);
        out.flush(); // expect the flush to come through to the mocked OutputStream
        out.close();
        replay(out);

        final OutputStream filteredOut =
            IoBuilder.forLogger(getExtendedLogger())
                .filter(out)
                .setLevel(LEVEL)
                .buildOutputStream();
        filteredOut.flush();
        filteredOut.close();
        verify(out);
    }

    @Test
    public void testWrite_ByteArray() throws Exception {
        final byte[] bytes = "byte[]".getBytes();
        this.out.write(bytes);
        assertMessages();
        this.out.write('\n');
        assertMessages("byte[]");
        if (this.wrapped != null) {
            assertEquals("byte[]\n", this.wrapped.toString());
        }
    }

    @Test
    public void testWrite_ByteArray_Offset_Length() throws Exception {
        final byte[] bytes = "byte[]".getBytes();
        final int middle = bytes.length / 2;
        final int length = bytes.length - middle;
        final String right = new String(bytes, middle, length);
        this.out.write(bytes, middle, length);
        assertMessages();
        this.out.write('\n');
        assertMessages(right);
        if (this.wrapped != null) {
            assertEquals("byte[]".substring(middle, bytes.length) + '\n', this.wrapped.toString());
        }
    }

    @Test
    public void testWrite_IgnoresWindowsNewline() throws IOException {
        this.out.write(FIRST.getBytes());
        this.out.write("\r\n".getBytes());
        this.out.write(LAST.getBytes());
        this.out.close();
        assertMessages(FIRST, LAST);
        if (this.wrapped != null) {
            assertEquals(FIRST + "\r\n" + LAST, this.wrapped.toString());
        }
    }

    @Test
    public void testWrite_Int() throws Exception {
        for (final byte b : "int".getBytes()) {
            this.out.write(b);
            assertMessages();
        }
        this.out.write('\n');
        assertMessages("int");
        if (this.wrapped != null) {
            assertEquals("int" + '\n', this.wrapped.toString());
        }
    }

    @Test
    public void testWrite_MultipleLines() throws IOException {
        this.out.write((FIRST + '\n' + LAST + '\n').getBytes());
        assertMessages(FIRST, LAST);
        if (this.wrapped != null) {
            assertEquals(FIRST + '\n' + LAST + '\n', this.wrapped.toString());
        }
    }
}

@@ -2384,32 +2384,37 @@
 (out);%0A%0A        
+try (
 final OutputStre
@@ -2573,34 +2573,38 @@
 ldOutputStream()
-;%0A
+) %7B%0A  
         filtered
@@ -2628,28 +2628,9 @@
     
-filteredOut.close();
+%7D
 %0A   

Use Java 7 try-with-resources.