|
| 1 | +/* |
| 2 | + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html |
| 3 | + */ |
| 4 | + |
| 5 | +package net.sourceforge.pmd.eclipse.ui.actions; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | + |
| 10 | +import org.apache.commons.io.FilenameUtils; |
| 11 | +import org.apache.commons.io.IOUtils; |
| 12 | +import org.eclipse.core.resources.IFile; |
| 13 | +import org.eclipse.core.resources.IProject; |
| 14 | +import org.eclipse.core.resources.IResource; |
| 15 | +import org.eclipse.core.runtime.CoreException; |
| 16 | +import org.eclipse.jface.action.IAction; |
| 17 | +import org.eclipse.jface.viewers.ISelection; |
| 18 | +import org.eclipse.jface.viewers.StructuredSelection; |
| 19 | +import org.eclipse.swt.widgets.Display; |
| 20 | +import org.junit.After; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import net.sourceforge.pmd.eclipse.EclipseUtils; |
| 26 | +import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; |
| 27 | +import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties; |
| 28 | + |
| 29 | +public class PMDGenerateASTActionTest { |
| 30 | + private IProject testProject; |
| 31 | + private IFile testFile; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() throws Exception { |
| 35 | + // 1. Create a Java project |
| 36 | + this.testProject = EclipseUtils.createJavaProject("PMDGenerateASTActionTest"); |
| 37 | + Assert.assertTrue("A test project cannot be created; the tests cannot be performed.", |
| 38 | + this.testProject != null && this.testProject.exists() && this.testProject.isAccessible()); |
| 39 | + |
| 40 | + // 2. Create a test source file inside that project |
| 41 | + this.testFile = EclipseUtils.createTestSourceFile(this.testProject); |
| 42 | + final InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java"); |
| 43 | + Assert.assertNotNull("Cannot find the test source file", is); |
| 44 | + is.close(); |
| 45 | + |
| 46 | + // 3. Enable PMD for the test project |
| 47 | + IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager() |
| 48 | + .loadProjectProperties(testProject); |
| 49 | + properties.setPmdEnabled(true); |
| 50 | + } |
| 51 | + |
| 52 | + @After |
| 53 | + public void tearDown() throws Exception { |
| 54 | + try { |
| 55 | + if (this.testProject != null) { |
| 56 | + if (this.testProject.exists() && this.testProject.isAccessible()) { |
| 57 | + EclipseUtils.removePMDNature(this.testProject); |
| 58 | + this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 59 | + this.testProject.delete(true, true, null); |
| 60 | + this.testProject = null; |
| 61 | + } |
| 62 | + } |
| 63 | + } catch (final Exception e) { |
| 64 | + System.out.println("Exception " + e.getClass().getName() + " when tearing down. Ignored."); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void runGenerateASTAction() throws CoreException, InterruptedException, IOException { |
| 70 | + Display.getDefault().syncExec(new Runnable() { |
| 71 | + @Override |
| 72 | + public void run() { |
| 73 | + ISelection selection = new StructuredSelection(testFile); |
| 74 | + PMDGenerateASTAction action = new PMDGenerateASTAction(); |
| 75 | + action.selectionChanged(null, selection); |
| 76 | + action.run((IAction) null); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + String astFilename = FilenameUtils.getBaseName(testFile.getName()) + ".ast"; |
| 81 | + IResource astFile; |
| 82 | + long start = System.currentTimeMillis(); |
| 83 | + do { |
| 84 | + Thread.sleep(500); |
| 85 | + astFile = testFile.getParent().findMember(astFilename); |
| 86 | + } while (astFile == null || (System.currentTimeMillis() - start) > 60_000); |
| 87 | + |
| 88 | + Assert.assertNotNull("No AST file has been generated", astFile); |
| 89 | + IFile adapter = (IFile) astFile.getAdapter(IFile.class); |
| 90 | + String content = IOUtils.toString(adapter.getContents(), adapter.getCharset()); |
| 91 | + Assert.assertTrue(content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")); |
| 92 | + Assert.assertTrue(content.contains("<CompilationUnit")); |
| 93 | + } |
| 94 | +} |
0 commit comments