8000 Pull #16872: api: add new int process(List<Path> files) method to Roo… · checkstyle/checkstyle@2aaf914 · GitHub
Skip to content

Commit 2aaf914

Browse files
author
Vincent Potucek
committed
Pull #16872: api: add new int process(List<Path> files) method to RootModule
1 parent b24e789 commit 2aaf914

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

src/main/java/com/puppycrawl/tools/checkstyle/Checker.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import java.io.UnsupportedEncodingException;
2727
import java.nio.charset.Charset;
2828
import java.nio.charset.StandardCharsets;
29+
import java.nio.file.Path;
2930
import java.util.ArrayList;
31+
import java.util.Collection;
3032
import java.util.List;
3133
import java.util.Locale;
3234
import java.util.Set;
@@ -237,6 +239,13 @@ public int process(List<File> files) throws CheckstyleException {
237239
return errorCount;
238240
}
239241

242+
@Override
243+
public int process(Collection<Path> paths) throws CheckstyleException {
244+
return process(paths.stream()
245+
.map(Path::toFile)
246+
.collect(Collectors.toUnmodifiableList()));
247+
}
248+
240249
/**
241250
* Returns a set of external configuration resource locations which are used by all file set
242251
* checks and filters.

src/main/java/com/puppycrawl/tools/checkstyle/api/RootModule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.puppycrawl.tools.checkstyle.api;
2121

2222
import java.io.File;
23+
import java.nio.file.Path;
24+
import java.util.Collection;
2325
import java.util.List;
2426

2527
/**
@@ -40,9 +42,23 @@ public interface RootModule extends Configurable {
4042
* @return the total number of audit events with error severity found
4143
* @throws CheckstyleException if error condition within Checkstyle occurs
4244
* @see #destroy()
45+
* @deprecated Use {@link #process(Collection)}
4346
*/
47+
@Deprecated(since = "9.3")
4448
int process(List<File> files) throws CheckstyleException;
4549

50+
/**
51+
* Processes a set of files.
52+
* Once this is done, it is highly recommended to call for
53+
* the destroy method to close and remove the listeners.
54+
*
55+
* @param files the list of files to be audited.
56+
* @return the total number of audit events with error severity found
57+
* @throws CheckstyleException if error condition within Checkstyle occurs
58+
* @see #destroy()
59+
*/
60+
int process(Collection<Path> files) throws CheckstyleException;
61+
4662
/**
4763
* Add the listener that will be used to receive events from the audit.
4864
*

src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_STARTED_MESSAGE;
2626
import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_NO_NEWLINE_EOF;
2727
import static com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck.MSG_KEY;
28+
import static org.mockito.Mockito.doReturn;
2829

2930
import java.io.BufferedReader;
3031
import java.io.ByteArrayInputStream;
@@ -41,6 +42,8 @@
4142
import java.lang.reflect.Method;
4243
import java.nio.charset.StandardCharsets;
4344
import java.nio.file.Files;
45+
import java.nio.file.Path;
46+
import java.nio.file.Paths;
4447
import java.util.ArrayList;
4548
import java.util.Arrays;
4649
import java.util.Collections;
@@ -55,8 +58,11 @@
5558
import java.util.UUID;
5659
import java.util.stream.Collectors;
5760

61+
import org.junit.jupiter.api.Nested;
5862
import org.junit.jupiter.api.Test;
5963
import org.junit.jupiter.api.io.TempDir;
64+
import org.mockito.ArgumentCaptor;
65+
import org.mockito.Mockito;
6066

6167
import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
6268
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
@@ -1733,6 +1739,85 @@ public void testRelativizedFileExclusion() throws Exception {
17331739
tempFile.getName(), expected);
17341740
}
17351741

1742+
@Nested
1743+
public class TestProcessPathCollection {
1744+
1745+
@Test
1746+
public void testFileExtensionsPath() throws Exception {
1747+
final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
1748+
checkerConfig.addProperty("charset", StandardCharsets.UTF_8.name());
1749+
checkerConfig.addProperty("cacheFile", createTempFile("junit").getAbsolutePath());
1750+
1751+
final Checker checker = new Checker();
1752+
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
1753+
checker.configure(checkerConfig);
1754+
1755+
final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
1756+
checker.addListener(auditAdapter);
1757+
1758+
final List<Path> files = new ArrayList<>();
1759+
files.add(Paths.get("file.pdf"));
1760+
files.add(Paths.get("file.java"));
1761+
checker.setFileExtensions("java", "xml", "properties");
1762+
checker.setCacheFile(createTempFile("junit").getAbsolutePath());
1763+
final int counter = checker.process(files);
1764+
1765+
// comparing to 1 as there is only one legal file in input
1766+
final int numLegalFiles = 1;
1767+
assertWithMessage("There were more legal files than expected")
1768+
.that(counter)
1769+
.isEqualTo(numLegalFiles);
1770+
assertWithMessage("Audit was started on larger amount of files than expected")
1771+
.that(auditAdapter.getNumFilesStarted())
1772+
.isEqualTo(numLegalFiles);
1773+
assertWithMessage("Audit was finished on larger amount of files than expected")
1774+
.that(auditAdapter.getNumFilesFinished())
1775+
.isEqualTo(numLegalFiles);
1776+
assertWithMessage("Cache should not contain any file")
1777+
.that(TestUtil.<PropertyCacheFile>getInternalState(checker, "cacheFile")
1778+
.get(Paths.get("file.java").toAbsolutePath().toString()))
1779+
.isNull();
1780+
}
1781+
1782+
@Test
1783+
public void testProcessPathCollection() throws Exception {
1784+
final Checker checker = new Checker();
1785+
final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
1786+
checker.addListener(auditAdapter);
1787+
1788+
final File tempFile1 = createTempFile("test1", ".java");
1789+
final File tempFile2 = createTempFile("test2", ".java");
1790+
1791+
final Checker spyChecker = Mockito.spy(checker);
1792+
doReturn(0).when(spyChecker).process(Collections.<File>emptyList());
1793+
1794+
spyChecker.process(Arrays.asList(
1795+
tempFile1.toPath(),
1796+
tempFile2.toPath()
1797+
));
1798+
1799+
final ArgumentCaptor<List<File>> filesCaptor = ArgumentCaptor.forClass(List.class);
1800+
Mockito.verify(spyChecker).process(filesCaptor.capture());
1801+
final List<File> processedFiles = filesCaptor.getValue();
1802+
assertWithMessage("Processed files count mismatch")
1803+
.that(processedFiles)
1804+
.hasSize(2);
1805+
assertWithMessage("First file mismatch")
1806+
.that(processedFiles.get(0))
1807+
.isEqualTo(tempFile1);
1808+
assertWithMessage("Second file mismatch")
1809+
.that(processedFiles.get(1))
1810+
.isEqualTo(tempFile2);
1811+
assertWithMessage("Audit started event not fired")
1812+
.that(auditAdapter.wasCalled())
1813+
.isTrue();
1814+
assertWithMessage("Incorrect number of files processed")
1815+
.that(auditAdapter.getNumFilesStarted())
1816+
.isEqualTo(2);
1817+
}
1818+
1819+
}
1820+
17361821
public static class DefaultLoggerWithCounter extends DefaultLogger {
17371822

17381823
private int fileStartedCount;

src/test/java/com/puppycrawl/tools/checkstyle/internal/testmodules/TestRootModuleChecker.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
package com.puppycrawl.tools.checkstyle.internal.testmodules;
2121

2222
import java.io.File;
23+
import java.nio.file.Path;
2324
import java.util.ArrayList;
25+
import java.util.Collection;
2426
import java.util.Collections;
2527
import java.util.List;
28+
import java.util.stream.Collectors;
2629

2730
import com.puppycrawl.tools.checkstyle.api.AuditListener;
2831
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
@@ -55,6 +58,13 @@ public int process(List<File> files) {
5558
return 0;
5659
}
5760

61+
@Override
62+
public int process(Collection<Path> files) {
63+
return process(files.stream()
64+
.map(Path::toFile)
65+
.collect(Collectors.toUnmodifiableList()));
66+
}
67+
5868
@Override
5969
public void addListener(AuditListener listener) {
6070
// not used

src/test/java/com/puppycrawl/tools/checkstyle/utils/ModuleReflectionUtilTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import java.io.File;
2727
import java.io.IOException;
28+
import java.nio.file.Path;
29+
i 8D0D mport java.util.Collection;
2830
import java.util.Collections;
2931
import java.util.List;
3032
import java.util.Set;
@@ -301,6 +303,11 @@ public int process(List<File> files) {
301303
return 0;
302304
}
303305

306+
@Override
307+
public int process(Collection<Path> files) {
308+
return 0;
309+
}
310+
304311
@Override
305312
public void destroy() {
306313
// dummy method

0 commit comments

Comments
 (0)
0