-
-
Notifications
You must be signed in to change notification settings - Fork 159
Updated readInt method so streams are closed on failure or success #1476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srnkan
wants to merge
2
commits into
processing:main
Choose a base branch
from
srnkan:ClosingStreamsIssue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,9 @@ sourceSets{ | |
| kotlin{ | ||
| srcDirs("test") | ||
| } | ||
| java{ | ||
| srcDirs("test") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package processing.app; | ||
| import org.junit.jupiter.api.*; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.mockito.MockedConstruction; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.io.*; | ||
| import java.net.*; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.*; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| class UpdateCheckTest { | ||
|
|
||
| @TempDir | ||
| Path tempDir; | ||
|
|
||
| // Helper: write content to a temp file and return its URL string | ||
| private String createTempFile(String content) throws IOException { | ||
| Path file = tempDir.resolve("test.txt"); | ||
| Files.writeString(file, content, StandardCharsets.UTF_8); | ||
| return file.toUri().toString(); | ||
| } | ||
|
|
||
|
|
||
| // tests to show that the method returns what it should | ||
| @Test | ||
| void readInt_simpleInteger_returnsCorrectValue() throws IOException { | ||
| String url = createTempFile("42\n"); | ||
| assertEquals(42, UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_negativeInteger_returnsCorrectValue() throws IOException { | ||
| String url = createTempFile("-7\n"); | ||
| assertEquals(-7, UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_integerWithLeadingAndTrailingWhitespace_returnsCorrectValue() throws IOException { | ||
| String url = createTempFile(" 100 \n"); | ||
| assertEquals(100, UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_integerWithNoNewline_returnsCorrectValue() throws IOException { | ||
| String url = createTempFile("99"); | ||
| assertEquals(99, UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_maxInt_returnsCorrectValue() throws IOException { | ||
| String url = createTempFile(String.valueOf(Integer.MAX_VALUE)); | ||
| assertEquals(Integer.MAX_VALUE, UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_minInt_returnsCorrectValue() throws IOException { | ||
| String url = createTempFile(String.valueOf(Integer.MIN_VALUE)); | ||
| assertEquals(Integer.MIN_VALUE, UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_integerWithMultipleLines_readsOnlyFirstLine() throws IOException { | ||
| String url = createTempFile("5\n10\n15"); | ||
| assertEquals(5, UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| // checks for if errors are correctly reported | ||
| @Test | ||
| void readInt_nonNumericContent_throwsNumberFormatException() throws IOException { | ||
| String url = createTempFile("not-a-number\n"); | ||
| assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_emptyFile_throwsNullPointerException() throws IOException { | ||
| String url = createTempFile(""); | ||
| // readLine() returns null on empty stream → trim() throws NPE | ||
| assertThrows(Exception.class, () -> UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_blankLine_throwsNumberFormatException() throws IOException { | ||
| String url = createTempFile(" \n"); | ||
| assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_floatValue_throwsNumberFormatException() throws IOException { | ||
| String url = createTempFile("3.14\n"); | ||
| assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_overflowValue_throwsNumberFormatException() throws IOException { | ||
| String url = createTempFile("99999999999999\n"); | ||
| assertThrows(NumberFormatException.class, () -> UpdateCheck.readInt(url)); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_invalidUrl_throwsMalformedURLException() { | ||
| assertThrows(MalformedURLException.class, | ||
| () -> UpdateCheck.readInt("not-a-valid-url")); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_nonExistentFile_throwsIOException() { | ||
| String nonExistent = tempDir.resolve("ghost.txt").toUri().toString(); | ||
| assertThrows(IOException.class, () -> UpdateCheck.readInt(nonExistent)); | ||
| } | ||
|
|
||
| // checks for if streams are closed | ||
| @Test | ||
| void readInt_streamIsClosedAfterSuccessfulRead() throws IOException { | ||
| // Spy on the InputStream to verify close() is called | ||
| Path file = tempDir.resolve("close_test.txt"); | ||
| Files.writeString(file, "7", StandardCharsets.UTF_8); | ||
|
|
||
| URL url = file.toUri().toURL(); | ||
| InputStream realStream = url.openStream(); | ||
| InputStream spyStream = spy(realStream); | ||
|
|
||
| try (MockedConstruction<URL> mockedUrl = mockConstruction(URL.class, | ||
| (mock, ctx) -> when(mock.openStream()).thenReturn(spyStream))) { | ||
|
|
||
| UpdateCheck.readInt(file.toUri().toString()); | ||
| } | ||
|
|
||
| verify(spyStream, atLeastOnce()).close(); | ||
| } | ||
|
|
||
| @Test | ||
| void readInt_streamIsClosedEvenWhenParseThrows() throws IOException { | ||
| Path file = tempDir.resolve("bad_close_test.txt"); | ||
| Files.writeString(file, "not-a-number", StandardCharsets.UTF_8); | ||
|
|
||
| URL url = file.toUri().toURL(); | ||
| InputStream realStream = url.openStream(); | ||
| InputStream spyStream = spy(realStream); | ||
|
|
||
| try (MockedConstruction<URL> mockedUrl = mockConstruction(URL.class, | ||
| (mock, ctx) -> when(mock.openStream()).thenReturn(spyStream))) { | ||
|
|
||
| assertThrows(NumberFormatException.class, | ||
| () -> UpdateCheck.readInt(file.toUri().toString())); | ||
| } | ||
|
|
||
| verify(spyStream, atLeastOnce()).close(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is really cool! good use of try-with-resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added tests to the UpdateCheckTest.java file, with 16 tests. Also, Mark and I noticed that in the build.gradle.kts file under the app build, only kotlin tests are ran. So, I modified that file to include java tests.