|
5 | 5 | package net.sourceforge.pmd.eclipse.runtime.cmd; |
6 | 6 |
|
7 | 7 | import java.io.InputStream; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
8 | 10 | import java.util.ArrayList; |
9 | 11 | import java.util.Arrays; |
10 | 12 | import java.util.List; |
|
14 | 16 | import org.eclipse.core.resources.IFile; |
15 | 17 | import org.eclipse.core.resources.IMarker; |
16 | 18 | import org.eclipse.core.resources.IProject; |
| 19 | +import org.eclipse.core.resources.IProjectDescription; |
17 | 20 | import org.eclipse.core.resources.IResource; |
| 21 | +import org.eclipse.core.resources.IWorkspaceRoot; |
18 | 22 | import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 23 | +import org.eclipse.core.resources.ResourcesPlugin; |
19 | 24 | import org.eclipse.core.runtime.CoreException; |
20 | 25 | import org.junit.After; |
21 | 26 | import org.junit.Assert; |
@@ -155,6 +160,70 @@ public void testProjectBuildPath() throws Exception { |
155 | 160 | } |
156 | 161 | } |
157 | 162 |
|
| 163 | + /** |
| 164 | + * https://github.com/pmd/pmd-eclipse-plugin/issues/96 |
| 165 | + */ |
| 166 | + @Test |
| 167 | + public void testProjectBuildPathOutsideWorkspace() throws Exception { |
| 168 | + String projectName = "PMDTestProject2"; |
| 169 | + Path tempDir = Files.createTempDirectory(projectName); |
| 170 | + boolean oldSetting = PMDPlugin.getDefault().getPreferencesManager().loadPreferences() |
| 171 | + .isProjectBuildPathEnabled(); |
| 172 | + final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 173 | + final IProject newProject = root.getProject(projectName); |
| 174 | + |
| 175 | + try { |
| 176 | + final IProjectDescription description = newProject.getWorkspace().newProjectDescription(projectName); |
| 177 | + if (!newProject.exists()) { |
| 178 | + description.setLocationURI(tempDir.toUri()); |
| 179 | + newProject.create(description, null); |
| 180 | + } |
| 181 | + |
| 182 | + if (!newProject.isOpen()) { |
| 183 | + newProject.open(null); |
| 184 | + } |
| 185 | + EclipseUtils.addJavaNature(newProject); |
| 186 | + |
| 187 | + IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager() |
| 188 | + .loadProjectProperties(newProject); |
| 189 | + properties.setPmdEnabled(true); |
| 190 | + Rule compareObjectsWithEquals = properties.getProjectRuleSet().getRuleByName("CompareObjectsWithEquals"); |
| 191 | + RuleSet projectRuleSet = RuleSetUtil.newSingle(compareObjectsWithEquals); |
| 192 | + properties.setProjectRuleSet(projectRuleSet); |
| 193 | + |
| 194 | + PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(true); |
| 195 | + EclipseUtils.createTestSourceFile(newProject, "/src/MyEnum.java", "public enum MyEnum { A, B }"); |
| 196 | + IFile sourceFile = EclipseUtils.createTestSourceFile(newProject, "/src/Foo.java", |
| 197 | + "class Foo {\n" + " boolean bar(MyEnum a, MyEnum b) {\n" + " return a == b;\n" + // line 3 |
| 198 | + " }\n" + "}"); |
| 199 | + newProject.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 200 | + newProject.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 201 | + |
| 202 | + ReviewCodeCmd cmd = new ReviewCodeCmd(); |
| 203 | + cmd.addResource(newProject); |
| 204 | + cmd.performExecute(); |
| 205 | + cmd.join(); |
| 206 | + Map<IFile, Set<MarkerInfo2>> markers = cmd.getMarkers(); |
| 207 | + // with type resolution, this comparison is ok, as MyEnum is a enum |
| 208 | + Assert.assertTrue("Type Resolution didn't work", markers.get(sourceFile).isEmpty()); |
| 209 | + |
| 210 | + // without type resolution, there is a violation |
| 211 | + PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(false); |
| 212 | + cmd = new ReviewCodeCmd(); |
| 213 | + cmd.addResource(newProject); |
| 214 | + cmd.performExecute(); |
| 215 | + cmd.join(); |
| 216 | + markers = cmd.getMarkers(); |
| 217 | + // there is a violation expected without type resolution |
| 218 | + Assert.assertFalse(markers.get(sourceFile).isEmpty()); |
| 219 | + |
| 220 | + } finally { |
| 221 | + PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(oldSetting); |
| 222 | + |
| 223 | + newProject.delete(true, true, null); |
| 224 | + } |
| 225 | + } |
| 226 | + |
158 | 227 | /** |
159 | 228 | * The ReviewCodeCmd must also work on a ResourceDelta |
160 | 229 | */ |
|
0 commit comments