mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.codec.HexUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@@ -12,21 +12,32 @@ import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 文件类型判断单元测试
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class FileTypeUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void fileTypeUtilTest() {
|
||||
public void getTypeTest() {
|
||||
final String type = FileTypeUtil.getType(ResourceUtil.getStream("hutool.jpg"));
|
||||
Assert.assertEquals("jpg", type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTypeTest() {
|
||||
final File file = FileUtil.file("hutool.jpg");
|
||||
final String type = FileTypeUtil.getType(file);
|
||||
Assert.assertEquals("jpg", type);
|
||||
|
||||
FileTypeUtil.putFileType("ffd8ffe000104a464946", "new_jpg");
|
||||
final String oldType = FileTypeUtil.putFileType("ffd8ffe000104a464946", "new_jpg");
|
||||
Assert.assertNull(oldType);
|
||||
|
||||
final String newType = FileTypeUtil.getType(file);
|
||||
Assert.assertEquals("new_jpg", newType);
|
||||
|
||||
FileTypeUtil.removeFileType("ffd8ffe000104a464946");
|
||||
final String type2 = FileTypeUtil.getType(file);
|
||||
Assert.assertEquals("jpg", type2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -49,7 +60,7 @@ public class FileTypeUtilTest {
|
||||
@Ignore
|
||||
public void ofdTest() {
|
||||
final File file = FileUtil.file("e:/test.ofd");
|
||||
final String hex = IoUtil.readHex28Upper(FileUtil.getInputStream(file));
|
||||
final String hex = FileTypeUtil.readHex28Upper(FileUtil.getInputStream(file));
|
||||
Console.log(hex);
|
||||
final String type = FileTypeUtil.getType(file);
|
||||
Console.log(type);
|
||||
@@ -72,13 +83,14 @@ public class FileTypeUtilTest {
|
||||
final BufferedInputStream inputStream = FileUtil.getInputStream(file);
|
||||
inputStream.mark(0);
|
||||
final String type = FileTypeUtil.getType(inputStream);
|
||||
Assert.assertEquals("jpg", type);
|
||||
|
||||
inputStream.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void webpTest(){
|
||||
public void webpTest() {
|
||||
// https://gitee.com/dromara/hutool/issues/I5BGTF
|
||||
final File file = FileUtil.file("d:/test/a.webp");
|
||||
final BufferedInputStream inputStream = FileUtil.getInputStream(file);
|
||||
@@ -87,8 +99,14 @@ public class FileTypeUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHexTest() {
|
||||
final String id3 = HexUtil.encodeHexStr("8BPS");
|
||||
Console.log(id3);
|
||||
public void readHex28LowerTest() {
|
||||
final String s = FileTypeUtil.readHex28Lower(ResourceUtil.getStream("hutool.jpg"));
|
||||
Assert.assertEquals("ffd8ffe000104a46494600010101006000600000ffe1095845786966", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readHex28UpperTest() {
|
||||
final String s = FileTypeUtil.readHex28Upper(ResourceUtil.getStream("hutool.jpg"));
|
||||
Assert.assertEquals("FFD8FFE000104A46494600010101006000600000FFE1095845786966", s);
|
||||
}
|
||||
}
|
||||
|
@@ -1,13 +1,27 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.collection.iter.LineIter;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.io.stream.EmptyOutputStream;
|
||||
import cn.hutool.core.io.stream.StrInputStream;
|
||||
import cn.hutool.core.lang.func.SerConsumer;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PushbackReader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class IoUtilTest {
|
||||
|
||||
@@ -33,4 +47,111 @@ public class IoUtilTest {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readUtf8LinesTest() {
|
||||
final ArrayList<String> strings = IoUtil.readUtf8Lines(ResourceUtil.getStream("text.txt"), ListUtil.of());
|
||||
Assert.assertEquals(3, strings.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readUtf8LinesTest2() {
|
||||
IoUtil.readUtf8Lines(ResourceUtil.getStream("text.txt"), (SerConsumer<String>) Assert::assertNotNull);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBufferedTest() {
|
||||
final BufferedInputStream stream = IoUtil.toBuffered(
|
||||
new ByteArrayInputStream("hutool".getBytes()), IoUtil.DEFAULT_BUFFER_SIZE);
|
||||
|
||||
Assert.assertNotNull(stream);
|
||||
Assert.assertEquals("hutool", IoUtil.readUtf8(stream));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBufferedOfOutTest() {
|
||||
final BufferedOutputStream stream = IoUtil.toBuffered(
|
||||
EmptyOutputStream.INSTANCE, 512);
|
||||
|
||||
Assert.assertNotNull(stream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBufferedOfReaderTest() {
|
||||
final BufferedReader reader = IoUtil.toBuffered(
|
||||
new StringReader("hutool"), 512);
|
||||
|
||||
Assert.assertNotNull(reader);
|
||||
|
||||
Assert.assertEquals("hutool", IoUtil.read(reader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBufferedOfWriterTest() throws IOException {
|
||||
final StringWriter stringWriter = new StringWriter();
|
||||
final BufferedWriter writer = IoUtil.toBuffered(
|
||||
stringWriter, 512);
|
||||
|
||||
Assert.assertNotNull(writer);
|
||||
writer.write("hutool");
|
||||
writer.flush();
|
||||
|
||||
Assert.assertEquals("hutool", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBufferedOfWriterTest2() throws IOException {
|
||||
final StringWriter stringWriter = new StringWriter();
|
||||
final BufferedWriter writer = IoUtil.toBuffered(stringWriter);
|
||||
|
||||
Assert.assertNotNull(writer);
|
||||
writer.write("hutool");
|
||||
writer.flush();
|
||||
|
||||
Assert.assertEquals("hutool", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toPushBackReaderTest() throws IOException {
|
||||
final PushbackReader reader = IoUtil.toPushBackReader(new StringReader("hutool"), 12);
|
||||
final int read = reader.read();
|
||||
Assert.assertEquals('h', read);
|
||||
reader.unread(read);
|
||||
|
||||
Assert.assertEquals("hutool", IoUtil.read(reader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toAvailableStreamTest() {
|
||||
final InputStream in = IoUtil.toAvailableStream(StrInputStream.ofUtf8("hutool"));
|
||||
final String read = IoUtil.readUtf8(in);
|
||||
Assert.assertEquals("hutool", read);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeCloseTest() {
|
||||
IoUtil.writeClose(EmptyOutputStream.INSTANCE, "hutool".getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeUtf8Test() {
|
||||
IoUtil.writeUtf8(EmptyOutputStream.INSTANCE, false, "hutool");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closeIfPossibleTest() {
|
||||
IoUtil.closeIfPossible(new Object());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentEqualsTest() {
|
||||
final boolean b = IoUtil.contentEquals(new StringReader("hutool"), new StringReader("hutool"));
|
||||
Assert.assertTrue(b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lineIterTest() {
|
||||
final LineIter strings = IoUtil.lineIter(ResourceUtil.getStream("text.txt"), CharsetUtil.UTF_8);
|
||||
strings.forEach(Assert::assertNotNull);
|
||||
}
|
||||
}
|
||||
|
24
hutool-core/src/test/java/cn/hutool/core/io/NioUtilTest.java
Executable file
24
hutool-core/src/test/java/cn/hutool/core/io/NioUtilTest.java
Executable file
@@ -0,0 +1,24 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.io.stream.EmptyOutputStream;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class NioUtilTest {
|
||||
@Test
|
||||
public void copyByNIOTest() {
|
||||
final long size = NioUtil.copyByNIO(ResourceUtil.getStream("hutool.jpg"), EmptyOutputStream.INSTANCE, NioUtil.DEFAULT_MIDDLE_BUFFER_SIZE, null);
|
||||
Assert.assertEquals(22807, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readUtf8Test() throws IOException {
|
||||
final String s = NioUtil.readUtf8(FileChannel.open(FileUtil.file("text.txt").toPath()));
|
||||
Assert.assertTrue(StrUtil.isNotBlank(s));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user