/*
 *  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.harmony.test.func.api.java.io.share.FilterOutputStream;


import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.harmony.test.func.api.java.io.share.OutputStream.OutputStreamTestShared;
import org.apache.harmony.test.func.api.java.io.share.MockOutputStream;
import org.apache.harmony.test.func.api.java.io.share.MultiThreadRunner;
import org.apache.harmony.share.Result;

public class FilterOutputStreamTestShared extends OutputStreamTestShared {
    OutputStream underlyingStream = null;
    
    protected OutputStream getTestedOutputStream(int maxSize)
            throws IOException {
        underlyingStream = super.getTestedOutputStream(maxSize);
        return getOutputStream(underlyingStream);
    }

    protected String getWrittenAsString(OutputStream os) {
        return super.getWrittenAsString(underlyingStream);
    }
    
    protected OutputStream getOutputStream(OutputStream os) {
        return new FilterOutputStream(os);
    }

    public Result testClose() throws IOException {
        MockOutputStream os = new MockOutputStream(5);
        OutputStream fos = getOutputStream(os);
        
        MultiThreadRunner.waitAtBarrier();

        fos.write("xyz".getBytes());
        fos.close();

        if (os.isClosed() && os.isFlushed()
                && "xyz".equals(os.getWrittenAsString())) {
            return passed();
        }

        return failed("expected underlying stream to be flushed and closed");
    }

    public Result testFlush() throws IOException {
        MockOutputStream os = new MockOutputStream(5);
        OutputStream fos = getOutputStream(os);

        MultiThreadRunner.waitAtBarrier();

        fos.write("abc".getBytes());
        fos.flush();
        if (os.isFlushed() && "abc".equals(os.getWrittenAsString())) {
            return passed();
        }

        return failed("expected underlying stream to be flushed and all pending data written to it");
    }

    public Result testWrite0001() throws IOException {
        return super.testWrite0001();
    }

    public Result testWriteByte() throws IOException {
        return super.testWriteByte();
    }

    public Result testWriteIndexOutOfBounds0001() throws IOException {
        return super.testWriteIndexOutOfBounds0001();
    }

    public Result testWriteIndexOutOfBounds0002() throws IOException {
        return super.testWriteIndexOutOfBounds0002();
    }

    public Result testWriteIndexOutOfBounds0003() throws IOException {
        return super.testWriteIndexOutOfBounds0003();
    }

    public Result testWriteNull() throws IOException {
        return super.testWriteNull();
    }

    public Result testWriteSlice() throws IOException {
        return super.testWriteSlice();
    }

    public Result testOutField() throws IOException {
        MultiThreadRunner.waitAtBarrier();

        OutputStream os = new MockOutputStream(0);
        TestOut tos = new TestOut(os);
        
        if (os == tos.getOut()) {
            return passed();
        }
        return failed("wrong value in 'out' field");
    }
}

class TestOut extends FilterOutputStream {
    public TestOut(OutputStream arg0) {
        super(arg0);
    }

    OutputStream getOut() {
        return out;
    }
}

@@ -982,16 +982,48 @@
 tStream;
+%0Aimport java.lang.reflect.Field;
 %0A%0Aimport
@@ -1372,50 +1372,8 @@
 d %7B%0A
-    OutputStream underlyingStream = null;%0A
     
@@ -1480,27 +1480,31 @@
     
-underlying
+return getOutput
 Stream
- = 
+(
 supe
@@ -1539,88 +1539,235 @@
 ize)
+)
 ;%0A    
+%7D%0A%0A
     
-return getOutputStream(underlyingStream);%0A    %7D%0A%0A    protected String 
+protected String getWrittenAsString(OutputStream os) %7B%0A        try %7B%0A            final Field c = FilterOutputStream.class.getDeclaredField(%22out%22);%0A            %0A%09    c.setAccessible(true);%0A            return super.
 getW
@@ -1773,32 +1773,33 @@
 WrittenAsString(
+(
 OutputStream os)
@@ -1786,38 +1786,45 @@
 ng((OutputStream
- os) %7B
+) c.get(os));
 %0A        return 
@@ -1820,58 +1820,100 @@
     
-return super.getWrittenAsString(underlyingStream);
+%7D catch(Exception ex) %7B%0A            ex.printStackTrace();%0A            return null;%0A        %7D
 %0A   

Patch for HARMONY-4864 ([buildtest][func] The test FilterOutputStreamTestShared is incorrect)