Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0e7142

Browse files
committed
implemented tests on jar file so we can be sure extraction from jar works correctly
Switched to use @tempdir extension so that folder is cleaned automatically
1 parent e02a114 commit f0e7142

4 files changed

Lines changed: 97 additions & 151 deletions

File tree

build.gradle.kts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ version = if (tag != null && "^[0-9.]+$".toRegex().matches(tag)) tag else baseVe
1111

1212
val coverageResourcesVersion = "1.0.1"
1313
val ojdbcVersion = "12.2.0.1"
14-
val junitVersion = "5.4.1"
14+
val junitVersion = "5.4.2"
1515

1616
val deployerJars by configurations.creating
1717

@@ -70,6 +70,26 @@ tasks {
7070
}
7171
}
7272

73+
// run tests using compiled jar + dependencies and tests classes
74+
val binaryTest = create<Test>("binaryTest") {
75+
dependsOn(jar, testClasses)
76+
77+
doFirst {
78+
classpath = project.files("$buildDir/libs/java-api-$baseVersion.jar", "$buildDir/classes/java/test", configurations.testRuntimeClasspath)
79+
testClassesDirs = sourceSets.getByName("test").output.classesDirs
80+
}
81+
82+
useJUnitPlatform {
83+
includeTags("binary")
84+
}
85+
testLogging {
86+
events("passed", "skipped", "failed")
87+
exceptionFormat = TestExceptionFormat.FULL
88+
showStackTraces = true
89+
showStandardStreams = true
90+
}
91+
}
92+
7393
val intTest = create<Test>("intTest") {
7494
dependsOn(test)
7595
doFirst {
@@ -91,6 +111,7 @@ tasks {
91111
// add integration tests to the whole check
92112
named("check") {
93113
dependsOn(intTest)
114+
dependsOn(binaryTest)
94115
}
95116

96117
val coverageResourcesDirectory = "${project.buildDir}/resources/main/CoverageHTMLReporter"
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package org.utplsql.api;
22

3-
import org.utplsql.api.reporter.CoverageHTMLReporter;
4-
53
import java.io.IOException;
64
import java.net.URI;
75
import java.net.URISyntaxException;
86
import java.nio.file.*;
9-
import java.util.ArrayList;
7+
import java.nio.file.attribute.BasicFileAttributes;
108
import java.util.Collections;
11-
import java.util.List;
129

1310
/**
1411
* Helper class for dealing with Resources
@@ -21,47 +18,50 @@ private ResourceUtil() {
2118
}
2219

2320
/**
24-
* Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system
21+
* Copy directory from a jar file to the destination folder
2522
*
26-
* @param resourceName The name of the resource
27-
* @return Path to the resource, either in JAR or on file system
28-
* @throws IOException
29-
* @throws URISyntaxException
23+
* @param resourceAsPath The resource to get children from
24+
* @param targetDirectory If set to true it will only return files, not directories
3025
*/
31-
public static Path getPathToResource(String resourceName) throws IOException, URISyntaxException {
32-
URI uri = CoverageHTMLReporter.class.getResource(resourceName).toURI();
33-
Path myPath;
34-
if (uri.getScheme().equalsIgnoreCase("jar")) {
35-
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
36-
myPath = fileSystem.getPath(resourceName);
37-
} else {
38-
myPath = Paths.get(uri);
26+
public static void copyResources(Path resourceAsPath, Path targetDirectory) {
27+
String resourceName = "/" + resourceAsPath.toString();
28+
try {
29+
Files.createDirectories(targetDirectory);
30+
URI uri = ResourceUtil.class.getResource(resourceName).toURI();
31+
Path myPath;
32+
if (uri.getScheme().equalsIgnoreCase("jar")) {
33+
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
34+
myPath = fileSystem.getPath(resourceName);
35+
copyRecursive(myPath, targetDirectory);
36+
}
37+
} else {
38+
myPath = Paths.get(uri);
39+
copyRecursive(myPath, targetDirectory);
40+
}
41+
} catch (IOException | URISyntaxException e) {
42+
throw new RuntimeException(e);
3943
}
40-
41-
return myPath;
4244
}
4345

44-
/**
45-
* Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path.
46-
*
47-
* @param resourceAsPath The resource to get children from
48-
* @param filesOnly If set to true it will only return files, not directories
49-
* @return List of relative Paths to the children
50-
* @throws IOException
51-
* @throws URISyntaxException
52-
*/
53-
public static List<Path> getListOfChildren(Path resourceAsPath, boolean filesOnly) throws IOException, URISyntaxException {
54-
55-
Path resourcePath = getPathToResource("/" + resourceAsPath.toString());
56-
int relativeStartIndex = resourcePath.getNameCount() - resourceAsPath.getNameCount();
57-
58-
final List<Path> result = new ArrayList<>();
46+
private static void copyRecursive(Path from, Path targetDirectory) throws IOException {
47+
Files.walkFileTree(from, new SimpleFileVisitor<Path>() {
5948

60-
Files.walk(resourcePath)
61-
.filter(p -> !filesOnly || p.toFile().isFile())
62-
.forEach(p -> result.add(p.subpath(relativeStartIndex, p.getNameCount())));
49+
private Path currentTarget;
6350

51+
@Override
52+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
53+
super.preVisitDirectory(dir, attrs);
54+
currentTarget = targetDirectory.resolve(from.relativize(dir).toString());
55+
Files.createDirectories(currentTarget);
56+
return FileVisitResult.CONTINUE;
57+
}
6458

65-
return result;
59+
@Override
60+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
61+
super.visitFile(file, attrs);
62+
Files.copy(file, targetDirectory.resolve(from.relativize(file).toString()), StandardCopyOption.REPLACE_EXISTING);
63+
return FileVisitResult.CONTINUE;
64+
}
65+
});
6666
}
6767
}

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

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@
22

33
import org.utplsql.api.ResourceUtil;
44

5-
import java.io.IOException;
6-
import java.io.InputStream;
7-
import java.net.URISyntaxException;
8-
import java.nio.file.Files;
95
import java.nio.file.Path;
106
import java.nio.file.Paths;
11-
import java.nio.file.StandardCopyOption;
12-
import java.util.List;
13-
import java.util.function.Consumer;
147

158
public class CoverageHTMLReporter extends DefaultReporter {
169

@@ -29,46 +22,14 @@ public CoverageHTMLReporter(String selfType, Object[] attributes) {
2922
super(selfType, attributes);
3023
}
3124

32-
/**
33-
* Copies files from Classpath to a target directory.
34-
* Can omit the first x folders of the asset-path when copying to the target directory
35-
*
36-
* @param assetPath Path of the asset in the classpath
37-
* @param targetDirectory Target directory to copy the asset to
38-
* @param filterNumOfFolders Omits the first x folders of the path when copying the asset to the target directory
39-
* @throws IOException
40-
*/
41-
private static void copyFileFromClasspath(Path assetPath, Path targetDirectory, int filterNumOfFolders) throws IOException {
42-
43-
Path assetStartPath = assetPath.subpath(filterNumOfFolders, assetPath.getNameCount());
44-
Path targetAssetPath = targetDirectory.resolve(Paths.get(assetStartPath.toString()));
45-
46-
Files.createDirectories(targetAssetPath.getParent());
47-
48-
try (InputStream is = CoverageHTMLReporter.class.getClassLoader()
49-
.getResourceAsStream(assetPath.toString())
50-
) {
51-
Files.copy(is, targetAssetPath, StandardCopyOption.REPLACE_EXISTING);
52-
}
53-
}
54-
5525
/**
5626
* Write the bundled assets necessary for the HTML Coverage report to a given targetPath
5727
*
5828
* @param targetDirectory Directory where the assets should be stored
5929
* @throws RuntimeException
6030
*/
61-
public static void writeReportAssetsTo(Path targetDirectory) throws RuntimeException {
62-
63-
try {
64-
Files.createDirectories(targetDirectory);
65-
66-
List<Path> paths = ResourceUtil.getListOfChildren(Paths.get("CoverageHTMLReporter"), true);
67-
68-
paths.forEach((ThrowingConsumer<Path>) p -> copyFileFromClasspath(p, targetDirectory, 1));
69-
} catch (IOException | URISyntaxException e) {
70-
throw new RuntimeException(e);
71-
}
31+
static void writeReportAssetsTo(Path targetDirectory) throws RuntimeException {
32+
ResourceUtil.copyResources(Paths.get("CoverageHTMLReporter"), targetDirectory);
7233
}
7334

7435
@Override
@@ -116,23 +77,4 @@ public void setAssetsPath(String assetsPath) {
11677
this.assetsPath = assetsPath;
11778
}
11879

119-
/**
120-
* Functional Interface just to throw Exception from Consumer
121-
*
122-
* @param <T>
123-
*/
124-
@FunctionalInterface
125-
public interface ThrowingConsumer<T> extends Consumer<T> {
126-
127-
@Override
128-
default void accept(final T elem) {
129-
try {
130-
acceptThrows(elem);
131-
} catch (final Exception e) {
132-
throw new RuntimeException(e);
133-
}
134-
}
135-
136-
void acceptThrows(T t) throws IOException;
137-
}
13880
}
Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,63 @@
11
package org.utplsql.api.reporter;
22

3-
import org.junit.jupiter.api.AfterAll;
3+
import org.junit.jupiter.api.Tag;
44
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.io.TempDir;
56

67
import java.io.File;
7-
import java.io.IOException;
8-
import java.nio.file.*;
9-
import java.nio.file.attribute.BasicFileAttributes;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
1010

1111
import static org.junit.jupiter.api.Assertions.assertTrue;
1212

13+
@Tag("binary")
1314
class CoverageHTMLReporterAssetTest {
1415

1516
private static final String TEST_FOLDER = "__testAssets";
1617

17-
@AfterAll
18-
static void clearTestAssetsFolder() {
19-
try {
20-
Files.walkFileTree(Paths.get(TEST_FOLDER), new SimpleFileVisitor<Path>() {
21-
@Override
22-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
23-
Files.delete(file);
24-
return FileVisitResult.CONTINUE;
25-
}
26-
27-
@Override
28-
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
29-
Files.delete(dir);
30-
return FileVisitResult.CONTINUE;
31-
}
32-
});
33-
} catch (IOException e) {
34-
e.printStackTrace();
35-
}
36-
}
18+
@TempDir
19+
Path tempDir;
3720

3821
private void testFileExists(Path filePath) {
39-
File f = new File(filePath.toUri());
22+
File f = new File(tempDir.resolve(TEST_FOLDER).resolve(filePath).toUri());
4023

4124
assertTrue(f.exists(), () -> "File " + f.toString() + " does not exist");
4225
}
4326

4427
@Test
4528
void writeReporterAssetsTo() throws RuntimeException {
4629

47-
Path targetPath = Paths.get(TEST_FOLDER);
30+
Path targetPath = tempDir.resolve(TEST_FOLDER);
4831

4932
// Act
5033
CoverageHTMLReporter.writeReportAssetsTo(targetPath);
5134

52-
testFileExists(targetPath.resolve(Paths.get("colorbox", "border.png")));
53-
testFileExists(targetPath.resolve(Paths.get("colorbox", "controls.png")));
54-
testFileExists(targetPath.resolve(Paths.get("colorbox", "loading.gif")));
55-
testFileExists(targetPath.resolve(Paths.get("colorbox", "loading_background.png")));
56-
57-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_flat_0_aaaaaa_40x100.png")));
58-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_flat_75_ffffff_40x100.png")));
59-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_55_fbf9ee_1x400.png")));
60-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_65_ffffff_1x400.png")));
61-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_75_dadada_1x400.png")));
62-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_75_e6e6e6_1x400.png")));
63-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_95_fef1ec_1x400.png")));
64-
testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_highlight-soft_75_cccccc_1x100.png")));
65-
testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_2e83ff_256x240.png")));
66-
testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_222222_256x240.png")));
67-
testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_454545_256x240.png")));
68-
testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_888888_256x240.png")));
69-
testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_cd0a0a_256x240.png")));
70-
71-
testFileExists(targetPath.resolve(Paths.get("application.css")));
72-
testFileExists(targetPath.resolve(Paths.get("application.js")));
73-
testFileExists(targetPath.resolve(Paths.get("favicon_green.png")));
74-
testFileExists(targetPath.resolve(Paths.get("favicon_red.png")));
75-
testFileExists(targetPath.resolve(Paths.get("favicon_yellow.png")));
76-
testFileExists(targetPath.resolve(Paths.get("loading.gif")));
77-
testFileExists(targetPath.resolve(Paths.get("magnify.png")));
35+
testFileExists(Paths.get("colorbox", "border.png"));
36+
testFileExists(Paths.get("colorbox", "controls.png"));
37+
testFileExists(Paths.get("colorbox", "loading.gif"));
38+
testFileExists(Paths.get("colorbox", "loading_background.png"));
39+
40+
testFileExists(Paths.get("images", "ui-bg_flat_0_aaaaaa_40x100.png"));
41+
testFileExists(Paths.get("images", "ui-bg_flat_75_ffffff_40x100.png"));
42+
testFileExists(Paths.get("images", "ui-bg_glass_55_fbf9ee_1x400.png"));
43+
testFileExists(Paths.get("images", "ui-bg_glass_65_ffffff_1x400.png"));
44+
testFileExists(Paths.get("images", "ui-bg_glass_75_dadada_1x400.png"));
45+
testFileExists(Paths.get("images", "ui-bg_glass_75_e6e6e6_1x400.png"));
46+
testFileExists(Paths.get("images", "ui-bg_glass_95_fef1ec_1x400.png"));
47+
testFileExists(Paths.get("images", "ui-bg_highlight-soft_75_cccccc_1x100.png"));
48+
testFileExists(Paths.get("images", "ui-icons_2e83ff_256x240.png"));
49+
testFileExists(Paths.get("images", "ui-icons_222222_256x240.png"));
50+
testFileExists(Paths.get("images", "ui-icons_454545_256x240.png"));
51+
testFileExists(Paths.get("images", "ui-icons_888888_256x240.png"));
52+
testFileExists(Paths.get("images", "ui-icons_cd0a0a_256x240.png"));
53+
54+
testFileExists(Paths.get("application.css"));
55+
testFileExists(Paths.get("application.js"));
56+
testFileExists(Paths.get("favicon_green.png"));
57+
testFileExists(Paths.get("favicon_red.png"));
58+
testFileExists(Paths.get("favicon_yellow.png"));
59+
testFileExists(Paths.get("loading.gif"));
60+
testFileExists(Paths.get("magnify.png"));
7861

7962
}
8063
}

0 commit comments

Comments
 (0)