Attempt to fix AssertionError when getting input reader - getDocument should not cause init of editor if the file is not opened

http://www.netbeans.org/issues/show_bug.cgi?id=71141


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4214 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Radim Kubacki
2006-02-09 14:02:42 +00:00
parent a43d7d89e0
commit 9376ba1fb4
3 changed files with 38 additions and 8 deletions

View File

@ -1,6 +1,8 @@
- fixed some bugs
http://www.netbeans.org/issues/show_bug.cgi?id=72017
http://www.netbeans.org/issues/show_bug.cgi?id=72215
http://www.netbeans.org/issues/show_bug.cgi?id=71141
- few UI tweaks
Version 1.5
- Updated pmd to version 3.5

View File

@ -36,6 +36,8 @@ import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDException;
@ -330,15 +332,19 @@ public class RunPMDAction extends CookieAction {
* @exception IOException if the object can't be read
*/
private static Reader getSourceReader( DataObject dataobject ) throws IOException {
Reader reader;
Reader reader = null;
EditorCookie editor = ( EditorCookie )dataobject.getCookie( EditorCookie.class );
//If it's the currently open document that's being checked
if( editor != null && editor.getOpenedPanes() != null ) {
String text = editor.getOpenedPanes()[0].getText();
reader = new StringReader( text );
}
else {
if (editor != null) {
StyledDocument doc = editor.getDocument();
if (doc != null) {
try {
reader = new StringReader (doc.getText(0, doc.getLength()));
} catch (BadLocationException ex) {
// OK, fallback to read from FO
}
}
}
if (reader == null) {
Iterator iterator = dataobject.files().iterator();
FileObject file = ( FileObject )iterator.next();
reader = new BufferedReader( new InputStreamReader( file.getInputStream() ) );

View File

@ -26,10 +26,12 @@
*/
package pmd;
import java.io.PrintStream;
import junit.framework.*;
import java.util.Collections;
import java.util.List;
import org.netbeans.junit.NbTestCase;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
@ -87,6 +89,26 @@ public class RunPMDActionTest extends NbTestCase {
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());
f1 = dir.createData("PMDSample.java");
assertNotNull("Cannot create file in work dir", f1);
FileLock l = null;
try {
l = f1.lock();
PrintStream ps = new PrintStream (f1.getOutputStream(l));
ps.print("public class PMDSample { PMDSample () {} }");
ps.close();
}
finally {
if (l != null) {
l.releaseLock();
}
}
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 PMDSample.java file", 0, result.size());
}
/**