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.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import java.io.File;
27  import java.lang.reflect.Field;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.SortedSet;
32  
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  import com.puppycrawl.tools.checkstyle.Checker;
37  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
38  
39  public class AbstractFileSetCheckTest {
40  
41      @Test
42      public void testProcessSequential() throws Exception {
43          final DummyFileSetCheck check = new DummyFileSetCheck();
44          check.configure(new DefaultConfiguration("filesetcheck"));
45          check.setFileExtensions("tmp");
46          final File firstFile = new File("inputAbstractFileSetCheck.tmp");
47          final SortedSet<LocalizedMessage> firstFileMessages =
48              check.process(firstFile, new FileText(firstFile, Collections.emptyList()));
49  
50          assertEquals("Invalid message", "File should not be empty.",
51              firstFileMessages.first().getMessage());
52  
53          final Field field = AbstractFileSetCheck.class.getDeclaredField("MESSAGE_COLLECTOR");
54          field.setAccessible(true);
55          @SuppressWarnings("unchecked")
56          final SortedSet<LocalizedMessage> internalMessages =
57                  ((ThreadLocal<SortedSet<LocalizedMessage>>) field.get(null)).get();
58          assertTrue("Internal message should be empty, but was not", internalMessages.isEmpty());
59  
60          final File secondFile = new File("inputAbstractFileSetCheck.txt");
61          final List<String> lines = Arrays.asList("key=value", "ext=tmp");
62          final SortedSet<LocalizedMessage> secondFileMessages =
63              check.process(secondFile, new FileText(secondFile, lines));
64  
65          assertTrue("Message should be empty, but was not", secondFileMessages.isEmpty());
66      }
67  
68      @Test
69      public void testProcessException() throws Exception {
70          final ExceptionFileSetCheck check = new ExceptionFileSetCheck();
71          check.configure(new DefaultConfiguration("filesetcheck"));
72          check.setFileExtensions("tmp");
73          final File firstFile = new File("inputAbstractFileSetCheck.tmp");
74  
75          try {
76              check.process(firstFile, new FileText(firstFile, Collections.emptyList()));
77              fail("Exception is expected");
78          }
79          catch (IllegalArgumentException ex) {
80              // exception is expected
81          }
82  
83          final Field field = AbstractFileSetCheck.class.getDeclaredField("MESSAGE_COLLECTOR");
84          field.setAccessible(true);
85          @SuppressWarnings("unchecked")
86          final SortedSet<LocalizedMessage> internalMessages =
87                  ((ThreadLocal<SortedSet<LocalizedMessage>>) field.get(null)).get();
88          assertEquals("Internal message should only have 1", 1, internalMessages.size());
89  
90          // again to prove only 1 violation exists
91          final File secondFile = new File("inputAbstractFileSetCheck.tmp");
92          try {
93              check.process(secondFile, new FileText(secondFile, Collections.emptyList()));
94              fail("Exception is expected");
95          }
96          catch (IllegalArgumentException ex) {
97              // exception is expected
98          }
99  
100         @SuppressWarnings("unchecked")
101         final SortedSet<LocalizedMessage> internalMessages2 =
102             ((ThreadLocal<SortedSet<LocalizedMessage>>) field.get(null)).get();
103         assertEquals("Internal message should only have 1 again", 1, internalMessages2.size());
104     }
105 
106     @Test
107     public void testGetFileExtension() {
108         final DummyFileSetCheck check = new DummyFileSetCheck();
109         check.setFileExtensions("tmp", ".java");
110         final String[] expectedExtensions = {".tmp", ".java"};
111 
112         Assert.assertArrayEquals("Invalid extensions",
113                 expectedExtensions, check.getFileExtensions());
114     }
115 
116     /**
117      * This javadoc exists only to suppress Intellij Idea inspection.
118      */
119     @Test
120     public void testSetExtensionThrowsExceptionWhenTheyAreNull() {
121         final DummyFileSetCheck check = new DummyFileSetCheck();
122         try {
123             check.setFileExtensions((String[]) null);
124             fail("Expected exception.");
125         }
126         catch (IllegalArgumentException exception) {
127             assertEquals("Invalid exception message",
128                     "Extensions array can not be null", exception.getMessage());
129         }
130     }
131 
132     @Test
133     public void testGetMessageDispatcher() {
134         final DummyFileSetCheck check = new DummyFileSetCheck();
135         final Checker checker = new Checker();
136         check.setMessageDispatcher(checker);
137 
138         assertEquals("Invalid message dispatcher", checker, check.getMessageDispatcher());
139     }
140 
141     private static class DummyFileSetCheck extends AbstractFileSetCheck {
142         private static final String MSG_KEY = "File should not be empty.";
143 
144         @Override
145         protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
146             if (fileText.size() == 0) {
147                 log(1, MSG_KEY);
148             }
149         }
150     }
151 
152     private static class ExceptionFileSetCheck extends AbstractFileSetCheck {
153         private static final String MSG_KEY = "Test.";
154         private int count = 1;
155 
156         @Override
157         protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
158             log(count, MSG_KEY);
159             count++;
160             throw new IllegalArgumentException("Test");
161         }
162     }
163 }