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