Skip to content

Commit

Permalink
Fix integration test MPMD-165
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Oct 24, 2020
1 parent 5048ab8 commit bd5b017
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/it/MPMD-165/src/main/java/test/MyClass.java
Expand Up @@ -24,7 +24,7 @@ public class MyClass

public static void main( String[] args )
{
return;
return; // This is a UnnecessaryReturn violation
}

}
62 changes: 61 additions & 1 deletion src/main/java/org/apache/maven/plugins/pmd/exec/PmdResult.java
Expand Up @@ -21,6 +21,8 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FilterReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
Expand Down Expand Up @@ -60,7 +62,8 @@ public boolean hasViolations()

private void loadResult( File pmdFile, String encoding ) throws MavenReportException
{
try ( Reader reader1 = new InputStreamReader( new FileInputStream( pmdFile ), encoding ) )
try ( Reader reader1 = new BomFilter( encoding, new InputStreamReader(
new FileInputStream( pmdFile ), encoding ) ) )
{
PmdXpp3Reader reader = new PmdXpp3Reader();
PmdErrorDetail details = reader.read( reader1, false );
Expand All @@ -82,6 +85,63 @@ private void loadResult( File pmdFile, String encoding ) throws MavenReportExcep
}
}

// Note: This seems to be a bug in PMD's XMLRenderer. The BOM is rendered multiple times.
// once at the beginning of the file, which is Ok, but also in the middle of the file.
// This filter just skips all BOMs if the encoding is not UTF-8
private static class BomFilter extends FilterReader
{
private static final char BOM = '\uFEFF';
private final boolean filter;

BomFilter( String encoding, Reader in )
{
super( in );
filter = !"UTF-8".equalsIgnoreCase( encoding );
}

@Override
public int read() throws IOException
{
int c = super.read();

if ( !filter )
{
return c;
}

while ( c != -1 && c == BOM )
{
c = super.read();
}
return c;
}

@Override
public int read( char[] cbuf, int off, int len ) throws IOException
{
int count = super.read( cbuf, off, len );

if ( !filter )
{
return count;
}

if ( count != -1 )
{
for ( int i = off; i < off + count; i++ )
{
if ( cbuf[i] == BOM )
{
// shift the content one char to the left
System.arraycopy( cbuf, i + 1, cbuf, i, off + count - 1 - i );
count--;
}
}
}
return count;
}
}

public Collection<Violation> getViolations()
{
return violations;
Expand Down

0 comments on commit bd5b017

Please sign in to comment.