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 43dce28 commit f574754
Showing 1 changed file with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

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

private final String key3 = "test-key-3";
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 @@ -149,9 +151,48 @@ public void testStoreAndLoad() throws IOException {
}

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

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


out = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(out);
properties.list(writer);
writer.close();
properties.clear();
Assert.assertTrue(properties.isEmpty());

input = new ByteArrayInputStream(out.toByteArray());
Reader reader = new InputStreamReader(input);
properties.load(reader);
reader.close();
Assert.assertFalse(properties.isEmpty());
}

@Test
public void testRead() {
Properties properties = new MultiBizProperties(URLClassLoader.class.getName());
properties.setProperty(key1, value1);
Assert.assertEquals(properties.keys().nextElement(), key1);
Assert.assertTrue(properties.containsValue(value1));

AtomicInteger num = new AtomicInteger();
properties.forEach((k, v) -> {
Assert.assertEquals(k, key1);
Assert.assertEquals(v, value1);
num.incrementAndGet();
});
Assert.assertEquals(num.get(), 1);

Assert.assertEquals(properties.getOrDefault(key1, value2), value1);
Assert.assertEquals(properties.getOrDefault(key2, value2), value2);


Assert.assertEquals(properties.putIfAbsent(key1, value2), value1);
Assert.assertNull(properties.putIfAbsent(key2, value2));

Assert.assertEquals(properties.computeIfAbsent(key3, k -> value1), value1);
}


}

0 comments on commit f574754

Please sign in to comment.