more reliably closing progress handle
and finally I've writen a test for this


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4188 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Radim Kubacki
2006-02-01 15:29:59 +00:00
parent f204d3613a
commit 74b6df5d7a
5 changed files with 214 additions and 103 deletions

View File

@ -1,3 +1,5 @@
- fixed NPE when scanning is on (http://www.netbeans.org/issues/show_bug.cgi?id=72017)
Version 1.5
- Updated pmd to version 3.5
- Options customization updated to plug into new NetBeans 5.0 options dialog

View File

@ -1,5 +1,5 @@
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=6fa991b9
nbproject/build-impl.xml.data.CRC32=1c3d5cdd
nbproject/build-impl.xml.script.CRC32=4930d82d
nbproject/build-impl.xml.stylesheet.CRC32=d126b16b

View File

@ -12,3 +12,4 @@ nbm.module.author=Ole-Martin M\u00c3\u00b8rk, Gunnlaugur \u00c3\u009e\u00c3\u00b
#you are building your module against, and into which your module will be built.
#Available in project.properties.
test.unit.run.cp.extra=${netbeans.dest.dir}/platform6/modules/org-netbeans-modules-masterfs.jar

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2003, the pmd-netbeans team
* Copyright (c) 2002-2006, the pmd-netbeans team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -146,109 +146,117 @@ public class RunPMDAction extends CookieAction {
}
/**
* Runs PMD on the given list of DataObjects, with no callback.
* This just calls {@link #checkCookies(List, RunPMDCallback)} with a default callback that displays
* progress in the status bar.
*
* @param dataobjects the list of data objects to run PMD on, not null. Elements are instanceof
* {@link DataObject}.
* @return the list of rule violations found in the run, not null. Elements are instanceof {@link Fault}.
* @throws IOException on failure to read one of the files or to write to the output window.
*/
public static List checkCookies( List dataobjects ) throws IOException {
SourceLevelQuery sourceLevelQuery =
(SourceLevelQuery) Lookup.getDefault().lookup(SourceLevelQuery.class);
RuleSet set = constructRuleSets();
PMD pmd_1_3 = null;
PMD pmd_1_4 = null;
PMD pmd_1_5 = null;
ArrayList list = new ArrayList( 100 );
CancelCallback cancel = new CancelCallback ();
ProgressHandle prgHdl = ProgressHandleFactory.createHandle("PMD check", cancel); // PENDING action to show output
prgHdl.start(dataobjects.size());
for( int i = 0; i < dataobjects.size(); i++ ) {
if (cancel.isCancelled())
break;
DataObject dataobject = ( DataObject )dataobjects.get( i );
prgHdl.progress(dataobject.getName(), i); // TODO: I18N 'name', x of y
FileObject fobj = dataobject.getPrimaryFile();
String name = ClassPath.getClassPath( fobj, ClassPath.SOURCE ).getResourceName( fobj, '.', false );
//The file is not a java file
if( !dataobject.getPrimaryFile().hasExt( "java" ) || dataobject.getCookie( LineCookie.class ) == null ) {
continue;
}
String sourceLevel = sourceLevelQuery.getSourceLevel(fobj);
// choose the correct PMD to use according to the source level
PMD pmd = null;
if (sourceLevel != null) {
if (sourceLevel.equals("1.5")) {
if (pmd_1_5 == null)
pmd_1_5 = new PMD(new TargetJDK1_5());
pmd = pmd_1_5;
} else if (sourceLevel.equals("1.3")) {
if (pmd_1_3 == null)
pmd_1_3 = new PMD(new TargetJDK1_3());
pmd = pmd_1_3;
}
}
// default to JDK 1.4 if we don't know any better...
if (pmd == null) {
if (pmd_1_4 == null)
pmd_1_4 = new PMD(new TargetJDK1_4());
pmd = pmd_1_4;
}
/**
* Runs PMD on the given list of DataObjects, with no callback.
* This just calls {@link #checkCookies(List, RunPMDCallback)} with a default callback that displays
* progress in the status bar.
*
* @param dataobjects the list of data objects to run PMD on. Elements are instanceof
* {@link DataObject}.
* @return the list of rule violations found in the run, not null. Elements are instanceof {@link Fault}.
* @throws IOException on failure to read one of the files or to write to the output window.
*/
public static List checkCookies( List/*<DataObject>*/ dataobjects ) throws IOException {
assert dataobjects != null: "Cannot pass null to RunPMDAction.checkCookies()";
SourceLevelQuery sourceLevelQuery =
(SourceLevelQuery) Lookup.getDefault().lookup(SourceLevelQuery.class);
RuleSet set = constructRuleSets();
PMD pmd_1_3 = null;
PMD pmd_1_4 = null;
PMD pmd_1_5 = null;
ArrayList list = new ArrayList( 100 );
Reader reader;
try {
reader = getSourceReader( dataobject );
}
catch( IOException ioe) {
Fault fault = new Fault( 1, name, "IOException reading file for class " + name + ": " + ioe.toString());
ErrorManager.getDefault().notify( ioe );
list.add( fault );
FaultRegistry.getInstance().registerFault( fault, dataobject );
continue;
}
RuleContext ctx = new RuleContext();
Report report = new Report();
ctx.setReport( report );
ctx.setSourceCodeFilename( name );
try {
pmd.processFile( reader, set, ctx );
}
catch( PMDException e ) {
Fault fault = new Fault( 1, name, e );
ErrorManager.getDefault().log(ErrorManager.ERROR, "PMD threw exception " + e.toString());
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
// XXX why to report this ?
list.add( fault );
FaultRegistry.getInstance().registerFault( fault, dataobject );
}
CancelCallback cancel = new CancelCallback();
ProgressHandle prgHdl = ProgressHandleFactory.createHandle("PMD check", cancel); // PENDING action to show output
prgHdl.start(dataobjects.size());
try {
for( int i = 0; i < dataobjects.size(); i++ ) {
if (cancel.isCancelled())
break;
DataObject dataobject = ( DataObject )dataobjects.get( i );
prgHdl.progress(dataobject.getName(), i); // TODO: I18N 'name', x of y
FileObject fobj = dataobject.getPrimaryFile();
ClassPath cp = ClassPath.getClassPath( fobj, ClassPath.SOURCE );
if (cp == null) {
// not on any classpath, ignore
continue;
}
String name = cp.getResourceName( fobj, '.', false );
Iterator iterator = ctx.getReport().iterator();
while( iterator.hasNext() ) {
RuleViolation violation = ( RuleViolation )iterator.next();
StringBuffer buffer = new StringBuffer();
buffer.append( violation.getRule().getName() ).append( ", " );
buffer.append( violation.getDescription() );
Fault fault = new Fault( violation.getNode().getBeginLine(),
violation.getFilename(),
buffer.toString() );
list.add( fault );
FaultRegistry.getInstance().registerFault( fault, dataobject );
}
}
prgHdl.finish();
Collections.sort( list );
return list;
}
//The file is not a java file
if( !dataobject.getPrimaryFile().hasExt( "java" ) || dataobject.getCookie( LineCookie.class ) == null ) {
continue;
}
String sourceLevel = sourceLevelQuery.getSourceLevel(fobj);
// choose the correct PMD to use according to the source level
PMD pmd = null;
if (sourceLevel != null) {
if (sourceLevel.equals("1.5")) {
if (pmd_1_5 == null)
pmd_1_5 = new PMD(new TargetJDK1_5());
pmd = pmd_1_5;
} else if (sourceLevel.equals("1.3")) {
if (pmd_1_3 == null)
pmd_1_3 = new PMD(new TargetJDK1_3());
pmd = pmd_1_3;
}
}
// default to JDK 1.4 if we don't know any better...
if (pmd == null) {
if (pmd_1_4 == null)
pmd_1_4 = new PMD(new TargetJDK1_4());
pmd = pmd_1_4;
}
Reader reader;
try {
reader = getSourceReader( dataobject );
} catch( IOException ioe) {
Fault fault = new Fault( 1, name, "IOException reading file for class " + name + ": " + ioe.toString());
ErrorManager.getDefault().notify( ioe );
list.add( fault );
FaultRegistry.getInstance().registerFault( fault, dataobject );
continue;
}
RuleContext ctx = new RuleContext();
Report report = new Report();
ctx.setReport( report );
ctx.setSourceCodeFilename( name );
try {
pmd.processFile( reader, set, ctx );
} catch( PMDException e ) {
Fault fault = new Fault( 1, name, e );
ErrorManager.getDefault().log(ErrorManager.ERROR, "PMD threw exception " + e.toString());
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
// XXX why to report this ?
list.add( fault );
FaultRegistry.getInstance().registerFault( fault, dataobject );
}
Iterator iterator = ctx.getReport().iterator();
while( iterator.hasNext() ) {
RuleViolation violation = ( RuleViolation )iterator.next();
StringBuffer buffer = new StringBuffer();
buffer.append( violation.getRule().getName() ).append( ", " );
buffer.append( violation.getDescription() );
Fault fault = new Fault( violation.getNode().getBeginLine(),
violation.getFilename(),
buffer.toString() );
list.add( fault );
FaultRegistry.getInstance().registerFault( fault, dataobject );
}
}
}
finally {
prgHdl.finish();
}
Collections.sort( list );
return list;
}
/**

View File

@ -0,0 +1,100 @@
/*
* Copyright (c) 2002-2003, the pmd-netbeans team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
package pmd;
import junit.framework.*;
import java.util.Collections;
import java.util.List;
import org.netbeans.junit.NbTestCase;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
/**
* @author radim
*/
public class RunPMDActionTest extends NbTestCase {
public RunPMDActionTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
}
public static Test suite() {
TestSuite suite = new TestSuite(RunPMDActionTest.class);
return suite;
}
/**
* Test of getHelpCtx method, of class pmd.RunPMDAction.
*/
public void testGetHelpCtx() {
pmd.RunPMDAction instance = (RunPMDAction)RunPMDAction.get(RunPMDAction.class);
assertNotNull("There is no help context associated to RunPMDAction", instance.getHelpCtx());
}
/**
* Test of checkCookies method, of class pmd.RunPMDAction.
*/
public void testCheckCookies() throws Exception {
clearWorkDir();
// List dataobjects = null;
// List expResult = null;
List result;
// try on empty list
result = pmd.RunPMDAction.checkCookies(Collections.EMPTY_LIST);
// assertEquals(expResult, result);
FileObject dir = FileUtil.toFileObject(getWorkDir());
assertNotNull("Cannot find FileObject for work dir", dir);
FileObject f1;
f1 = dir.createData("MANIFEST.MF");
assertNotNull("Cannot create file in work dir", f1);
DataObject d1 = DataObject.find(f1);
assertNotNull("Cannot find a data object", d1);
result = pmd.RunPMDAction.checkCookies(Collections.singletonList(d1));
assertEquals("There should be no error for MANIFEST.MF file", 0, result.size());
}
/**
* Test of asynchronous method, of class pmd.RunPMDAction.
*/
public void testAsynchronous() {
pmd.RunPMDAction instance = (RunPMDAction)RunPMDAction.get(RunPMDAction.class);
assertTrue("RunPMDAction should be asynchronous", instance.asynchronous());
}
}