Skip to content

Commit 2d9b218

Browse files
committed
Add test for PMDGenerateASTAction
1 parent 82fea1d commit 2d9b218

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

net.sourceforge.pmd.eclipse.plugin.test/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<artifactId>tycho-surefire-plugin</artifactId>
2525
<configuration>
2626
<useUIHarness>true</useUIHarness>
27+
<useUIThread>false</useUIThread>
2728
<showEclipseLog>true</showEclipseLog>
2829
<trimStackTrace>false</trimStackTrace>
2930
<!-- http://wiki.eclipse.org/Eclipse4/RCP/FAQ#Why_won.27t_my_application_start.3F -->

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/ui/actions/PMDCheckActionTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.eclipse.core.runtime.jobs.Job;
1515
import org.eclipse.jface.viewers.ISelection;
1616
import org.eclipse.jface.viewers.StructuredSelection;
17+
import org.eclipse.swt.widgets.Display;
1718
import org.junit.After;
1819
import org.junit.Assert;
1920
import org.junit.Before;
@@ -66,10 +67,15 @@ public void runCheckAction() throws CoreException, InterruptedException {
6667
IMarker[] markers = testProject.findMarkers("net.sourceforge.pmd.eclipse.plugin.pmdMarker", true, IResource.DEPTH_INFINITE);
6768
Assert.assertEquals(0, markers.length);
6869

69-
ISelection selection = new StructuredSelection(testProject);
70-
PMDCheckAction action = new PMDCheckAction();
71-
action.selectionChanged(null, selection);
72-
action.run(null);
70+
Display.getDefault().syncExec(new Runnable() {
71+
@Override
72+
public void run() {
73+
ISelection selection = new StructuredSelection(testProject);
74+
PMDCheckAction action = new PMDCheckAction();
75+
action.selectionChanged(null, selection);
76+
action.run(null);
77+
}
78+
});
7379

7480
while (isReviewCodeJobStillRunning()) {
7581
Thread.sleep(500);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/writer/impl/AstWriterImpl.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.OutputStream;
88
import javax.xml.parsers.FactoryConfigurationError;
9+
import javax.xml.transform.OutputKeys;
910
import javax.xml.transform.Transformer;
1011
import javax.xml.transform.TransformerException;
1112
import javax.xml.transform.TransformerFactory;
@@ -26,20 +27,14 @@
2627
*
2728
*/
2829
class AstWriterImpl implements IAstWriter {
29-
/**
30-
* @see net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter#write(java.io.Writer,
31-
* net.sourceforge.pmd.ast.ASTCompilationUnit)
32-
*/
30+
@Override
3331
public void write(OutputStream outputStream, ASTCompilationUnit compilationUnit) throws WriterException {
3432
try {
3533
Document doc = compilationUnit.getAsDocument();
3634
Transformer transformer = TransformerFactory.newInstance().newTransformer();
35+
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
3736
transformer.transform(new DOMSource(doc), new StreamResult(outputStream));
38-
} catch (DOMException e) {
39-
throw new WriterException(e);
40-
} catch (FactoryConfigurationError e) {
41-
throw new WriterException(e);
42-
} catch (TransformerException e) {
37+
} catch (DOMException | FactoryConfigurationError | TransformerException e) {
4338
throw new WriterException(e);
4439
}
4540
}

0 commit comments

Comments
 (0)