Skip to content

Commit 3330528

Browse files
committed
Refactor copy-method to be much more elegant, now
1 parent b3c635c commit 3330528

File tree

3 files changed

+112
-39
lines changed

3 files changed

+112
-39
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package org.utplsql.api.reporter;
22

33
import org.utplsql.api.CustomTypes;
4+
import org.utplsql.api.ResourceUtil;
45

56
import java.io.IOException;
67
import java.io.InputStream;
8+
import java.net.URISyntaxException;
79
import java.nio.file.Files;
810
import java.nio.file.Path;
911
import java.nio.file.Paths;
1012
import java.sql.SQLException;
1113
import java.sql.SQLInput;
1214
import java.sql.SQLOutput;
15+
import java.util.List;
16+
import java.util.function.Consumer;
1317

1418
public class CoverageHTMLReporter extends Reporter {
1519

@@ -64,55 +68,63 @@ public void writeSQL(SQLOutput stream) throws SQLException {
6468
stream.writeString(getAssetsPath());
6569
}
6670

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());
6985

7086
try (InputStream is = CoverageHTMLReporter.class.getClassLoader()
71-
.getResourceAsStream(
72-
Paths.get("CoverageHTMLReporter").resolve(assetPath).toString()
73-
)
87+
.getResourceAsStream(assetPath.toString())
7488
) {
75-
Files.copy( is, targetDirectory.resolve(assetPath) );
89+
Files.copy( is, targetAssetPath );
7690
}
7791
}
7892

7993
/** Write the bundled assets necessary for the HTML Coverage report to a given targetPath
8094
*
8195
* @param targetDirectory Directory where the assets should be stored
82-
* @throws IOException
96+
* @throws RuntimeException
8397
*/
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);
115104

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+
}
116127

128+
void acceptThrows( T t ) throws IOException;
117129
}
118130
}

src/test/java/org/utplsql/api/reporter/CoverageHTMLReporterAssetTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void writeReporterAssetsTo()
5656
testFileExists(targetPath.resolve(Paths.get("loading.gif")));
5757
testFileExists(targetPath.resolve(Paths.get("magnify.png")));
5858
}
59-
catch ( IOException e )
59+
catch ( RuntimeException e )
6060
{
6161
fail(e);
6262
}

0 commit comments

Comments
 (0)