Last active
August 31, 2015 08:01
-
-
Save simonbrandhof/665cc68b3e34e961a355 to your computer and use it in GitHub Desktop.
CE API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Option 1: a single extension point to define metadata and compute measures | |
| // advantage : single interface | |
| // drawback : interface has 2 methods | |
| @ExtensionPoint | |
| public interface MeasureComputer { | |
| // advantage : pattern is not based on method "done()". It enforces the test | |
| // to validate MeasureComputerDefinition. | |
| MeasureComputeDefinition define(MeasureComputerDefinitionContext defContext); | |
| void compute(MeasureComputerContext context); | |
| } | |
| // implementation is provided by the platform | |
| public interface MeasureComputerDefinitionBuilder { | |
| MeasureComputerDefinitionBuilder setInputMetrics(String... metrics); | |
| MeasureComputerDefinitionBuilder setOutputMetrics(String... metrics); | |
| MeasureComputerDefinition build(); | |
| } | |
| public interface MeasureComputerDefinition { | |
| List<String> getInputMetrics(); | |
| List<String> getOutputMetrics(); | |
| } | |
| public interface MeasureComputerDefinitionContext { | |
| MeasureComputerDefinitionBuilder newDefinitionBuilder(); | |
| } | |
| // implementation is provided by the platform | |
| public interface MeasureComputerContext { | |
| // fails if the metric was not declared as an input metric in MeasureComputerDefinition | |
| @CheckForNull | |
| Measure getMeasure(String metric); | |
| // ... | |
| } | |
| // example | |
| public class NclocMeasureComputer implements MeasureComputer { | |
| @Override | |
| public MeasureComputerDefinition define(MeasureComputerDefinitionContext defContext) { | |
| return defContext.newDefinitionBuilder() | |
| .setOutputMetrics("ncloc") | |
| .build(); | |
| } | |
| @Override | |
| public void compute(MeasureComputerContext context) { | |
| if (!context.hasMeasure("ncloc")) { | |
| // sum of children | |
| context.addMeasure("ncloc", ...); | |
| } | |
| } | |
| } | |
| public class NclocMeasureComputerTest { | |
| NclocMeasureComputer underTest = new NclocMeasureComputer(); | |
| @Test | |
| public void test_definition() { | |
| TestMeasureComputerDefinitionContext defContext = new TestMeasureComputerDefinitionContext(); | |
| MeasureComputerDefinition def = underTest.define(defContext); | |
| assertThat(def).isNotNull(); | |
| assertThat(def.getOutputMetrics()).containsOnly("ncloc"); | |
| } | |
| @Test | |
| public void sum_ncloc_of_children() { | |
| // TestMeasureComputerContext calls underTest.define() in order to | |
| // inject the definition to MeasureComputerContext | |
| // --> advantage: it verifies that the input/output metrics are | |
| // correctly defined | |
| TestMeasureComputerContext context = new TestMeasureComputerContext(underTest); | |
| context.addChildMeasure("ncloc", 3); | |
| context.addChildMeasure("ncloc", 5); | |
| underTest.compute(context); | |
| assertThat(context.getMeasureValue("ncloc")).isEqualTo(8); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment