View Javadoc
1   package org.apache.maven.shared.utils.testhelpers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.shared.utils.io.FileUtils;
23  import org.apache.maven.shared.utils.io.IOUtil;
24  import org.junit.rules.TemporaryFolder;
25  
26  import java.io.*;
27  
28  /**
29   * A few utility methods for file based tests
30   */
31  public final class FileTestHelper
32  {
33  
34      private FileTestHelper()
35      {
36          // utility function doesn't need a public ct
37      }
38  
39      public static void generateTestData( OutputStream out, long size )
40          throws IOException
41      {
42          for ( int i = 0; i < size; i++ )
43          {
44              // nice varied byte pattern compatible with Readers and Writers
45              out.write( (byte) ( ( i % 127 ) + 1 ) );
46          }
47      }
48  
49      public static void generateTestFile( File testfile, int size )
50          throws IOException
51      {
52          if ( testfile.exists() )
53          {
54              //noinspection ResultOfMethodCallIgnored
55              testfile.delete();
56          }
57  
58          OutputStream os = new FileOutputStream( testfile );
59          generateTestData( os, size );
60          os.flush();
61          os.close();
62      }
63  
64  
65  
66      public static void createLineBasedFile( File file, String[] data )
67          throws IOException
68      {
69          if ( file.getParentFile() != null && !file.getParentFile().exists() )
70          {
71              throw new IOException( "Cannot create file " + file + " as the parent directory does not exist" );
72          }
73          PrintWriter output = new PrintWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ) );
74          try
75          {
76              for ( String aData : data )
77              {
78                  output.println( aData );
79              }
80          }
81          finally
82          {
83              IOUtil.close( output );
84          }
85      }
86  
87      /**
88       * Check if the given file exists in the given folder and remove it.
89       *
90       * @return the File object for a new file
91       * @throws IOException
92       */
93      public static File newFile( TemporaryFolder folder, String filename )
94              throws IOException
95      {
96          File destination = folder.newFile( filename );
97  
98          if ( destination.exists() )
99          {
100             FileUtils.forceDelete( destination );
101         }
102         return destination;
103     }
104 }