55package net .sourceforge .pmd .eclipse .runtime .cmd ;
66
77import java .io .InputStream ;
8+ import java .io .InputStreamReader ;
9+ import java .io .Reader ;
810
911import org .eclipse .core .resources .IFile ;
1012import org .eclipse .core .resources .IFolder ;
13+ import org .eclipse .core .resources .IMarker ;
1114import org .eclipse .core .resources .IProject ;
1215import org .eclipse .core .resources .IResource ;
1316import org .eclipse .core .runtime .CoreException ;
1720import org .junit .Test ;
1821
1922import net .sourceforge .pmd .eclipse .EclipseUtils ;
23+ import net .sourceforge .pmd .eclipse .plugin .PMDPlugin ;
2024import net .sourceforge .pmd .eclipse .runtime .PMDRuntimeConstants ;
25+ import net .sourceforge .pmd .eclipse .runtime .properties .IProjectProperties ;
26+ import net .sourceforge .pmd .eclipse .util .internal .IOUtil ;
2127import net .sourceforge .pmd .renderers .HTMLRenderer ;
28+ import net .sourceforge .pmd .renderers .TextRenderer ;
2229
2330/**
2431 * Test the report rendering
@@ -42,6 +49,11 @@ public void setUp() throws Exception {
4249 try (InputStream is = EclipseUtils .getResourceStream (this .testProject , "/src/Test.java" )) {
4350 Assert .assertNotNull ("Cannot find the test source file" , is );
4451 }
52+
53+ // 3. Enable PMD for the test project
54+ IProjectProperties properties = PMDPlugin .getDefault ().getPropertiesManager ()
55+ .loadProjectProperties (testProject );
56+ properties .setPmdEnabled (true );
4557 }
4658
4759 @ After
@@ -59,14 +71,14 @@ public void tearDown() throws Exception {
5971 }
6072
6173 /**
62- * Test the basic usage of the report rendering command
63- *
74+ * Test the basic usage of the report rendering command.
6475 */
6576 @ Test
6677 public void testRenderReportCmdBasic () throws CoreException {
6778 final ReviewCodeCmd reviewCmd = new ReviewCodeCmd ();
6879 reviewCmd .addResource (this .testProject );
6980 reviewCmd .performExecute ();
81+ reviewCmd .join ();
7082
7183 final RenderReportsCmd cmd = new RenderReportsCmd ();
7284 cmd .setProject (this .testProject );
@@ -91,6 +103,58 @@ public void testRenderReportCmdBasic() throws CoreException {
91103 }
92104 }
93105
106+ /**
107+ * Test text format renderer.
108+ */
109+ @ Test
110+ public void testRenderReportCmdText () throws Exception {
111+ final ReviewCodeCmd reviewCmd = new ReviewCodeCmd ();
112+ reviewCmd .addResource (this .testProject );
113+ reviewCmd .performExecute ();
114+ reviewCmd .join ();
115+
116+ IMarker [] markers = this .testProject .findMarkers (PMDRuntimeConstants .PMD_MARKER , true , IResource .DEPTH_INFINITE );
117+ Assert .assertEquals (14 , markers .length );
118+
119+ final RenderReportsCmd cmd = new RenderReportsCmd ();
120+ cmd .setProject (this .testProject );
121+ cmd .registerRenderer (new TextRenderer (), PMDRuntimeConstants .TXT_REPORT_NAME );
122+ cmd .performExecute ();
123+ cmd .join ();
124+
125+ final IFolder reportFolder = this .testProject .getFolder (PMDRuntimeConstants .REPORT_FOLDER );
126+ Assert .assertTrue (reportFolder .exists ());
127+
128+ final IFile reportFile = reportFolder .getFile (PMDRuntimeConstants .TXT_REPORT_NAME );
129+ Assert .assertTrue (reportFile .exists ());
130+ try (Reader reader = new InputStreamReader (reportFile .getContents (), reportFile .getCharset ())) {
131+ String report = IOUtil .toString (reader );
132+ Assert .assertEquals (1 , countMatches (report , "src/Test.java:5:\t NoPackage:\t NoPackage: All classes, interfaces, enums and annotations must belong to a named package" ));
133+ String [] lines = report .split ("\r \n |\n " );
134+ Assert .assertEquals (markers .length , lines .length );
135+ }
136+
137+ this .testProject .deleteMarkers (PMDRuntimeConstants .PMD_MARKER , true , IResource .DEPTH_INFINITE );
138+
139+ if (reportFile .exists ()) {
140+ reportFile .delete (true , false , null );
141+ }
142+
143+ if (reportFolder .exists ()) {
144+ reportFolder .delete (true , false , null );
145+ }
146+ }
147+
148+ private static int countMatches (String s , String pattern ) {
149+ int count = 0 ;
150+ int index = s .indexOf (pattern );
151+ while (index != -1 ) {
152+ count ++;
153+ index = s .indexOf (pattern , index + pattern .length ());
154+ }
155+ return count ;
156+ }
157+
94158 /**
95159 * Test robustness #1
96160 */
0 commit comments