Skip to content

Commit

Permalink
Fix NPE when configuration resource does not exist
Browse files Browse the repository at this point in the history
This prevents an NPE to be thrown in tests if a configuration file does not exist
  • Loading branch information
gastaldi committed Mar 9, 2022
1 parent f88f210 commit ac26947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -777,12 +779,16 @@ public QuarkusProdModeTest withConfigurationResource(String resourceName) {
customApplicationProperties = new Properties();
}
try {
try (InputStream in = ClassLoader.getSystemResourceAsStream(resourceName)) {
URL systemResource = ClassLoader.getSystemResource(resourceName);
if (systemResource == null) {
throw new FileNotFoundException("Resource '" + resourceName + "' not found");
}
try (InputStream in = systemResource.openStream()) {
customApplicationProperties.load(in);
}
return this;
} catch (IOException e) {
throw new RuntimeException("Could not load resource: '" + resourceName + "'");
throw new UncheckedIOException("Could not load resource: '" + resourceName + "'", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -791,12 +794,16 @@ public QuarkusUnitTest withConfigurationResource(String resourceName) {
customApplicationProperties = new Properties();
}
try {
try (InputStream in = ClassLoader.getSystemResourceAsStream(resourceName)) {
URL systemResource = ClassLoader.getSystemResource(resourceName);
if (systemResource == null) {
throw new FileNotFoundException("Resource '" + resourceName + "' not found");
}
try (InputStream in = systemResource.openStream()) {
customApplicationProperties.load(in);
}
return this;
} catch (IOException e) {
throw new RuntimeException("Could not load resource: '" + resourceName + "'");
throw new UncheckedIOException("Could not load resource: '" + resourceName + "'", e);
}
}

Expand Down

0 comments on commit ac26947

Please sign in to comment.