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.api;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.fail;
24  import static org.mockito.Matchers.any;
25  import static org.mockito.Mockito.times;
26  import static org.powermock.api.mockito.PowerMockito.doNothing;
27  import static org.powermock.api.mockito.PowerMockito.mockStatic;
28  import static org.powermock.api.mockito.PowerMockito.verifyStatic;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.Reader;
33  import java.nio.charset.StandardCharsets;
34  import java.util.Locale;
35  
36  import org.junit.Test;
37  import org.junit.runner.RunWith;
38  import org.powermock.core.classloader.annotations.PrepareForTest;
39  import org.powermock.modules.junit4.PowerMockRunner;
40  
41  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
42  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
43  
44  @RunWith(PowerMockRunner.class)
45  @PrepareForTest(CommonUtils.class)
46  public class FileTextTest extends AbstractPathTestSupport {
47      @Override
48      protected String getPackageLocation() {
49          return "com/puppycrawl/tools/checkstyle/api/filetext";
50      }
51  
52      @Test
53      public void testUnsupportedCharset() throws IOException {
54          // just to make UT coverage 100%
55          final String charsetName = "STRANGE_CHARSET";
56          try {
57              final Object test = new FileText(new File("any name"), charsetName);
58              fail("UnsupportedEncodingException is expected but got " + test);
59          }
60          catch (IllegalStateException ex) {
61              assertEquals("Invalid exception message",
62                      "Unsupported charset: " + charsetName, ex.getMessage());
63          }
64  
65      }
66  
67      @Test
68      public void testSupportedCharset() throws IOException {
69          //check if reader finally closed
70          mockStatic(CommonUtils.class);
71          doNothing().when(CommonUtils.class);
72          CommonUtils.close(any(Reader.class));
73  
74          final String charsetName = StandardCharsets.ISO_8859_1.name();
75          final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
76                  charsetName);
77          assertEquals("Invalid charset name", charsetName, fileText.getCharset().name());
78  
79          verifyStatic(times(2));
80          CommonUtils.close(any(Reader.class));
81      }
82  
83      @Test
84      public void testLineColumnBeforeCopyConstructor() throws IOException {
85          final String charsetName = StandardCharsets.ISO_8859_1.name();
86          final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
87                  charsetName);
88          final LineColumn lineColumn = fileText.lineColumn(100);
89          final FileText copy = new FileText(fileText);
90          assertEquals("Invalid linecolumn", lineColumn, copy.lineColumn(100));
91      }
92  
93      @Test
94      public void testLineColumnAfterCopyConstructor() throws IOException {
95          final String charsetName = StandardCharsets.ISO_8859_1.name();
96          final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
97                  charsetName);
98          final FileText copy = new FileText(fileText);
99          final LineColumn lineColumn = copy.lineColumn(100);
100         assertEquals("Invalid line", 3, lineColumn.getLine());
101         if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("windows")) {
102             assertEquals("Invalid column", 44, lineColumn.getColumn());
103         }
104         else {
105             assertEquals("Invalid column", 46, lineColumn.getColumn());
106         }
107     }
108 
109     @Test
110     public void testLineColumnAtTheStartOfFile() throws IOException {
111         final String charsetName = StandardCharsets.ISO_8859_1.name();
112         final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
113                 charsetName);
114         final FileText copy = new FileText(fileText);
115         final LineColumn lineColumn = copy.lineColumn(0);
116         assertEquals("Invalid line", 1, lineColumn.getLine());
117         assertEquals("Invalid column", 0, lineColumn.getColumn());
118     }
119 }