< prev index next >

test/sun/security/provider/FileInputStreamPool/FileInputStreamPoolTest.java

Print this page
rev 15227 : imported patch vm_api
   1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  52         byte[] readBytes = new byte[bytes.length];
  53         int nread = in1.read(readBytes);
  54         assertTrue(bytes.length == nread,
  55             "short read: " + nread +
  56                 " bytes of expected: " + bytes.length);
  57         assertTrue(Arrays.equals(readBytes, bytes),
  58             "readBytes: " + Arrays.toString(readBytes) +
  59                 " not equal to expected: " + Arrays.toString(bytes));
  60     }
  61 
  62     static void assertTrue(boolean test, String message) {
  63         if (!test) {
  64             throw new AssertionError(message);
  65         }
  66     }
  67 
  68     static void processReferences() {
  69         // make JVM process References
  70         System.gc();
  71         // help ReferenceHandler thread enqueue References
  72         while (TestProxy.Reference_tryHandlePending(false)) {}
  73         // help run Finalizers
  74         System.runFinalization();
  75     }
  76 
  77     public static void main(String[] args) throws Exception {
  78         // 1st create temporary file
  79         File file = File.createTempFile("test", ".dat");
  80         try (AutoCloseable acf = () -> {
  81             // On Windows, failure to delete file is probably a consequence
  82             // of the file still being opened - so the test should fail.
  83             assertTrue(file.delete(),
  84                 "Can't delete: " + file + " (is it still open?)");
  85         }) {
  86             try (FileOutputStream out = new FileOutputStream(file)) {
  87                 out.write(bytes);
  88             }
  89 
  90             // test caching 1t time
  91             testCaching(file);
  92 
  93             processReferences();
  94 
  95             // test caching 2nd time - this should only succeed if the stream
  96             // is re-opened as a consequence of cleared WeakReference
  97             testCaching(file);
  98 
  99             processReferences();
 100         }
 101     }
 102 
 103     /**
 104      * A proxy for (package)private static methods:
 105      *   sun.security.provider.FileInputStreamPool.getInputStream
 106      *   java.lang.ref.Reference.tryHandlePending
 107      */
 108     static class TestProxy {
 109         private static final Method getInputStreamMethod;
 110         private static final Method tryHandlePendingMethod;
 111 
 112         static {
 113             try {
 114                 Class<?> fileInputStreamPoolClass =
 115                     Class.forName("sun.security.provider.FileInputStreamPool");
 116                 getInputStreamMethod =
 117                     fileInputStreamPoolClass.getDeclaredMethod(
 118                         "getInputStream", File.class);
 119                 getInputStreamMethod.setAccessible(true);
 120 
 121                 tryHandlePendingMethod = Reference.class.getDeclaredMethod(
 122                     "tryHandlePending", boolean.class);
 123                 tryHandlePendingMethod.setAccessible(true);
 124             } catch (Exception e) {
 125                 throw new Error(e);
 126             }
 127         }
 128 
 129         static InputStream FileInputStreamPool_getInputStream(File file)
 130             throws IOException {
 131             try {
 132                 return (InputStream) getInputStreamMethod.invoke(null, file);
 133             } catch (InvocationTargetException e) {
 134                 Throwable te = e.getTargetException();
 135                 if (te instanceof IOException) {
 136                     throw (IOException) te;
 137                 } else if (te instanceof RuntimeException) {
 138                     throw (RuntimeException) te;
 139                 } else if (te instanceof Error) {
 140                     throw (Error) te;
 141                 } else {
 142                     throw new UndeclaredThrowableException(te);
 143                 }
 144             } catch (IllegalAccessException e) {
 145                 throw new RuntimeException(e);
 146             }
 147         }
 148 
 149         static boolean Reference_tryHandlePending(boolean waitForNotify) {
 150             try {
 151                 return (boolean) tryHandlePendingMethod
 152                     .invoke(null, waitForNotify);
 153             } catch (InvocationTargetException e) {
 154                 Throwable te = e.getTargetException();
 155                 if (te instanceof RuntimeException) {


 156                     throw (RuntimeException) te;
 157                 } else if (te instanceof Error) {
 158                     throw (Error) te;
 159                 } else {
 160                     throw new UndeclaredThrowableException(te);
 161                 }
 162             } catch (IllegalAccessException e) {
 163                 throw new RuntimeException(e);
 164             }
 165         }
 166     }
 167 }
   1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  52         byte[] readBytes = new byte[bytes.length];
  53         int nread = in1.read(readBytes);
  54         assertTrue(bytes.length == nread,
  55             "short read: " + nread +
  56                 " bytes of expected: " + bytes.length);
  57         assertTrue(Arrays.equals(readBytes, bytes),
  58             "readBytes: " + Arrays.toString(readBytes) +
  59                 " not equal to expected: " + Arrays.toString(bytes));
  60     }
  61 
  62     static void assertTrue(boolean test, String message) {
  63         if (!test) {
  64             throw new AssertionError(message);
  65         }
  66     }
  67 
  68     static void processReferences() {
  69         // make JVM process References
  70         System.gc();
  71         // help ReferenceHandler thread enqueue References
  72         while (TestProxy.Reference_waitForReferenceProcessing()) { }
  73         // help run Finalizers
  74         System.runFinalization();
  75     }
  76 
  77     public static void main(String[] args) throws Exception {
  78         // 1st create temporary file
  79         File file = File.createTempFile("test", ".dat");
  80         try (AutoCloseable acf = () -> {
  81             // On Windows, failure to delete file is probably a consequence
  82             // of the file still being opened - so the test should fail.
  83             assertTrue(file.delete(),
  84                 "Can't delete: " + file + " (is it still open?)");
  85         }) {
  86             try (FileOutputStream out = new FileOutputStream(file)) {
  87                 out.write(bytes);
  88             }
  89 
  90             // test caching 1t time
  91             testCaching(file);
  92 
  93             processReferences();
  94 
  95             // test caching 2nd time - this should only succeed if the stream
  96             // is re-opened as a consequence of cleared WeakReference
  97             testCaching(file);
  98 
  99             processReferences();
 100         }
 101     }
 102 
 103     /**
 104      * A proxy for (package)private static methods:
 105      *   sun.security.provider.FileInputStreamPool.getInputStream
 106      *   java.lang.ref.Reference.waitForReferenceProcessing
 107      */
 108     static class TestProxy {
 109         private static final Method getInputStreamMethod;
 110         private static final Method waitForReferenceProcessingMethod;
 111 
 112         static {
 113             try {
 114                 Class<?> fileInputStreamPoolClass =
 115                     Class.forName("sun.security.provider.FileInputStreamPool");
 116                 getInputStreamMethod =
 117                     fileInputStreamPoolClass.getDeclaredMethod(
 118                         "getInputStream", File.class);
 119                 getInputStreamMethod.setAccessible(true);
 120 
 121                 waitForReferenceProcessingMethod =
 122                     Reference.class.getDeclaredMethod("waitForReferenceProcessing");
 123                 waitForReferenceProcessingMethod.setAccessible(true);
 124             } catch (Exception e) {
 125                 throw new Error(e);
 126             }
 127         }
 128 
 129         static InputStream FileInputStreamPool_getInputStream(File file)
 130             throws IOException {
 131             try {
 132                 return (InputStream) getInputStreamMethod.invoke(null, file);
 133             } catch (InvocationTargetException e) {
 134                 Throwable te = e.getTargetException();
 135                 if (te instanceof IOException) {
 136                     throw (IOException) te;
 137                 } else if (te instanceof RuntimeException) {
 138                     throw (RuntimeException) te;
 139                 } else if (te instanceof Error) {
 140                     throw (Error) te;
 141                 } else {
 142                     throw new UndeclaredThrowableException(te);
 143                 }
 144             } catch (IllegalAccessException e) {
 145                 throw new RuntimeException(e);
 146             }
 147         }
 148 
 149         static boolean Reference_waitForReferenceProcessing() {
 150             try {
 151                 return (boolean) waitForReferenceProcessingMethod.invoke(null);

 152             } catch (InvocationTargetException e) {
 153                 Throwable te = e.getTargetException();
 154                 if (te instanceof InterruptedException) {
 155                     return true;
 156                 } else if (te instanceof RuntimeException) {
 157                     throw (RuntimeException) te;
 158                 } else if (te instanceof Error) {
 159                     throw (Error) te;
 160                 } else {
 161                     throw new UndeclaredThrowableException(te);
 162                 }
 163             } catch (IllegalAccessException e) {
 164                 throw new RuntimeException(e);
 165             }
 166         }
 167     }
 168 }
< prev index next >