### Question:
PmdExecutor { public Report execute() { final Profiler profiler = Profiler.create(LOGGER).startInfo("Execute PMD " + PMD.VERSION); final ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); try (URLClassLoader classLoader = createClassloader()) { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); return executePmd(classLoader); } catch (IOException e) { LOGGER.error("Failed to close URLClassLoader.", e); } finally { Thread.currentThread().setContextClassLoader(initialClassLoader); profiler.stopInfo(); } return null; } PmdExecutor(FileSystem fileSystem, RulesProfile rulesProfile, PmdProfileExporter pmdProfileExporter,
                       PmdConfiguration pmdConfiguration, JavaResourceLocator javaResourceLocator, Configuration settings); Report execute();  }### Answer:
@Test void should_execute_pmd_on_source_files_and_test_files() throws Exception { DefaultInputFile srcFile = file("src/Class.java", Type.MAIN); DefaultInputFile tstFile = file("test/ClassTest.java", Type.TEST); setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml"); setupPmdRuleSet(PmdConstants.TEST_REPOSITORY_KEY, "junit.xml"); fileSystem.add(srcFile); fileSystem.add(tstFile); Report report = pmdExecutor.execute(); assertThat(report).isNotNull(); settings.removeProperty(PmdConstants.JAVA_SOURCE_VERSION); report = pmdExecutor.execute(); assertThat(report).isNotNull(); }
@Test void should_dump_configuration_as_xml() { when(pmdProfileExporter.exportProfile(PmdConstants.REPOSITORY_KEY, rulesProfile)).thenReturn(PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/simple.xml")); when(pmdProfileExporter.exportProfile(PmdConstants.TEST_REPOSITORY_KEY, rulesProfile)).thenReturn(PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/junit.xml")); Report report = pmdExecutor.execute(); verify(pmdConfiguration).dumpXmlReport(report); }
@Test void should_dump_ruleset_as_xml() throws Exception { DefaultInputFile srcFile = file("src/Class.java", Type.MAIN); DefaultInputFile tstFile = file("test/ClassTest.java", Type.TEST); setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml"); setupPmdRuleSet(PmdConstants.TEST_REPOSITORY_KEY, "junit.xml"); fileSystem.add(srcFile); fileSystem.add(tstFile); pmdExecutor.execute(); verify(pmdConfiguration).dumpXmlRuleSet(PmdConstants.REPOSITORY_KEY, PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/simple.xml")); verify(pmdConfiguration).dumpXmlRuleSet(PmdConstants.TEST_REPOSITORY_KEY, PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/junit.xml")); }
@Test void invalid_classpath_element() { File invalidFile = mock(File.class); when(invalidFile.toURI()).thenReturn(URI.create("x: when(javaResourceLocator.classpath()).thenReturn(ImmutableList.of(invalidFile)); final Throwable thrown = catchThrowable(() -> pmdExecutor.execute()); assertThat(thrown) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Classpath"); }
@Test void unknown_pmd_ruleset() { String profileContent = "content"; when(pmdProfileExporter.exportProfile(PmdConstants.REPOSITORY_KEY, rulesProfile)).thenReturn(profileContent); when(pmdConfiguration.dumpXmlRuleSet(PmdConstants.REPOSITORY_KEY, profileContent)).thenReturn(new File("unknown")); DefaultInputFile srcFile = file("src/Class.java", Type.MAIN); fileSystem.add(srcFile); final Throwable thrown = catchThrowable(() -> pmdExecutor.execute()); assertThat(thrown) .isInstanceOf(IllegalStateException.class) .hasCauseInstanceOf(RuleSetNotFoundException.class); }