File tree Expand file tree Collapse file tree 2 files changed +13
-11
lines changed
spring-ai-alibaba-core/src
main/java/com/alibaba/cloud/ai/document
test/java/com/alibaba/cloud/ai/document Expand file tree Collapse file tree 2 files changed +13
-11
lines changed Original file line number Diff line number Diff line change 18
18
import org .springframework .ai .document .Document ;
19
19
import org .springframework .util .Assert ;
20
20
21
+ import java .io .IOException ;
21
22
import java .io .InputStream ;
22
23
import java .nio .charset .Charset ;
23
24
import java .nio .charset .StandardCharsets ;
@@ -44,13 +45,17 @@ public TextDocumentParser(Charset charset) {
44
45
@ Override
45
46
public List <Document > parse (InputStream inputStream ) {
46
47
try {
47
- String text = new String (inputStream .readAllBytes (), charset );
48
+ // Read all text from the input stream and decode it using the specified
49
+ // character set.
50
+ String text = new String (inputStream .readAllBytes (), this .charset );
51
+ // If the text is completely empty, report an illegal argument exception.
48
52
if (text .isBlank ()) {
49
- throw new Exception ( );
53
+ throw new IllegalArgumentException ( "text must not be blank" );
50
54
}
51
55
return Collections .singletonList (new Document (text ));
52
56
}
53
- catch (Exception e ) {
57
+ catch (IOException e ) {
58
+ // Convert any IO exception into a RuntimeException for propagation.
54
59
throw new RuntimeException (e );
55
60
}
56
61
}
Original file line number Diff line number Diff line change @@ -62,24 +62,21 @@ void testParseWithCustomCharset() {
62
62
63
63
@ Test
64
64
void testParseEmptyText () {
65
- // Test parsing empty text should throw exception
65
+ // An empty string should trigger an IllegalArgumentException.
66
66
String text = "" ;
67
67
TextDocumentParser parser = new TextDocumentParser ();
68
68
69
- RuntimeException exception = assertThrows (RuntimeException .class , () -> parser .parse (toInputStream (text )));
70
-
71
- assertThat (exception ).hasRootCauseInstanceOf (Exception .class );
69
+ assertThrows (IllegalArgumentException .class , () -> parser .parse (toInputStream (text )));
72
70
}
73
71
74
72
@ Test
75
73
void testParseBlankText () {
76
- // Test parsing blank text (only whitespace) should throw exception
74
+ // A blank string containing spaces or inserted characters should also throw an
75
+ // IllegalArgumentException.
77
76
String text = " \n \t " ;
78
77
TextDocumentParser parser = new TextDocumentParser ();
79
78
80
- RuntimeException exception = assertThrows (RuntimeException .class , () -> parser .parse (toInputStream (text )));
81
-
82
- assertThat (exception ).hasRootCauseInstanceOf (Exception .class );
79
+ assertThrows (IllegalArgumentException .class , () -> parser .parse (toInputStream (text )));
83
80
}
84
81
85
82
@ Test
You can’t perform that action at this time.
0 commit comments