1 /*
   2  * Copyright (c) 2016, 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                 String s = f.toPath().getFileName().toString();
  48                 if (!s.startsWith(ps[0].substring(0, 3))) {
  49                     System.err.printf("%s did not start with %s%n", s,
  50                         ps[0].substring(0, 3));
  51                     failures++;
  52                 }
  53                 if (ps[1].startsWith(".")
  54                     && !s.contains(ps[1].substring(0, 4))) {
  55                     System.err.printf("%s did not contain %s%n", s,
  56                         ps[1].substring(0, 4));;
  57                     failures++;
  58                 }
  59             } catch (IOException e) {
  60                 failures++;
  61                 System.err.println();
  62                 e.printStackTrace();
  63                 System.err.println();
  64             }
  65             index++;
  66         }
  67 
  68         if (failures != 0) {
  69             throw new RuntimeException("Test failed!");
  70         }
  71     }
  72 }