Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
添加单测代码
Browse files Browse the repository at this point in the history
  • Loading branch information
巨鹿 committed Jan 17, 2024
1 parent 38c93be commit ef97a5e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,25 @@
*/
public class MultiBizProperties extends Properties {

private final String bizClassLoaderName;
private final String bizClassLoaderName;

private static final String BIZ_CLASS_LOADER = "com.alipay.sofa.ark.container.service.classloader.BizClassLoader";
private static final String BIZ_CLASS_LOADER = "com.alipay.sofa.ark.container.service.classloader.BizClassLoader";

private Map<ClassLoader, Set<String>> modifiedKeysMap = new HashMap<>();
private Map<ClassLoader, Set<String>> modifiedKeysMap = new HashMap<>();

private final Properties baseProperties;
private Map<ClassLoader, Properties> bizPropertiesMap;
private final Properties baseProperties;
private Map<ClassLoader, Properties> bizPropertiesMap;

public MultiBizProperties(String bizClassLoaderName, Properties baseProperties) {
private MultiBizProperties(String bizClassLoaderName, Properties baseProperties) {
this.bizPropertiesMap = new HashMap<>();
this.baseProperties = baseProperties;
this.bizClassLoaderName = bizClassLoaderName;
}

public MultiBizProperties(String bizClassLoaderName) {
this(bizClassLoaderName, new Properties());
}

public synchronized Object setProperty(String key, String value) {
addModifiedKey(key);
return getWriteProperties().setProperty(key, value);
Expand Down Expand Up @@ -199,9 +203,10 @@ public synchronized void clear() {
public synchronized Object clone() {
MultiBizProperties mbp = new MultiBizProperties(bizClassLoaderName, baseProperties);
mbp.bizPropertiesMap = new HashMap<>();
bizPropertiesMap.forEach((k, p) -> mbp.put(k, p.clone()));
bizPropertiesMap.forEach((k, p) -> mbp.bizPropertiesMap.put(k, (Properties) p.clone()));
mbp.bizPropertiesMap.putAll(bizPropertiesMap);
mbp.modifiedKeysMap = modifiedKeysMap;
mbp.modifiedKeysMap = new HashMap<>();
modifiedKeysMap.forEach((k, s) -> mbp.modifiedKeysMap.put(k, new HashSet<>(s)));
return mbp;
}

Expand Down Expand Up @@ -435,7 +440,7 @@ private void addModifiedKeys(Collection<String> keys) {
public static void initSystem(String bizClassLoaderName) {
Properties properties = System.getProperties();
MultiBizProperties multiBizProperties = new MultiBizProperties(bizClassLoaderName,
properties);
properties);
System.setProperties(multiBizProperties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;

public class MultiBizPropertiesTest {
private final String key1 = "test-key-1";
private final String key1 = "test-key-1";
private final String value1 = "test-value-1";
private final String value2 = "test-value-2";

private ClassLoader baseClassLoader;
private ClassLoader baseClassLoader;

@Before
public void before() {
Expand Down Expand Up @@ -103,4 +107,54 @@ public void testGetAndClear() {
Assert.assertEquals(value2, System.getProperty(key1));
}

@Test
public void testClone() {
Properties properties = System.getProperties();
Properties other = (Properties) properties.clone();
Assert.assertEquals(properties, other);
}


@Test
public void testStoreAndLoad() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Properties properties = new MultiBizProperties(URLClassLoader.class.getName());
properties.put(key1, value1);
properties.putAll(System.getProperties());
int size = properties.size();
properties.store(out, "test store");
properties.clear();
Assert.assertEquals(properties.size(), 0);
ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
properties.load(input);
Assert.assertEquals(properties.size(), size);


out = new ByteArrayOutputStream();
properties.save(out, "test store");
properties.clear();
Assert.assertEquals(properties.size(), 0);
input = new ByteArrayInputStream(out.toByteArray());
properties.load(input);
Assert.assertEquals(properties.size(), size);


out = new ByteArrayOutputStream();
properties.storeToXML(out, "test store");
properties.clear();
Assert.assertEquals(properties.size(), 0);
// System.out.println(out);
input = new ByteArrayInputStream(out.toByteArray());
try {
properties.loadFromXML(input);
} catch (Throwable e) {
e.printStackTrace();
}

Assert.assertEquals(properties.size(), size);

Assert.assertTrue(properties.containsKey(key1));
Assert.assertTrue(properties.contains(value1));
}

}

0 comments on commit ef97a5e

Please sign in to comment.