Search in sources :

Example 1 with DebugAuditAdapter

use of com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter in project checkstyle by checkstyle.

the class CheckerTest method testIgnoredFileExtensions.

@Test
public void testIgnoredFileExtensions() throws Exception {
    final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
    checkerConfig.addProperty("charset", StandardCharsets.UTF_8.name());
    final File tempFile = File.createTempFile("junit", null, temporaryFolder);
    checkerConfig.addProperty("cacheFile", tempFile.getPath());
    final Checker checker = new Checker();
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(checkerConfig);
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    final List<File> allIgnoredFiles = new ArrayList<>();
    final File ignoredFile = new File("file.pdf");
    allIgnoredFiles.add(ignoredFile);
    final String[] fileExtensions = { "java", "xml", "properties" };
    checker.setFileExtensions(fileExtensions);
    checker.setCacheFile(File.createTempFile("junit", null, temporaryFolder).getPath());
    final int counter = checker.process(allIgnoredFiles);
    // comparing to 0 as there is no legal file in input
    final int numLegalFiles = 0;
    assertWithMessage("There were more legal files than expected").that(counter).isEqualTo(numLegalFiles);
    assertWithMessage("Audit was started on larger amount of files than expected").that(auditAdapter.getNumFilesStarted()).isEqualTo(numLegalFiles);
    assertWithMessage("Audit was finished on larger amount of files than expected").that(auditAdapter.getNumFilesFinished()).isEqualTo(numLegalFiles);
}
Also used : ArrayList(java.util.ArrayList) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with DebugAuditAdapter

use of com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter in project checkstyle by checkstyle.

the class CheckerTest method testFileExtensions.

@Test
public void testFileExtensions() throws Exception {
    final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
    checkerConfig.addProperty("charset", StandardCharsets.UTF_8.name());
    checkerConfig.addProperty("cacheFile", File.createTempFile("junit", null, temporaryFolder).getPath());
    final Checker checker = new Checker();
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(checkerConfig);
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    final List<File> files = new ArrayList<>();
    final File file = new File("file.pdf");
    files.add(file);
    final File otherFile = new File("file.java");
    files.add(otherFile);
    final String[] fileExtensions = { "java", "xml", "properties" };
    checker.setFileExtensions(fileExtensions);
    checker.setCacheFile(File.createTempFile("junit", null, temporaryFolder).getPath());
    final int counter = checker.process(files);
    // comparing to 1 as there is only one legal file in input
    final int numLegalFiles = 1;
    final PropertyCacheFile cache = TestUtil.getInternalState(checker, "cacheFile");
    assertWithMessage("There were more legal files than expected").that(counter).isEqualTo(numLegalFiles);
    assertWithMessage("Audit was started on larger amount of files than expected").that(auditAdapter.getNumFilesStarted()).isEqualTo(numLegalFiles);
    assertWithMessage("Audit was finished on larger amount of files than expected").that(auditAdapter.getNumFilesFinished()).isEqualTo(numLegalFiles);
    assertWithMessage("Cache shout not contain any file").that(cache.get(new File("file.java").getCanonicalPath())).isNull();
}
Also used : ArrayList(java.util.ArrayList) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with DebugAuditAdapter

use of com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter in project checkstyle by checkstyle.

the class CheckerTest method testAddListener.

@Test
public void testAddListener() throws Exception {
    final Checker checker = new Checker();
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    // Let's try fire some events
    getFireAuditStartedMethod().invoke(checker);
    assertWithMessage("Checker.fireAuditStarted() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditStarted() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    getFireAuditFinished().invoke(checker);
    assertWithMessage("Checker.fireAuditFinished() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditFinished() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    checker.fireFileStarted("Some File Name");
    assertWithMessage("Checker.fireFileStarted() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileStarted() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    checker.fireFileFinished("Some File Name");
    assertWithMessage("Checker.fireFileFinished() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileFinished() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    final SortedSet<Violation> violations = new TreeSet<>();
    violations.add(new Violation(1, 0, "a Bundle", "message.key", new Object[] { "arg" }, null, getClass(), null));
    checker.fireErrors("Some File Name", violations);
    assertWithMessage("Checker.fireErrors() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireErrors() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeSet(java.util.TreeSet) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) Test(org.junit.jupiter.api.Test)

Example 4 with DebugAuditAdapter

use of com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter in project checkstyle by checkstyle.

the class CheckerTest method testDestroy.

@Test
public void testDestroy() throws Exception {
    final Checker checker = new Checker();
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    final TestFileSetCheck fileSet = new TestFileSetCheck();
    checker.addFileSetCheck(fileSet);
    final DebugFilter filter = new DebugFilter();
    checker.addFilter(filter);
    final TestBeforeExecutionFileFilter fileFilter = new TestBeforeExecutionFileFilter();
    checker.addBeforeExecutionFileFilter(fileFilter);
    // should remove all listeners, file sets, and filters
    checker.destroy();
    final File tempFile = File.createTempFile("junit", null, temporaryFolder);
    checker.process(Collections.singletonList(tempFile));
    final SortedSet<Violation> violations = new TreeSet<>();
    violations.add(new Violation(1, 0, "a Bundle", "message.key", new Object[] { "arg" }, null, getClass(), null));
    checker.fireErrors("Some File Name", violations);
    assertWithMessage("Checker.destroy() doesn't remove listeners.").that(auditAdapter.wasCalled()).isFalse();
    assertWithMessage("Checker.destroy() doesn't remove file sets.").that(fileSet.wasCalled()).isFalse();
    assertWithMessage("Checker.destroy() doesn't remove filters.").that(filter.wasCalled()).isFalse();
    assertWithMessage("Checker.destroy() doesn't remove file filters.").that(fileFilter.wasCalled()).isFalse();
}
Also used : TestFileSetCheck(com.puppycrawl.tools.checkstyle.internal.testmodules.TestFileSetCheck) Violation(com.puppycrawl.tools.checkstyle.api.Violation) TestBeforeExecutionFileFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.TestBeforeExecutionFileFilter) TreeSet(java.util.TreeSet) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) File(java.io.File) DebugFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter) Test(org.junit.jupiter.api.Test)

Example 5 with DebugAuditAdapter

use of com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter in project checkstyle by checkstyle.

the class CheckerTest method testRemoveListener.

@Test
public void testRemoveListener() throws Exception {
    final Checker checker = new Checker();
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    final DebugAuditAdapter aa2 = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    checker.addListener(aa2);
    checker.removeListener(auditAdapter);
    // Let's try fire some events
    getFireAuditStartedMethod().invoke(checker);
    assertWithMessage("Checker.fireAuditStarted() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditStarted() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.resetListener();
    getFireAuditFinished().invoke(checker);
    assertWithMessage("Checker.fireAuditFinished() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditFinished() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.resetListener();
    checker.fireFileStarted("Some File Name");
    assertWithMessage("Checker.fireFileStarted() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileStarted() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.resetListener();
    checker.fireFileFinished("Some File Name");
    assertWithMessage("Checker.fireFileFinished() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileFinished() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.resetListener();
    final SortedSet<Violation> violations = new TreeSet<>();
    violations.add(new Violation(1, 0, "a Bundle", "message.key", new Object[] { "arg" }, null, getClass(), null));
    checker.fireErrors("Some File Name", violations);
    assertWithMessage("Checker.fireErrors() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireErrors() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeSet(java.util.TreeSet) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

DebugAuditAdapter (com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter)7 Test (org.junit.jupiter.api.Test)7 File (java.io.File)4 Violation (com.puppycrawl.tools.checkstyle.api.Violation)3 TreeSet (java.util.TreeSet)3 ArrayList (java.util.ArrayList)2 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 DebugFilter (com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter)1 TestBeforeExecutionFileFilter (com.puppycrawl.tools.checkstyle.internal.testmodules.TestBeforeExecutionFileFilter)1 TestFileSetCheck (com.puppycrawl.tools.checkstyle.internal.testmodules.TestFileSetCheck)1