Skip to content

Commit

Permalink
Add a test for testing a private method annotated with @Gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
Channyboy committed Aug 31, 2022
1 parent 96af76b commit b2515ba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
@@ -1,4 +1,5 @@
/**
* Copyright (c) 2013, 2022 Contributors to the Eclipse Foundation
* Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -25,12 +26,23 @@ public class GaugeMethodBean {

private long gauge;

private long privateGauge;

@Gauge(name = "gaugeMethod", unit = MetricUnits.NONE)
public long getGauge() {
return gauge;
}

@Gauge(name = "privateGaugeMethod", unit = MetricUnits.NONE)
private long getPrivateGauge() {
return privateGauge;
}

public void setGauge(long gauge) {
this.gauge = gauge;
}

public void setPrivateGauge(long gauge) {
this.privateGauge = gauge;
}
}
@@ -1,4 +1,5 @@
/**
* Copyright (c) 2013, 2022 Contributors to the Eclipse Foundation
* Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -41,8 +42,12 @@ public class GaugeMethodBeanTest {

private final static String GAUGE_NAME = MetricRegistry.name(GaugeMethodBean.class, "gaugeMethod");

private final static String PRIVATE_GAUGE_NAME = MetricRegistry.name(GaugeMethodBean.class, "privateGaugeMethod");

private static MetricID gaugeMID;

private static MetricID privateGaugeMID;

@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create(WebArchive.class)
Expand All @@ -63,13 +68,16 @@ public void instantiateApplicationScopedBeanAndTest() {
// Let's trigger the instantiation of the application scoped bean explicitly
// as only a proxy gets injected otherwise
bean.getGauge();

/*
* The MetricID relies on the MicroProfile Config API. Running a managed arquillian container will result with
* the MetricID being created in a client process that does not contain the MPConfig impl.
*
* This will cause client instantiated MetricIDs to throw an exception. (i.e the global MetricIDs)
*/
gaugeMID = new MetricID(GAUGE_NAME);

privateGaugeMID = new MetricID(PRIVATE_GAUGE_NAME);
}

@Test
Expand All @@ -95,4 +103,28 @@ public void callGaugeAfterSetterCall() {
bean.setGauge(value);
assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(value)));
}

@Test
@InSequence(3)
public void privateGaugeCalledWithDefaultValue() {
@SuppressWarnings("unchecked")
Gauge<Long> gauge = (Gauge<Long>) registry.getGauge(privateGaugeMID);
assertThat("Gauge is not registered correctly", gauge, notNullValue());

// Make sure that the gauge has the expected value
assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(0L)));
}

@Test
@InSequence(4)
public void callPrivateGaugeAfterSetterCall() {
@SuppressWarnings("unchecked")
Gauge<Long> gauge = (Gauge<Long>) registry.getGauge(privateGaugeMID);
assertThat("Gauge is not registered correctly", gauge, notNullValue());

// Call the setter method and assert the gauge is up-to-date
long value = Math.round(Math.random() * Long.MAX_VALUE);
bean.setPrivateGauge(value);
assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(value)));
}
}

0 comments on commit b2515ba

Please sign in to comment.