/* * 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 * @summary Check whether File.createTempFile can handle special parameters * on Windows platforms * @author Dan Xu */ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; public class SpecialTempFile { /** * The maximum time, 2 seconds, to wait for each test. */ private static final int TIME_OUT = 2; static class TestTask implements Runnable { private String name; private String[] prefix; private String[] suffix; public TestTask(String test, String[] pre, String[] suf) { name = test; prefix = pre; suffix = suf; } @Override public void run() { if (prefix == null || suffix == null || prefix.length != suffix.length) { return; } final String exceptionMsg = "Unable to create temporary file"; final String errMsg = "IOException is expected"; for (int i = 0; i < prefix.length; i++) { boolean exceptionThrown = false; File f = null; System.out.println("In test " + name + ", creating temp file with prefix, " + prefix[i] + ", suffix, " + suffix[i]); try { f = File.createTempFile(prefix[i], suffix[i]); } catch (IOException e) { if (exceptionMsg.equals(e.getMessage())) exceptionThrown = true; } if (!exceptionThrown || f != null) throw new RuntimeException(errMsg); } } } public static void main(String[] args) throws Exception { if (!System.getProperty("os.name").startsWith("Windows")) return; List testList = new ArrayList<>(2); // Test JDK-8013827 String[] resvPre = {"LPT1.package.zip", "com7.4.package.zip"}; String[] resvSuf = {".temp", ".temp"}; testList.add(new TestTask("ReservedName", resvPre, resvSuf)); // Test JDK-8011950 String[] slashPre = {"///..///", "temp", "///..///"}; String[] slashSuf = {".temp", "///..///..", "///..///.."}; testList.add(new TestTask("SlashedName", slashPre, slashSuf)); ExecutorService executor = Executors.newSingleThreadExecutor(); try { for (int i = 0; i < testList.size(); i++) { Future future = executor.submit(testList.get(i)); future.get(TIME_OUT, TimeUnit.SECONDS); } } finally { executor.shutdownNow(); } } }