|
1 | 1 | package org.utplsql.api.reporter;
|
2 | 2 |
|
3 | 3 | import org.utplsql.api.CustomTypes;
|
| 4 | +import org.utplsql.api.ResourceUtil; |
4 | 5 |
|
5 | 6 | import java.io.IOException;
|
6 | 7 | import java.io.InputStream;
|
| 8 | +import java.net.URISyntaxException; |
7 | 9 | import java.nio.file.Files;
|
8 | 10 | import java.nio.file.Path;
|
9 | 11 | import java.nio.file.Paths;
|
10 | 12 | import java.sql.SQLException;
|
11 | 13 | import java.sql.SQLInput;
|
12 | 14 | import java.sql.SQLOutput;
|
| 15 | +import java.util.List; |
| 16 | +import java.util.function.Consumer; |
13 | 17 |
|
14 | 18 | public class CoverageHTMLReporter extends Reporter {
|
15 | 19 |
|
@@ -64,55 +68,63 @@ public void writeSQL(SQLOutput stream) throws SQLException {
|
64 | 68 | stream.writeString(getAssetsPath());
|
65 | 69 | }
|
66 | 70 |
|
67 |
| - private static void copyFileFromClasspath( Path assetPath, Path targetDirectory ) throws IOException { |
68 |
| - Files.createDirectories(targetDirectory.resolve(assetPath).getParent()); |
| 71 | + /** Copies files from Classpath to a target directory. |
| 72 | + * Can omit the first x folders of the asset-path when copying to the target directory |
| 73 | + * |
| 74 | + * @param assetPath Path of the asset in the classpath |
| 75 | + * @param targetDirectory Target directory to copy the asset to |
| 76 | + * @param filterNumOfFolders Omits the first x folders of the path when copying the asset to the target directory |
| 77 | + * @throws IOException |
| 78 | + */ |
| 79 | + private static void copyFileFromClasspath( Path assetPath, Path targetDirectory, int filterNumOfFolders ) throws IOException { |
| 80 | + |
| 81 | + Path assetStartPath = assetPath.subpath(filterNumOfFolders, assetPath.getNameCount()); |
| 82 | + Path targetAssetPath = targetDirectory.resolve(assetStartPath); |
| 83 | + |
| 84 | + Files.createDirectories(targetAssetPath.getParent()); |
69 | 85 |
|
70 | 86 | try (InputStream is = CoverageHTMLReporter.class.getClassLoader()
|
71 |
| - .getResourceAsStream( |
72 |
| - Paths.get("CoverageHTMLReporter").resolve(assetPath).toString() |
73 |
| - ) |
| 87 | + .getResourceAsStream(assetPath.toString()) |
74 | 88 | ) {
|
75 |
| - Files.copy( is, targetDirectory.resolve(assetPath) ); |
| 89 | + Files.copy( is, targetAssetPath ); |
76 | 90 | }
|
77 | 91 | }
|
78 | 92 |
|
79 | 93 | /** Write the bundled assets necessary for the HTML Coverage report to a given targetPath
|
80 | 94 | *
|
81 | 95 | * @param targetDirectory Directory where the assets should be stored
|
82 |
| - * @throws IOException |
| 96 | + * @throws RuntimeException |
83 | 97 | */
|
84 |
| - public static void writeReportAssetsTo(Path targetDirectory) throws IOException { |
85 |
| - |
86 |
| - Files.createDirectories(targetDirectory); |
87 |
| - |
88 |
| - // Simplest approach to start with |
89 |
| - copyFileFromClasspath(Paths.get("application.css"), targetDirectory); |
90 |
| - copyFileFromClasspath(Paths.get("application.js"), targetDirectory); |
91 |
| - copyFileFromClasspath(Paths.get("favicon_green.png"), targetDirectory); |
92 |
| - copyFileFromClasspath(Paths.get("favicon_red.png"), targetDirectory); |
93 |
| - copyFileFromClasspath(Paths.get("favicon_yellow.png"), targetDirectory); |
94 |
| - copyFileFromClasspath(Paths.get("loading.gif"), targetDirectory); |
95 |
| - copyFileFromClasspath(Paths.get("magnify.png"), targetDirectory); |
96 |
| - |
97 |
| - copyFileFromClasspath(Paths.get("colorbox", "border.png"), targetDirectory); |
98 |
| - copyFileFromClasspath(Paths.get("colorbox", "controls.png"), targetDirectory); |
99 |
| - copyFileFromClasspath(Paths.get("colorbox", "loading.gif"), targetDirectory); |
100 |
| - copyFileFromClasspath(Paths.get("colorbox", "loading_background.png"), targetDirectory); |
101 |
| - |
102 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_flat_0_aaaaaa_40x100.png"), targetDirectory); |
103 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_flat_75_ffffff_40x100.png"), targetDirectory); |
104 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_glass_55_fbf9ee_1x400.png"), targetDirectory); |
105 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_glass_65_ffffff_1x400.png"), targetDirectory); |
106 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_glass_75_dadada_1x400.png"), targetDirectory); |
107 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_glass_75_e6e6e6_1x400.png"), targetDirectory); |
108 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_glass_95_fef1ec_1x400.png"), targetDirectory); |
109 |
| - copyFileFromClasspath(Paths.get("images", "ui-bg_highlight-soft_75_cccccc_1x100.png"), targetDirectory); |
110 |
| - copyFileFromClasspath(Paths.get("images", "ui-icons_2e83ff_256x240.png"), targetDirectory); |
111 |
| - copyFileFromClasspath(Paths.get("images", "ui-icons_222222_256x240.png"), targetDirectory); |
112 |
| - copyFileFromClasspath(Paths.get("images", "ui-icons_454545_256x240.png"), targetDirectory); |
113 |
| - copyFileFromClasspath(Paths.get("images", "ui-icons_888888_256x240.png"), targetDirectory); |
114 |
| - copyFileFromClasspath(Paths.get("images", "ui-icons_cd0a0a_256x240.png"), targetDirectory); |
| 98 | + public static void writeReportAssetsTo(Path targetDirectory) throws RuntimeException { |
| 99 | + |
| 100 | + try { |
| 101 | + Files.createDirectories(targetDirectory); |
| 102 | + |
| 103 | + List<Path> paths = ResourceUtil.getListOfChildren(Paths.get("CoverageHTMLReporter"), true); |
115 | 104 |
|
| 105 | + paths.forEach((ThrowingConsumer<Path>) p -> copyFileFromClasspath(p, targetDirectory, 1) ); |
| 106 | + } |
| 107 | + catch ( IOException | URISyntaxException e ) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** Functional Interface just to throw Exception from Consumer |
| 113 | + * |
| 114 | + * @param <T> |
| 115 | + */ |
| 116 | + @FunctionalInterface |
| 117 | + public interface ThrowingConsumer<T> extends Consumer<T> { |
| 118 | + |
| 119 | + @Override |
| 120 | + default void accept(final T elem) { |
| 121 | + try { |
| 122 | + acceptThrows(elem); |
| 123 | + } catch (final Exception e) { |
| 124 | + throw new RuntimeException(e); |
| 125 | + } |
| 126 | + } |
116 | 127 |
|
| 128 | + void acceptThrows( T t ) throws IOException; |
117 | 129 | }
|
118 | 130 | }
|
0 commit comments