Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ protected static void merge(Properties targetProperties,
if (!targetProperties.containsKey(propertyName)) {
String propertyValue = (String) entry.getValue();
targetProperties.setProperty(propertyName, propertyValue);
} else {
// when the key values of the same target and source are different, the value of source is taken
if (!targetProperties.get(propertyName).equals(sourceProperties.get(propertyName))) {
targetProperties.setProperty(propertyName, (String) sourceProperties.get(propertyName));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.alibaba.nacos.spring.util;

import java.lang.reflect.Field;
import java.util.Properties;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.ReflectionUtils;

Expand All @@ -38,6 +40,31 @@ public class NacosUtilsTest {

@NacosInjected(properties = @NacosProperties(serverAddr = "test"))
private Object object2 = new Object();


Properties globalProperties;

Properties currentProperties = new Properties();

@Before
public void setUp(){
globalProperties = new Properties();
globalProperties.setProperty("namespace","nacos_ns_playground");
globalProperties.setProperty("username","nacos");
globalProperties.setProperty("enableRemoteSyncConfig","true");
globalProperties.setProperty("configLongPollTimeout","30000");
globalProperties.setProperty("configRetryTime","2000");
globalProperties.setProperty("encode","UTF-8");
globalProperties.setProperty("serverAddr","http://test01-nacos.api.net:8080");
globalProperties.setProperty("maxRetry","3");
globalProperties.setProperty("password","nacos");

currentProperties = new Properties();
currentProperties.setProperty("password","nacos_test");
currentProperties.setProperty("namespace","nacos_ns_playground");
currentProperties.setProperty("encode","UTF-8");
currentProperties.setProperty("serverAddr","http://test02-nacos.api.net:8848");
}

@Test
public void testIsDefault() {
Expand All @@ -53,8 +80,16 @@ private void testIsDefault(String fieldName, boolean expectedValue) {
NacosInjected nacosInjected = objectField.getAnnotation(NacosInjected.class);

NacosProperties nacosProperties = nacosInjected.properties();

Assert.assertEquals(expectedValue, NacosUtils.isDefault(nacosProperties));

}

/**
* NacosUtils merge test.
*/
@Test
public void merge() {
NacosUtils.merge(globalProperties, currentProperties);
Assert.assertEquals(globalProperties.getProperty("serverAddr"), "http://test02-nacos.api.net:8848");
Assert.assertEquals(globalProperties.size(), 9);
}
}