Path: blob/master/test/jdk/java/io/File/createTempFile/SpecialTempFile.java
41152 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8013827 8011950 8017212 802512826* @summary Check whether File.createTempFile can handle special parameters27* @author Dan Xu28*/2930import java.io.File;31import java.io.IOException;32import java.nio.file.Files;33import java.nio.file.Path;34import java.nio.file.Paths;3536public class SpecialTempFile {3738private static void test(String name, String[] prefix, String[] suffix,39boolean exceptionExpected) throws IOException40{41if (prefix == null || suffix == null42|| prefix.length != suffix.length)43{44return;45}4647final String exceptionMsg = "Unable to create temporary file";48String[] dirs = { null, "." };4950Path testPath = Paths.get(System.getProperty("test.dir", "."));51for (int i = 0; i < prefix.length; i++) {52boolean exceptionThrown = false;53File f = null;5455for (String dir: dirs) {56Path tempDir = Files.createTempDirectory(testPath, dir);57System.out.println("In test " + name +58", creating temp file with prefix, " +59prefix[i] + ", suffix, " + suffix[i] +60", in dir, " + tempDir);6162try {63f = File.createTempFile(prefix[i], suffix[i],64tempDir.toFile());65} catch (IOException e) {66if (exceptionExpected) {67if (e.getMessage().startsWith(exceptionMsg))68exceptionThrown = true;69else70System.out.println("Wrong error message:" +71e.getMessage());72} else {73throw e;74}75}7677if (exceptionExpected && (!exceptionThrown || f != null))78throw new RuntimeException("IOException is expected");79}80}81}8283public static void main(String[] args) throws Exception {84// Common test85final String name = "SpecialTempFile";86String[] nulPre = { name + "\u0000" };87String[] nulSuf = { ".test" };88test("NulName", nulPre, nulSuf, true);8990// Test JDK-802512891String[] goodPre = { "///..///", "/foo" };92String[] goodSuf = { ".temp", ".tmp" };93test("goodName", goodPre, goodSuf, false);9495// Test JDK-801195096String[] slashPre = { "temp", "///..///", "/foo" };97String[] slashSuf = { "///..///..", "///..///..", "///..///.." };98test("SlashedName", slashPre, slashSuf, true);99100// Windows tests101if (!System.getProperty("os.name").startsWith("Windows"))102return;103104// Test JDK-8013827105String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };106String[] resvSuf = { ".temp", ".temp" };107test("ReservedName", resvPre, resvSuf, true);108}109}110111112