33import static org .hamcrest .Matchers .*;
44import static org .hamcrest .MatcherAssert .assertThat ;
55
6+ import java .io .IOException ;
7+ import java .nio .charset .StandardCharsets ;
8+ import java .nio .file .Files ;
9+ import java .nio .file .Path ;
610import java .util .Set ;
711import org .junit .Before ;
812import org .junit .Test ;
13+ import org .javacs .lsp .DidOpenTextDocumentParams ;
14+ import org .javacs .lsp .TextDocumentItem ;
915
1016public class FileStoreTest {
1117
1218 @ Before
1319 public void setWorkspaceRoot () {
20+ FileStore .reset ();
1421 FileStore .setWorkspaceRoots (Set .of (LanguageServerFixture .DEFAULT_WORKSPACE_ROOT ));
1522 }
1623
@@ -26,4 +33,30 @@ public void missingFile() {
2633 assertThat (FileStore .packageName (file ), nullValue ());
2734 assertThat (FileStore .modified (file ), nullValue ());
2835 }
36+
37+ @ Test
38+ public void inputReadersPreferActiveDocumentContents () throws IOException {
39+ Path file = Files .createTempFile ("FileStoreTest" , ".java" );
40+ try {
41+ Files .writeString (file , "class Foo { int onDisk; }\n " );
42+
43+ var open = new DidOpenTextDocumentParams ();
44+ open .textDocument = new TextDocumentItem ();
45+ open .textDocument .uri = file .toUri ();
46+ open .textDocument .languageId = "java" ;
47+ open .textDocument .version = 1 ;
48+ open .textDocument .text = "class Foo { int inMemory; }\n " ;
49+ FileStore .open (open );
50+
51+ var fromInputStream = new String (FileStore .inputStream (file ).readAllBytes (), StandardCharsets .UTF_8 );
52+ var fromBufferedReader = FileStore .bufferedReader (file ).readLine ();
53+
54+ assertThat (fromInputStream , containsString ("inMemory" ));
55+ assertThat (fromInputStream , not (containsString ("onDisk" )));
56+ assertThat (fromBufferedReader , containsString ("inMemory" ));
57+ assertThat (fromBufferedReader , not (containsString ("onDisk" )));
58+ } finally {
59+ Files .deleteIfExists (file );
60+ }
61+ }
2962}
0 commit comments