Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into laurents59-master
  • Loading branch information
Antonio Muñiz committed May 18, 2015
2 parents 182e0c9 + 01021fb commit ede386a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
12 changes: 5 additions & 7 deletions src/main/java/org/sonar/report/pdf/batch/PDFGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.resources.Project;
import org.sonar.report.pdf.ExecutivePDFReporter;
import org.sonar.report.pdf.PDFReporter;
Expand All @@ -41,20 +42,18 @@ public class PDFGenerator {
private static final Logger LOG = LoggerFactory.getLogger(PDFGenerator.class);

private String sonarHostUrl;

private String username;

private String password;

private String sonarBranch;

private String reportType;

private Project project;
private FileSystem fs;

public PDFGenerator(final Project project, final String sonarHostUrl, final String username, final String password,
public PDFGenerator(final Project project, final FileSystem fs, final String sonarHostUrl, final String username, final String password,
final String sonarBranch, final String reportType) {
this.project = project;
this.fs = fs;
this.sonarHostUrl = sonarHostUrl;
this.username = username;
this.password = password;
Expand All @@ -81,8 +80,7 @@ public void execute() {
Credentials credentials = new Credentials(config.getProperty("sonar.base.url"), username, password);

String sonarProjectId = project.getEffectiveKey();
String path = project.getFileSystem().getSonarWorkingDirectory().getAbsolutePath() + "/"
+ sonarProjectId.replace(':', '-') + ".pdf";
String path = fs.workDir().getAbsolutePath() + "/" + sonarProjectId.replace(':', '-') + ".pdf";

if (sonarBranch != null) {
sonarProjectId += ":" + sonarBranch;
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/org/sonar/report/pdf/batch/PDFPostJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.sonar.api.batch.CheckProject;
import org.sonar.api.batch.PostJob;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.report.pdf.util.FileUploader;

Expand All @@ -51,26 +53,33 @@ public class PDFPostJob implements PostJob, CheckProject {

public static final String SONAR_BRANCH = "sonar.branch";
public static final String SONAR_BRANCH_DEFAULT_VALUE = null;

private final Settings settings;
private final FileSystem fs;

public PDFPostJob(Settings settings, FileSystem fs) {
this.settings = settings;
this.fs = fs;
}

@Override
public boolean shouldExecuteOnProject(final Project project) {
return !project.getConfiguration().getBoolean(SKIP_PDF_KEY, SKIP_PDF_DEFAULT_VALUE);
return settings.hasKey(SKIP_PDF_KEY) ? !settings.getBoolean(SKIP_PDF_KEY) : !SKIP_PDF_DEFAULT_VALUE;
}

@Override
public void executeOn(final Project project, final SensorContext context) {
LOG.info("Executing decorator: PDF Report");
String sonarHostUrl = project.getConfiguration().getString(SONAR_HOST_URL, SONAR_HOST_URL_DEFAULT_VALUE);
String username = project.getConfiguration().getString(USERNAME, USERNAME_DEFAULT_VALUE);
String password = project.getConfiguration().getString(PASSWORD, PASSWORD_DEFAULT_VALUE);
String branch = project.getConfiguration().getString(SONAR_BRANCH, SONAR_BRANCH_DEFAULT_VALUE);
String reportType = project.getConfiguration().getString(REPORT_TYPE, REPORT_TYPE_DEFAULT_VALUE);
PDFGenerator generator = new PDFGenerator(project, sonarHostUrl, username, password, branch, reportType);
String sonarHostUrl = settings.hasKey(SONAR_HOST_URL) ? settings.getString(SONAR_HOST_URL) : SONAR_HOST_URL_DEFAULT_VALUE;
String username = settings.hasKey(USERNAME) ? settings.getString(USERNAME) : USERNAME_DEFAULT_VALUE;
String password = settings.hasKey(PASSWORD) ? settings.getString(PASSWORD) : PASSWORD_DEFAULT_VALUE;
String branch = settings.hasKey(SONAR_BRANCH) ? settings.getString(SONAR_BRANCH) : SONAR_BRANCH_DEFAULT_VALUE;
String reportType = settings.hasKey(REPORT_TYPE) ? settings.getString(REPORT_TYPE) : REPORT_TYPE_DEFAULT_VALUE;
PDFGenerator generator = new PDFGenerator(project, fs, sonarHostUrl, username, password, branch, reportType);

generator.execute();

String path = project.getFileSystem().getSonarWorkingDirectory().getAbsolutePath() + "/"
+ project.getEffectiveKey().replace(':', '-') + ".pdf";
String path = fs.workDir().getAbsolutePath() + "/" + project.getEffectiveKey().replace(':', '-') + ".pdf";

File pdf = new File(path);
if (pdf.exists()) {
Expand Down
49 changes: 30 additions & 19 deletions src/test/java/org/sonar/report/pdf/test/PDFPostJobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,40 @@
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.report.pdf.batch.PDFPostJob;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class PDFPostJobTest {

@Test(groups = { "post-job" })
public void doNotExecuteIfSkipParameter() {
PropertiesConfiguration conf = new PropertiesConfiguration();
conf.setProperty(PDFPostJob.SKIP_PDF_KEY, Boolean.TRUE);

Project project = mock(Project.class);
when(project.getConfiguration()).thenReturn(conf);

assertFalse(new PDFPostJob().shouldExecuteOnProject(project));
}

@Test(groups = { "post-job" })
public void shouldExecuteIfNoSkipParameter() {
Project project = mock(Project.class);
when(project.getConfiguration()).thenReturn(new PropertiesConfiguration());

assertTrue(new PDFPostJob().shouldExecuteOnProject(project));
}
private Project project;
private Settings settings;
private DefaultFileSystem fs;
private PDFPostJob pdfPostJob;

@BeforeGroups(groups = { "post-job" })
public void before() {
project = mock(Project.class);
settings = mock(Settings.class);
fs = new DefaultFileSystem();
pdfPostJob = new PDFPostJob(settings, fs);
}

@Test(groups = { "post-job" })
public void doNotExecuteIfSkipParameter() {
when(settings.hasKey(PDFPostJob.SKIP_PDF_KEY)).thenReturn(Boolean.TRUE);
when(settings.getBoolean(PDFPostJob.SKIP_PDF_KEY)).thenReturn(Boolean.TRUE);

assertFalse(pdfPostJob.shouldExecuteOnProject(project));
}

@Test(groups = { "post-job" })
public void shouldExecuteIfNoSkipParameter() {
when(settings.hasKey(PDFPostJob.SKIP_PDF_KEY)).thenReturn(Boolean.FALSE);

assertTrue(pdfPostJob.shouldExecuteOnProject(project));
}
}

0 comments on commit ede386a

Please sign in to comment.