扩展IoUtil、BooleanUtil两个工具类

This commit is contained in:
zhangrenhua
2022-07-11 16:15:46 +08:00
parent a38e3e52af
commit eefdd7fc4f
4 changed files with 118 additions and 4 deletions

View File

@@ -5,8 +5,8 @@ import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.nio.charset.Charset;
public class IoUtilTest {
@@ -32,4 +32,28 @@ public class IoUtilTest {
throw new IORuntimeException(e);
}
}
@Test
public void copyToByteArrayTest() throws Exception {
try(InputStream is1 = ResourceUtil.getStream("hutool.jpg");
InputStream is2 = ResourceUtil.getStream("hutool.jpg")){
byte[] copiedBytes = IoUtil.copyToByteArray(is1);
byte[] readBytes = IoUtil.readBytes(is2);
Assert.assertArrayEquals(readBytes, copiedBytes);
}
}
@Test
public void copyToStringTest() throws Exception {
String str = "abc123";
try(InputStream is1 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset()));
InputStream is2 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset()))){
String copiedString = IoUtil.copyToString(is1, Charset.defaultCharset());
String readString = IoUtil.read(is2, Charset.defaultCharset());
Assert.assertEquals(readString, copiedString);
}
}
}

View File

@@ -4,7 +4,41 @@ import org.junit.Assert;
import org.junit.Test;
public class BooleanUtilTest {
@Test
public void testVerifyBooleanString(){
/*true的各种形式*/
Assert.assertTrue(BooleanUtil.verifyBooleanString("true"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("yes"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("t"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("OK"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("1"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("On"));
Assert.assertTrue(BooleanUtil.verifyBooleanString(""));
Assert.assertTrue(BooleanUtil.verifyBooleanString(""));
Assert.assertTrue(BooleanUtil.verifyBooleanString(""));
/*false的各种形式*/
Assert.assertTrue(BooleanUtil.verifyBooleanString("false"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("no"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("n"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("f"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("0"));
Assert.assertTrue(BooleanUtil.verifyBooleanString("off"));
Assert.assertTrue(BooleanUtil.verifyBooleanString(""));
Assert.assertTrue(BooleanUtil.verifyBooleanString(""));
Assert.assertTrue(BooleanUtil.verifyBooleanString(""));
Assert.assertTrue(BooleanUtil.verifyBooleanString(""));
/*非正常的bool字符串*/
Assert.assertFalse(BooleanUtil.verifyBooleanString(null));
Assert.assertFalse(BooleanUtil.verifyBooleanString(""));
Assert.assertFalse(BooleanUtil.verifyBooleanString("x"));
Assert.assertFalse(BooleanUtil.verifyBooleanString("a"));
Assert.assertFalse(BooleanUtil.verifyBooleanString("99"));
Assert.assertFalse(BooleanUtil.verifyBooleanString("q23"));
}
@Test
public void toBooleanTest() {
Assert.assertTrue(BooleanUtil.toBoolean("true"));
@@ -16,7 +50,7 @@ public class BooleanUtilTest {
Assert.assertTrue(BooleanUtil.toBoolean(""));
Assert.assertTrue(BooleanUtil.toBoolean(""));
Assert.assertTrue(BooleanUtil.toBoolean(""));
Assert.assertFalse(BooleanUtil.toBoolean("false"));
Assert.assertFalse(BooleanUtil.toBoolean("6455434"));
Assert.assertFalse(BooleanUtil.toBoolean(""));