--- /dev/null 2014-12-15 09:34:06.624986903 +0100 +++ new/test/sun/security/provider/FileInputStreamPool/FileInputStreamPoolTest.java 2014-12-18 13:58:40.528007419 +0100 @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.UndeclaredThrowableException; +import java.util.Arrays; + +/** + * @test a unit test for sun.security.provider.FileInputStreamPool + * @bug 8047769 + * @summary test behaviour of sun.security.provider.FileInputStreamPool + */ + +public class FileInputStreamPoolTest { + + static final byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8}; + + static void testCaching(File file) throws IOException { + InputStream in1 = FileInputStreamPoolProxy.getInputStream(file); + InputStream in2 = FileInputStreamPoolProxy.getInputStream(file); + assert in1 == in2; + + byte[] readBytes = new byte[bytes.length]; + int nread = in1.read(readBytes); + assert nread == bytes.length; // if this fails then we are already at EOF? + assert Arrays.equals(readBytes, bytes); + } + + public static void main(String[] args) throws Exception { + // 1st create temporary file + File file = File.createTempFile("test", ".dat"); + try { + try (FileOutputStream out = new FileOutputStream(file)) { + out.write(bytes); + } + + // test caching 1t time + testCaching(file); + + // make GC process WeakReferences + System.gc(); + // wait a while for ReferenceHandler to enqueue WeakReferences + // if this fails on slow or busy machines, sleep should be + // replaced with: + // while (java.lang.ref.Reference.tryHandlePending(false)) {} + Thread.sleep(500L); + + // test caching 2nd time - this should only succeed if + // the stream has been closed and re-opened as a consequence + // of cleared WeakReference + testCaching(file); + } finally { + file.delete(); + } + } + + /** + * A proxy for package-private sun.security.provider.FileInputStreamPool + */ + static class FileInputStreamPoolProxy { + private static final Method getInputStreamMethod; + + static { + try { + Class fileInputStreamPoolClass = Class.forName("sun.security.provider.FileInputStreamPool"); + getInputStreamMethod = fileInputStreamPoolClass.getDeclaredMethod("getInputStream", File.class); + getInputStreamMethod.setAccessible(true); + } catch (Exception e) { + throw new Error(e); + } + } + + static InputStream getInputStream(File file) throws IOException { + try { + return (InputStream) getInputStreamMethod.invoke(null, file); + } catch (InvocationTargetException e) { + Throwable te = e.getTargetException(); + if (te instanceof IOException) { + throw (IOException) te; + } else if (te instanceof RuntimeException) { + throw (RuntimeException) te; + } else if (te instanceof Error) { + throw (Error) te; + } else { + throw new UndeclaredThrowableException(te); + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } +}