From 4e37fa0c016f69f265ee6edd543a059fb929d79a Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Thu, 24 Nov 2022 07:26:09 +0800 Subject: [PATCH] Fix jacoco arg line not work in jdk17 (#10994) --- .github/workflows/build-and-test-pr.yml | 4 +- .../config/EnvironmentConfiguration.java | 14 ++- .../config/EnvironmentConfigurationTest.java | 86 +++++-------------- pom.xml | 27 +++++- 4 files changed, 62 insertions(+), 69 deletions(-) diff --git a/.github/workflows/build-and-test-pr.yml b/.github/workflows/build-and-test-pr.yml index 8480eb1d1a..e155a6aa4e 100644 --- a/.github/workflows/build-and-test-pr.yml +++ b/.github/workflows/build-and-test-pr.yml @@ -199,12 +199,12 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | source ${{ github.workspace }}/.tmp/decrypted-sonarcloud-token - ./mvnw --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Pjacoco,jdk15ge-simple,'!jdk15ge' -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=apache -Dsonar.projectKey=apache_dubbo -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -DskipIntegrationTests=false -Dcheckstyle.skip=false -Dcheckstyle_unix.skip=false -Drat.skip=false -Dmaven.javadoc.skip=true -DembeddedZookeeperPath=${{ github.workspace }}/.tmp/zookeeper -Dsonar.coverage.jacoco.xmlReportPaths=dubbo-test/dubbo-dependencies-all/target/site/jacoco-aggregate/jacoco.xml -Dsonar.login=${SONAR_TOKEN} + ./mvnw --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Pjacoco,jdk15ge-simple,'!jdk15ge',jacoco089 -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=apache -Dsonar.projectKey=apache_dubbo -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -DskipIntegrationTests=false -Dcheckstyle.skip=false -Dcheckstyle_unix.skip=false -Drat.skip=false -Dmaven.javadoc.skip=true -DembeddedZookeeperPath=${{ github.workspace }}/.tmp/zookeeper -Dsonar.coverage.jacoco.xmlReportPaths=dubbo-test/dubbo-dependencies-all/target/site/jacoco-aggregate/jacoco.xml -Dsonar.login=${SONAR_TOKEN} - name: "Test with Maven without SonarCloud Scan" if: ${{ github.repository != 'apache/dubbo' }} timeout-minutes: 70 run: | - ./mvnw --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Pjacoco,jdk15ge-simple,'!jdk15ge' -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -DskipIntegrationTests=false -Dcheckstyle.skip=false -Dcheckstyle_unix.skip=false -Drat.skip=false -Dmaven.javadoc.skip=true -DembeddedZookeeperPath=${{ github.workspace }}/.tmp/zookeeper + ./mvnw --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Pjacoco,jdk15ge-simple,'!jdk15ge',jacoco089 -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -DskipIntegrationTests=false -Dcheckstyle.skip=false -Dcheckstyle_unix.skip=false -Drat.skip=false -Dmaven.javadoc.skip=true -DembeddedZookeeperPath=${{ github.workspace }}/.tmp/zookeeper - name: "Upload coverage to Codecov" uses: codecov/codecov-action@v3 diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/EnvironmentConfiguration.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/EnvironmentConfiguration.java index 227b180897..8ab70dd0fa 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/EnvironmentConfiguration.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/EnvironmentConfiguration.java @@ -27,14 +27,24 @@ public class EnvironmentConfiguration implements Configuration { @Override public Object getInternalProperty(String key) { - String value = System.getenv(key); + String value = getenv(key); if (StringUtils.isEmpty(value)) { - value = System.getenv(StringUtils.toOSStyleKey(key)); + value = getenv(StringUtils.toOSStyleKey(key)); } return value; } public Map getProperties() { + return getenv(); + } + + // Adapt to System api, design for unit test + + protected String getenv(String key) { + return System.getenv(key); + } + + protected Map getenv() { return System.getenv(); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java index b2433a1590..f6806b7ab4 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java @@ -16,13 +16,9 @@ */ package org.apache.dubbo.common.config; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.lang.reflect.Field; -import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -34,71 +30,33 @@ class EnvironmentConfigurationTest { private static final String MOCK_KEY = "DUBBO_KEY"; private static final String MOCK_VALUE = "mockValue"; - /** - * Init. - */ - @BeforeEach - public void init() { - - } - @Test void testGetInternalProperty() { Map map = new HashMap<>(); map.put(MOCK_KEY, MOCK_VALUE); - try { - setEnv(map); - EnvironmentConfiguration configuration = new EnvironmentConfiguration(); - // this UT maybe only works on particular platform, assert only when value is not null. - Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty("dubbo.key")); - Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty("key")); - Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty("dubbo_key")); - Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty(MOCK_KEY)); - } catch (Exception e) { - // skip test. - e.printStackTrace(); - } - } - - protected static void setEnv(Map newenv) throws Exception { - try { - Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); - Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); - theEnvironmentField.setAccessible(true); - Map env = (Map) theEnvironmentField.get(null); - env.putAll(newenv); - Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment"); - theCaseInsensitiveEnvironmentField.setAccessible(true); - Map cienv = (Map) theCaseInsensitiveEnvironmentField.get(null); - cienv.putAll(newenv); - } catch (NoSuchFieldException e) { - Class[] classes = Collections.class.getDeclaredClasses(); - Map env = System.getenv(); - for (Class cl : classes) { - if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Object obj = field.get(env); - Map map = (Map) obj; - map.clear(); - map.putAll(newenv); - } + EnvironmentConfiguration configuration = new EnvironmentConfiguration() { + @Override + protected String getenv(String key) { + return map.get(key); } - } + }; + // this UT maybe only works on particular platform, assert only when value is not null. + Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty("dubbo.key")); + Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty("key")); + Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty("dubbo_key")); + Assertions.assertEquals(MOCK_VALUE, configuration.getInternalProperty(MOCK_KEY)); } - private static void updateEnv(String name, String val) throws ReflectiveOperationException { - Map env = System.getenv(); - Field field = env.getClass().getDeclaredField("m"); - field.setAccessible(true); - ((Map) field.get(env)).put(name, val); + @Test + void testGetProperties() { + Map map = new HashMap<>(); + map.put(MOCK_KEY, MOCK_VALUE); + EnvironmentConfiguration configuration = new EnvironmentConfiguration() { + @Override + protected Map getenv() { + return map; + } + }; + Assertions.assertEquals(map, configuration.getProperties()); } - /** - * Clean. - */ - @AfterEach - public void clean(){ - - } - -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index 439d5f1de2..2b8b2b7e89 100644 --- a/pom.xml +++ b/pom.xml @@ -538,7 +538,7 @@ true once - ${argline} + ${argline} ${jacocoArgLine} @@ -567,6 +567,31 @@ + + + jacoco089 + + 0.8.9-SNAPSHOT + + + + apache.snapshots + Apache Snapshot Repository + https://repository.apache.org/snapshots + + false + + + + oss.snapshots + Oss Repository + https://oss.sonatype.org/content/repositories/snapshots + + false + + + +