1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8013827 8011950 27 * @summary Check whether File.createTempFile can handle special parameters 28 * on Windows platforms 29 * @author Dan Xu 30 */ 31 32 import java.io.File; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.List; 36 import java.util.concurrent.ExecutorService; 37 import java.util.concurrent.Executors; 38 import java.util.concurrent.Future; 39 import java.util.concurrent.TimeUnit; 40 41 public class SpecialTempFile { 42 /** 43 * The maximum time, 2 seconds, to wait for each test. 44 */ 45 private static final int TIME_OUT = 2; 46 47 static class TestTask implements Runnable { 48 49 private String name; 50 private String[] prefix; 51 private String[] suffix; 52 53 public TestTask(String test, String[] pre, String[] suf) { 54 name = test; 55 prefix = pre; 56 suffix = suf; 57 } 58 59 @Override 60 public void run() { 61 if (prefix == null || suffix == null 62 || prefix.length != suffix.length) 63 { 64 return; 65 } 66 67 final String exceptionMsg = "Unable to create temporary file"; 68 final String errMsg = "IOException is expected"; 69 70 for (int i = 0; i < prefix.length; i++) { 71 boolean exceptionThrown = false; 72 File f = null; 73 System.out.println("In test " + name 74 + ", creating temp file with prefix, " 75 + prefix[i] + ", suffix, " + suffix[i]); 76 try { 77 f = File.createTempFile(prefix[i], suffix[i]); 78 } catch (IOException e) { 79 if (exceptionMsg.equals(e.getMessage())) 80 exceptionThrown = true; 81 } 82 if (!exceptionThrown || f != null) 83 throw new RuntimeException(errMsg); 84 } 85 } 86 } 87 88 public static void main(String[] args) throws Exception { 89 if (!System.getProperty("os.name").startsWith("Windows")) 90 return; 91 92 List<Runnable> testList = new ArrayList<>(2); 93 // Test JDK-8013827 94 String[] resvPre = {"LPT1.package.zip", "com7.4.package.zip"}; 95 String[] resvSuf = {".temp", ".temp"}; 96 testList.add(new TestTask("ReservedName", resvPre, resvSuf)); 97 98 // Test JDK-8011950 99 String[] slashPre = {"///..///", "temp", "///..///"}; 100 String[] slashSuf = {".temp", "///..///..", "///..///.."}; 101 testList.add(new TestTask("SlashedName", slashPre, slashSuf)); 102 103 ExecutorService executor = Executors.newSingleThreadExecutor(); 104 try { 105 for (int i = 0; i < testList.size(); i++) { 106 Future<?> future = executor.submit(testList.get(i)); 107 future.get(TIME_OUT, TimeUnit.SECONDS); 108 } 109 } finally { 110 executor.shutdownNow(); 111 } 112 } 113 }