Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,17 @@ public TextDocumentParser(Charset charset) {
@Override
public List<Document> parse(InputStream inputStream) {
try {
String text = new String(inputStream.readAllBytes(), charset);
// Read all text from the input stream and decode it using the specified
// character set.
String text = new String(inputStream.readAllBytes(), this.charset);
// If the text is completely empty, report an illegal argument exception.
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) {
// Convert any IO exception into a RuntimeException for propagation.
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,21 @@ void testParseWithCustomCharset() {

@Test
void testParseEmptyText() {
// Test parsing empty text should throw exception
// An empty string should trigger an 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
// A blank string containing spaces or inserted characters should also throw an
// 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