Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #9 from dbmeneses/master
Browse files Browse the repository at this point in the history
Improve coverage
  • Loading branch information
henryju committed Oct 9, 2015
2 parents 7cdacd4 + 55dd73a commit 01ef11c
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/test/java/org/codehaus/mojo/sonar/SonarMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.codehaus.mojo.sonar;

import org.junit.Before;
import org.apache.maven.plugin.logging.Log;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
Expand All @@ -42,6 +44,11 @@
import java.io.IOException;
import java.util.Properties;

import static org.mockito.Mockito.atLeastOnce;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.MapAssert.entry;

Expand All @@ -56,12 +63,20 @@ public class SonarMojoTest
@Rule
public MojoRule mojoRule = new MojoRule();

private Log mockedLogger;

private SonarMojo getMojo( File baseDir )
throws Exception
{
return (SonarMojo) mojoRule.lookupConfiguredMojo( baseDir, "sonar" );
}

@Before
public void setUpMocks()
{
mockedLogger = mock( Log.class );
}

@Test
public void executeMojo()
throws Exception
Expand Down Expand Up @@ -120,7 +135,7 @@ public void shouldExportDependencies()
new DefaultArtifactRepository( "local", localRepo.toURI().toURL().toString(), new DefaultRepositoryLayout() );
File baseDir = executeProject( "export-dependencies", localRepo );

Properties outProps = readProps();
Properties outProps = readProps( "target/dump.properties" );
String libJson = outProps.getProperty( "sonar.maven.projectDependencies" );

JSONAssert.assertEquals( "[{\"k\":\"commons-io:commons-io\",\"v\":\"2.4\",\"s\":\"compile\",\"d\":["
Expand Down Expand Up @@ -158,6 +173,18 @@ public void findbugsExcludeFile()
{
executeProject( "project-with-findbugs", temp.newFile() );
assertPropsContains( entry( "sonar.findbugs.excludeFilters", "findbugs-exclude.xml" ) );
assertThat( readProps( "target/dump.properties.global" ) ).excludes( ( entry( "sonar.verbose", "true" ) ) );

}

@Test
public void verbose()
throws Exception
{
when( mockedLogger.isDebugEnabled() ).thenReturn( true );
executeProject( "project-with-findbugs", temp.newFile() );
verify( mockedLogger, atLeastOnce() ).isDebugEnabled();
assertThat( readProps( "target/dump.properties.global" ) ).includes( ( entry( "sonar.verbose", "true" ) ) );
}

private File executeProject( String projectName, File localRepo )
Expand All @@ -169,6 +196,8 @@ private File executeProject( String projectName, File localRepo )
File baseDir = new File( "src/test/resources/org/codehaus/mojo/sonar/SonarMojoTest/" + projectName );
SonarMojo mojo = getMojo( baseDir );
mojo.setLocalRepository( artifactRepo );
mojo.setLog( mockedLogger );

mojo.execute();

return baseDir;
Expand All @@ -177,16 +206,16 @@ private File executeProject( String projectName, File localRepo )
private void assertPropsContains( MapAssert.Entry... entries )
throws FileNotFoundException, IOException
{
assertThat( readProps() ).includes( entries );
assertThat( readProps( "target/dump.properties" ) ).includes( entries );
}

private Properties readProps()
private Properties readProps( String filePath )
throws FileNotFoundException, IOException
{
FileInputStream fis = null;
try
{
File dump = new File( "target/dump.properties" );
File dump = new File( filePath );
Properties props = new Properties();
fis = new FileInputStream( dump );
props.load( fis );
Expand Down

0 comments on commit 01ef11c

Please sign in to comment.