View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.commons.io.build.AbstractOriginTest;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.params.ParameterizedTest;
28  import org.junit.jupiter.params.provider.EnumSource;
29  
30  /**
31   * Tests {@link IORandomAccessFile}.
32   */
33  class IORandomAccessFileTest {
34  
35      protected static final String FILE_NAME_RW = "target/" + AbstractOriginTest.class.getSimpleName() + ".txt";
36  
37      private File newFileFixture() throws IOException {
38          final File file = new File(FILE_NAME_RW);
39          FileUtils.touch(file);
40          return file;
41      }
42  
43      @ParameterizedTest
44      @EnumSource(RandomAccessFileMode.class)
45      void testFile(final RandomAccessFileMode mode) throws IOException {
46          final File file = newFileFixture();
47          final String modeStr = mode.getMode();
48          try (IORandomAccessFile raf = new IORandomAccessFile(file, modeStr)) {
49              assertEquals(file, raf.getFile());
50              assertEquals(modeStr, raf.getMode());
51          }
52      }
53  
54      @ParameterizedTest
55      @EnumSource(RandomAccessFileMode.class)
56      void testString(final RandomAccessFileMode mode) throws IOException {
57          final File file = newFileFixture();
58          final String modeStr = mode.getMode();
59          try (IORandomAccessFile raf = new IORandomAccessFile(FILE_NAME_RW, modeStr)) {
60              assertEquals(file, raf.getFile());
61              assertEquals(modeStr, raf.getMode());
62          }
63      }
64  
65      @Test
66      void testToString() throws IOException {
67          final File file = newFileFixture();
68          try (IORandomAccessFile raf = new IORandomAccessFile(FILE_NAME_RW, "r")) {
69              assertEquals(file.toString(), raf.toString());
70          }
71      }
72  }