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 
  35 public class SpecialTempFile {
  36 
  37     private static void test(String name, String[] prefix, String[] suffix) {
  38         if (prefix == null || suffix == null
  39             || prefix.length != suffix.length)
  40         {
  41             return;
  42         }
  43 
  44         final String exceptionMsg = "Unable to create temporary file";
  45         final String errMsg = "IOException is expected";
  46 
  47         for (int i = 0; i < prefix.length; i++) {
  48             boolean exceptionThrown = false;
  49             File f = null;
  50             System.out.println("In test " + name
  51                                + ", creating temp file with prefix, "
  52                                + prefix[i] + ", suffix, " + suffix[i]);
  53             try {
  54                 f = File.createTempFile(prefix[i], suffix[i]);
  55             } catch (IOException e) {
  56                 if (exceptionMsg.equals(e.getMessage()))
  57                     exceptionThrown = true;
  58                 else
  59                     System.out.println("Wrong error message:" + e.getMessage());
  60             }
  61             if (!exceptionThrown || f != null)
  62                 throw new RuntimeException(errMsg);
  63         }
  64     }
  65 
  66     public static void main(String[] args) throws Exception {
  67         if (!System.getProperty("os.name").startsWith("Windows"))
  68             return;
  69 
  70         // Test JDK-8013827
  71         String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
  72         String[] resvSuf = { ".temp", ".temp" };
  73         test("ReservedName", resvPre, resvSuf);
  74 
  75         // Test JDK-8011950
  76         String[] slashPre = { "///..///", "temp", "///..///" };
  77         String[] slashSuf = { ".temp", "///..///..", "///..///.." };
  78         test("SlashedName", slashPre, slashSuf);
  79     }
  80 }