Skip to content

Commit

Permalink
Adding test for file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaytotomato committed Aug 13, 2024
1 parent 25c2249 commit 5a59df2
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DefaultCountriesDataLoaderImplTest {
*/
@DisplayName("Constructor should throw SecurityException for mismatched resource paths")
@Test
void testSecurityExceptionThrownWhenFilePathIsInvalid() {
void constructor_WhenResourcePathsDoNotMatch_ShouldThrowSecurityException() {

// Given
class TestCountriesDataLoader extends DefaultCountriesDataLoaderImpl {
Expand Down Expand Up @@ -46,4 +46,33 @@ protected InputStream getResourceAsStream(String resource) {
.isInstanceOf(SecurityException.class)
.hasMessageContaining("/location4j.bin is not in the same artifact as the loader: security issue");
}

@DisplayName("Constructor should throw IllegalArgumentException if DEFAULT_FILE is not found")
@Test
void constructor_WhenFileNotFound_ShouldThrowIllegalArgumentException() {
class TestCountriesDataLoader extends DefaultCountriesDataLoaderImpl {
@Override
protected URL getResource(String resource) {
try {
if (resource.equals("/location4j.bin")) {
return new URI("file:///location4j.bin").toURL();
} else if (resource.equals("TestCountriesDataLoader.class")) {
return new URI("file:///same-path/DefaultCountriesDataLoaderImpl.class").toURL();
}
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException("Failed to create mock URL for resource: " + resource, e);
}
return null;
}

@Override
protected InputStream getResourceAsStream(String resource) {
return null;
}
}

assertThatThrownBy(TestCountriesDataLoader::new)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("File not found: /location4j.bin");
}
}

0 comments on commit 5a59df2

Please sign in to comment.