|
| 1 | +package aquality.selenium.utils; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.UncheckedIOException; |
| 7 | +import java.net.URL; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.util.Objects; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +class ResourceFile { |
| 13 | + private final String resourceName; |
| 14 | + private final String fileCanonicalPath; |
| 15 | + private final String fileContent; |
| 16 | + |
| 17 | + ResourceFile(String resourceName){ |
| 18 | + this.resourceName = resourceName; |
| 19 | + fileCanonicalPath = getResourcePath(resourceName); |
| 20 | + fileContent = getResourceFileContent(resourceName); |
| 21 | + } |
| 22 | + |
| 23 | + String getResourceFileContent(final String resourceName) { |
| 24 | + InputStreamReader inputStream = new InputStreamReader(Objects.requireNonNull(JsonFile.class.getClassLoader().getResourceAsStream(resourceName)), StandardCharsets.UTF_8); |
| 25 | + try (BufferedReader br = new BufferedReader(inputStream)) { |
| 26 | + return br.lines().collect(Collectors.joining(System.lineSeparator())); |
| 27 | + } catch (IOException e) { |
| 28 | + throw new UncheckedIOException(String.format("Reading of resource file '%1$s' was failed", resourceName), e); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + static String getResourcePath(final String resourceName) { |
| 33 | + try{ |
| 34 | + URL resourceURL = JsonFile.class.getClassLoader().getResource(resourceName); |
| 35 | + return Objects.requireNonNull(resourceURL).getPath(); |
| 36 | + }catch (NullPointerException e){ |
| 37 | + throw new IllegalArgumentException(String.format("Resource file %1$s was not found or cannot be loaded", resourceName), e); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public String getResourceName() { |
| 42 | + return resourceName; |
| 43 | + } |
| 44 | + |
| 45 | + public String getFileCanonicalPath() { |
| 46 | + return fileCanonicalPath; |
| 47 | + } |
| 48 | + |
| 49 | + public String getFileContent() { |
| 50 | + return fileContent; |
| 51 | + } |
| 52 | +} |
0 commit comments