view test/java/io/File/createTempFile/SpecialTempFile.java @ 6384:3e7df93144c6

8025128: File.createTempFile fails if prefix is absolute path Reviewed-by: alanb, darcy
author dxu
date Fri, 27 Sep 2013 00:01:04 +0100
parents 6b5c05cfff7c
children
line wrap: on
line source

/*
 * Copyright (c) 2013, 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.
 */

/*
 * @test
 * @bug 8013827 8011950 8025128
 * @summary Check whether File.createTempFile can handle special parameters
 *          on Windows platforms
 * @author Dan Xu
 */

import java.io.File;
import java.io.IOException;

public class SpecialTempFile {

    private static void test(String name, String[] prefix, String[] suffix,
                             boolean expectedException) throws IOException
    {
        if (prefix == null || suffix == null
            || prefix.length != suffix.length)
        {
            return;
        }

        final String exceptionMsg = "Unable to create temporary file";

        for (int i = 0; i < prefix.length; i++) {
            boolean exceptionThrown = false;
            File f = null;

            String[] dirs = { null, "." };

            for (String dir : dirs ) {
                System.out.println("In test " + name +
                                   ", creating temp file with prefix, " +
                                   prefix[i] + ", suffix, " + suffix[i] +
                                   ", in dir, " + dir);

                try {
                    if (dir == null || dir.isEmpty())
                        f = File.createTempFile(prefix[i], suffix[i]);
                    else
                        f = File.createTempFile(prefix[i], suffix[i], new File(dir));
                } catch (IOException e) {
                    if (expectedException) {
                        if (e.getMessage().startsWith(exceptionMsg))
                            exceptionThrown = true;
                        else
                            System.out.println("Wrong error message:" +
                                               e.getMessage());
                    } else {
                        throw e;
                    }
                }

                if (expectedException && (!exceptionThrown || f != null))
                    throw new RuntimeException("IOException is expected");
            }
        }
    }

    public static void main(String[] args) throws Exception {
        // Test JDK-8025128
        String[] goodPre = { "///..///", "/foo" };
        String[] goodSuf = { ".temp", ".tmp" };
        test("goodName", goodPre, goodSuf, false);

        // Test JDK-8011950
        String[] slashPre = { "temp", "///..///", "/foo" };
        String[] slashSuf = { "///..///..", "///..///..", "///..///.." };
        test("SlashedName", slashPre, slashSuf, true);

        if (!System.getProperty("os.name").startsWith("Windows"))
            return;

        // Test JDK-8013827
        String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
        String[] resvSuf = { ".temp", ".temp" };
        test("ReservedName", resvPre, resvSuf, true);
    }
}