Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.ai.document.Document;
import org.springframework.util.Assert;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -44,13 +45,16 @@ public TextDocumentParser(Charset charset) {
@Override
public List<Document> parse(InputStream inputStream) {
try {
String text = new String(inputStream.readAllBytes(), charset);
// 读取入口流所有文本,且使用指定的字符集解码
String text = new String(inputStream.readAllBytes(), this.charset);
// 如果文本全部为空白,则报告非法参数异常
if (text.isBlank()) {
throw new Exception();
throw new IllegalArgumentException("text must not be blank");
}
return Collections.singletonList(new Document(text));
}
catch (Exception e) {
catch (IOException e) {
// 将任何 IO 异常转换为 RuntimeException 以便传播
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,20 @@ void testParseWithCustomCharset() {

@Test
void testParseEmptyText() {
// Test parsing empty text should throw exception
// 空字符串应当触发 IllegalArgumentException
String text = "";
TextDocumentParser parser = new TextDocumentParser();

RuntimeException exception = assertThrows(RuntimeException.class, () -> parser.parse(toInputStream(text)));

assertThat(exception).hasRootCauseInstanceOf(Exception.class);
assertThrows(IllegalArgumentException.class, () -> parser.parse(toInputStream(text)));
}

@Test
void testParseBlankText() {
// Test parsing blank text (only whitespace) should throw exception
// 含有空格或插入字符的空白字符串同样应抛出 IllegalArgumentException
String text = " \n\t ";
TextDocumentParser parser = new TextDocumentParser();

RuntimeException exception = assertThrows(RuntimeException.class, () -> parser.parse(toInputStream(text)));

assertThat(exception).hasRootCauseInstanceOf(Exception.class);
assertThrows(IllegalArgumentException.class, () -> parser.parse(toInputStream(text)));
}

@Test
Expand Down
Loading