test/jdk/jdk/nio/zipfs/ReleaseDeflater.java
changeset 59112 fe87a92570db
equal deleted inserted replaced
59110:8c4c358272a9 59112:fe87a92570db
       
     1 /*
       
     2  * Copyright 2019 Amazon.com, Inc. 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  *
       
    23  */
       
    24 
       
    25 /*
       
    26  * @test
       
    27  * @bug 8234011
       
    28  * @summary Check that jdk.nio.zipfs.ZipFileSystem doesn't cache more than ZipFileSystem.MAX_FLATER Inflater/Deflater objects
       
    29  * @run main ReleaseDeflater
       
    30  * @modules jdk.zipfs/jdk.nio.zipfs:+open
       
    31  * @author Volker Simonis
       
    32  */
       
    33 
       
    34 import java.io.InputStream;
       
    35 import java.io.OutputStream;
       
    36 import java.lang.reflect.Field;
       
    37 import java.nio.file.FileSystem;
       
    38 import java.nio.file.FileSystems;
       
    39 import java.nio.file.Files;
       
    40 import java.nio.file.Path;
       
    41 import java.nio.file.Paths;
       
    42 import java.nio.file.spi.FileSystemProvider;
       
    43 import java.util.List;
       
    44 import java.util.Map;
       
    45 import java.util.ArrayList;
       
    46 
       
    47 public class ReleaseDeflater {
       
    48     public static void main(String[] args) throws Throwable {
       
    49         Path zipFile = Paths.get("ReleaseDeflaterTest.zip");
       
    50         try (FileSystem fs = FileSystems.newFileSystem(zipFile, Map.of("create", true))) {
       
    51             FileSystemProvider zprov = fs.provider();
       
    52             Path test = fs.getPath("test.txt");
       
    53             int STREAMS = 100;
       
    54             List<OutputStream> ostreams = new ArrayList<>(STREAMS);
       
    55             List<InputStream> istreams = new ArrayList<>(STREAMS);
       
    56             for (int i = 0; i < STREAMS; i++) {
       
    57                 OutputStream zos = zprov.newOutputStream(test);
       
    58                 ostreams.add(zos);
       
    59                 zos.write("Hello".getBytes());
       
    60             }
       
    61             for (OutputStream os : ostreams) {
       
    62                 os.close();
       
    63             }
       
    64             for (int i = 0; i < STREAMS; i++) {
       
    65                 InputStream zis = zprov.newInputStream(test);
       
    66                 istreams.add(zis);
       
    67             }
       
    68             for (InputStream is : istreams) {
       
    69                 is.close();
       
    70             }
       
    71             try {
       
    72                 Field max_flaters = fs.getClass().getDeclaredField("MAX_FLATER");
       
    73                 max_flaters.setAccessible(true);
       
    74                 int MAX_FLATERS = max_flaters.getInt(fs);
       
    75                 Field inflaters = fs.getClass().getDeclaredField("inflaters");
       
    76                 inflaters.setAccessible(true);
       
    77                 int inflater_count = ((List<?>) inflaters.get(fs)).size();
       
    78                 if (inflater_count > MAX_FLATERS) {
       
    79                     throw new Exception("Too many inflaters " + inflater_count);
       
    80                 }
       
    81                 Field deflaters = fs.getClass().getDeclaredField("deflaters");
       
    82                 deflaters.setAccessible(true);
       
    83                 int deflater_count = ((List<?>) deflaters.get(fs)).size();
       
    84                 if (deflater_count > MAX_FLATERS) {
       
    85                     throw new Exception("Too many deflaters " + deflater_count);
       
    86                 }
       
    87             } catch (NoSuchFieldException nsfe) {
       
    88                 // Probably the implementation has changed, so there's not much we can do...
       
    89                 throw new RuntimeException("Implementation of jdk.nio.zipfs.ZipFileSystem changed - disable or fix the test");
       
    90             }
       
    91         } finally {
       
    92             Files.deleteIfExists(zipFile);
       
    93         }
       
    94 
       
    95     }
       
    96 }