langtools/test/tools/javac/T6558476.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 16800 3267051107a1
child 27234 f3f667e5870c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     1
/*
16800
3267051107a1 8010511: Tests are creating files in /tmp
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     4
 *
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     8
 *
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    13
 * accompanied this code).
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    14
 *
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3656
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3656
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3656
diff changeset
    21
 * questions.
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    22
 */
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    23
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    24
/*
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    25
 * @test
16800
3267051107a1 8010511: Tests are creating files in /tmp
jjg
parents: 5520
diff changeset
    26
 * @bug 6558476
3267051107a1 8010511: Tests are creating files in /tmp
jjg
parents: 5520
diff changeset
    27
 * @summary com/sun/tools/javac/Main.compile don't release file handles on return
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    28
 * @run main/othervm -Xmx512m -Xms512m  T6558476
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    29
 */
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    30
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    31
import java.io.File;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    32
import java.io.FileInputStream;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    33
import java.io.FileOutputStream;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    34
import java.io.IOException;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    35
import java.util.Random;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    36
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    37
import com.sun.tools.javac.Main;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    38
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    39
public class T6558476 {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    40
    private static File copyFileTo(File file, File directory) throws IOException {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    41
        File newFile = new File(directory, file.getName());
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    42
        FileInputStream fis = null;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    43
        FileOutputStream fos = null;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    44
        try {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    45
            fis = new FileInputStream(file);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    46
            fos = new FileOutputStream(newFile);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    47
            byte buff[] = new byte[1024];
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    48
            int val;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    49
            while ((val = fis.read(buff)) > 0)
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    50
                fos.write(buff, 0, val);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    51
        } finally {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    52
            if (fis != null)
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    53
                fis.close();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    54
            if (fos != null)
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    55
                fos.close();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    56
        }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    57
        return newFile;
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    58
    }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    59
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    60
    private static String generateJavaClass(String className) {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    61
        StringBuffer sb = new StringBuffer();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    62
        sb.append("import sun.net.spi.nameservice.dns.DNSNameService;\n");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    63
        sb.append("public class ");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    64
        sb.append(className);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    65
        sb.append(" {\n");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    66
        sb.append("  public void doStuff() {\n");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    67
        sb.append("    DNSNameService dns = null;\n");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    68
        sb.append("  }\n");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    69
        sb.append("}\n");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    70
        return sb.toString();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    71
    }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    72
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    73
    public static void main(String[] args) throws IOException {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    74
        File javaHomeDir = new File(System.getProperty("java.home"));
16800
3267051107a1 8010511: Tests are creating files in /tmp
jjg
parents: 5520
diff changeset
    75
        File outputDir = new File("outputDir" + new Random().nextInt(65536));
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    76
        outputDir.mkdir();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    77
        outputDir.deleteOnExit();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    78
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    79
        File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    80
        File tmpJar = copyFileTo(dnsjarfile, outputDir);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    81
        String className = "TheJavaFile";
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    82
        File javaFile = new File(outputDir, className + ".java");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    83
        javaFile.deleteOnExit();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    84
        FileOutputStream fos = new FileOutputStream(javaFile);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    85
        fos.write(generateJavaClass(className).getBytes());
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    86
        fos.close();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    87
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    88
        int rc = Main.compile(new String[]{"-d", outputDir.getPath(),
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    89
                    "-classpath",
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    90
                    tmpJar.getPath(),
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    91
                    javaFile.getAbsolutePath()});
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    92
        if (rc != 0) {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    93
            throw new Error("Couldn't compile the file (exit code=" + rc + ")");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    94
        }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    95
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    96
        if (tmpJar.delete()) {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    97
            System.out.println("jar file successfully deleted");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    98
        } else {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
    99
            throw new Error("Error deleting file \"" + tmpJar.getPath() + "\"");
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
   100
        }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
   101
    }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
diff changeset
   102
}