View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.utils;
21  
22  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.net.URI;
30  import java.net.URL;
31  
32  import org.junit.Rule;
33  import org.junit.Test;
34  import org.junit.rules.TemporaryFolder;
35  import org.junit.runner.RunWith;
36  import org.mockito.BDDMockito;
37  import org.mockito.Mockito;
38  import org.powermock.api.mockito.PowerMockito;
39  import org.powermock.core.classloader.annotations.PrepareForTest;
40  import org.powermock.modules.junit4.PowerMockRunner;
41  
42  @RunWith(PowerMockRunner.class)
43  public class FilterUtilsTest {
44      @Rule
45      public final TemporaryFolder temporaryFolder = new TemporaryFolder();
46  
47      @Test
48      public void testIsProperUtilsClass() throws ReflectiveOperationException {
49          assertTrue("Constructor is not private",
50                  isUtilsClassHasPrivateConstructor(FilterUtils.class, true));
51      }
52  
53      @Test
54      public void testExistingFile() throws Exception {
55          final File file = temporaryFolder.newFile();
56          assertTrue("Suppression file exists",
57                  FilterUtils.isFileExists(file.getPath()));
58      }
59  
60      @Test
61      public void testNonExistentFile() {
62          assertFalse("Suppression file does not exist",
63                  FilterUtils.isFileExists("non-existent.xml"));
64      }
65  
66      @Test
67      @PrepareForTest({FilterUtils.class, CommonUtils.class})
68      public void testExceptionOnClosing() throws Exception {
69          final File file = temporaryFolder.newFile("existing.xml");
70          final InputStream inputStream = PowerMockito.mock(InputStream.class);
71          Mockito.doThrow(IOException.class).when(inputStream).close();
72  
73          final URL url = PowerMockito.mock(URL.class);
74          BDDMockito.given(url.openStream()).willReturn(inputStream);
75  
76          final URI uri = PowerMockito.mock(URI.class);
77          BDDMockito.given(uri.toURL()).willReturn(url);
78  
79          PowerMockito.mockStatic(CommonUtils.class);
80  
81          final String fileName = file.getPath();
82          BDDMockito.given(CommonUtils.getUriByFilename(fileName)).willReturn(uri);
83          assertFalse("Should be false, because error on close",
84                  FilterUtils.isFileExists(fileName));
85      }
86  }