From dc202cb4b67e811dc7c21b98416dcd41a867bacc Mon Sep 17 00:00:00 2001 From: Bartosz Spyrko-Smietanko Date: Fri, 14 May 2021 14:28:37 +0100 Subject: [PATCH] Verify that HELP lines are not duplicated --- .../metrics/test/MetricAppBean.java | 10 +++++ .../metrics/test/MpMetricTest.java | 40 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MetricAppBean.java b/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MetricAppBean.java index da7bd670..74319854 100644 --- a/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MetricAppBean.java +++ b/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MetricAppBean.java @@ -190,6 +190,16 @@ public long countMeB() { return 666666; } + @Counted(name = "counter.with.desc", tags={"number=one"}, description = "description") + public void counterWithDesc1() { + + } + + @Counted(name = "counter.with.desc", tags={"number=two"}, description = "description") + public void counterWithDesc2() { + + } + public void gaugeMe() { @SuppressWarnings("unchecked") diff --git a/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MpMetricTest.java b/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MpMetricTest.java index cc865107..665fab12 100644 --- a/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MpMetricTest.java +++ b/tck/rest/src/main/java/org/eclipse/microprofile/metrics/test/MpMetricTest.java @@ -47,6 +47,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; +import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.inject.Inject; @@ -55,7 +56,9 @@ import javax.xml.parsers.ParserConfigurationException; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.hamcrest.Description; import org.hamcrest.Matcher; +import org.hamcrest.BaseMatcher; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.junit.Arquillian; @@ -1274,7 +1277,42 @@ public void testApplicationSimpleTimerUnitOpenMetrics() { ; } - + @Test + @RunAsClient + public void testApplicationContainsHelpMessageOnce() { + given().header("Accept", TEXT_PLAIN).when().get("/metrics/application") + .then().statusCode(200) + .and() + .body(containsLineOnce("HELP application_org_eclipse_microprofile_metrics_test_MetricAppBean_counter_with_desc_total description")); + } + + /** + * Checks that given line appears in the body only once. + * + * @param expected + * @return + */ + private Matcher containsLineOnce(String expected) { + return new BaseMatcher() { + @Override + public void describeTo(Description description) { + description.appendText("Body should contain line [" + expected + "] only once."); + } + + @Override + public boolean matches(Object o) { + String body = (String) o; + Pattern pattern = Pattern.compile(expected); + java.util.regex.Matcher matcher = pattern.matcher(body); + int count = 0; + while (matcher.find()) { + count++; + } + return count == 1; + } + }; + } + /** * Checks that the value is within tolerance of the expected value *