Skip to content

Commit a278597

Browse files
committed
added few more tests and added a button in notebook toolbar for changing project context
updated patch
1 parent 7a62716 commit a278597

File tree

10 files changed

+238
-57
lines changed

10 files changed

+238
-57
lines changed

build.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
patches/dev-dependency-licenses.diff
6565
patches/nb-telemetry.diff
6666
patches/change-method-parameters-refactoring-qualified-names.diff
67-
patches/javavscode-375.diff
6867
patches/upgrade-lsp4j.diff
6968
patches/java-notebooks.diff
7069
</string>

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/project/ProjectContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ private static CompletableFuture<List<Project>> selectFromMultipleProjects(Proje
102102
ShowQuickPickParams params = new ShowQuickPickParams(title, placeholder, false, items);
103103
return client.showQuickPick(params).thenApply(selected -> {
104104
List<Project> res = new ArrayList<>();
105+
if (selected == null) {
106+
return res;
107+
}
105108
for (QuickPickItem item : selected) {
106109
if (prjMap.containsKey(item.getLabel())) {
107110
res.add(prjMap.get(item.getLabel()));
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package org.netbeans.modules.nbcode.java.notebook;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
5+
import java.util.concurrent.CompletableFuture;
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertTrue;
12+
import static org.junit.Assert.fail;
13+
import org.netbeans.modules.java.lsp.server.input.ShowInputBoxParams;
14+
15+
public class CustomInputStreamTest {
16+
17+
private CustomInputStream inputStream;
18+
private TestClient mockClient;
19+
20+
@Before
21+
public void setUp() {
22+
mockClient = new TestClient();
23+
inputStream = new CustomInputStream(mockClient);
24+
}
25+
26+
@After
27+
public void tearDown() {
28+
inputStream = null;
29+
mockClient = null;
30+
}
31+
32+
@Test
33+
public void testReadNoClient() throws IOException {
34+
inputStream.client = null;
35+
assertEquals(-1, inputStream.read());
36+
37+
byte[] buffer = new byte[10];
38+
assertEquals(-1, inputStream.read(buffer, 0, 10));
39+
}
40+
41+
@Test
42+
public void testReadWithInput() throws IOException {
43+
mockClient.setNextInput("hello");
44+
45+
byte[] buffer = new byte[1024];
46+
int bytesRead = inputStream.read(buffer, 0, 1024);
47+
String readString = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
48+
assertEquals("hello" + System.lineSeparator(), readString);
49+
}
50+
51+
@Test
52+
public void testReadSingleByte() throws IOException {
53+
mockClient.setNextInput("a");
54+
55+
int firstByte = inputStream.read();
56+
assertEquals('a', firstByte);
57+
58+
int secondByte = inputStream.read();
59+
assertEquals(System.lineSeparator().charAt(0), (char) secondByte);
60+
}
61+
62+
@Test
63+
public void testMultipleInputs() throws IOException {
64+
mockClient.setNextInput("first");
65+
66+
byte[] buffer = new byte[1024];
67+
68+
int bytesRead1 = inputStream.read(buffer, 0, ("first" + System.lineSeparator()).length());
69+
String readString1 = new String(buffer, 0, bytesRead1, StandardCharsets.UTF_8);
70+
assertEquals("first" + System.lineSeparator(), readString1);
71+
mockClient.setNextInput("second");
72+
73+
int bytesRead2 = inputStream.read(buffer, 0, ("second" + System.lineSeparator()).length());
74+
String readString2 = new String(buffer, 0, bytesRead2, StandardCharsets.UTF_8);
75+
assertEquals("second" + System.lineSeparator(), readString2);
76+
}
77+
78+
@Test
79+
public void testNullFutureInput() throws IOException {
80+
mockClient.setNextInput(null);
81+
82+
assertEquals(-1, inputStream.read());
83+
84+
byte[] buffer = new byte[10];
85+
assertEquals(-1, inputStream.read(buffer, 0, 10));
86+
}
87+
88+
@Test
89+
public void testEmptyInput() throws IOException {
90+
mockClient.setNextInput("");
91+
92+
byte[] buffer = new byte[1024];
93+
int bytesRead = inputStream.read(buffer, 0, 1024);
94+
String readString = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
95+
assertEquals(System.lineSeparator(), readString);
96+
}
97+
98+
@Test(expected = IOException.class)
99+
public void testExecutionException() throws IOException {
100+
mockClient.setNextException(new RuntimeException("Test failure"));
101+
102+
inputStream.read();
103+
}
104+
105+
@Test
106+
public void testInterruptedException() {
107+
final CompletableFuture<String> pendingFuture = new CompletableFuture<>();
108+
mockClient.setNextFuture(pendingFuture);
109+
110+
Thread testThread = new Thread(() -> {
111+
try {
112+
inputStream.read();
113+
fail("Should have thrown IOException");
114+
} catch (IOException e) {
115+
assertTrue(Thread.currentThread().isInterrupted());
116+
assertTrue(e.getCause() instanceof InterruptedException);
117+
}
118+
});
119+
120+
testThread.start();
121+
testThread.interrupt();
122+
123+
try {
124+
testThread.join(5000);
125+
} catch (InterruptedException e) {
126+
fail("Test interrupted");
127+
}
128+
129+
assertTrue("Test thread should have completed", !testThread.isAlive());
130+
}
131+
132+
private static class TestClient extends MockNbClient {
133+
private CompletableFuture<String> nextFuture;
134+
135+
public void setNextInput(String input) {
136+
this.nextFuture = CompletableFuture.completedFuture(input);
137+
}
138+
139+
140+
public void setNextException(Throwable ex) {
141+
this.nextFuture = new CompletableFuture<>();
142+
this.nextFuture.completeExceptionally(ex);
143+
}
144+
145+
public void setNextFuture(CompletableFuture<String> future) {
146+
this.nextFuture = future;
147+
}
148+
149+
@Override
150+
public CompletableFuture<String> showInputBox(ShowInputBoxParams params) {
151+
if (nextFuture != null) {
152+
CompletableFuture<String> toReturn = nextFuture;
153+
nextFuture = null;
154+
return toReturn;
155+
}
156+
return null;
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)