diff --git a/.github/actions/build/action.yml b/.github/actions/build/action.yml index affd2a320..2b63165d0 100644 --- a/.github/actions/build/action.yml +++ b/.github/actions/build/action.yml @@ -44,19 +44,27 @@ runs: path: ~/.m2/repository key: ${{ runner.os }}-maven-agent-test-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-maven-agent-test- + - uses: actions/setup-java@v2 + with: + distribution: temurin + java-version: 17 - name: Build SkyWalking Agent shell: bash run: | echo "::group::Build SkyWalking Agent" # Retry one more time due to frequent "maven connection reset" - ./mvnw --batch-mode clean package -Dmaven.test.skip || \ - ./mvnw --batch-mode clean package -Dmaven.test.skip + ./mvnw -q --batch-mode clean package -Dmaven.test.skip || \ + ./mvnw -q --batch-mode clean package -Dmaven.test.skip echo "::endgroup::" - uses: actions/upload-artifact@v2 name: Upload Agent with: name: skywalking-agent path: skywalking-agent + - uses: actions/setup-java@v2 + with: + distribution: temurin + java-version: 8 - name: Build Docker Image shell: bash run: | diff --git a/.github/actions/run/action.yml b/.github/actions/run/action.yml index 57b711de0..02fdb8503 100644 --- a/.github/actions/run/action.yml +++ b/.github/actions/run/action.yml @@ -51,5 +51,11 @@ runs: shell: bash run: | echo "::group::Run Plugin Test ${{ inputs.test_case }}" - bash test/plugin/run.sh --image_version=${{ inputs.java_version }}-1.0.0 ${{ inputs.test_case }} + bash test/plugin/run.sh ${{ inputs.test_case }} echo "::endgroup::" + - uses: actions/upload-artifact@v2 + name: Upload Agent + if: always() + with: + path: test/plugin/workspace + name: test-plugin-workspace-${{ inputs.test_case }} diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b643ada24..09a380ae2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,9 +20,6 @@ on: pull_request: schedule: - cron: '0 18 * * *' # TimeZone: UTC 0 - push: - branches: - - test/ci/* concurrency: group: ci-it-${{ github.event.pull_request.number || github.ref }} @@ -51,10 +48,7 @@ jobs: fail-fast: true matrix: os: [ ubuntu, macos, windows ] - java-version: [ 8 ] - include: - - os: ubuntu - java-version: 11 + java-version: [ 17 ] steps: - uses: actions/checkout@v2 with: @@ -67,15 +61,11 @@ jobs: restore-keys: ${{ runner.os }}-maven-ci- - uses: actions/setup-java@v2 with: - distribution: adopt + distribution: temurin java-version: ${{ matrix.java-version }} - name: Check Javaagent Plugin List run: tools/plugin/check-javaagent-plugin-list.sh - name: Install and Test - if: matrix.java-version == '8' && matrix.os != 'windows' - run: ./mvnw -q --batch-mode clean verify install javadoc:javadoc - - name: Install and Test - if: matrix.java-version == '11' || matrix.os == 'windows' run: ./mvnw -q --batch-mode clean verify install ci: diff --git a/.github/workflows/codeql.yaml b/.github/workflows/codeql.yaml index e5951d5cb..0932d63ce 100644 --- a/.github/workflows/codeql.yaml +++ b/.github/workflows/codeql.yaml @@ -48,6 +48,11 @@ jobs: with: submodules: true + - uses: actions/setup-java@v2 + with: + distribution: temurin + java-version: 17 + - name: Initialize CodeQL uses: github/codeql-action/init@v1 with: diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 21629de21..6901a2cd9 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -37,14 +37,19 @@ jobs: - uses: actions/checkout@v2 with: submodules: true + - uses: actions/setup-java@v2 + with: + distribution: temurin + java-version: 17 - name: Build SkyWalking agent docker image run: make build docker.java8 - name: Build consumer & provider images shell: bash run: | echo "::group::Install SNAPSHOT apm-application-toolkit" - ./mvnw -DskipTests -N install - ./mvnw -f apm-application-toolkit -DskipTests -am install + ./mvnw -B -Dmaven.test.skip -N install + ./mvnw -B -pl :apm-test-tools -Dmaven.test.skip -am install + ./mvnw -B -f apm-application-toolkit -Dmaven.test.skip -am install echo "::endgroup::" echo "::group::" SW_VERSION=$(./mvnw -q -DforceStdout -N org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version) diff --git a/.github/workflows/plugins-jdk17-test.0.yaml b/.github/workflows/plugins-jdk17-test.0.yaml index ddd07cfa3..46ec012f6 100644 --- a/.github/workflows/plugins-jdk17-test.0.yaml +++ b/.github/workflows/plugins-jdk17-test.0.yaml @@ -27,9 +27,6 @@ on: - 'test/plugin/**' - '**/pom.xml' - '!**.md' - push: - branches: - - test/ci/* concurrency: group: plugins-jdk17-${{ github.event.pull_request.number || github.ref }} diff --git a/.github/workflows/plugins-jdk17-test.1.yaml b/.github/workflows/plugins-jdk17-test.1.yaml new file mode 100644 index 000000000..d820466d8 --- /dev/null +++ b/.github/workflows/plugins-jdk17-test.1.yaml @@ -0,0 +1,70 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Test + +on: + pull_request: + paths: + - '.github/workflows/plugins-*.yaml' + - 'apm-application-toolkit/**' + - 'apm-commons/**' + - 'apm-protocol/**' + - 'apm-sniffer/**' + - 'test/plugin/**' + - '**/pom.xml' + - '!**.md' + +concurrency: + group: plugins-jdk17-1-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + build: + name: Build + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v2 + with: + submodules: true + - name: Build + uses: ./.github/actions/build + with: + base_image_java: eclipse-temurin:17-jdk + base_image_tomcat: tomcat:10.1-jdk17-temurin + + test: + needs: [ build ] + name: ${{ matrix.case }} + runs-on: ubuntu-latest + timeout-minutes: 90 + strategy: + matrix: + case: + - spring-6.x-scenario + steps: + - uses: actions/checkout@v2 + with: + submodules: true + - uses: actions/setup-java@v2 + with: + distribution: 'temurin' + java-version: '17' + - name: Run Test + uses: ./.github/actions/run + with: + test_case: ${{ matrix.case }} diff --git a/.github/workflows/plugins-test.0.yaml b/.github/workflows/plugins-test.0.yaml index eccf2d116..57cec68cf 100644 --- a/.github/workflows/plugins-test.0.yaml +++ b/.github/workflows/plugins-test.0.yaml @@ -27,9 +27,6 @@ on: - 'test/plugin/**' - '**/pom.xml' - '!**.md' - push: - branches: - - test/ci/* concurrency: group: plugins-0-${{ github.event.pull_request.number || github.ref }} @@ -95,7 +92,7 @@ jobs: submodules: true - uses: actions/setup-java@v2 with: - distribution: adopt + distribution: temurin java-version: 8 - name: Run Plugin Test uses: ./.github/actions/run diff --git a/.github/workflows/plugins-test.1.yaml b/.github/workflows/plugins-test.1.yaml index 2b4bfa926..df63d4d80 100644 --- a/.github/workflows/plugins-test.1.yaml +++ b/.github/workflows/plugins-test.1.yaml @@ -27,9 +27,6 @@ on: - 'test/plugin/**' - '**/pom.xml' - '!**.md' - push: - branches: - - test/ci/* concurrency: group: plugins-1-${{ github.event.pull_request.number || github.ref }} @@ -87,7 +84,7 @@ jobs: submodules: true - uses: actions/setup-java@v2 with: - distribution: adopt + distribution: temurin java-version: 8 - name: Run Plugin Test uses: ./.github/actions/run diff --git a/.github/workflows/plugins-test.2.yaml b/.github/workflows/plugins-test.2.yaml index cb076b5fe..1b6ecaa9f 100644 --- a/.github/workflows/plugins-test.2.yaml +++ b/.github/workflows/plugins-test.2.yaml @@ -27,9 +27,6 @@ on: - 'test/plugin/**' - '**/pom.xml' - '!**.md' - push: - branches: - - test/ci/* concurrency: group: plugins-2-${{ github.event.pull_request.number || github.ref }} @@ -86,7 +83,7 @@ jobs: submodules: true - uses: actions/setup-java@v2 with: - distribution: adopt + distribution: temurin java-version: 8 - name: Run Plugin Test uses: ./.github/actions/run diff --git a/.github/workflows/plugins-test.3.yaml b/.github/workflows/plugins-test.3.yaml index 0a8cdab1d..932429d16 100644 --- a/.github/workflows/plugins-test.3.yaml +++ b/.github/workflows/plugins-test.3.yaml @@ -27,9 +27,6 @@ on: - 'test/plugin/**' - '**/pom.xml' - '!**.md' - push: - branches: - - test/ci/* concurrency: group: plugins-3-${{ github.event.pull_request.number || github.ref }} @@ -114,7 +111,7 @@ jobs: submodules: true - uses: actions/setup-java@v2 with: - distribution: adopt + distribution: temurin java-version: 8 - name: Install Oracle Libs if: matrix.case == 'oracle-scenario' diff --git a/.github/workflows/plugins-tomcat10-test.0.yaml b/.github/workflows/plugins-tomcat10-test.0.yaml index cc16810d2..6ba26a754 100644 --- a/.github/workflows/plugins-tomcat10-test.0.yaml +++ b/.github/workflows/plugins-tomcat10-test.0.yaml @@ -27,9 +27,6 @@ on: - 'test/plugin/**' - '**/pom.xml' - '!**.md' - push: - branches: - - test/ci/* concurrency: group: plugins-tomcat10-${{ github.event.pull_request.number || github.ref }} @@ -64,7 +61,7 @@ jobs: submodules: true - uses: actions/setup-java@v2 with: - distribution: adopt + distribution: temurin java-version: 8 - name: Run Plugin Test uses: ./.github/actions/run diff --git a/.licenserc.yaml b/.licenserc.yaml index 5c9130433..ae96eaac9 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -22,6 +22,7 @@ header: copyright-owner: Apache Software Foundation paths-ignore: + - 'org.mockito.plugins.MockMaker' - '.github/PULL_REQUEST_TEMPLATE' - '**/.gitignore' - '.gitmodules' diff --git a/CHANGES.md b/CHANGES.md index 323d4adee..506132583 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,7 +6,10 @@ Release Notes. ------------------ * Enhance lettuce plugin to adopt uniform tags. -* Expose complete Tracing APIs in the tracing toolkit +* Expose complete Tracing APIs in the tracing toolkit. +* Add plugin to trace Spring 6 and Resttemplate 6. +* Move the baseline to JDK 17 for development, the runtime baseline is still Java 8 compatible. +* Remove Powermock entirely from the test cases. #### Documentation * Update docs of Tracing APIs, reorganize the API docs into six parts diff --git a/apm-application-toolkit/apm-toolkit-micrometer-registry/pom.xml b/apm-application-toolkit/apm-toolkit-micrometer-registry/pom.xml index 457bb4ba3..36384e9e8 100644 --- a/apm-application-toolkit/apm-toolkit-micrometer-registry/pom.xml +++ b/apm-application-toolkit/apm-toolkit-micrometer-registry/pom.xml @@ -44,5 +44,11 @@ micrometer-core ${micrometer-core.version} + + org.apache.skywalking + apm-test-tools + ${project.version} + test + diff --git a/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingCounterTest.java b/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingCounterTest.java index a0dbd1ee4..a03e7bd50 100644 --- a/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingCounterTest.java +++ b/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingCounterTest.java @@ -18,17 +18,16 @@ package org.apache.skywalking.apm.meter.micrometer; -import io.micrometer.core.instrument.Counter; +import java.util.Arrays; +import org.apache.skywalking.apm.agent.test.helper.FieldGetter; import org.junit.Assert; import org.junit.Test; -import org.powermock.reflect.Whitebox; - -import java.util.Arrays; +import io.micrometer.core.instrument.Counter; public class SkywalkingCounterTest { @Test - public void testCounter() { + public void testCounter() throws IllegalAccessException, NoSuchFieldException { // Creating a simplified micrometer counter final SkywalkingMeterRegistry registry = new SkywalkingMeterRegistry(); Counter counter = registry.counter("test_counter", "skywalking", "test"); @@ -36,21 +35,19 @@ public class SkywalkingCounterTest { // Check Skywalking counter type Assert.assertTrue(counter instanceof SkywalkingCounter); final SkywalkingCounter skywalkingCounter = (SkywalkingCounter) counter; - final org.apache.skywalking.apm.toolkit.meter.Counter realCounter = - Whitebox.getInternalState(skywalkingCounter, "counter"); + final org.apache.skywalking.apm.toolkit.meter.Counter realCounter = FieldGetter.getValue(skywalkingCounter, "counter"); Assert.assertNotNull(realCounter); } @Test - public void testRateCounter() { + public void testRateCounter() throws IllegalAccessException, NoSuchFieldException { final SkywalkingMeterRegistry registry = new SkywalkingMeterRegistry(new SkywalkingConfig(Arrays.asList("test_rate_counter"))); final Counter counter = registry.counter("test_rate_counter", "skywalking", "test"); // Check Skywalking counter type Assert.assertTrue(counter instanceof SkywalkingCounter); final SkywalkingCounter skywalkingCounter = (SkywalkingCounter) counter; - final org.apache.skywalking.apm.toolkit.meter.Counter realCounter = - Whitebox.getInternalState(skywalkingCounter, "counter"); + final org.apache.skywalking.apm.toolkit.meter.Counter realCounter = FieldGetter.getValue(skywalkingCounter, "counter"); Assert.assertNotNull(realCounter); } } diff --git a/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingMeterRegistryTest.java b/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingMeterRegistryTest.java index 3d237803e..d958de404 100644 --- a/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingMeterRegistryTest.java +++ b/apm-application-toolkit/apm-toolkit-micrometer-registry/src/test/java/org/apache/skywalking/apm/meter/micrometer/SkywalkingMeterRegistryTest.java @@ -88,7 +88,6 @@ public class SkywalkingMeterRegistryTest { .register(registry); } - /** * Working on {@link io.micrometer.core.instrument.Gauge} check */ diff --git a/apm-commons/apm-datacarrier/pom.xml b/apm-commons/apm-datacarrier/pom.xml index 36ca7d8db..936055a41 100644 --- a/apm-commons/apm-datacarrier/pom.xml +++ b/apm-commons/apm-datacarrier/pom.xml @@ -33,6 +33,12 @@ jmh-generator-annprocess test + + + uk.org.webcompere + system-stubs-junit4 + test + diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/DataCarrierTest.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/DataCarrierTest.java index 5d20cce8e..d548af55e 100644 --- a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/DataCarrierTest.java +++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/DataCarrierTest.java @@ -29,43 +29,37 @@ import org.apache.skywalking.apm.commons.datacarrier.partition.ProducerThreadPar import org.apache.skywalking.apm.commons.datacarrier.partition.SimpleRollingPartitioner; import org.junit.Assert; import org.junit.Test; -import org.powermock.api.support.membermodification.MemberModifier; public class DataCarrierTest { @Test - public void testCreateDataCarrier() throws IllegalAccessException { + public void testCreateDataCarrier() throws IllegalAccessException, NoSuchFieldException { DataCarrier carrier = new DataCarrier<>(5, 100, BufferStrategy.IF_POSSIBLE); - Channels channels = (Channels) (MemberModifier.field(DataCarrier.class, "channels") - .get(carrier)); + Channels channels = FieldGetter.getValue(carrier, "channels"); Assert.assertEquals(5, channels.getChannelSize()); QueueBuffer buffer = channels.getBuffer(0); Assert.assertEquals(100, buffer.getBufferSize()); - Assert.assertEquals(MemberModifier.field(buffer.getClass(), "strategy").get(buffer), BufferStrategy.IF_POSSIBLE); - Assert.assertEquals(MemberModifier.field(buffer.getClass(), "strategy") - .get(buffer), BufferStrategy.IF_POSSIBLE); + Assert.assertEquals(FieldGetter.getValue(buffer, "strategy"), BufferStrategy.IF_POSSIBLE); + Assert.assertEquals(FieldGetter.getValue(buffer, "strategy"), BufferStrategy.IF_POSSIBLE); - Assert.assertEquals(MemberModifier.field(Channels.class, "dataPartitioner") - .get(channels) - .getClass(), SimpleRollingPartitioner.class); + Assert.assertEquals(FieldGetter.getValue(channels, "dataPartitioner") + .getClass(), SimpleRollingPartitioner.class); carrier.setPartitioner(new ProducerThreadPartitioner()); - Assert.assertEquals(MemberModifier.field(Channels.class, "dataPartitioner") - .get(channels) - .getClass(), ProducerThreadPartitioner.class); + Assert.assertEquals(FieldGetter.getValue(channels, "dataPartitioner") + .getClass(), ProducerThreadPartitioner.class); } @Test - public void testProduce() throws IllegalAccessException { + public void testProduce() throws IllegalAccessException, NoSuchFieldException { DataCarrier carrier = new DataCarrier(2, 100); Assert.assertTrue(carrier.produce(new SampleData().setName("a"))); Assert.assertTrue(carrier.produce(new SampleData().setName("b"))); Assert.assertTrue(carrier.produce(new SampleData().setName("c"))); Assert.assertTrue(carrier.produce(new SampleData().setName("d"))); - Channels channels = (Channels) (MemberModifier.field(DataCarrier.class, "channels") - .get(carrier)); + Channels channels = FieldGetter.getValue(carrier, "channels"); QueueBuffer buffer1 = channels.getBuffer(0); List result = new ArrayList(); @@ -80,7 +74,7 @@ public class DataCarrierTest { } @Test - public void testIfPossibleProduce() throws IllegalAccessException { + public void testIfPossibleProduce() throws IllegalAccessException, NoSuchFieldException { DataCarrier carrier = new DataCarrier(2, 100, BufferStrategy.IF_POSSIBLE); for (int i = 0; i < 200; i++) { @@ -91,8 +85,7 @@ public class DataCarrierTest { Assert.assertFalse(carrier.produce(new SampleData().setName("d" + i + "_2"))); } - Channels channels = (Channels) (MemberModifier.field(DataCarrier.class, "channels") - .get(carrier)); + Channels channels = FieldGetter.getValue(carrier, "channels"); QueueBuffer buffer1 = channels.getBuffer(0); List result = new ArrayList(); buffer1.obtain(result); diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java index 9b1a8525a..435fda9a9 100644 --- a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java +++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java @@ -18,29 +18,25 @@ package org.apache.skywalking.apm.commons.datacarrier; +import static org.junit.Assert.assertEquals; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; +import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(EnvUtil.class) +@RunWith(MockitoJUnitRunner.class) public class EnvUtilTest { + @Rule + public EnvironmentVariablesRule environmentVariablesRule = new EnvironmentVariablesRule(); @Before public void before() { - - PowerMockito.mockStatic(System.class); - - when(System.getenv("myInt")).thenReturn("123"); - when(System.getenv("wrongInt")).thenReturn("wrong123"); - when(System.getenv("myLong")).thenReturn("12345678901234567"); - when(System.getenv("wrongLong")).thenReturn("wrong123"); + environmentVariablesRule.set("myInt", "123"); + environmentVariablesRule.set("wrongInt", "wrong123"); + environmentVariablesRule.set("myLong", "12345678901234567"); + environmentVariablesRule.set("wrongLong", "wrong123"); } @Test @@ -55,4 +51,4 @@ public class EnvUtilTest { assertEquals(987654321987654321L, EnvUtil.getLong("wrongLong", 987654321987654321L)); } -} \ No newline at end of file +} diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/FieldGetter.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/FieldGetter.java new file mode 100644 index 000000000..3c636c596 --- /dev/null +++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/FieldGetter.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.commons.datacarrier; + +import java.lang.reflect.Field; + +public class FieldGetter { + public static T getValue(Object instance, + String fieldName) throws IllegalAccessException, NoSuchFieldException { + Field field = instance.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return (T) field.get(instance); + } +} diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumeDriverTest.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumeDriverTest.java index b96e08bb2..b78c9ade2 100644 --- a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumeDriverTest.java +++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumeDriverTest.java @@ -18,41 +18,39 @@ package org.apache.skywalking.apm.commons.datacarrier.consumer; +import org.apache.skywalking.apm.commons.datacarrier.FieldGetter; import org.apache.skywalking.apm.commons.datacarrier.SampleData; import org.apache.skywalking.apm.commons.datacarrier.buffer.BufferStrategy; import org.apache.skywalking.apm.commons.datacarrier.buffer.Channels; import org.apache.skywalking.apm.commons.datacarrier.partition.SimpleRollingPartitioner; import org.junit.Assert; import org.junit.Test; -import org.powermock.api.support.membermodification.MemberModifier; public class ConsumeDriverTest { @Test - public void testBeginConsumeDriver() throws IllegalAccessException { + public void testBeginConsumeDriver() throws IllegalAccessException, NoSuchFieldException { Channels channels = new Channels(2, 100, new SimpleRollingPartitioner(), BufferStrategy.BLOCKING); ConsumeDriver pool = new ConsumeDriver("default", channels, new SampleConsumer(), 2, 20); pool.begin(channels); - ConsumerThread[] threads = (ConsumerThread[]) MemberModifier.field(ConsumeDriver.class, "consumerThreads") - .get(pool); + ConsumerThread[] threads = FieldGetter.getValue(pool, "consumerThreads"); Assert.assertEquals(2, threads.length); Assert.assertTrue(threads[0].isAlive()); Assert.assertTrue(threads[1].isAlive()); } @Test - public void testCloseConsumeDriver() throws InterruptedException, IllegalAccessException { + public void testCloseConsumeDriver() throws InterruptedException, IllegalAccessException, NoSuchFieldException { Channels channels = new Channels(2, 100, new SimpleRollingPartitioner(), BufferStrategy.BLOCKING); ConsumeDriver pool = new ConsumeDriver("default", channels, new SampleConsumer(), 2, 20); pool.begin(channels); Thread.sleep(5000); pool.close(channels); - ConsumerThread[] threads = (ConsumerThread[]) MemberModifier.field(ConsumeDriver.class, "consumerThreads") - .get(pool); + ConsumerThread[] threads = FieldGetter.getValue(pool, "consumerThreads"); Assert.assertEquals(2, threads.length); - Assert.assertFalse((Boolean) MemberModifier.field(ConsumerThread.class, "running").get(threads[0])); - Assert.assertFalse((Boolean) MemberModifier.field(ConsumerThread.class, "running").get(threads[1])); + Assert.assertFalse(FieldGetter.getValue(threads[0], "running")); + Assert.assertFalse(FieldGetter.getValue(threads[1], "running")); } } diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumerTest.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumerTest.java index 17942d504..173af5437 100644 --- a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumerTest.java +++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumerTest.java @@ -24,10 +24,10 @@ import java.util.List; import java.util.Properties; import java.util.concurrent.LinkedBlockingQueue; import org.apache.skywalking.apm.commons.datacarrier.DataCarrier; +import org.apache.skywalking.apm.commons.datacarrier.FieldGetter; import org.apache.skywalking.apm.commons.datacarrier.SampleData; import org.junit.Assert; import org.junit.Test; -import org.powermock.api.support.membermodification.MemberModifier; public class ConsumerTest { public static LinkedBlockingQueue BUFFER = new LinkedBlockingQueue(); @@ -35,7 +35,7 @@ public class ConsumerTest { public static boolean IS_OCCUR_ERROR = false; @Test - public void testConsumerLessThanChannel() throws IllegalAccessException { + public void testConsumerLessThanChannel() throws IllegalAccessException, NoSuchFieldException { final DataCarrier carrier = new DataCarrier(2, 100); @@ -127,11 +127,10 @@ public class ConsumerTest { } } - private IConsumer getConsumer(DataCarrier carrier) throws IllegalAccessException { - ConsumeDriver pool = (ConsumeDriver) MemberModifier.field(DataCarrier.class, "driver").get(carrier); - ConsumerThread[] threads = (ConsumerThread[]) MemberModifier.field(ConsumeDriver.class, "consumerThreads") - .get(pool); + private IConsumer getConsumer(DataCarrier carrier) throws IllegalAccessException, NoSuchFieldException { + ConsumeDriver pool = FieldGetter.getValue(carrier, "driver"); + ConsumerThread[] threads = FieldGetter.getValue(pool, "consumerThreads"); - return (IConsumer) MemberModifier.field(ConsumerThread.class, "consumer").get(threads[0]); + return FieldGetter.getValue(threads[0], "consumer"); } } diff --git a/apm-commons/apm-datacarrier/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/apm-commons/apm-datacarrier/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 000000000..1f0955d45 --- /dev/null +++ b/apm-commons/apm-datacarrier/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline diff --git a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java index f09af7458..c350b803e 100644 --- a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java +++ b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java @@ -127,7 +127,6 @@ public class ConfigInitializer { return collection; } - /** * Convert string value to typical type. * diff --git a/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/ConfigInitializerTest.java b/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/ConfigInitializerTest.java index 44420e406..74d8ed595 100644 --- a/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/ConfigInitializerTest.java +++ b/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/ConfigInitializerTest.java @@ -21,10 +21,10 @@ package org.apache.skywalking.apm.util; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.internal.util.collections.Sets; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; @@ -68,8 +68,8 @@ public class ConfigInitializerTest { Assert.assertArrayEquals(new Integer[]{1, 2, 3, 4}, TestPropertiesObject.Level1Object.LIST_INT_ATTR_ED2.toArray()); Assert.assertArrayEquals(new String[]{}, TestPropertiesObject.Level1Object.SET_STR_EMPTY_ATTR.toArray()); Assert.assertArrayEquals(new Boolean[]{true, false}, TestPropertiesObject.Level1Object.LIST_BOOL_ATTR.toArray()); - Assert.assertEquals(Sets.newSet("a", "b", "c", "d"), TestPropertiesObject.Level1Object.SET_STR_ATTR); - Assert.assertEquals(Sets.newSet(1, 2, 3, 4), TestPropertiesObject.Level1Object.SET_INT_ATTR); + Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c", "d")), TestPropertiesObject.Level1Object.SET_STR_ATTR); + Assert.assertEquals(new HashSet<>(Arrays.asList(1, 2, 3, 4)), TestPropertiesObject.Level1Object.SET_INT_ATTR); Assert.assertArrayEquals(new Boolean[]{true}, TestPropertiesObject.Level1Object.SET_BOOL_ATTR.toArray()); Assert.assertEquals(TestColorEnum.RED, TestPropertiesObject.Level1Object.Level2Object.ENUM_ATTR); //make sure that when descs is empty,toString() work right; diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckContext.java index 7c9e1b407..93e8522b4 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckContext.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckContext.java @@ -36,7 +36,7 @@ public enum ExceptionCheckContext { public boolean isError(Throwable throwable) { Class clazz = throwable.getClass(); - return errorStatusExceptions.contains(clazz) || (!ignoredExceptions.contains(clazz)); + return errorStatusExceptions.contains(clazz) || !ignoredExceptions.contains(clazz); } public void registerIgnoredException(Throwable throwable) { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java index 66147ab89..949f8d581 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java @@ -101,7 +101,6 @@ public abstract class AbstractClassEnhancePluginDefine { return newClassBuilder; } - /** * Begin to define how to enhance class. After invoke this method, only means definition is finished. * diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileTaskChannelService.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileTaskChannelService.java index 20d2a35fb..cfe1465ad 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileTaskChannelService.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileTaskChannelService.java @@ -63,7 +63,6 @@ public class ProfileTaskChannelService implements BootService, Runnable, GRPCCha // gRPC stub private volatile ProfileTaskGrpc.ProfileTaskBlockingStub profileTaskBlockingStub; - // segment snapshot sender private final BlockingQueue snapshotQueue = new LinkedBlockingQueue<>( Config.Profile.SNAPSHOT_TRANSPORT_BUFFER_SIZE); diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ThreadProfiler.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ThreadProfiler.java index 856f12900..e2425eb10 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ThreadProfiler.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ThreadProfiler.java @@ -103,7 +103,7 @@ public class ThreadProfiler { } // if is first dump, check is can start profiling - if (dumpSequence == 0 && (!executionContext.isStartProfileable())) { + if (dumpSequence == 0 && !executionContext.isStartProfileable()) { return null; } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java index c59a68966..435817a65 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java @@ -28,7 +28,6 @@ import io.grpc.Metadata; import io.grpc.MethodDescriptor; import org.apache.skywalking.apm.agent.core.conf.Config; - /** * Add agent version(Described in MANIFEST.MF) to the connection establish stage. */ diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/util/IOUtils.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/util/IOUtils.java index 0856f1bab..5a907da1e 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/util/IOUtils.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/util/IOUtils.java @@ -56,7 +56,6 @@ public class IOUtils { return output.toByteArray(); } - /** * Copy bytes from an InputStream to an * OutputStream. diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/IgnoreSuffixPatternsWatcherTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/IgnoreSuffixPatternsWatcherTest.java index a2e7108f8..0ad42ee6c 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/IgnoreSuffixPatternsWatcherTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/IgnoreSuffixPatternsWatcherTest.java @@ -22,13 +22,13 @@ import org.apache.skywalking.apm.agent.core.boot.ServiceManager; import org.apache.skywalking.apm.agent.core.conf.dynamic.AgentConfigChangeWatcher; import org.apache.skywalking.apm.agent.core.conf.dynamic.watcher.IgnoreSuffixPatternsWatcher; import org.apache.skywalking.apm.agent.core.context.ContextManagerExtendService; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.apache.skywalking.apm.agent.core.test.tools.AgentServiceRule; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.powermock.reflect.Whitebox; public class IgnoreSuffixPatternsWatcherTest { @@ -48,9 +48,9 @@ public class IgnoreSuffixPatternsWatcherTest { } @Test - public void testConfigModifyEvent() { - IgnoreSuffixPatternsWatcher ignoreSuffixPatternsWatcher = Whitebox.getInternalState(contextManagerExtendService - , "ignoreSuffixPatternsWatcher"); + public void testConfigModifyEvent() throws IllegalAccessException, NoSuchFieldException { + IgnoreSuffixPatternsWatcher ignoreSuffixPatternsWatcher = + FieldGetter.getValue(contextManagerExtendService, "ignoreSuffixPatternsWatcher"); ignoreSuffixPatternsWatcher.notify(new AgentConfigChangeWatcher.ConfigChangeEvent( ".txt,.log", AgentConfigChangeWatcher.EventType.MODIFY @@ -60,9 +60,9 @@ public class IgnoreSuffixPatternsWatcherTest { } @Test - public void testConfigDeleteEvent() { - IgnoreSuffixPatternsWatcher ignoreSuffixPatternsWatcher = Whitebox.getInternalState(contextManagerExtendService - , "ignoreSuffixPatternsWatcher"); + public void testConfigDeleteEvent() throws IllegalAccessException, NoSuchFieldException { + IgnoreSuffixPatternsWatcher ignoreSuffixPatternsWatcher = + FieldGetter.getValue(contextManagerExtendService, "ignoreSuffixPatternsWatcher"); ignoreSuffixPatternsWatcher.notify(new AgentConfigChangeWatcher.ConfigChangeEvent( null, AgentConfigChangeWatcher.EventType.DELETE diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/SamplingRateWatcherTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/SamplingRateWatcherTest.java index ab3094629..e06548e1b 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/SamplingRateWatcherTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/watcher/SamplingRateWatcherTest.java @@ -21,6 +21,7 @@ package org.apache.skywalking.apm.agent.core.conf.watcher; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; import org.apache.skywalking.apm.agent.core.conf.dynamic.AgentConfigChangeWatcher; import org.apache.skywalking.apm.agent.core.conf.dynamic.watcher.SamplingRateWatcher; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.apache.skywalking.apm.agent.core.sampling.SamplingService; import org.apache.skywalking.apm.agent.core.test.tools.AgentServiceRule; import org.junit.AfterClass; @@ -28,7 +29,6 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.powermock.reflect.Whitebox; public class SamplingRateWatcherTest { @@ -48,9 +48,8 @@ public class SamplingRateWatcherTest { } @Test - public void testConfigModifyEvent() { - SamplingRateWatcher samplingRateWatcher = Whitebox.getInternalState( - samplingService, "samplingRateWatcher"); + public void testConfigModifyEvent() throws IllegalAccessException, NoSuchFieldException { + SamplingRateWatcher samplingRateWatcher = FieldGetter.getValue(samplingService, "samplingRateWatcher"); samplingRateWatcher.notify(new AgentConfigChangeWatcher.ConfigChangeEvent( "10", AgentConfigChangeWatcher.EventType.MODIFY @@ -60,9 +59,9 @@ public class SamplingRateWatcherTest { } @Test - public void testConfigDeleteEvent() { - SamplingRateWatcher samplingRateWatcher = Whitebox.getInternalState( - samplingService, "samplingRateWatcher"); + public void testConfigDeleteEvent() throws IllegalAccessException, NoSuchFieldException { + SamplingRateWatcher samplingRateWatcher = + FieldGetter.getValue(samplingService, "samplingRateWatcher"); samplingRateWatcher.notify(new AgentConfigChangeWatcher.ConfigChangeEvent( null, AgentConfigChangeWatcher.EventType.DELETE diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/ExtensionContextTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/ExtensionContextTest.java index 39b6732c0..ba3fe78ad 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/ExtensionContextTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/ExtensionContextTest.java @@ -18,19 +18,18 @@ package org.apache.skywalking.apm.agent.core.context; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import org.apache.skywalking.apm.agent.core.context.tag.Tags; import org.apache.skywalking.apm.agent.core.context.trace.NoopSpan; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class ExtensionContextTest { @Test @@ -87,9 +86,7 @@ public class ExtensionContextTest { context.handle(span); verify(span, times(0)).skipAnalysis(); - PowerMockito.mockStatic(System.class); - PowerMockito.when(System.currentTimeMillis()).thenReturn(1602743904804L + 500); - span = PowerMockito.mock(NoopSpan.class); + span = mock(NoopSpan.class); context.deserialize("0-1602743904804"); context.handle(span); verify(span, times(0)).tag(Tags.TRANSMISSION_LATENCY, "500"); diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckStrategyTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckStrategyTest.java index e3bb9cb8a..9f298365a 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckStrategyTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/ExceptionCheckStrategyTest.java @@ -21,11 +21,11 @@ package org.apache.skywalking.apm.agent.core.context.status; import java.util.Set; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; import org.apache.skywalking.apm.agent.core.conf.Config; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.powermock.api.support.membermodification.MemberModifier; public class ExceptionCheckStrategyTest { @@ -37,13 +37,11 @@ public class ExceptionCheckStrategyTest { } @After - public void after() throws IllegalAccessException { - ((Set) MemberModifier - .field(ExceptionCheckContext.class, "ignoredExceptions") - .get(ExceptionCheckContext.INSTANCE)).clear(); - ((Set) MemberModifier - .field(ExceptionCheckContext.class, "errorStatusExceptions") - .get(ExceptionCheckContext.INSTANCE)).clear(); + public void after() throws IllegalAccessException, NoSuchFieldException { + ((Set) FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "ignoredExceptions")).clear(); + ((Set) FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "errorStatusExceptions")).clear(); } @Test diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckServiceCheckTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckServiceCheckTest.java index a1bb5a40c..9892884d0 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckServiceCheckTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckServiceCheckTest.java @@ -21,11 +21,11 @@ package org.apache.skywalking.apm.agent.core.context.status; import java.util.Set; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; import org.apache.skywalking.apm.agent.core.conf.Config; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.powermock.api.support.membermodification.MemberModifier; public class StatusCheckServiceCheckTest { @@ -44,13 +44,11 @@ public class StatusCheckServiceCheckTest { } @After - public void after() throws IllegalAccessException { - ((Set) MemberModifier - .field(ExceptionCheckContext.class, "ignoredExceptions") - .get(ExceptionCheckContext.INSTANCE)).clear(); - ((Set) MemberModifier - .field(ExceptionCheckContext.class, "errorStatusExceptions") - .get(ExceptionCheckContext.INSTANCE)).clear(); + public void after() throws IllegalAccessException, NoSuchFieldException { + ((Set) FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "ignoredExceptions")).clear(); + ((Set) FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "errorStatusExceptions")).clear(); } @Test diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckerTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckerTest.java index dc936946b..f3b6f2208 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckerTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/status/StatusCheckerTest.java @@ -18,17 +18,16 @@ package org.apache.skywalking.apm.agent.core.context.status; +import static org.apache.skywalking.apm.agent.core.context.status.StatusChecker.HIERARCHY_MATCH; +import static org.apache.skywalking.apm.agent.core.context.status.StatusChecker.OFF; import java.util.Set; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; import org.apache.skywalking.apm.agent.core.conf.Config; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.powermock.api.support.membermodification.MemberModifier; - -import static org.apache.skywalking.apm.agent.core.context.status.StatusChecker.HIERARCHY_MATCH; -import static org.apache.skywalking.apm.agent.core.context.status.StatusChecker.OFF; public class StatusCheckerTest { @@ -40,13 +39,11 @@ public class StatusCheckerTest { } @After - public void after() throws IllegalAccessException { - ((Set) MemberModifier - .field(ExceptionCheckContext.class, "ignoredExceptions") - .get(ExceptionCheckContext.INSTANCE)).clear(); - ((Set) MemberModifier - .field(ExceptionCheckContext.class, "errorStatusExceptions") - .get(ExceptionCheckContext.INSTANCE)).clear(); + public void after() throws IllegalAccessException, NoSuchFieldException { + ((Set) FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "ignoredExceptions")).clear(); + ((Set) FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "errorStatusExceptions")).clear(); } @Test @@ -58,20 +55,18 @@ public class StatusCheckerTest { } @Test - public void checkInheritNamedAndAnnotationMatchStatusChecker() throws IllegalAccessException { + public void checkInheritNamedAndAnnotationMatchStatusChecker() throws IllegalAccessException, NoSuchFieldException { Assert.assertTrue(HIERARCHY_MATCH.checkStatus(new Throwable())); Assert.assertTrue(HIERARCHY_MATCH.checkStatus(new IllegalArgumentException())); Assert.assertFalse(HIERARCHY_MATCH.checkStatus(new TestNamedMatchException())); Assert.assertFalse(HIERARCHY_MATCH.checkStatus(new TestHierarchyMatchException())); Assert.assertFalse(HIERARCHY_MATCH.checkStatus(new TestAnnotationMatchException())); - Set ignoredExceptions = (Set) MemberModifier - .field(ExceptionCheckContext.class, "ignoredExceptions") - .get(ExceptionCheckContext.INSTANCE); - Set errorStatusExceptions = (Set) MemberModifier - .field(ExceptionCheckContext.class, "errorStatusExceptions") - .get(ExceptionCheckContext.INSTANCE); + Set ignoredExceptions = FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "ignoredExceptions"); + Set errorStatusExceptions = FieldGetter.getValue( + ExceptionCheckContext.INSTANCE, "errorStatusExceptions"); Assert.assertTrue(ignoredExceptions.size() > 0); Assert.assertTrue(errorStatusExceptions.size() > 0); } -} \ No newline at end of file +} diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/SystemOutWriterTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/SystemOutWriterTest.java index b2dfd312f..38ad47636 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/SystemOutWriterTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/SystemOutWriterTest.java @@ -22,11 +22,9 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; - +import static org.mockito.ArgumentMatchers.anyString; import java.io.PrintStream; -import static org.mockito.Matchers.anyString; - public class SystemOutWriterTest { private static PrintStream OUT_REF; diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/WriterFactoryTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/WriterFactoryTest.java index cc3620a6e..0ac8cbcb0 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/WriterFactoryTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/WriterFactoryTest.java @@ -18,36 +18,41 @@ package org.apache.skywalking.apm.agent.core.logging.core; +import static org.junit.Assert.assertTrue; import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath; import org.apache.skywalking.apm.agent.core.conf.Config; import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer; import org.apache.skywalking.apm.agent.core.plugin.PluginFinder; +import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.BDDMockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; -import static org.junit.Assert.assertTrue; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(value = { - SnifferConfigInitializer.class, - PluginFinder.class, - AgentPackagePath.class -}) +@RunWith(MockitoJUnitRunner.class) public class WriterFactoryTest { + private MockedStatic mockedSnifferConfigInitializer = + Mockito.mockStatic(SnifferConfigInitializer.class); + private MockedStatic mockedPluginFinder = + Mockito.mockStatic(PluginFinder.class); + private MockedStatic mockedAgentPackagePath = + Mockito.mockStatic(AgentPackagePath.class); + + @After + public void tearDown() { + mockedSnifferConfigInitializer.close(); + mockedPluginFinder.close(); + mockedAgentPackagePath.close(); + } @Test public void alwaysReturnSystemLogWriteWithSetLoggingDir() { Config.Logging.OUTPUT = LogOutput.CONSOLE; - PowerMockito.mockStatic(SnifferConfigInitializer.class); - PowerMockito.mockStatic(PluginFinder.class); - PowerMockito.mockStatic(AgentPackagePath.class); - BDDMockito.given(SnifferConfigInitializer.isInitCompleted()).willReturn(true); - BDDMockito.given(PluginFinder.isPluginInitCompleted()).willReturn(true); - BDDMockito.given(AgentPackagePath.isPathFound()).willReturn(true); + + mockedSnifferConfigInitializer.when(SnifferConfigInitializer::isInitCompleted).thenReturn(true); + mockedPluginFinder.when(PluginFinder::isPluginInitCompleted).thenReturn(true); + mockedAgentPackagePath.when(AgentPackagePath::isPathFound).thenReturn(true); assertTrue(SnifferConfigInitializer.isInitCompleted()); assertTrue(PluginFinder.isPluginInitCompleted()); @@ -60,12 +65,9 @@ public class WriterFactoryTest { @Test public void returnFileWriterWriteWithBlankLoggingDir() { Config.Logging.OUTPUT = LogOutput.FILE; - PowerMockito.mockStatic(SnifferConfigInitializer.class); - PowerMockito.mockStatic(PluginFinder.class); - PowerMockito.mockStatic(AgentPackagePath.class); - BDDMockito.given(SnifferConfigInitializer.isInitCompleted()).willReturn(true); - BDDMockito.given(PluginFinder.isPluginInitCompleted()).willReturn(true); - BDDMockito.given(AgentPackagePath.isPathFound()).willReturn(true); + mockedSnifferConfigInitializer.when(SnifferConfigInitializer::isInitCompleted).thenReturn(true); + mockedPluginFinder.when(PluginFinder::isPluginInitCompleted).thenReturn(true); + mockedAgentPackagePath.when(AgentPackagePath::isPathFound).thenReturn(true); assertTrue(SnifferConfigInitializer.isInitCompleted()); assertTrue(PluginFinder.isPluginInitCompleted()); @@ -74,4 +76,4 @@ public class WriterFactoryTest { IWriter logWriter = WriterFactory.getLogWriter(); assertTrue(logWriter instanceof FileWriter); } -} \ No newline at end of file +} diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/CounterTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/CounterTest.java index 117e44924..a61d007fe 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/CounterTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/CounterTest.java @@ -18,7 +18,11 @@ package org.apache.skywalking.apm.agent.core.meter; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.apache.skywalking.apm.agent.core.test.tools.AgentServiceRule; import org.apache.skywalking.apm.network.language.agent.v3.Label; import org.apache.skywalking.apm.network.language.agent.v3.MeterData; @@ -29,11 +33,6 @@ import org.junit.Assert; import org.junit.Rule; import org.junit.Test; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import org.powermock.reflect.Whitebox; - public class CounterTest { @Rule public AgentServiceRule agentServiceRule = new AgentServiceRule(); @@ -44,9 +43,9 @@ public class CounterTest { } @After - public void after() { + public void after() throws IllegalAccessException, NoSuchFieldException { final MeterService meterService = ServiceManager.INSTANCE.findService(MeterService.class); - ((ConcurrentHashMap) Whitebox.getInternalState(meterService, "meterMap")).clear(); + ((ConcurrentHashMap) FieldGetter.getValue(meterService, "meterMap")).clear(); } @Test diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/GaugeTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/GaugeTest.java index e0efaf5b1..f17bc174f 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/GaugeTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/GaugeTest.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.apache.skywalking.apm.agent.core.test.tools.AgentServiceRule; import org.apache.skywalking.apm.network.language.agent.v3.Label; import org.apache.skywalking.apm.network.language.agent.v3.MeterData; @@ -31,7 +32,6 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; -import org.powermock.reflect.Whitebox; public class GaugeTest { @Rule @@ -43,9 +43,9 @@ public class GaugeTest { } @After - public void after() { + public void after() throws IllegalAccessException, NoSuchFieldException { final MeterService meterService = ServiceManager.INSTANCE.findService(MeterService.class); - ((ConcurrentHashMap) Whitebox.getInternalState(meterService, "meterMap")).clear(); + ((ConcurrentHashMap) FieldGetter.getValue(meterService, "meterMap")).clear(); } @Test diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/HistogramTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/HistogramTest.java index bd6b2b858..da6c8dc7a 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/HistogramTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/HistogramTest.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.apache.skywalking.apm.agent.core.test.tools.AgentServiceRule; import org.apache.skywalking.apm.network.language.agent.v3.Label; import org.apache.skywalking.apm.network.language.agent.v3.MeterData; @@ -31,7 +32,6 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; -import org.powermock.reflect.Whitebox; public class HistogramTest { @Rule @@ -43,18 +43,18 @@ public class HistogramTest { } @After - public void after() { + public void after() throws IllegalAccessException, NoSuchFieldException { final MeterService meterService = ServiceManager.INSTANCE.findService(MeterService.class); - ((ConcurrentHashMap) Whitebox.getInternalState(meterService, "meterMap")).clear(); + ((ConcurrentHashMap) FieldGetter.getValue(meterService, "meterMap")).clear(); } @Test - public void testBuckets() { + public void testBuckets() throws IllegalAccessException, NoSuchFieldException { final MeterId meterId = new MeterId("test", MeterType.COUNTER, Arrays.asList(new MeterTag("k1", "v1"))); // Check buckets final Histogram histogram = MeterFactory.histogram("test").steps(Arrays.asList(2d, 5d)).minValue(2d).build(); - final Histogram.Bucket[] buckets = (Histogram.Bucket[]) Whitebox.getInternalState(histogram, "buckets"); + final Histogram.Bucket[] buckets = FieldGetter.getValue(histogram, "buckets"); Assert.assertEquals(2, buckets.length); Assert.assertEquals(buckets[0], new Histogram.Bucket(2)); Assert.assertEquals(buckets[1], new Histogram.Bucket(5)); diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/MeterIdTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/MeterIdTest.java index 7ccc3a76b..7db17c05d 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/MeterIdTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/meter/MeterIdTest.java @@ -21,15 +21,15 @@ package org.apache.skywalking.apm.agent.core.meter; import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.apache.skywalking.apm.agent.core.context.util.FieldGetter; import org.apache.skywalking.apm.network.language.agent.v3.Label; import org.junit.Assert; import org.junit.Test; -import org.powermock.reflect.Whitebox; public class MeterIdTest { @Test - public void testTransformTags() { + public void testTransformTags() throws IllegalAccessException, NoSuchFieldException { MeterId meterId = new MeterId("test", MeterType.COUNTER, Arrays.asList(new MeterTag("k1", "v1"))); // Label message check @@ -41,7 +41,7 @@ public class MeterIdTest { Assert.assertEquals(MeterType.COUNTER, meterId.getType()); // Must cache the Label message - final List