1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.io;
19
20import junit.framework.TestSuite;
21import org.apache.harmony.testframework.SinkTester;
22import org.apache.harmony.testframework.WrapperTester;
23import tests.support.Streams;
24
25import java.io.BufferedOutputStream;
26import java.io.ByteArrayInputStream;
27import java.io.ByteArrayOutputStream;
28import java.io.DataOutputStream;
29import java.io.File;
30import java.io.FileInputStream;
31import java.io.FileOutputStream;
32import java.io.FilterOutputStream;
33import java.io.IOException;
34import java.io.ObjectInputStream;
35import java.io.ObjectOutputStream;
36import java.io.OutputStream;
37import java.io.PipedInputStream;
38import java.io.PipedOutputStream;
39import java.io.PrintStream;
40import java.util.concurrent.Callable;
41import java.util.concurrent.ExecutorService;
42import java.util.concurrent.Executors;
43import java.util.concurrent.Future;
44
45/**
46 * Tests basic {@link OutputStream} behaviors for the luni implementations of
47 * the type.
48 */
49public class OutputStreamTesterTest {
50
51    public static junit.framework.Test suite() {
52        TestSuite suite = new TestSuite();
53
54        // sink tests
55        suite.addTest(new FileOutputStreamSinkTester(true).createTests());
56        suite.addTest(new FileOutputStreamSinkTester(false).createTests());
57        suite.addTest(new ByteArrayOutputStreamSinkTester(0).setThrowsExceptions(false).createTests());
58        suite.addTest(new ByteArrayOutputStreamSinkTester(4).setThrowsExceptions(false).createTests());
59        suite.addTest(new PipedOutputStreamSinkTester().createTests());
60
61        // wrapper tests
62        suite.addTest(new BufferedOutputStreamTester(1).createTests());
63        suite.addTest(new BufferedOutputStreamTester(5).createTests());
64        suite.addTest(new BufferedOutputStreamTester(1024).createTests());
65        suite.addTest(new FilterOutputStreamTester().createTests());
66        suite.addTest(new DataOutputStreamTester().createTests());
67        // fails wrapperTestFlushThrowsViaClose() and sinkTestWriteAfterClose():
68        // suite.addTest(new ObjectOutputStreamTester().createTests());
69        suite.addTest(new PrintStreamTester().setThrowsExceptions(false).createTests());
70
71        return suite;
72    }
73
74    private static class FileOutputStreamSinkTester extends SinkTester {
75
76        private final boolean append;
77        private File file;
78
79        private FileOutputStreamSinkTester(boolean append) {
80            this.append = append;
81        }
82
83        public OutputStream create() throws IOException {
84            file = File.createTempFile("FileOutputStreamSinkTester", "tmp");
85            file.deleteOnExit();
86            return new FileOutputStream(file, append);
87        }
88
89        public byte[] getBytes() throws IOException {
90            return Streams.streamToBytes(new FileInputStream(file));
91        }
92    }
93
94    private static class ByteArrayOutputStreamSinkTester extends SinkTester {
95
96        private final int size;
97        private ByteArrayOutputStream stream;
98
99        private ByteArrayOutputStreamSinkTester(int size) {
100            this.size = size;
101        }
102
103        public OutputStream create() throws IOException {
104            stream = new ByteArrayOutputStream(size);
105            return stream;
106        }
107
108        public byte[] getBytes() throws IOException {
109            return stream.toByteArray();
110        }
111    }
112
113    private static class PipedOutputStreamSinkTester extends SinkTester {
114
115        private ExecutorService executor;
116        private Future<byte[]> future;
117
118        public OutputStream create() throws IOException {
119            final PipedInputStream in = new PipedInputStream();
120            PipedOutputStream out = new PipedOutputStream(in);
121
122            executor = Executors.newSingleThreadExecutor();
123            future = executor.submit(new Callable<byte[]>() {
124                final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
125                public byte[] call() throws Exception {
126                    byte[] buffer = new byte[256];
127                    int count;
128                    while ((count = in.read(buffer)) != -1) {
129                        bytes.write(buffer, 0, count);
130                    }
131                    return bytes.toByteArray();
132                }
133            });
134
135            return out;
136        }
137
138        public byte[] getBytes() throws Exception {
139            executor.shutdown();
140            return future.get();
141        }
142    }
143
144    private static class FilterOutputStreamTester extends WrapperTester {
145
146        public OutputStream create(OutputStream delegate) throws Exception {
147            return new FilterOutputStream(delegate);
148        }
149
150        public byte[] decode(byte[] delegateBytes) throws Exception {
151            return delegateBytes;
152        }
153    }
154
155    private static class BufferedOutputStreamTester extends WrapperTester {
156        private final int bufferSize;
157
158        private BufferedOutputStreamTester(int bufferSize) {
159            this.bufferSize = bufferSize;
160        }
161
162        public OutputStream create(OutputStream delegate) throws Exception {
163            return new BufferedOutputStream(delegate, bufferSize);
164        }
165
166        public byte[] decode(byte[] delegateBytes) throws Exception {
167            return delegateBytes;
168        }
169    }
170
171    private static class DataOutputStreamTester extends WrapperTester {
172
173        public OutputStream create(OutputStream delegate) throws Exception {
174            return new DataOutputStream(delegate);
175        }
176
177        public byte[] decode(byte[] delegateBytes) throws Exception {
178            return delegateBytes;
179        }
180    }
181
182    private static class ObjectOutputStreamTester extends WrapperTester {
183
184        public OutputStream create(OutputStream delegate) throws Exception {
185            return new ObjectOutputStream(delegate);
186        }
187
188        public byte[] decode(byte[] delegateBytes) throws Exception {
189            return Streams.streamToBytes(new ObjectInputStream(
190                    new ByteArrayInputStream(delegateBytes)));
191        }
192    }
193
194    private static class PrintStreamTester extends WrapperTester {
195
196        public OutputStream create(OutputStream delegate) throws Exception {
197            return new PrintStream(delegate);
198        }
199
200        public byte[] decode(byte[] delegateBytes) throws Exception {
201            return delegateBytes;
202        }
203    }
204}
205