View Javadoc
1   /*
2    * Copyright (C) 2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.gmavenplus.util;
18  
19  import org.junit.Test;
20  
21  import java.io.*;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.mockito.Mockito.doThrow;
25  import static org.mockito.Mockito.mock;
26  
27  
28  /**
29   * Unit tests for the FileUtils class.
30   *
31   * @author Keegan Witt
32   */
33  public class FileUtilsTest {
34      private static final IOException ioException = new IOException("Intentionally blowing up.");
35  
36      @Test
37      public void testGetFileExtension() {
38          assertEquals("gz", FileUtils.getFileExtension("foo.tar.gz"));
39      }
40  
41      @Test
42      public void testGetNameWithoutExtension() {
43          assertEquals("foo.tar", FileUtils.getNameWithoutExtension("foo.tar.gz"));
44      }
45  
46      @Test
47      public void testCloseInputStreamQuietly() throws Exception {
48          InputStream inputStream = mock(InputStream.class);
49          doThrow(ioException).when(inputStream).close();
50          FileUtils.closeQuietly(inputStream);
51      }
52  
53      @Test
54      public void testCloseOutputStreamQuietly() throws Exception {
55          OutputStream outputStream = mock(OutputStream.class);
56          doThrow(ioException).when(outputStream).close();
57          FileUtils.closeQuietly(outputStream);
58      }
59  
60      @Test
61      public void testCloseReaderQuietly() throws Exception {
62          Reader reader = mock(Reader.class);
63          doThrow(ioException).when(reader).close();
64          FileUtils.closeQuietly(reader);
65      }
66  
67      @Test
68      public void testCloseWriterQuietly() throws Exception {
69          Writer writer = mock(Writer.class);
70          doThrow(ioException).when(writer).close();
71          FileUtils.closeQuietly(writer);
72      }
73  
74  }