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.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 // TODO: Rewrite this test so that id doesn't need a suite(). 52 private static junit.framework.Test suite() { 53 TestSuite suite = new TestSuite(); 54 55 // sink tests 56 suite.addTest(new FileOutputStreamSinkTester(true).createTests()); 57 suite.addTest(new FileOutputStreamSinkTester(false).createTests()); 58 suite.addTest(new ByteArrayOutputStreamSinkTester(0).setThrowsExceptions(false).createTests()); 59 suite.addTest(new ByteArrayOutputStreamSinkTester(4).setThrowsExceptions(false).createTests()); 60 suite.addTest(new PipedOutputStreamSinkTester().createTests()); 61 62 // wrapper tests 63 suite.addTest(new BufferedOutputStreamTester(1).createTests()); 64 suite.addTest(new BufferedOutputStreamTester(5).createTests()); 65 suite.addTest(new BufferedOutputStreamTester(1024).createTests()); 66 suite.addTest(new FilterOutputStreamTester().createTests()); 67 suite.addTest(new DataOutputStreamTester().createTests()); 68 // fails wrapperTestFlushThrowsViaClose() and sinkTestWriteAfterClose(): 69 // suite.addTest(new ObjectOutputStreamTester().createTests()); 70 suite.addTest(new PrintStreamTester().setThrowsExceptions(false).createTests()); 71 72 return suite; 73 } 74 75 private static class FileOutputStreamSinkTester extends SinkTester { 76 77 private final boolean append; 78 private File file; 79 80 private FileOutputStreamSinkTester(boolean append) { 81 this.append = append; 82 } 83 84 public OutputStream create() throws IOException { 85 file = File.createTempFile("FileOutputStreamSinkTester", "tmp"); 86 file.deleteOnExit(); 87 return new FileOutputStream(file, append); 88 } 89 90 public byte[] getBytes() throws IOException { 91 return Streams.streamToBytes(new FileInputStream(file)); 92 } 93 } 94 95 private static class ByteArrayOutputStreamSinkTester extends SinkTester { 96 97 private final int size; 98 private ByteArrayOutputStream stream; 99 100 private ByteArrayOutputStreamSinkTester(int size) { 101 this.size = size; 102 } 103 104 public OutputStream create() throws IOException { 105 stream = new ByteArrayOutputStream(size); 106 return stream; 107 } 108 109 public byte[] getBytes() throws IOException { 110 return stream.toByteArray(); 111 } 112 } 113 114 private static class PipedOutputStreamSinkTester extends SinkTester { 115 116 private ExecutorService executor; 117 private Future<byte[]> future; 118 119 public OutputStream create() throws IOException { 120 final PipedInputStream in = new PipedInputStream(); 121 PipedOutputStream out = new PipedOutputStream(in); 122 123 executor = Executors.newSingleThreadExecutor(); 124 future = executor.submit(new Callable<byte[]>() { 125 final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 126 127 public byte[] call() throws Exception { 128 byte[] buffer = new byte[256]; 129 int count; 130 while ((count = in.read(buffer)) != -1) { 131 bytes.write(buffer, 0, count); 132 } 133 return bytes.toByteArray(); 134 } 135 }); 136 137 return out; 138 } 139 140 public byte[] getBytes() throws Exception { 141 executor.shutdown(); 142 return future.get(); 143 } 144 } 145 146 private static class FilterOutputStreamTester extends WrapperTester { 147 148 public OutputStream create(OutputStream delegate) throws Exception { 149 return new FilterOutputStream(delegate); 150 } 151 152 public byte[] decode(byte[] delegateBytes) throws Exception { 153 return delegateBytes; 154 } 155 } 156 157 private static class BufferedOutputStreamTester extends WrapperTester { 158 private final int bufferSize; 159 160 private BufferedOutputStreamTester(int bufferSize) { 161 this.bufferSize = bufferSize; 162 } 163 164 public OutputStream create(OutputStream delegate) throws Exception { 165 return new BufferedOutputStream(delegate, bufferSize); 166 } 167 168 public byte[] decode(byte[] delegateBytes) throws Exception { 169 return delegateBytes; 170 } 171 } 172 173 private static class DataOutputStreamTester extends WrapperTester { 174 175 public OutputStream create(OutputStream delegate) throws Exception { 176 return new DataOutputStream(delegate); 177 } 178 179 public byte[] decode(byte[] delegateBytes) throws Exception { 180 return delegateBytes; 181 } 182 } 183 184 private static class ObjectOutputStreamTester extends WrapperTester { 185 186 public OutputStream create(OutputStream delegate) throws Exception { 187 return new ObjectOutputStream(delegate); 188 } 189 190 public byte[] decode(byte[] delegateBytes) throws Exception { 191 return Streams.streamToBytes(new ObjectInputStream( 192 new ByteArrayInputStream(delegateBytes))); 193 } 194 } 195 196 private static class PrintStreamTester extends WrapperTester { 197 198 public OutputStream create(OutputStream delegate) throws Exception { 199 return new PrintStream(delegate); 200 } 201 202 public byte[] decode(byte[] delegateBytes) throws Exception { 203 return delegateBytes; 204 } 205 } 206} 207