|
25 | 25 | import static com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_STARTED_MESSAGE;
|
26 | 26 | import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_NO_NEWLINE_EOF;
|
27 | 27 | import static com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck.MSG_KEY;
|
| 28 | +import static org.mockito.Mockito.doReturn; |
28 | 29 |
|
29 | 30 | import java.io.BufferedReader;
|
30 | 31 | import java.io.ByteArrayInputStream;
|
|
41 | 42 | import java.lang.reflect.Method;
|
42 | 43 | import java.nio.charset.StandardCharsets;
|
43 | 44 | import java.nio.file.Files;
|
| 45 | +import java.nio.file.Path; |
| 46 | +import java.nio.file.Paths; |
44 | 47 | import java.util.ArrayList;
|
45 | 48 | import java.util.Arrays;
|
46 | 49 | import java.util.Collections;
|
|
55 | 58 | import java.util.UUID;
|
56 | 59 | import java.util.stream.Collectors;
|
57 | 60 |
|
| 61 | +import org.junit.jupiter.api.Nested; |
58 | 62 | import org.junit.jupiter.api.Test;
|
59 | 63 | import org.junit.jupiter.api.io.TempDir;
|
| 64 | +import org.mockito.ArgumentCaptor; |
| 65 | +import org.mockito.Mockito; |
60 | 66 |
|
61 | 67 | import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
|
62 | 68 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
|
@@ -1733,6 +1739,85 @@ public void testRelativizedFileExclusion() throws Exception {
|
1733 | 1739 | tempFile.getName(), expected);
|
1734 | 1740 | }
|
1735 | 1741 |
|
| 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 | + |
1736 | 1821 | public static class DefaultLoggerWithCounter extends DefaultLogger {
|
1737 | 1822 |
|
1738 | 1823 | private int fileStartedCount;
|
|
0 commit comments