From 7677c79b68f6a7df19b05137776db8a195a165b2 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 23 Dec 2022 19:04:01 +0800 Subject: [PATCH] Refactor reflection agent test cases (#23064) --- .../metrics/prometheus/wrapper/type/CounterWrapperTest.java | 6 +++--- .../metrics/prometheus/wrapper/type/GaugeWrapperTest.java | 6 +++--- .../prometheus/wrapper/type/HistogramWrapperTest.java | 6 +++--- .../metrics/prometheus/wrapper/type/SummaryWrapperTest.java | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java index c902f087a2412..8437c68f1b9e4 100644 --- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java +++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type; import io.prometheus.client.Counter; -import org.apache.shardingsphere.agent.core.util.AgentReflectionUtil; import org.junit.Test; +import org.mockito.internal.configuration.plugins.Plugins; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -27,12 +27,12 @@ public final class CounterWrapperTest { @Test - public void assertCreate() { + public void assertCreate() throws ReflectiveOperationException { Counter counter = Counter.build().name("a").help("help").create(); CounterWrapper counterWrapper = new CounterWrapper(counter); counterWrapper.inc(); counterWrapper.inc(1); - counter = (Counter) AgentReflectionUtil.getFieldValue(counterWrapper, "counter"); + counter = (Counter) Plugins.getMemberAccessor().get(CounterWrapper.class.getDeclaredField("counter"), counterWrapper); assertThat(counter.get(), is(2.0)); } } diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java index ec260526602e8..31325d8f207ee 100644 --- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java +++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type; import io.prometheus.client.Gauge; -import org.apache.shardingsphere.agent.core.util.AgentReflectionUtil; import org.junit.Test; +import org.mockito.internal.configuration.plugins.Plugins; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -27,12 +27,12 @@ public final class GaugeWrapperTest { @Test - public void assertCreate() { + public void assertCreate() throws ReflectiveOperationException { Gauge gauge = Gauge.build().name("a").help("help").create(); GaugeWrapper gaugeWrapper = new GaugeWrapper(gauge); gaugeWrapper.inc(); gaugeWrapper.inc(1); - gauge = (Gauge) AgentReflectionUtil.getFieldValue(gaugeWrapper, "gauge"); + gauge = (Gauge) Plugins.getMemberAccessor().get(GaugeWrapper.class.getDeclaredField("gauge"), gaugeWrapper); assertThat(gauge.get(), is(2.0)); } } diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java index eb2d42f07d1b1..3b3564f310f0b 100644 --- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java +++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type; import io.prometheus.client.Histogram; -import org.apache.shardingsphere.agent.core.util.AgentReflectionUtil; import org.junit.Test; +import org.mockito.internal.configuration.plugins.Plugins; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -27,11 +27,11 @@ public final class HistogramWrapperTest { @Test - public void assertCreate() { + public void assertCreate() throws ReflectiveOperationException { Histogram histogram = Histogram.build().name("a").help("help").create(); HistogramWrapper histogramWrapper = new HistogramWrapper(histogram); histogramWrapper.observe(1); - histogram = (Histogram) AgentReflectionUtil.getFieldValue(histogramWrapper, "histogram"); + histogram = (Histogram) Plugins.getMemberAccessor().get(HistogramWrapper.class.getDeclaredField("histogram"), histogramWrapper); assertThat(histogram.collect().size(), is(1)); } } diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java index a8cb2b9b6fbd5..abc2221d39e54 100644 --- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java +++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type; import io.prometheus.client.Summary; -import org.apache.shardingsphere.agent.core.util.AgentReflectionUtil; import org.junit.Test; +import org.mockito.internal.configuration.plugins.Plugins; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -27,10 +27,10 @@ public final class SummaryWrapperTest { @Test - public void assertCreate() { + public void assertCreate() throws ReflectiveOperationException { SummaryWrapper summaryWrapper = new SummaryWrapper(Summary.build().name("a").help("help").create()); summaryWrapper.observe(1); - Summary summary = (Summary) AgentReflectionUtil.getFieldValue(summaryWrapper, "summary"); + Summary summary = (Summary) Plugins.getMemberAccessor().get(SummaryWrapper.class.getDeclaredField("summary"), summaryWrapper); assertThat(summary.collect().size(), is(1)); } }