Skip to content

Commit 92d6cb5

Browse files
committed
made tests for UpdateCheckTest.java
1 parent 2dbe7b3 commit 92d6cb5

File tree

2 files changed

+153
-2
lines changed

2 files changed

+153
-2
lines changed

app/src/processing/app/UpdateCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected boolean promptToOpenContributionManager() {
204204
*/
205205

206206

207-
protected int readInt(String filename) throws IOException {
207+
protected static int readInt(String filename) throws IOException {
208208
URL url = new URL(filename);
209209
// try-with-resources auto closes things of type "Closeable" even the code throws an error
210210
try(InputStream stream = url.openStream();
Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,155 @@
11
package processing.app;
2+
import org.junit.jupiter.api.*;
3+
import org.junit.jupiter.api.io.TempDir;
4+
import org.mockito.MockedConstruction;
5+
import org.mockito.Mockito;
26

3-
public class UpdateCheckTest {
7+
import java.io.*;
8+
import java.net.*;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.*;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
import static org.mockito.Mockito.*;
14+
15+
class StreamClosedTest {
16+
17+
@TempDir
18+
Path tempDir;
19+
20+
// Helper: write content to a temp file and return its URL string
21+
private String createTempFile(String content) throws IOException {
22+
Path file = tempDir.resolve("test.txt");
23+
Files.writeString(file, content, StandardCharsets.UTF_8);
24+
return file.toUri().toString();
25+
}
26+
27+
// --- Happy path ---
28+
29+
@Test
30+
void readInt_simpleInteger_returnsCorrectValue() throws IOException {
31+
String url = createTempFile("42\n");
32+
assertEquals(42, UpdateCheck.readInt(url));
33+
}
34+
35+
@Test
36+
void readInt_negativeInteger_returnsCorrectValue() throws IOException {
37+
String url = createTempFile("-7\n");
38+
assertEquals(-7, UpdateCheck.readInt(url));
39+
}
40+
41+
@Test
42+
void readInt_integerWithLeadingAndTrailingWhitespace_returnsCorrectValue() throws IOException {
43+
String url = createTempFile(" 100 \n");
44+
assertEquals(100, UpdateCheck.readInt(url));
45+
}
46+
47+
@Test
48+
void readInt_integerWithNoNewline_returnsCorrectValue() throws IOException {
49+
String url = createTempFile("99");
50+
assertEquals(99, UpdateCheck.readInt(url));
51+
}
52+
53+
@Test
54+
void readInt_maxInt_returnsCorrectValue() throws IOException {
55+
String url = createTempFile(String.valueOf(Integer.MAX_VALUE));
56+
assertEquals(Integer.MAX_VALUE, UpdateCheck.readInt(url));
57+
}
58+
59+
@Test
60+
void readInt_minInt_returnsCorrectValue() throws IOException {
61+
String url = createTempFile(String.valueOf(Integer.MIN_VALUE));
62+
assertEquals(Integer.MIN_VALUE, UpdateCheck.readInt(url));
63+
}
64+
65+
@Test
66+
void readInt_integerWithMultipleLines_readsOnlyFirstLine() throws IOException {
67+
String url = createTempFile("5\n10\n15");
68+
assertEquals(5, UpdateCheck.readInt(url));
69+
}
70+
71+
// --- Error cases ---
72+
73+
@Test
74+
void readInt_nonNumericContent_throwsNumberFormatException() throws IOException {
75+
String url = createTempFile("not-a-number\n");
76+
assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url));
77+
}
78+
79+
@Test
80+
void readInt_emptyFile_throwsNullPointerException() throws IOException {
81+
String url = createTempFile("");
82+
// readLine() returns null on empty stream → trim() throws NPE
83+
assertThrows(NullPointerException.class, () -> UpdateCheck.readInt(url));
84+
}
85+
86+
@Test
87+
void readInt_blankLine_throwsNumberFormatException() throws IOException {
88+
String url = createTempFile(" \n");
89+
assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url));
90+
}
91+
92+
@Test
93+
void readInt_floatValue_throwsNumberFormatException() throws IOException {
94+
String url = createTempFile("3.14\n");
95+
assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url));
96+
}
97+
98+
@Test
99+
void readInt_overflowValue_throwsNumberFormatException() throws IOException {
100+
String url = createTempFile("99999999999999\n");
101+
assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url));
102+
}
103+
104+
@Test
105+
void readInt_invalidUrl_throwsMalformedURLException() {
106+
assertThrows(MalformedURLException.class,
107+
() -> UpdateCheck.readInt("not-a-valid-url"));
108+
}
109+
110+
@Test
111+
void readInt_nonExistentFile_throwsIOException() {
112+
String nonExistent = tempDir.resolve("ghost.txt").toUri().toString();
113+
assertThrows(IOException.class, () -> UpdateCheck.readInt(nonExistent));
114+
}
115+
116+
// --- Stream is closed after use ---
117+
118+
@Test
119+
void readInt_streamIsClosedAfterSuccessfulRead() throws IOException {
120+
// Spy on the InputStream to verify close() is called
121+
Path file = tempDir.resolve("close_test.txt");
122+
Files.writeString(file, "7", StandardCharsets.UTF_8);
123+
124+
URL url = file.toUri().toURL();
125+
InputStream realStream = url.openStream();
126+
InputStream spyStream = spy(realStream);
127+
128+
try (MockedConstruction<URL> mockedUrl = mockConstruction(URL.class,
129+
(mock, ctx) -> when(mock.openStream()).thenReturn(spyStream))) {
130+
131+
UpdateCheck.readInt(file.toUri().toString());
132+
}
133+
134+
verify(spyStream, atLeastOnce()).close();
135+
}
136+
137+
@Test
138+
void readInt_streamIsClosedEvenWhenParseThrows() throws IOException {
139+
Path file = tempDir.resolve("bad_close_test.txt");
140+
Files.writeString(file, "not-a-number", StandardCharsets.UTF_8);
141+
142+
URL url = file.toUri().toURL();
143+
InputStream realStream = url.openStream();
144+
InputStream spyStream = spy(realStream);
145+
146+
try (MockedConstruction<URL> mockedUrl = mockConstruction(URL.class,
147+
(mock, ctx) -> when(mock.openStream()).thenReturn(spyStream))) {
148+
149+
assertThrows(NumberFormatException.class,
150+
() -> UpdateCheck.readInt(file.toUri().toString()));
151+
}
152+
153+
verify(spyStream, atLeastOnce()).close();
154+
}
4155
}

0 commit comments

Comments
 (0)