From d2ccdb9d7897b7735a0b57ed7a21ab54e4fc26cd Mon Sep 17 00:00:00 2001 From: Jakub Bochenski Date: Mon, 16 Jan 2017 16:48:21 +0100 Subject: [PATCH] profileFormat can now take multiple, comma-separated formats --- .../jcgay/maven/profiler/Configuration.java | 28 +++++++++++++++---- .../profiler/reporting/CompositeReporter.java | 22 +++++++++++++++ .../maven/profiler/ConfigurationTest.groovy | 20 +++++++++++-- 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/main/java/fr/jcgay/maven/profiler/reporting/CompositeReporter.java diff --git a/src/main/java/fr/jcgay/maven/profiler/Configuration.java b/src/main/java/fr/jcgay/maven/profiler/Configuration.java index 954da27..f645199 100644 --- a/src/main/java/fr/jcgay/maven/profiler/Configuration.java +++ b/src/main/java/fr/jcgay/maven/profiler/Configuration.java @@ -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; @@ -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 reporters = compose(forMap(ImmutableMap.builder() + .put("html", new HtmlReporter()) + .put("json", new JsonReporter()) + .build()), new Function(){ + @Override + public String apply(String it) { + return it.toLowerCase(); + }}); private final boolean isProfiling; private final Reporter reporter; @@ -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 formats = asList(System.getProperty(PROFILE_FORMAT,"html").split(",")); + return new CompositeReporter(transform(formats,reporters)); } private static boolean isSortingActive() { diff --git a/src/main/java/fr/jcgay/maven/profiler/reporting/CompositeReporter.java b/src/main/java/fr/jcgay/maven/profiler/reporting/CompositeReporter.java new file mode 100644 index 0000000..113f1db --- /dev/null +++ b/src/main/java/fr/jcgay/maven/profiler/reporting/CompositeReporter.java @@ -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 delegates; + + public CompositeReporter(Collection delegates) { + this.delegates = delegates; + } + + @Override + public void write(Data data, ReportDirectory directory) { + for (Reporter r : delegates) { + r.write(data, directory); + } + } + +} diff --git a/src/test/groovy/fr/jcgay/maven/profiler/ConfigurationTest.groovy b/src/test/groovy/fr/jcgay/maven/profiler/ConfigurationTest.groovy index cb69585..3d74617 100644 --- a/src/test/groovy/fr/jcgay/maven/profiler/ConfigurationTest.groovy +++ b/src/test/groovy/fr/jcgay/maven/profiler/ConfigurationTest.groovy @@ -39,7 +39,7 @@ class ConfigurationTest { def result = Configuration.read() - assertThat(result.reporter()).isExactlyInstanceOf(HtmlReporter) + assertThat(result.reporter().delegates).extracting("class").containsExactly(HtmlReporter) } @DataProvider @@ -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') @@ -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) } }