diff --git a/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/model/ArexMocker.java b/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/model/ArexMocker.java index e100109f5..ff72edd69 100644 --- a/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/model/ArexMocker.java +++ b/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/model/ArexMocker.java @@ -39,7 +39,6 @@ public ArexMocker(MockCategoryType categoryType) { this.categoryType = categoryType; this.appId = System.getProperty(ConfigConstants.SERVICE_NAME); this.recordVersion = System.getProperty(ConfigConstants.AGENT_VERSION); - this.tags = (Map) System.getProperties().getOrDefault(ConfigConstants.MOCKER_TAGS, Collections.emptyMap()); } /** diff --git a/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/StringUtil.java b/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/StringUtil.java index a61bb4544..f7f8b4d66 100644 --- a/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/StringUtil.java +++ b/arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/StringUtil.java @@ -106,6 +106,20 @@ private static void indent(StringBuilder sb, int indent) { } } + public static String mapToString(Map map) { + if (MapUtils.isEmpty(map)) { + return StringUtil.EMPTY; + } + StringBuilder stringBuilder = new StringBuilder(); + for (Map.Entry entry : map.entrySet()) { + stringBuilder.append(entry.getKey()) + .append("=") + .append(entry.getValue()) + .append(";"); + } + return stringBuilder.toString(); + } + public static Map asMap(String content) { if (isEmpty(content)) { return Collections.emptyMap(); diff --git a/arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/StringUtilTest.java b/arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/StringUtilTest.java index c32344826..71c095da5 100644 --- a/arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/StringUtilTest.java +++ b/arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/StringUtilTest.java @@ -425,4 +425,12 @@ void splitByLastSeparator() { actualResult = StringUtil.splitByLastSeparator(val, ','); assertArrayEquals(new String[] {"x,,y", "z"}, actualResult); } + + @Test + void mapToString() { + Map map = new HashMap<>(); + map.put("a", "b"); + map.put("c", "d"); + assertEquals("a=b;c=d;", StringUtil.mapToString(map)); + } } diff --git a/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/config/Config.java b/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/config/Config.java index c9d564670..7fea9f3c8 100644 --- a/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/config/Config.java +++ b/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/config/Config.java @@ -47,6 +47,7 @@ public static Config get() { private final String recordVersion; private final Set includeServiceOperations; private final Set coveragePackages = new ConcurrentHashSet<>(); + private final Map mockerTags; Config(boolean enableDebug, String serviceName, List dynamicClassList, Map properties, @@ -60,6 +61,7 @@ public static Config get() { this.recordRate = recordRate; this.recordVersion = properties.get("arex.agent.version"); this.includeServiceOperations = StringUtil.splitToSet(properties.get("includeServiceOperations"), SEPARATOR); + this.mockerTags = StringUtil.asMap(System.getProperty(ConfigConstants.MOCKER_TAGS)); buildCoveragePackages(properties); buildDynamicClassInfo(); } @@ -107,6 +109,10 @@ private void buildDynamicClassInfo() { this.dynamicAbstractClassList = list.toArray(StringUtil.EMPTY_STRING_ARRAY); } + public Map getMockerTags() { + return mockerTags; + } + public Set getCoveragePackages() { return coveragePackages; } diff --git a/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/util/MockUtils.java b/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/util/MockUtils.java index dd2d36155..90da2fe64 100644 --- a/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/util/MockUtils.java +++ b/arex-instrumentation-api/src/main/java/io/arex/inst/runtime/util/MockUtils.java @@ -78,6 +78,7 @@ public static ArexMocker createNettyProvider(String pattern) { public static ArexMocker create(MockCategoryType categoryType, String operationName) { ArexMocker mocker = new ArexMocker(categoryType); + mocker.setTags(Config.get().getMockerTags()); long createTime = System.currentTimeMillis(); ArexContext context = ContextManager.currentContext(); if (context != null) { diff --git a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/config/ConfigTest.java b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/config/ConfigTest.java index 5d3a060d5..cec1f8870 100644 --- a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/config/ConfigTest.java +++ b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/config/ConfigTest.java @@ -2,11 +2,14 @@ import io.arex.agent.bootstrap.constants.ConfigConstants; import io.arex.agent.bootstrap.util.ConcurrentHashSet; +import io.arex.agent.bootstrap.util.MapUtils; +import io.arex.agent.bootstrap.util.StringUtil; import io.arex.inst.runtime.context.RecordLimiter; import io.arex.inst.runtime.listener.EventProcessor; import io.arex.inst.runtime.model.ArexConstants; import io.arex.inst.runtime.model.DynamicClassEntity; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -17,9 +20,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.mockito.MockedStatic; import org.mockito.Mockito; +import java.util.Map; import java.util.function.Predicate; import java.util.stream.Stream; @@ -147,4 +150,18 @@ void buildCoveragePackages() { System.clearProperty(ArexConstants.SPRING_SCAN_PACKAGES); } + + @Test + void arexMockerTags() { + ConfigBuilder configBuilder = ConfigBuilder.create("mock"); + configBuilder.build(); + assertTrue(MapUtils.isEmpty(Config.get().getMockerTags())); + Map tags = new HashMap<>(); + tags.put("key1", "value1"); + tags.put("key2", "value2"); + System.setProperty(ConfigConstants.MOCKER_TAGS, StringUtil.mapToString(tags)); + configBuilder.build(); + assertEquals("value1", Config.get().getMockerTags().get("key1")); + assertEquals("value2", Config.get().getMockerTags().get("key2")); + } } diff --git a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/match/ReplayMatcherTest.java b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/match/ReplayMatcherTest.java index c7732a3ca..4b9ab32f0 100644 --- a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/match/ReplayMatcherTest.java +++ b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/match/ReplayMatcherTest.java @@ -57,4 +57,4 @@ void match() { Mockito.when(MockUtils.methodRequestTypeHash(requestMocker)).thenReturn(2); assertNull(ReplayMatcher.match(requestMocker, MockStrategyEnum.FIND_LAST)); } -} \ No newline at end of file +} diff --git a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/MergeRecordUtilTest.java b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/MergeRecordUtilTest.java index 2ed57f6bc..4e136aaa4 100644 --- a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/MergeRecordUtilTest.java +++ b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/MergeRecordUtilTest.java @@ -141,4 +141,4 @@ static Stream recordRemainCase() { arguments(contextSupplier2.get(), asserts2) ); } -} \ No newline at end of file +} diff --git a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/ReplayUtilTest.java b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/ReplayUtilTest.java index 9ce517c61..0f9e4411c 100644 --- a/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/ReplayUtilTest.java +++ b/arex-instrumentation-api/src/test/java/io/arex/inst/runtime/util/ReplayUtilTest.java @@ -99,4 +99,4 @@ static Stream replayAllMockerCase() { arguments(mocker2) ); } -} \ No newline at end of file +} diff --git a/arex-instrumentation-foundation/src/main/java/io/arex/foundation/services/ConfigService.java b/arex-instrumentation-foundation/src/main/java/io/arex/foundation/services/ConfigService.java index 554c7fbd7..885ab091a 100644 --- a/arex-instrumentation-foundation/src/main/java/io/arex/foundation/services/ConfigService.java +++ b/arex-instrumentation-foundation/src/main/java/io/arex/foundation/services/ConfigService.java @@ -11,7 +11,6 @@ import io.arex.foundation.util.NetUtils; import io.arex.agent.bootstrap.util.StringUtil; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -136,7 +135,7 @@ public Map getSystemProperties() { map.put(key, value); buildTags(mockerTags, key, value); } - properties.put(ConfigConstants.MOCKER_TAGS, Collections.unmodifiableMap(mockerTags)); + System.setProperty(ConfigConstants.MOCKER_TAGS, StringUtil.mapToString(mockerTags)); return map; } diff --git a/arex-instrumentation/httpclient/arex-httpclient-feign/src/test/java/io/arex/inst/httpclient/feign/FeignClientInstrumentationTest.java b/arex-instrumentation/httpclient/arex-httpclient-feign/src/test/java/io/arex/inst/httpclient/feign/FeignClientInstrumentationTest.java index 275adba80..75726d5c4 100644 --- a/arex-instrumentation/httpclient/arex-httpclient-feign/src/test/java/io/arex/inst/httpclient/feign/FeignClientInstrumentationTest.java +++ b/arex-instrumentation/httpclient/arex-httpclient-feign/src/test/java/io/arex/inst/httpclient/feign/FeignClientInstrumentationTest.java @@ -6,6 +6,7 @@ import feign.Response; import io.arex.agent.bootstrap.model.MockResult; import io.arex.inst.httpclient.common.HttpClientExtractor; +import io.arex.inst.runtime.config.ConfigBuilder; import io.arex.inst.runtime.context.ContextManager; import io.arex.inst.runtime.context.RepeatedCollectManager; import io.arex.inst.runtime.util.IgnoreUtils; @@ -24,6 +25,7 @@ static void setUp() { Mockito.mockStatic(IgnoreUtils.class); Mockito.mockStatic(ContextManager.class); Mockito.mockStatic(RepeatedCollectManager.class); + ConfigBuilder.create("test").build(); } @AfterAll