View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.BufferedOutputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.lang.reflect.Method;
25  import java.nio.file.Files;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.TestInfo;
29  
30  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  /**
34   * Base class for testcases doing tests with files.
35   *
36   * @author Jeremias Maerki
37   * @since 3.4.0
38   */
39  public abstract class FileBasedTestCase {
40      private static File testDir;
41  
42      private TestInfo testInfo;
43  
44      public static File getTestDirectory() {
45          if (testDir == null) {
46              testDir = (new File("target/test/io/")).getAbsoluteFile();
47          }
48          return testDir;
49      }
50  
51      /**
52       * <p>createFile.</p>
53       *
54       * @param file a {@link java.io.File} object.
55       * @param size a long.
56       * @throws java.io.IOException if any.
57       */
58      protected void createFile(final File file, final long size) throws IOException {
59          if (!file.getParentFile().exists()) {
60              throw new IOException("Cannot create file " + file + " as the parent directory does not exist");
61          }
62  
63          byte[] data = generateTestData(size);
64          try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
65              output.write(data);
66          }
67      }
68  
69      /**
70       * <p>createSymlink.</p>
71       *
72       * @param link   a {@link java.io.File} object.
73       * @param target a {@link java.io.File} object.
74       */
75      protected void createSymlink(final File link, final File target) {
76          try {
77              String[] args = {"ln", "-s", target.getAbsolutePath(), link.getAbsolutePath()};
78              Process process = Runtime.getRuntime().exec(args);
79              process.waitFor();
80          } catch (Exception e) {
81              // assume platform does not support "ln" command, tests should be skipped
82          }
83      }
84  
85      protected byte[] generateTestData(final long size) {
86          try {
87              ByteArrayOutputStream baout = new ByteArrayOutputStream();
88              generateTestData(baout, size);
89              return baout.toByteArray();
90          } catch (IOException ioe) {
91              throw new RuntimeException("This should never happen: " + ioe.getMessage());
92          }
93      }
94  
95      protected void generateTestData(final OutputStream out, final long size) throws IOException {
96          for (int i = 0; i < size; i++) {
97              // output.write((byte)'X');
98  
99              // nice varied byte pattern compatible with Readers and Writers
100             out.write((byte) ((i % 127) + 1));
101         }
102     }
103 
104     protected void checkFile(final File file, final File referenceFile) throws Exception {
105         assertTrue(file.exists(), "Check existence of output file");
106         assertEqualContent(referenceFile, file);
107     }
108 
109     /** Assert that the content of two files is the same. */
110     private void assertEqualContent(final File f0, final File f1) throws IOException {
111         /*
112          * This doesn't work because the filesize isn't updated until the file is closed. assertTrue( "The files " + f0
113          * + " and " + f1 + " have differing file sizes (" + f0.length() + " vs " + f1.length() + ")", ( f0.length() ==
114          * f1.length() ) );
115          */
116         byte[] buf0 = Files.readAllBytes(f0.toPath());
117         byte[] buf1 = Files.readAllBytes(f0.toPath());
118         assertArrayEquals(buf0, buf1, "The files " + f0 + " and " + f1 + " have different content");
119     }
120 
121     /**
122      * Assert that the content of a file is equal to that in a byte[].
123      *
124      * @param b0 an array of {@link byte} objects.
125      * @param file a {@link java.io.File} object.
126      * @throws java.io.IOException if any.
127      */
128     protected void assertEqualContent(final byte[] b0, final File file) throws IOException {
129         byte[] b1 = Files.readAllBytes(file.toPath());
130         assertArrayEquals(b0, b1, "Content differs");
131     }
132 
133     protected void assertIsDirectory(File file) {
134         assertTrue(file.exists(), "The File doesn't exists: " + file.getAbsolutePath());
135 
136         assertTrue(file.isDirectory(), "The File isn't a directory: " + file.getAbsolutePath());
137     }
138 
139     @BeforeEach
140     void init(TestInfo testInfo) {
141         this.testInfo = testInfo;
142     }
143 
144     protected String getTestMethodName() {
145         return testInfo.getTestMethod().map(Method::getName).orElse(null);
146     }
147 }