|
| 1 | +/** |
| 2 | + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html |
| 3 | + */ |
| 4 | + |
| 5 | +package net.sourceforge.pmd.eclipse.runtime.cmd; |
| 6 | + |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | + |
| 9 | +import org.apache.commons.io.IOUtils; |
| 10 | +import org.eclipse.core.resources.IFolder; |
| 11 | +import org.eclipse.core.resources.IMarker; |
| 12 | +import org.eclipse.core.resources.IProject; |
| 13 | +import org.eclipse.core.resources.IResource; |
| 14 | +import org.eclipse.core.runtime.CoreException; |
| 15 | +import org.eclipse.core.runtime.Path; |
| 16 | +import org.eclipse.jdt.core.IClasspathEntry; |
| 17 | +import org.eclipse.jdt.core.IJavaProject; |
| 18 | +import org.eclipse.jdt.core.JavaCore; |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import net.sourceforge.pmd.eclipse.EclipseUtils; |
| 25 | +import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; |
| 26 | +import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants; |
| 27 | +import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties; |
| 28 | +import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager; |
| 29 | + |
| 30 | +import name.herlin.command.CommandException; |
| 31 | + |
| 32 | +/** |
| 33 | + * This tests execution of PMD with multiple rulesets |
| 34 | + */ |
| 35 | +public class MultipleRulesetsTest { |
| 36 | + private IProject testProject; |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUp() throws Exception { |
| 40 | + |
| 41 | + // 1. Create a Java project |
| 42 | + this.testProject = EclipseUtils.createJavaProject("PMDTestProject"); |
| 43 | + Assert.assertTrue("A test project cannot be created; the tests cannot be performed.", |
| 44 | + this.testProject != null && this.testProject.exists() && this.testProject.isAccessible()); |
| 45 | + |
| 46 | + // 2. Setup test folder |
| 47 | + this.testProject.getFolder("src/main/java").getFullPath().toFile().mkdirs(); |
| 48 | + IFolder folder = this.testProject.getFolder("/src/main"); |
| 49 | + folder.create(true, true, null); |
| 50 | + folder = folder.getFolder("java"); |
| 51 | + folder.create(true, true, null); |
| 52 | + IJavaProject javaProject = JavaCore.create(this.testProject); |
| 53 | + javaProject.setRawClasspath( |
| 54 | + new IClasspathEntry[] { JavaCore.newSourceEntry(testProject.getFolder("src/main/java").getFullPath()), |
| 55 | + JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER")) }, |
| 56 | + null); |
| 57 | + |
| 58 | + // 3. Create test sources |
| 59 | + folder.getFolder("first").create(true, true, null); |
| 60 | + EclipseUtils.createTestSourceFile(testProject, "/src/main/java/first/First.java", |
| 61 | + IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/First.java"), |
| 62 | + StandardCharsets.UTF_8.name())); |
| 63 | + folder.getFolder("second").create(true, true, null); |
| 64 | + EclipseUtils.createTestSourceFile(testProject, "/src/main/java/second/Second.java", |
| 65 | + IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/Second.java"), |
| 66 | + StandardCharsets.UTF_8.name())); |
| 67 | + folder.getFolder("third").create(true, true, null); |
| 68 | + EclipseUtils.createTestSourceFile(testProject, "/src/main/java/third/Third.java", |
| 69 | + IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/Third.java"), |
| 70 | + StandardCharsets.UTF_8.name())); |
| 71 | + |
| 72 | + // 4. Copy rulesets |
| 73 | + EclipseUtils.createTestSourceFile(testProject, "/ruleset1.xml", |
| 74 | + IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/ruleset1.xml"), |
| 75 | + StandardCharsets.UTF_8.name())); |
| 76 | + EclipseUtils.createTestSourceFile(testProject, "/ruleset2.xml", |
| 77 | + IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/ruleset2.xml"), |
| 78 | + StandardCharsets.UTF_8.name())); |
| 79 | + |
| 80 | + // 5. Enable and configure PMD for the test project |
| 81 | + IProjectPropertiesManager propertiesManager = PMDPlugin.getDefault().getPropertiesManager(); |
| 82 | + IProjectProperties properties = propertiesManager.loadProjectProperties(testProject); |
| 83 | + properties.setPmdEnabled(true); |
| 84 | + properties.setRuleSetStoredInProject(true); |
| 85 | + properties.setRuleSetFile("ruleset1.xml,ruleset2.xml"); |
| 86 | + propertiesManager.storeProjectProperties(properties); |
| 87 | + } |
| 88 | + |
| 89 | + @After |
| 90 | + public void tearDown() throws Exception { |
| 91 | + try { |
| 92 | + if (this.testProject != null) { |
| 93 | + if (this.testProject.exists() && this.testProject.isAccessible()) { |
| 94 | + EclipseUtils.removePMDNature(this.testProject); |
| 95 | + this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 96 | + this.testProject.delete(true, true, null); |
| 97 | + this.testProject = null; |
| 98 | + } |
| 99 | + } |
| 100 | + } catch (final Exception e) { |
| 101 | + System.out.println("Exception " + e.getClass().getName() + " when tearing down. Ignored."); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test the basic usage of the processor command |
| 107 | + */ |
| 108 | + @Test |
| 109 | + public void testReviewCmdBasic() throws CommandException, CoreException { |
| 110 | + final ReviewCodeCmd cmd = new ReviewCodeCmd(); |
| 111 | + cmd.addResource(this.testProject); |
| 112 | + cmd.performExecute(); |
| 113 | + cmd.join(); |
| 114 | + |
| 115 | + IMarker[] markersFirst = this.testProject.getFile("src/main/java/first/First.java") |
| 116 | + .findMarkers(PMDRuntimeConstants.PMD_MARKER_1, false, 1); |
| 117 | + Assert.assertEquals(2, markersFirst.length); |
| 118 | + assertHasRuleViolation(markersFirst, "ClassNamingConventions"); |
| 119 | + assertHasRuleViolation(markersFirst, "UseUtilityClass"); |
| 120 | + |
| 121 | + IMarker[] markersSecond = this.testProject.getFile("src/main/java/second/Second.java") |
| 122 | + .findMarkers(PMDRuntimeConstants.PMD_MARKER_1, false, 1); |
| 123 | + Assert.assertEquals(1, markersSecond.length); |
| 124 | + assertHasRuleViolation(markersSecond, "UseUtilityClass"); |
| 125 | + |
| 126 | + IMarker[] markersThird = this.testProject.getFile("src/main/java/third/Third.java") |
| 127 | + .findMarkers(PMDRuntimeConstants.PMD_MARKER_1, false, 1); |
| 128 | + Assert.assertEquals(1, markersThird.length); |
| 129 | + assertHasRuleViolation(markersThird, "ClassNamingConventions"); |
| 130 | + } |
| 131 | + |
| 132 | + private void assertHasRuleViolation(IMarker[] markers, String rulename) throws CoreException { |
| 133 | + boolean found = false; |
| 134 | + for (IMarker marker : markers) { |
| 135 | + if (marker.getAttribute(PMDRuntimeConstants.KEY_MARKERATT_RULENAME).equals(rulename)) { |
| 136 | + found = true; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + if (!found) { |
| 141 | + Assert.fail("Expected violation for rule " + rulename + ", but no violation found!"); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments