1 /*
   2  * Copyright (c) 2016, 2017, 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 8148023
  27  * @summary Verify that createTempFile() will not fail for long component names.
  28  */
  29 
  30 import java.io.File;
  31 import java.io.IOException;
  32 
  33 public class NameTooLong {
  34     public static void main(String[] args) {
  35         String[][] prefixSuffix = new String[][] {
  36             new String[] {"1234567890123456789012345678901234567xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890","txt"},
  37             new String[] {"prefix","1234567890123456789012345678901234567xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.txt"},
  38             new String[] {"prefix",".txt1234567890123456789012345678901234567xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"}
  39         };
  40 
  41         int failures = 0;
  42         int index = 0;
  43         for (String[] ps : prefixSuffix) {
  44             File f;
  45             try {
  46                 f = File.createTempFile(ps[0], ps[1],
  47                         new File(System.getProperty("test.dir", ".")));
  48                 String s = f.toPath().getFileName().toString();
  49                 if (!s.startsWith(ps[0].substring(0, 3))) {
  50                     System.err.printf("%s did not start with %s%n", s,
  51                         ps[0].substring(0, 3));
  52                     failures++;
  53                 }
  54                 if (ps[1].startsWith(".")
  55                     && !s.contains(ps[1].substring(0, 4))) {
  56                     System.err.printf("%s did not contain %s%n", s,
  57                         ps[1].substring(0, 4));;
  58                     failures++;
  59                 }
  60             } catch (IOException e) {
  61                 failures++;
  62                 System.err.println();
  63                 e.printStackTrace();
  64                 System.err.println();
  65             }
  66             index++;
  67         }
  68 
  69         if (failures != 0) {
  70             throw new RuntimeException("Test failed!");
  71         }
  72     }
  73 }