From bda50650a77ab6966dccbd927632b3ad6206d473 Mon Sep 17 00:00:00 2001 From: onewe Date: Wed, 21 Sep 2022 14:43:36 +0800 Subject: [PATCH] [ISSUE #8622] NProperties is renamed SearchableProperties and add some comments --- ...erties.java => NacosClientProperties.java} | 38 ++++--- .../client/env/SearchableProperties.java | 17 +-- ...st.java => NacosClientPropertiesTest.java} | 104 +++++++++--------- 3 files changed, 85 insertions(+), 74 deletions(-) rename client/src/main/java/com/alibaba/nacos/client/env/{NProperties.java => NacosClientProperties.java} (72%) rename client/src/test/java/com/alibaba/nacos/client/env/{NPropertiesTest.java => NacosClientPropertiesTest.java} (60%) diff --git a/client/src/main/java/com/alibaba/nacos/client/env/NProperties.java b/client/src/main/java/com/alibaba/nacos/client/env/NacosClientProperties.java similarity index 72% rename from client/src/main/java/com/alibaba/nacos/client/env/NProperties.java rename to client/src/main/java/com/alibaba/nacos/client/env/NacosClientProperties.java index c8b7e800b9a..a3643584c07 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/NProperties.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/NacosClientProperties.java @@ -19,14 +19,24 @@ import java.util.Properties; /** - * NProperties interface. - * + * NacosClientProperties interface. + * include all the properties from jvm args, system environment, default setting. + * more details you can see https://github.com/alibaba/nacos/issues/8622 * @author onewe */ -@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule") -public interface NProperties { +public interface NacosClientProperties { - NProperties PROTOTYPE = SearchableProperties.INSTANCE; + /** + * all the NacosClientProperties object must be created from PROTOTYPE, + * so child NacosClientProperties can be shared properties in the PROTOTYPE. + * it looks like this: + * |-PROTOTYPE----------------> ip=127.0.0.1 + * |---|-child1---------------> port=6379 + * if you search key called "port" from child1, certainly you will get 6379 + * if you search key called "ip" from child1, you will get 127.0.0.1. + * because the "PROTOTYPE" is shared. + */ + NacosClientProperties PROTOTYPE = SearchableProperties.INSTANCE; /** * get property, if the value can not be got by the special key, the null will be returned. @@ -106,28 +116,28 @@ public interface NProperties { void addProperties(Properties properties); /** - * Tests if the specified object is a key in this NProperties. + * Tests if the specified object is a key in this NacosClientProperties. * @param key key – possible key - * @return true if and only if the specified object is a key in this NProperties, false otherwise. + * @return true if and only if the specified object is a key in this NacosClientProperties, false otherwise. */ boolean containsKey(String key); /** - * get properties from NProperties. + * get properties from NacosClientProperties. * @return properties */ Properties asProperties(); /** - * create a new NProperties which scope is itself. - * @return NProperties + * create a new NacosClientProperties which scope is itself. + * @return NacosClientProperties */ - NProperties derive(); + NacosClientProperties derive(); /** - * create a NProperties and init. + * create a new NacosClientProperties from NacosClientProperties#PROTOTYPE and init. * @param properties properties - * @return NProperties + * @return NacosClientProperties */ - NProperties derive(Properties properties); + NacosClientProperties derive(Properties properties); } diff --git a/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java b/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java index c4da24c48a7..ac7d5b92f07 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java @@ -33,11 +33,12 @@ import java.util.stream.Collectors; /** - * Searchable NProperties. - * + * Searchable NacosClientProperties. + * the SearchableProperties that it can be specified search order by + * nacos.env.first * @author onewe */ -class SearchableProperties implements NProperties { +class SearchableProperties implements NacosClientProperties { private static final Logger LOGGER = LoggerFactory.getLogger(SearchableProperties.class); @@ -222,14 +223,14 @@ private List sortPropertySource(SourceType firstType, } @Override - public NProperties derive() { + public NacosClientProperties derive() { return new SearchableProperties(new PropertiesPropertySource(this.propertiesPropertySource)); } @Override - public NProperties derive(Properties properties) { - final NProperties nProperties = this.derive(); - nProperties.addProperties(properties); - return nProperties; + public NacosClientProperties derive(Properties properties) { + final NacosClientProperties nacosClientProperties = this.derive(); + nacosClientProperties.addProperties(properties); + return nacosClientProperties; } } diff --git a/client/src/test/java/com/alibaba/nacos/client/env/NPropertiesTest.java b/client/src/test/java/com/alibaba/nacos/client/env/NacosClientPropertiesTest.java similarity index 60% rename from client/src/test/java/com/alibaba/nacos/client/env/NPropertiesTest.java rename to client/src/test/java/com/alibaba/nacos/client/env/NacosClientPropertiesTest.java index fa6ea131fa0..6e0c361e1ef 100644 --- a/client/src/test/java/com/alibaba/nacos/client/env/NPropertiesTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/env/NacosClientPropertiesTest.java @@ -23,7 +23,7 @@ import java.util.Properties; -public class NPropertiesTest { +public class NacosClientPropertiesTest { @BeforeClass public static void init() { @@ -37,23 +37,23 @@ public static void teardown() { @Test public void testGetProperty() { - NProperties.PROTOTYPE.setProperty("nacos.home", "/home/nacos"); - final String value = NProperties.PROTOTYPE.getProperty("nacos.home"); + NacosClientProperties.PROTOTYPE.setProperty("nacos.home", "/home/nacos"); + final String value = NacosClientProperties.PROTOTYPE.getProperty("nacos.home"); Assert.assertEquals("/home/nacos", value); } @Test public void testGetPropertyMultiLayer() { - NProperties.PROTOTYPE.setProperty("top.layer", "top"); + NacosClientProperties.PROTOTYPE.setProperty("top.layer", "top"); - final NProperties layerAEnv = NProperties.PROTOTYPE.derive(); + final NacosClientProperties layerAEnv = NacosClientProperties.PROTOTYPE.derive(); layerAEnv.setProperty("a.layer", "a"); - final NProperties layerBEnv = layerAEnv.derive(); + final NacosClientProperties layerBEnv = layerAEnv.derive(); layerBEnv.setProperty("b.layer", "b"); - final NProperties layerCEnv = layerBEnv.derive(); + final NacosClientProperties layerCEnv = layerBEnv.derive(); layerCEnv.setProperty("c.layer", "c"); String value = layerCEnv.getProperty("c.layer"); @@ -71,70 +71,70 @@ public void testGetPropertyMultiLayer() { @Test public void testGetPropertyDefaultValue() { - final String value = NProperties.PROTOTYPE.getProperty("nacos.home.default", "/home/default_value"); + final String value = NacosClientProperties.PROTOTYPE.getProperty("nacos.home.default", "/home/default_value"); Assert.assertEquals("/home/default_value", value); } @Test public void testGetBoolean() { - NProperties.PROTOTYPE.setProperty("use.cluster", "true"); - final Boolean value = NProperties.PROTOTYPE.getBoolean("use.cluster"); + NacosClientProperties.PROTOTYPE.setProperty("use.cluster", "true"); + final Boolean value = NacosClientProperties.PROTOTYPE.getBoolean("use.cluster"); Assert.assertTrue(value); } @Test public void testGetBooleanDefaultValue() { - final Boolean value = NProperties.PROTOTYPE.getBoolean("use.cluster.default", false); + final Boolean value = NacosClientProperties.PROTOTYPE.getBoolean("use.cluster.default", false); Assert.assertFalse(value); } @Test public void testGetInteger() { - NProperties.PROTOTYPE.setProperty("max.timeout", "200"); - final Integer value = NProperties.PROTOTYPE.getInteger("max.timeout"); + NacosClientProperties.PROTOTYPE.setProperty("max.timeout", "200"); + final Integer value = NacosClientProperties.PROTOTYPE.getInteger("max.timeout"); Assert.assertEquals(200, value.intValue()); } @Test public void testGetIntegerDefaultValue() { - final Integer value = NProperties.PROTOTYPE.getInteger("max.timeout.default", 400); + final Integer value = NacosClientProperties.PROTOTYPE.getInteger("max.timeout.default", 400); Assert.assertEquals(400, value.intValue()); } @Test public void testGetLong() { - NProperties.PROTOTYPE.setProperty("connection.timeout", "200"); - final Long value = NProperties.PROTOTYPE.getLong("connection.timeout"); + NacosClientProperties.PROTOTYPE.setProperty("connection.timeout", "200"); + final Long value = NacosClientProperties.PROTOTYPE.getLong("connection.timeout"); Assert.assertEquals(200L, value.longValue()); } @Test public void testGetLongDefault() { - final Long value = NProperties.PROTOTYPE.getLong("connection.timeout.default", 400L); + final Long value = NacosClientProperties.PROTOTYPE.getLong("connection.timeout.default", 400L); Assert.assertEquals(400L, value.longValue()); } @Test public void testGetPropertyDefaultSetting() { - final String value = NProperties.PROTOTYPE.getProperty("nacos.home.default.test"); + final String value = NacosClientProperties.PROTOTYPE.getProperty("nacos.home.default.test"); Assert.assertEquals("/home/default_setting", value); } @Test public void setProperty() { - NProperties.PROTOTYPE.setProperty("nacos.set.property", "true"); - final String ret = NProperties.PROTOTYPE.getProperty("nacos.set.property"); + NacosClientProperties.PROTOTYPE.setProperty("nacos.set.property", "true"); + final String ret = NacosClientProperties.PROTOTYPE.getProperty("nacos.set.property"); Assert.assertEquals("true", ret); } @Test public void setPropertyWithScope() { - final NProperties properties = NProperties.PROTOTYPE.derive(); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); properties.setProperty("nacos.set.property.scope", "config"); - String ret = NProperties.PROTOTYPE.getProperty("nacos.set.property.scope"); + String ret = NacosClientProperties.PROTOTYPE.getProperty("nacos.set.property.scope"); Assert.assertNull(ret); ret = properties.getProperty("nacos.set.property.scope"); @@ -146,9 +146,9 @@ public void testAddProperties() { Properties properties = new Properties(); properties.setProperty("nacos.add.properties", "true"); - NProperties.PROTOTYPE.addProperties(properties); + NacosClientProperties.PROTOTYPE.addProperties(properties); - final String ret = NProperties.PROTOTYPE.getProperty("nacos.add.properties"); + final String ret = NacosClientProperties.PROTOTYPE.getProperty("nacos.add.properties"); Assert.assertEquals("true", ret); } @@ -159,13 +159,13 @@ public void testAddPropertiesWithScope() { Properties properties = new Properties(); properties.setProperty("nacos.add.properties.scope", "config"); - final NProperties nProperties = NProperties.PROTOTYPE.derive(); - nProperties.addProperties(properties); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(); + nacosClientProperties.addProperties(properties); - String ret = NProperties.PROTOTYPE.getProperty("nacos.add.properties.scope"); + String ret = NacosClientProperties.PROTOTYPE.getProperty("nacos.add.properties.scope"); Assert.assertNull(ret); - ret = nProperties.getProperty("nacos.add.properties.scope"); + ret = nacosClientProperties.getProperty("nacos.add.properties.scope"); Assert.assertEquals("config", ret); } @@ -175,9 +175,9 @@ public void testTestDerive() { Properties properties = new Properties(); properties.setProperty("nacos.derive.properties.scope", "derive"); - final NProperties nProperties = NProperties.PROTOTYPE.derive(properties); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); - final String value = nProperties.getProperty("nacos.derive.properties.scope"); + final String value = nacosClientProperties.getProperty("nacos.derive.properties.scope"); Assert.assertEquals("derive", value); @@ -185,27 +185,27 @@ public void testTestDerive() { @Test public void testContainsKey() { - NProperties.PROTOTYPE.setProperty("nacos.contains.key", "true"); + NacosClientProperties.PROTOTYPE.setProperty("nacos.contains.key", "true"); - boolean ret = NProperties.PROTOTYPE.containsKey("nacos.contains.key"); + boolean ret = NacosClientProperties.PROTOTYPE.containsKey("nacos.contains.key"); Assert.assertTrue(ret); - ret = NProperties.PROTOTYPE.containsKey("nacos.contains.key.in.sys"); + ret = NacosClientProperties.PROTOTYPE.containsKey("nacos.contains.key.in.sys"); Assert.assertFalse(ret); } @Test public void testContainsKeyMultiLayers() { - NProperties.PROTOTYPE.setProperty("top.layer", "top"); + NacosClientProperties.PROTOTYPE.setProperty("top.layer", "top"); - final NProperties layerAEnv = NProperties.PROTOTYPE.derive(); + final NacosClientProperties layerAEnv = NacosClientProperties.PROTOTYPE.derive(); layerAEnv.setProperty("a.layer", "a"); - final NProperties layerBEnv = layerAEnv.derive(); + final NacosClientProperties layerBEnv = layerAEnv.derive(); layerBEnv.setProperty("b.layer", "b"); - final NProperties layerCEnv = layerBEnv.derive(); + final NacosClientProperties layerCEnv = layerBEnv.derive(); layerCEnv.setProperty("c.layer", "c"); boolean exist = layerCEnv.containsKey("c.layer"); @@ -224,14 +224,14 @@ public void testContainsKeyMultiLayers() { @Test public void testContainsKeyWithScope() { - NProperties.PROTOTYPE.setProperty("nacos.contains.global.scope", "global"); - final NProperties namingProperties = NProperties.PROTOTYPE.derive(); + NacosClientProperties.PROTOTYPE.setProperty("nacos.contains.global.scope", "global"); + final NacosClientProperties namingProperties = NacosClientProperties.PROTOTYPE.derive(); namingProperties.setProperty("nacos.contains.naming.scope", "naming"); - boolean ret = NProperties.PROTOTYPE.containsKey("nacos.contains.global.scope"); + boolean ret = NacosClientProperties.PROTOTYPE.containsKey("nacos.contains.global.scope"); Assert.assertTrue(ret); - ret = NProperties.PROTOTYPE.containsKey("nacos.contains.naming.scope"); + ret = NacosClientProperties.PROTOTYPE.containsKey("nacos.contains.naming.scope"); Assert.assertFalse(ret); ret = namingProperties.containsKey("nacos.contains.naming.scope"); @@ -244,8 +244,8 @@ public void testContainsKeyWithScope() { @Test public void testAsProperties() { - NProperties.PROTOTYPE.setProperty("nacos.as.properties", "true"); - final Properties properties = NProperties.PROTOTYPE.asProperties(); + NacosClientProperties.PROTOTYPE.setProperty("nacos.as.properties", "true"); + final Properties properties = NacosClientProperties.PROTOTYPE.asProperties(); Assert.assertNotNull(properties); Assert.assertEquals("true", properties.getProperty("nacos.as.properties")); } @@ -253,10 +253,10 @@ public void testAsProperties() { @Test public void testAsPropertiesWithScope() { - NProperties.PROTOTYPE.setProperty("nacos.as.properties.global.scope", "global"); - NProperties.PROTOTYPE.setProperty("nacos.server.addr.scope", "global"); + NacosClientProperties.PROTOTYPE.setProperty("nacos.as.properties.global.scope", "global"); + NacosClientProperties.PROTOTYPE.setProperty("nacos.server.addr.scope", "global"); - final NProperties configProperties = NProperties.PROTOTYPE.derive(); + final NacosClientProperties configProperties = NacosClientProperties.PROTOTYPE.derive(); configProperties.setProperty("nacos.server.addr.scope", "config"); final Properties properties = configProperties.asProperties(); @@ -272,21 +272,21 @@ public void testAsPropertiesWithScope() { @Test public void testGetPropertyWithScope() { - NProperties.PROTOTYPE.setProperty("nacos.global.scope", "global"); + NacosClientProperties.PROTOTYPE.setProperty("nacos.global.scope", "global"); - final NProperties configProperties = NProperties.PROTOTYPE.derive(); + final NacosClientProperties configProperties = NacosClientProperties.PROTOTYPE.derive(); configProperties.setProperty("nacos.config.scope", "config"); - final NProperties namingProperties = NProperties.PROTOTYPE.derive(); + final NacosClientProperties namingProperties = NacosClientProperties.PROTOTYPE.derive(); namingProperties.setProperty("nacos.naming.scope", "naming"); - String ret = NProperties.PROTOTYPE.getProperty("nacos.global.scope"); + String ret = NacosClientProperties.PROTOTYPE.getProperty("nacos.global.scope"); Assert.assertEquals("global", ret); - ret = NProperties.PROTOTYPE.getProperty("nacos.config.scope"); + ret = NacosClientProperties.PROTOTYPE.getProperty("nacos.config.scope"); Assert.assertNull(ret); - ret = NProperties.PROTOTYPE.getProperty("nacos.naming.scope"); + ret = NacosClientProperties.PROTOTYPE.getProperty("nacos.naming.scope"); Assert.assertNull(ret); ret = configProperties.getProperty("nacos.config.scope");