You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by wa...@apache.org on 2019/06/01 17:18:14 UTC

[dubbo] branch master updated: [Test] add test for common-config (#4208)

This is an automated email from the ASF dual-hosted git repository.

wangxin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new e2965c2  [Test] add test for common-config (#4208)
e2965c2 is described below

commit e2965c2e1376d8ca597dbb60d55ea06f6ea0ed2f
Author: jimin <sl...@163.com>
AuthorDate: Sun Jun 2 01:18:08 2019 +0800

    [Test] add test for common-config (#4208)
---
 .../config/EnvironmentConfigurationTest.java       |  88 ++++++++++++++++
 .../common/config/InmemoryConfigurationTest.java   |  95 +++++++++++++++++
 .../common/config/SystemConfigurationTest.java     | 117 +++++++++++++++++++++
 3 files changed, 300 insertions(+)

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
new file mode 100644
index 0000000..00fda43
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.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;
+
+/**
+ * The type Environment configuration test.
+ */
+class EnvironmentConfigurationTest {
+
+    private static EnvironmentConfiguration environmentConfig;
+    private static final String MOCK_KEY = "mockKey";
+    private static final String MOCK_VALUE = "mockValue";
+    private static final String PATH_KEY="PATH";
+
+    /**
+     * Init.
+     */
+    @BeforeEach
+    public void init() {
+
+        environmentConfig = new EnvironmentConfiguration();
+    }
+
+    /**
+     * Test get internal property.
+     */
+    @Test
+    public void testGetInternalProperty(){
+        Assertions.assertNull(environmentConfig.getInternalProperty(MOCK_KEY));
+        Assertions.assertEquals(System.getenv(PATH_KEY),environmentConfig.getInternalProperty(PATH_KEY));
+
+    }
+
+    /**
+     * Test contains key.
+     */
+    @Test
+    public void testContainsKey(){
+        Assertions.assertTrue(environmentConfig.containsKey(PATH_KEY));
+        Assertions.assertFalse(environmentConfig.containsKey(MOCK_KEY));
+    }
+
+    /**
+     * Test get string.
+     */
+    @Test
+    public void testGetString(){
+        Assertions.assertNull(environmentConfig.getString(MOCK_KEY));
+        Assertions.assertEquals(MOCK_VALUE,environmentConfig.getString(MOCK_KEY,MOCK_VALUE));
+    }
+
+    /**
+     * Test get property.
+     */
+    @Test
+    public void testGetProperty(){
+        Assertions.assertNull(environmentConfig.getProperty(MOCK_KEY));
+        Assertions.assertEquals(MOCK_VALUE,environmentConfig.getProperty(MOCK_KEY,MOCK_VALUE));
+    }
+
+    /**
+     * Clean.
+     */
+    @AfterEach
+    public void clean(){
+
+    }
+
+}
\ No newline at end of file
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/InmemoryConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/InmemoryConfigurationTest.java
new file mode 100644
index 0000000..b577631
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/InmemoryConfigurationTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.dubbo.common.config;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The type Inmemory configuration test.
+ */
+class InmemoryConfigurationTest {
+
+    private static InmemoryConfiguration memConfig;
+    private static final String MOCK_KEY = "mockKey";
+    private static final String MOCK_VALUE = "mockValue";
+    private static final String MOCK_ONE_KEY = "one";
+    private static final String MOCK_TWO_KEY = "two";
+    private static final String MOCK_THREE_KEY = "three";
+
+    /**
+     * Init.
+     */
+    @BeforeEach
+    public void init() {
+
+        memConfig = new InmemoryConfiguration();
+    }
+
+    /**
+     * Test get mem property.
+     */
+    @Test
+    public void testGetMemProperty() {
+        Assertions.assertNull(memConfig.getInternalProperty(MOCK_KEY));
+        Assertions.assertFalse(memConfig.containsKey(MOCK_KEY));
+        Assertions.assertNull(memConfig.getString(MOCK_KEY));
+        Assertions.assertNull(memConfig.getProperty(MOCK_KEY));
+        memConfig.addProperty(MOCK_KEY, MOCK_VALUE);
+        Assertions.assertTrue(memConfig.containsKey(MOCK_KEY));
+        Assertions.assertEquals(MOCK_VALUE, memConfig.getInternalProperty(MOCK_KEY));
+        Assertions.assertEquals(MOCK_VALUE, memConfig.getString(MOCK_KEY, MOCK_VALUE));
+        Assertions.assertEquals(MOCK_VALUE, memConfig.getProperty(MOCK_KEY, MOCK_VALUE));
+
+    }
+
+    /**
+     * Test get properties.
+     */
+    @Test
+    public void testGetProperties() {
+        Assertions.assertNull(memConfig.getInternalProperty(MOCK_ONE_KEY));
+        Assertions.assertNull(memConfig.getInternalProperty(MOCK_TWO_KEY));
+        Map<String, String> proMap = new HashMap<>();
+        proMap.put(MOCK_ONE_KEY, MOCK_VALUE);
+        proMap.put(MOCK_TWO_KEY, MOCK_VALUE);
+        memConfig.addProperties(proMap);
+        Assertions.assertNotNull(memConfig.getInternalProperty(MOCK_ONE_KEY));
+        Assertions.assertNotNull(memConfig.getInternalProperty(MOCK_TWO_KEY));
+        Map<String, String> anotherProMap = new HashMap<>();
+        anotherProMap.put(MOCK_THREE_KEY, MOCK_VALUE);
+        memConfig.setProperties(anotherProMap);
+        Assertions.assertNotNull(memConfig.getInternalProperty(MOCK_THREE_KEY));
+        Assertions.assertNull(memConfig.getInternalProperty(MOCK_ONE_KEY));
+        Assertions.assertNull(memConfig.getInternalProperty(MOCK_TWO_KEY));
+
+    }
+
+    /**
+     * Clean.
+     */
+    @AfterEach
+    public void clean(){
+
+    }
+
+}
\ No newline at end of file
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/SystemConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/SystemConfigurationTest.java
new file mode 100644
index 0000000..75576be
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/SystemConfigurationTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.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;
+
+/**
+ * The type System configuration test.
+ */
+class SystemConfigurationTest {
+
+    private static SystemConfiguration sysConfig;
+    private static final String MOCK_KEY = "mockKey";
+    private static final String MOCK_STRING_VALUE = "mockValue";
+    private static final Boolean MOCK_BOOL_VALUE = Boolean.FALSE;
+    private static final Integer MOCK_INT_VALUE = Integer.MAX_VALUE;
+    private static final Long MOCK_LONG_VALUE = Long.MIN_VALUE;
+    private static final Short MOCK_SHORT_VALUE = Short.MIN_VALUE;
+    private static final Float MOCK_FLOAT_VALUE = Float.MIN_VALUE;
+    private static final Double MOCK_DOUBLE_VALUE = Double.MIN_VALUE;
+    private static final Byte MOCK_BYTE_VALUE = Byte.MIN_VALUE;
+    private static final String NOT_EXIST_KEY = "NOTEXIST";
+
+    /**
+     * Init.
+     */
+    @BeforeEach
+    public void init() {
+
+        sysConfig = new SystemConfiguration();
+    }
+
+    /**
+     * Test get sys property.
+     */
+    @Test
+    public void testGetSysProperty() {
+        Assertions.assertNull(sysConfig.getInternalProperty(MOCK_KEY));
+        Assertions.assertFalse(sysConfig.containsKey(MOCK_KEY));
+        Assertions.assertNull(sysConfig.getString(MOCK_KEY));
+        Assertions.assertNull(sysConfig.getProperty(MOCK_KEY));
+        System.setProperty(MOCK_KEY, MOCK_STRING_VALUE);
+        Assertions.assertTrue(sysConfig.containsKey(MOCK_KEY));
+        Assertions.assertEquals(MOCK_STRING_VALUE, sysConfig.getInternalProperty(MOCK_KEY));
+        Assertions.assertEquals(MOCK_STRING_VALUE, sysConfig.getString(MOCK_KEY, MOCK_STRING_VALUE));
+        Assertions.assertEquals(MOCK_STRING_VALUE, sysConfig.getProperty(MOCK_KEY, MOCK_STRING_VALUE));
+    }
+
+    /**
+     * Test convert.
+     */
+    @Test
+    public void testConvert() {
+        Assertions.assertEquals(
+            MOCK_STRING_VALUE, sysConfig.convert(String.class, NOT_EXIST_KEY, MOCK_STRING_VALUE));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_BOOL_VALUE));
+        Assertions.assertEquals(MOCK_BOOL_VALUE, sysConfig.convert(Boolean.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_STRING_VALUE));
+        Assertions.assertEquals(MOCK_STRING_VALUE, sysConfig.convert(String.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_INT_VALUE));
+        Assertions.assertEquals(MOCK_INT_VALUE, sysConfig.convert(Integer.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_LONG_VALUE));
+        Assertions.assertEquals(MOCK_LONG_VALUE, sysConfig.convert(Long.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_SHORT_VALUE));
+        Assertions.assertEquals(MOCK_SHORT_VALUE, sysConfig.convert(Short.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_FLOAT_VALUE));
+        Assertions.assertEquals(MOCK_FLOAT_VALUE, sysConfig.convert(Float.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_DOUBLE_VALUE));
+        Assertions.assertEquals(MOCK_DOUBLE_VALUE, sysConfig.convert(Double.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(MOCK_BYTE_VALUE));
+        Assertions.assertEquals(MOCK_BYTE_VALUE, sysConfig.convert(Byte.class, MOCK_KEY, null));
+        System.setProperty(MOCK_KEY, String.valueOf(ConfigMock.MockOne));
+        Assertions.assertEquals(ConfigMock.MockOne, sysConfig.convert(ConfigMock.class, MOCK_KEY, null));
+    }
+
+    /**
+     * Clean.
+     */
+    @AfterEach
+    public void clean() {
+        if (null != System.getProperty(MOCK_KEY)) {
+            System.clearProperty(MOCK_KEY);
+        }
+    }
+
+    /**
+     * The enum Config mock.
+     */
+    enum ConfigMock {
+        /**
+         * Mock one config mock.
+         */
+        MockOne,
+        /**
+         * Mock two config mock.
+         */
+        MockTwo
+    }
+
+}
\ No newline at end of file