#1550 添加单元测试;部分类移动到 api 模块

This commit is contained in:
rushsky518 2019-11-28 11:56:04 +08:00
parent ee660f335d
commit 3c1d6d00c3
12 changed files with 203 additions and 18 deletions

View File

@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.client.config.impl; package com.alibaba.nacos.api.config;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.client.config.impl; package com.alibaba.nacos.api.config;
/** /**
* ConfigChangeItem * ConfigChangeItem
@ -26,14 +26,6 @@ public class ConfigChangeItem {
private String newValue; private String newValue;
private PropertyChangeType type; private PropertyChangeType type;
public enum PropertyChangeType {
/** add */
ADDED,
/** modified */
MODIFIED,
/** deleted */
DELETED
}
public ConfigChangeItem(String key, String oldValue, String newValue) { public ConfigChangeItem(String key, String oldValue, String newValue) {
this.key = key; this.key = key;

View File

@ -0,0 +1,30 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.config;
/**
* Property Change Type
*
* @author rushsky518
*/
public enum PropertyChangeType {
/** add */
ADDED,
/** modified */
MODIFIED,
/** deleted */
DELETED
}

View File

@ -15,6 +15,8 @@
*/ */
package com.alibaba.nacos.api.config.listener; package com.alibaba.nacos.api.config.listener;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -39,5 +41,5 @@ public interface ConfigChangeParser {
* @return * @return
* @throws IOException * @throws IOException
*/ */
Map<String, Object> doParse(String oldContent, String newContent, String type) throws IOException; Map<String, ConfigChangeItem> doParse(String oldContent, String newContent, String type) throws IOException;
} }

View File

@ -15,6 +15,8 @@
*/ */
package com.alibaba.nacos.client.config.impl; package com.alibaba.nacos.client.config.impl;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.api.config.PropertyChangeType;
import com.alibaba.nacos.api.config.listener.ConfigChangeParser; import com.alibaba.nacos.api.config.listener.ConfigChangeParser;
import java.util.HashMap; import java.util.HashMap;
@ -38,7 +40,7 @@ public abstract class AbstractConfigChangeParser implements ConfigChangeParser {
return this.configType.equalsIgnoreCase(type); return this.configType.equalsIgnoreCase(type);
} }
protected Map filterChangeData(Map oldMap, Map newMap) { protected Map<String, ConfigChangeItem> filterChangeData(Map oldMap, Map newMap) {
Map<String, ConfigChangeItem> result = new HashMap<String, ConfigChangeItem>(16); Map<String, ConfigChangeItem> result = new HashMap<String, ConfigChangeItem>(16);
for (Iterator<Map.Entry<String, Object>> entryItr = oldMap.entrySet().iterator(); entryItr.hasNext();) { for (Iterator<Map.Entry<String, Object>> entryItr = oldMap.entrySet().iterator(); entryItr.hasNext();) {
Map.Entry<String, Object> e = entryItr.next(); Map.Entry<String, Object> e = entryItr.next();
@ -48,10 +50,10 @@ public abstract class AbstractConfigChangeParser implements ConfigChangeParser {
continue; continue;
} }
cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), newMap.get(e.getKey()).toString()); cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), newMap.get(e.getKey()).toString());
cci.setType(ConfigChangeItem.PropertyChangeType.MODIFIED); cci.setType(PropertyChangeType.MODIFIED);
} else { } else {
cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), null); cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), null);
cci.setType(ConfigChangeItem.PropertyChangeType.DELETED); cci.setType(PropertyChangeType.DELETED);
} }
result.put(e.getKey(), cci); result.put(e.getKey(), cci);
@ -61,7 +63,7 @@ public abstract class AbstractConfigChangeParser implements ConfigChangeParser {
Map.Entry<String, Object> e = entryItr.next(); Map.Entry<String, Object> e = entryItr.next();
if (!oldMap.containsKey(e.getKey())) { if (!oldMap.containsKey(e.getKey())) {
ConfigChangeItem cci = new ConfigChangeItem(e.getKey(), null, e.getValue().toString()); ConfigChangeItem cci = new ConfigChangeItem(e.getKey(), null, e.getValue().toString());
cci.setType(ConfigChangeItem.PropertyChangeType.ADDED); cci.setType(PropertyChangeType.ADDED);
result.put(e.getKey(), cci); result.put(e.getKey(), cci);
} }
} }

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.config.impl; package com.alibaba.nacos.client.config.impl;
import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigChangeEvent;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener; import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;

View File

@ -15,6 +15,7 @@
*/ */
package com.alibaba.nacos.client.config.impl; package com.alibaba.nacos.client.config.impl;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.client.utils.StringUtils;
import java.io.IOException; import java.io.IOException;
@ -33,7 +34,7 @@ public class PropertiesChangeParser extends AbstractConfigChangeParser {
} }
@Override @Override
public Map doParse(String oldContent, String newContent, String type) throws IOException { public Map<String, ConfigChangeItem> doParse(String oldContent, String newContent, String type) throws IOException {
Properties oldProps = new Properties(); Properties oldProps = new Properties();
Properties newProps = new Properties(); Properties newProps = new Properties();

View File

@ -15,6 +15,7 @@
*/ */
package com.alibaba.nacos.client.config.impl; package com.alibaba.nacos.client.config.impl;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.client.utils.StringUtils;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import java.util.*; import java.util.*;
@ -30,7 +31,7 @@ public class YmlChangeParser extends AbstractConfigChangeParser {
} }
@Override @Override
public Map<String, Object> doParse(String oldContent, String newContent, String type) { public Map<String, ConfigChangeItem> doParse(String oldContent, String newContent, String type) {
Map<String, Object> oldMap = Collections.emptyMap(); Map<String, Object> oldMap = Collections.emptyMap();
Map<String, Object> newMap = Collections.emptyMap(); Map<String, Object> newMap = Collections.emptyMap();

View File

@ -16,7 +16,7 @@
package com.alibaba.nacos.client.config.listener.impl; package com.alibaba.nacos.client.config.listener.impl;
import com.alibaba.nacos.api.config.listener.AbstractListener; import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.alibaba.nacos.client.config.impl.ConfigChangeEvent; import com.alibaba.nacos.api.config.ConfigChangeEvent;
/** /**
* AbstractConfigChangeListener * AbstractConfigChangeListener

View File

@ -0,0 +1,39 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.config.listener.impl;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.client.config.impl.ConfigChangeHandler;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
public class ConfigChangeHandlerTest {
@Test
public void testParseProperties() throws IOException {
Map properties = ConfigChangeHandler.getInstance().parseChangeData("", "app.name = nacos", "properties");
Assert.assertEquals("nacos", ((ConfigChangeItem)properties.get("app.name")).getNewValue());
}
@Test
public void testParseYaml() throws IOException {
Map properties = ConfigChangeHandler.getInstance().parseChangeData("", "app:\n name: nacos", "yaml");
Assert.assertEquals("nacos", ((ConfigChangeItem)properties.get("app.name")).getNewValue());
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.config.listener.impl;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.client.config.impl.PropertiesChangeParser;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
public class PropertiesChangeParserTest {
private PropertiesChangeParser parser = new PropertiesChangeParser();
private final String type = "properties";
@Test
public void testType() {
Assert.assertEquals(true, parser.isResponsibleFor(type));
}
@Test
public void testAddKey() throws IOException {
Map<String, ConfigChangeItem> map = parser.doParse("", "app.name = nacos", type);
Assert.assertEquals(null, map.get("app.name").getOldValue());
Assert.assertEquals("nacos", map.get("app.name").getNewValue());
}
@Test
public void testRemoveKey() throws IOException {
Map<String, ConfigChangeItem> map = parser.doParse("app.name = nacos", "", type);
Assert.assertEquals("nacos", map.get("app.name").getOldValue());
Assert.assertEquals(null, map.get("app.name").getNewValue());
}
@Test
public void testModifyKey() throws IOException {
Map<String, ConfigChangeItem> map = parser.doParse("app.name = rocketMQ", "app.name = nacos", type);
Assert.assertEquals("rocketMQ", map.get("app.name").getOldValue());
Assert.assertEquals("nacos", map.get("app.name").getNewValue());
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.config.listener.impl;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.client.config.impl.YmlChangeParser;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
public class YmlChangeParserTest {
private YmlChangeParser parser = new YmlChangeParser();
private final String type = "yaml";
@Test
public void testType() {
Assert.assertEquals(true, parser.isResponsibleFor(type));
}
@Test
public void testAddKey() throws IOException {
Map<String, ConfigChangeItem> map = parser.doParse("", "app:\n name: nacos", type);
Assert.assertEquals(null, map.get("app.name").getOldValue());
Assert.assertEquals("nacos", map.get("app.name").getNewValue());
}
@Test
public void testRemoveKey() throws IOException {
Map<String, ConfigChangeItem> map = parser.doParse("app:\n name: nacos", "", type);
Assert.assertEquals("nacos", map.get("app.name").getOldValue());
Assert.assertEquals(null, map.get("app.name").getNewValue());
}
@Test
public void testModifyKey() throws IOException {
Map<String, ConfigChangeItem> map = parser.doParse("app:\n name: rocketMQ", "app:\n name: nacos", type);
Assert.assertEquals("rocketMQ", map.get("app.name").getOldValue());
Assert.assertEquals("nacos", map.get("app.name").getNewValue());
}
}