tests/libjava-mauve/src/gnu/testlet/java/io/File/createFile.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.2
       
     2 
       
     3 // Copyright (C) 2005 Audrius Meskauskas (AudriusA@Bioinformatics.org)
       
     4 
       
     5 // Mauve is free software; you can redistribute it and/or modify
       
     6 // it under the terms of the GNU General Public License as published by
       
     7 // the Free Software Foundation; either version 2, or (at your option)
       
     8 // any later version.
       
     9 
       
    10 // Mauve is distributed in the hope that it will be useful,
       
    11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 // GNU General Public License for more details.
       
    14 
       
    15 // You should have received a copy of the GNU General Public License
       
    16 // along with Mauve; see the file COPYING.  If not, write to
       
    17 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    18 // Boston, MA 02111-1307, USA.  */
       
    19 
       
    20 
       
    21 package gnu.testlet.java.io.File;
       
    22 
       
    23 import gnu.testlet.TestHarness;
       
    24 import gnu.testlet.Testlet;
       
    25 
       
    26 import java.io.File;
       
    27 import java.io.IOException;
       
    28 import java.util.TreeSet;
       
    29 
       
    30 /**
       
    31  * This test checks if the File.createTempFile may return the same file
       
    32  * (wrong) if called from the parallel threads.
       
    33  */
       
    34 public class createFile implements Testlet
       
    35 {
       
    36   // How many files to ask?
       
    37   static int N_FILES = 100;
       
    38   
       
    39   // How many threads?
       
    40   static int N_THREADS = 10;
       
    41   
       
    42   String[][] returned;
       
    43   TestHarness harness;
       
    44   int completed;
       
    45   File tempDir;
       
    46 
       
    47   public void test(TestHarness a_harness)
       
    48   {
       
    49     try
       
    50       {
       
    51         harness = a_harness;
       
    52         tempDir = new File(harness.getTempDirectory());
       
    53         returned = new String[N_THREADS][];
       
    54         completed = 0;
       
    55 
       
    56         // Start threads.
       
    57         for (int thread = 0; thread < N_THREADS; thread++)
       
    58           {
       
    59             new tester(thread).start();
       
    60           }
       
    61 
       
    62         int n = 0;
       
    63         // Wait:
       
    64         while (completed < N_THREADS && (n++ < 600))
       
    65           {
       
    66             try
       
    67               {
       
    68                 Thread.sleep(100);
       
    69               }
       
    70             catch (InterruptedException iex)
       
    71               {
       
    72               }
       
    73           }
       
    74         if (completed < N_THREADS)
       
    75           harness.fail("Failed in 60 seconds. Probably hangs.");
       
    76 
       
    77         // Check for shared occurences:
       
    78         TreeSet allReturned = new TreeSet();
       
    79         String x;
       
    80         for (int thread = 0; thread < N_THREADS; thread++)
       
    81           {
       
    82             for (int file = 0; file < N_FILES; file++)
       
    83               {
       
    84                 x = (String) returned[thread][file];
       
    85                 if (allReturned.contains(x))
       
    86                   {
       
    87                     harness.fail("Multiple occurence of " + x);
       
    88                     return;
       
    89                   }
       
    90                 else
       
    91                   allReturned.add(x);
       
    92               }
       
    93           }
       
    94       }
       
    95     catch (Exception ex)
       
    96       {
       
    97         ex.printStackTrace();
       
    98       }
       
    99   }  
       
   100     
       
   101   class tester extends Thread
       
   102   {
       
   103     int thread_number;
       
   104     
       
   105     tester(int a_thread_number)
       
   106     {
       
   107       thread_number = a_thread_number;
       
   108       returned [thread_number] = new String[N_FILES];
       
   109     }
       
   110     
       
   111     public void run()
       
   112     {
       
   113       try
       
   114         {
       
   115           for (int file = 0; file < N_FILES; file++)
       
   116             {
       
   117               try
       
   118                 {
       
   119                   File tempFile = File.createTempFile("mauve", "cft", tempDir);
       
   120                   String s = tempFile.getAbsolutePath();
       
   121                   returned[thread_number][file] = s;
       
   122                   tempFile.delete();
       
   123                 }
       
   124               catch (IOException ioex)
       
   125                 {
       
   126                   harness.fail("IOException " + ioex);
       
   127                   // Force termination.
       
   128                   completed = N_THREADS + 1;
       
   129                 }
       
   130             }
       
   131         }
       
   132       finally
       
   133         {
       
   134           completed++;
       
   135         }
       
   136     }
       
   137   }
       
   138   
       
   139 }
       
   140