Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/File/createTempFile/SpecialTempFile.java
38821 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;3233public class SpecialTempFile {3435private static void test(String name, String[] prefix, String[] suffix,36boolean exceptionExpected) throws IOException37{38if (prefix == null || suffix == null39|| prefix.length != suffix.length)40{41return;42}4344final String exceptionMsg = "Unable to create temporary file";45String[] dirs = { null, "." };4647for (int i = 0; i < prefix.length; i++) {48boolean exceptionThrown = false;49File f = null;5051for (String dir: dirs) {52System.out.println("In test " + name +53", creating temp file with prefix, " +54prefix[i] + ", suffix, " + suffix[i] +55", in dir, " + dir);5657try {58if (dir == null || dir.isEmpty())59f = File.createTempFile(prefix[i], suffix[i]);60else61f = File.createTempFile(prefix[i], suffix[i], new File(dir));62} catch (IOException e) {63if (exceptionExpected) {64if (e.getMessage().startsWith(exceptionMsg))65exceptionThrown = true;66else67System.out.println("Wrong error message:" +68e.getMessage());69} else {70throw e;71}72}7374if (exceptionExpected && (!exceptionThrown || f != null))75throw new RuntimeException("IOException is expected");76}77}78}7980public static void main(String[] args) throws Exception {81// Common test82final String name = "SpecialTempFile";83File f = new File(System.getProperty("java.io.tmpdir"), name);84if (!f.exists()) {85f.createNewFile();86}87String[] nulPre = { name + "\u0000" };88String[] nulSuf = { ".test" };89test("NulName", nulPre, nulSuf, true);9091// Test JDK-802512892String[] goodPre = { "///..///", "/foo" };93String[] goodSuf = { ".temp", ".tmp" };94test("goodName", goodPre, goodSuf, false);9596// Test JDK-801195097String[] slashPre = { "temp", "///..///", "/foo" };98String[] slashSuf = { "///..///..", "///..///..", "///..///.." };99test("SlashedName", slashPre, slashSuf, true);100101// Windows tests102if (!System.getProperty("os.name").startsWith("Windows"))103return;104105// Test JDK-8013827106String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };107String[] resvSuf = { ".temp", ".temp" };108test("ReservedName", resvPre, resvSuf, true);109}110}111112113