Skip to content

Commit

Permalink
profileFormat can now take multiple, comma-separated formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-bochenski committed Jan 16, 2017
1 parent 49e86de commit d2ccdb9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
28 changes: 23 additions & 5 deletions src/main/java/fr/jcgay/maven/profiler/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package fr.jcgay.maven.profiler;

import static com.google.common.base.Functions.compose;
import static com.google.common.base.Functions.forMap;
import static com.google.common.collect.Collections2.transform;
import static java.util.Arrays.asList;

import java.util.List;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableMap;

import fr.jcgay.maven.profiler.reporting.CompositeReporter;
import fr.jcgay.maven.profiler.reporting.Reporter;
import fr.jcgay.maven.profiler.reporting.html.HtmlReporter;
import fr.jcgay.maven.profiler.reporting.json.JsonReporter;
Expand All @@ -12,6 +24,15 @@ public class Configuration {
private static final String PROFILE = "profile";
private static final String PROFILE_FORMAT = "profileFormat";
private static final String DISABLE_TIME_SORTING = "disableTimeSorting";

private static final Function<String,Reporter> reporters = compose(forMap(ImmutableMap.<String,Reporter>builder()
.put("html", new HtmlReporter())
.put("json", new JsonReporter())
.build()), new Function<String,String>(){
@Override
public String apply(String it) {
return it.toLowerCase();
}});

private final boolean isProfiling;
private final Reporter reporter;
Expand Down Expand Up @@ -47,11 +68,8 @@ private static Sorter chooseSorter() {
}

private static Reporter chooseReporter() {
String formatProperty = System.getProperty(PROFILE_FORMAT);
if (formatProperty != null && "json".equalsIgnoreCase(formatProperty)) {
return new JsonReporter();
}
return new HtmlReporter();
List<String> formats = asList(System.getProperty(PROFILE_FORMAT,"html").split(","));
return new CompositeReporter(transform(formats,reporters));
}

private static boolean isSortingActive() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fr.jcgay.maven.profiler.reporting;

import java.util.Collection;

import fr.jcgay.maven.profiler.reporting.template.Data;

public final class CompositeReporter implements Reporter {

private final Collection<? extends Reporter> delegates;

public CompositeReporter(Collection<? extends Reporter> delegates) {
this.delegates = delegates;
}

@Override
public void write(Data data, ReportDirectory directory) {
for (Reporter r : delegates) {
r.write(data, directory);
}
}

}
20 changes: 17 additions & 3 deletions src/test/groovy/fr/jcgay/maven/profiler/ConfigurationTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConfigurationTest {

def result = Configuration.read()

assertThat(result.reporter()).isExactlyInstanceOf(HtmlReporter)
assertThat(result.reporter().delegates).extracting("class").containsExactly(HtmlReporter)
}

@DataProvider
Expand All @@ -53,9 +53,23 @@ class ConfigurationTest {

def result = Configuration.read()

assertThat(result.reporter()).isExactlyInstanceOf(JsonReporter)
assertThat(result.reporter().delegates).extracting("class").containsExactly(JsonReporter)
}

@DataProvider
Object[][] 'two formats'() {
[['json,html'], ['JSON,HTML'], ['jSoN,HtMl']]
}

@Test(dataProvider = 'two formats')
void 'two report formats'(String format) {
System.setProperty('profileFormat', format)

def result = Configuration.read()

assertThat(result.reporter().delegates).extracting("class").containsExactly(JsonReporter,HtmlReporter)
}

@Test
void 'do not sort result, keep execution order'() {
System.setProperty('disableTimeSorting', 'true')
Expand All @@ -70,7 +84,7 @@ class ConfigurationTest {
def result = Configuration.read()

assertThat(result.isProfiling()).isFalse()
assertThat(result.reporter()).isExactlyInstanceOf(HtmlReporter)
assertThat(result.reporter().delegates).extracting("class").containsExactly(HtmlReporter)
assertThat(result.sorter()).isExactlyInstanceOf(ByExecutionTime)
}
}

0 comments on commit d2ccdb9

Please sign in to comment.