@@ -0,0 +1,61 @@ | |||
| 1 | + package org.utplsql.api; | ||
| 2 | + | ||
| 3 | + import org.utplsql.api.reporter.CoverageHTMLReporter; | ||
| 4 | + | ||
| 5 | + import java.io.IOException; | ||
| 6 | + import java.net.URI; | ||
| 7 | + import java.net.URISyntaxException; | ||
| 8 | + import java.nio.file.*; | ||
| 9 | + import java.util.ArrayList; | ||
| 10 | + import java.util.Collections; | ||
| 11 | + import java.util.List; | ||
| 12 | + | ||
| 13 | + /** Helper class for dealing with Resources | ||
| 14 | + * | ||
| 15 | + * @author pesse | ||
| 16 | + */ | ||
| 17 | + public class ResourceUtil { | ||
| 18 | + | ||
| 19 | + /** Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system | ||
| 20 | + * | ||
| 21 | + * @param resourceName The name of the resource | ||
| 22 | + * @return Path to the resource, either in JAR or on file system | ||
| 23 | + * @throws IOException | ||
| 24 | + * @throws URISyntaxException | ||
| 25 | + */ | ||
| 26 | + public static Path getPathToResource( String resourceName ) throws IOException, URISyntaxException { | ||
| 27 | + URI uri = CoverageHTMLReporter.class.getResource(resourceName).toURI(); | ||
| 28 | + Path myPath; | ||
| 29 | + if (uri.getScheme().equalsIgnoreCase("jar")) { | ||
| 30 | + FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap()); | ||
| 31 | + myPath = fileSystem.getPath(resourceName); | ||
| 32 | + } else { | ||
| 33 | + myPath = Paths.get(uri); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + return myPath; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path. | ||
| 40 | + * | ||
| 41 | + * @param resourceAsPath The resource to get children from | ||
| 42 | + * @param filesOnly If set to true it will only return files, not directories | ||
| 43 | + * @return List of relative Paths to the children | ||
| 44 | + * @throws IOException | ||
| 45 | + * @throws URISyntaxException | ||
| 46 | + */ | ||
| 47 | + public static List<Path> getListOfChildren( Path resourceAsPath, boolean filesOnly ) throws IOException, URISyntaxException { | ||
| 48 | + | ||
| 49 | + Path resourcePath = getPathToResource("/"+resourceAsPath.toString()); | ||
| 50 | + int relativeStartIndex = resourcePath.getNameCount()-resourceAsPath.getNameCount(); | ||
| 51 | + | ||
| 52 | + final List<Path> result = new ArrayList<>(); | ||
| 53 | + | ||
| 54 | + Files.walk(resourcePath) | ||
| 55 | + .filter(p -> !filesOnly || p.toFile().isFile()) | ||
| 56 | + .forEach( p -> result.add(p.subpath(relativeStartIndex, p.getNameCount()))); | ||
| 57 | + | ||
| 58 | + return result; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + } | ||
@@ -1,15 +1,19 @@ | |||
| 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 | } | |
@@ -56,7 +56,7 @@ public void writeReporterAssetsTo() | |||
| 56 | 56 | testFileExists(targetPath.resolve(Paths.get("loading.gif"))); | |
| 57 | 57 | testFileExists(targetPath.resolve(Paths.get("magnify.png"))); | |
| 58 | 58 | } | |
| 59 | - catch ( IOException e ) | ||
| 59 | + catch ( RuntimeException e ) | ||
| 60 | 60 | { | |
| 61 | 61 | fail(e); | |
| 62 | 62 | } | |
0 commit comments