From 3909b1b2e1112337132c9868122c005eafd16031 Mon Sep 17 00:00:00 2001 From: Pazus Date: Tue, 30 Apr 2019 09:48:47 +0300 Subject: [PATCH 1/4] Fix compatibility with java 9+ --- gradle/wrapper/gradle-wrapper.properties | 2 +- .../java/org/utplsql/api/ResourceUtil.java | 26 +++---------------- .../org/utplsql/api/reporter/Reporter.java | 14 +++++++++- .../org/utplsql/api/OptionalFeaturesIT.java | 1 - 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 51fb1c4..ee69dd6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/org/utplsql/api/ResourceUtil.java b/src/main/java/org/utplsql/api/ResourceUtil.java index 9f6c225..15b65ab 100644 --- a/src/main/java/org/utplsql/api/ResourceUtil.java +++ b/src/main/java/org/utplsql/api/ResourceUtil.java @@ -1,6 +1,5 @@ package org.utplsql.api; -import com.sun.nio.zipfs.ZipPath; import org.utplsql.api.reporter.CoverageHTMLReporter; import java.io.IOException; @@ -9,10 +8,7 @@ import java.nio.file.*; import java.util.ArrayList; import java.util.Collections; -import java.util.Enumeration; import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; /** * Helper class for dealing with Resources @@ -61,26 +57,10 @@ public static List getListOfChildren(Path resourceAsPath, boolean filesOnl final List result = new ArrayList<>(); - if (resourcePath instanceof ZipPath) { - try (ZipFile zf = new ZipFile(resourcePath.getFileSystem().toString())) { + Files.walk(resourcePath) + .filter(p -> !filesOnly || p.toFile().isFile()) + .forEach(p -> result.add(p.subpath(relativeStartIndex, p.getNameCount()))); - for (Enumeration list = zf.entries(); list.hasMoreElements(); ) { - ZipEntry entry = (ZipEntry) list.nextElement(); - // Get entry-path with root element so we can compare it - Path entryPath = resourcePath.getRoot().resolve(resourcePath.getFileSystem().getPath(entry.toString())); - - if (entryPath.startsWith(resourcePath) && (!filesOnly || !entry.isDirectory())) { - result.add(entryPath.subpath(relativeStartIndex, entryPath.getNameCount())); - } - } - } - resourcePath.getFileSystem().close(); - } else { - Files.walk(resourcePath) - .filter(p -> !filesOnly || p.toFile().isFile()) - .forEach(p -> result.add(p.subpath(relativeStartIndex, p.getNameCount()))); - - } return result; } diff --git a/src/main/java/org/utplsql/api/reporter/Reporter.java b/src/main/java/org/utplsql/api/reporter/Reporter.java index 6edd853..c905464 100644 --- a/src/main/java/org/utplsql/api/reporter/Reporter.java +++ b/src/main/java/org/utplsql/api/reporter/Reporter.java @@ -10,7 +10,6 @@ import org.utplsql.api.compatibility.CompatibilityProxy; import org.utplsql.api.outputBuffer.OutputBuffer; -import javax.xml.bind.DatatypeConverter; import java.sql.Connection; import java.sql.SQLException; @@ -114,4 +113,17 @@ public Datum toDatum(Connection c) throws SQLException { public OutputBuffer getOutputBuffer() { return outputBuffer; } + + private static class DatatypeConverter { + private static final char[] hexCode = "0123456789ABCDEF".toCharArray(); + + static String printHexBinary(byte[] data) { + StringBuilder r = new StringBuilder(data.length * 2); + for (byte b : data) { + r.append(hexCode[(b >> 4) & 0xF]); + r.append(hexCode[(b & 0xF)]); + } + return r.toString(); + } + } } diff --git a/src/test/java/org/utplsql/api/OptionalFeaturesIT.java b/src/test/java/org/utplsql/api/OptionalFeaturesIT.java index f2d8fa9..50118b0 100644 --- a/src/test/java/org/utplsql/api/OptionalFeaturesIT.java +++ b/src/test/java/org/utplsql/api/OptionalFeaturesIT.java @@ -1,7 +1,6 @@ package org.utplsql.api; import org.junit.jupiter.api.Test; -import org.omg.CORBA.DynAnyPackage.Invalid; import org.utplsql.api.compatibility.CompatibilityProxy; import org.utplsql.api.compatibility.OptionalFeatures; import org.utplsql.api.exception.InvalidVersionException; From 501b8da742a8f455efb2a88a289d97b317688711 Mon Sep 17 00:00:00 2001 From: Pazus Date: Tue, 30 Apr 2019 10:42:10 +0300 Subject: [PATCH 2/4] Update .travis.yml --- .travis.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 38f297b..474cdb6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -sudo: required +It’s sudo: required language: java services: @@ -32,6 +32,17 @@ env: - UTPLSQL_VERSION="develop" UTPLSQL_FILE="utPLSQL" +matrix: + include: + - env: UTPLSQL_VERSION="v3.1.6" + jdk: openjdk9 + - env: UTPLSQL_VERSION="v3.1.6" + jdk: openjdk10 + - env: UTPLSQL_VERSION="v3.1.6" + jdk: openjdk11 + - env: UTPLSQL_VERSION="v3.1.6" + jdk: openjdk12 + before_cache: - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ From e02a114b0f96a425ef18cbba26ee4441010687ea Mon Sep 17 00:00:00 2001 From: Pazus Date: Wed, 1 May 2019 08:22:52 +0300 Subject: [PATCH 3/4] fix type and remove sudo requirement as it seems it's not needed at all --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 474cdb6..186febe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ -It’s sudo: required language: java services: From f0e71421310cb00ab3c959d6611e0ab9dddc6a24 Mon Sep 17 00:00:00 2001 From: Pazus Date: Sun, 5 May 2019 09:27:21 +0300 Subject: [PATCH 4/4] 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 --- build.gradle.kts | 23 ++++- .../java/org/utplsql/api/ResourceUtil.java | 76 ++++++++-------- .../api/reporter/CoverageHTMLReporter.java | 62 +------------ .../CoverageHTMLReporterAssetTest.java | 87 ++++++++----------- 4 files changed, 97 insertions(+), 151 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 040014a..64c1534 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ version = if (tag != null && "^[0-9.]+$".toRegex().matches(tag)) tag else baseVe val coverageResourcesVersion = "1.0.1" val ojdbcVersion = "12.2.0.1" -val junitVersion = "5.4.1" +val junitVersion = "5.4.2" val deployerJars by configurations.creating @@ -70,6 +70,26 @@ tasks { } } + // run tests using compiled jar + dependencies and tests classes + val binaryTest = create("binaryTest") { + dependsOn(jar, testClasses) + + doFirst { + classpath = project.files("$buildDir/libs/java-api-$baseVersion.jar", "$buildDir/classes/java/test", configurations.testRuntimeClasspath) + testClassesDirs = sourceSets.getByName("test").output.classesDirs + } + + useJUnitPlatform { + includeTags("binary") + } + testLogging { + events("passed", "skipped", "failed") + exceptionFormat = TestExceptionFormat.FULL + showStackTraces = true + showStandardStreams = true + } + } + val intTest = create("intTest") { dependsOn(test) doFirst { @@ -91,6 +111,7 @@ tasks { // add integration tests to the whole check named("check") { dependsOn(intTest) + dependsOn(binaryTest) } val coverageResourcesDirectory = "${project.buildDir}/resources/main/CoverageHTMLReporter" diff --git a/src/main/java/org/utplsql/api/ResourceUtil.java b/src/main/java/org/utplsql/api/ResourceUtil.java index 15b65ab..aebb355 100644 --- a/src/main/java/org/utplsql/api/ResourceUtil.java +++ b/src/main/java/org/utplsql/api/ResourceUtil.java @@ -1,14 +1,11 @@ package org.utplsql.api; -import org.utplsql.api.reporter.CoverageHTMLReporter; - import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.*; -import java.util.ArrayList; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Collections; -import java.util.List; /** * Helper class for dealing with Resources @@ -21,47 +18,50 @@ private ResourceUtil() { } /** - * Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system + * Copy directory from a jar file to the destination folder * - * @param resourceName The name of the resource - * @return Path to the resource, either in JAR or on file system - * @throws IOException - * @throws URISyntaxException + * @param resourceAsPath The resource to get children from + * @param targetDirectory If set to true it will only return files, not directories */ - public static Path getPathToResource(String resourceName) throws IOException, URISyntaxException { - URI uri = CoverageHTMLReporter.class.getResource(resourceName).toURI(); - Path myPath; - if (uri.getScheme().equalsIgnoreCase("jar")) { - FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap()); - myPath = fileSystem.getPath(resourceName); - } else { - myPath = Paths.get(uri); + public static void copyResources(Path resourceAsPath, Path targetDirectory) { + String resourceName = "/" + resourceAsPath.toString(); + try { + Files.createDirectories(targetDirectory); + URI uri = ResourceUtil.class.getResource(resourceName).toURI(); + Path myPath; + if (uri.getScheme().equalsIgnoreCase("jar")) { + try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) { + myPath = fileSystem.getPath(resourceName); + copyRecursive(myPath, targetDirectory); + } + } else { + myPath = Paths.get(uri); + copyRecursive(myPath, targetDirectory); + } + } catch (IOException | URISyntaxException e) { + throw new RuntimeException(e); } - - return myPath; } - /** - * Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path. - * - * @param resourceAsPath The resource to get children from - * @param filesOnly If set to true it will only return files, not directories - * @return List of relative Paths to the children - * @throws IOException - * @throws URISyntaxException - */ - public static List getListOfChildren(Path resourceAsPath, boolean filesOnly) throws IOException, URISyntaxException { - - Path resourcePath = getPathToResource("/" + resourceAsPath.toString()); - int relativeStartIndex = resourcePath.getNameCount() - resourceAsPath.getNameCount(); - - final List result = new ArrayList<>(); + private static void copyRecursive(Path from, Path targetDirectory) throws IOException { + Files.walkFileTree(from, new SimpleFileVisitor() { - Files.walk(resourcePath) - .filter(p -> !filesOnly || p.toFile().isFile()) - .forEach(p -> result.add(p.subpath(relativeStartIndex, p.getNameCount()))); + private Path currentTarget; + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + super.preVisitDirectory(dir, attrs); + currentTarget = targetDirectory.resolve(from.relativize(dir).toString()); + Files.createDirectories(currentTarget); + return FileVisitResult.CONTINUE; + } - return result; + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + super.visitFile(file, attrs); + Files.copy(file, targetDirectory.resolve(from.relativize(file).toString()), StandardCopyOption.REPLACE_EXISTING); + return FileVisitResult.CONTINUE; + } + }); } } \ No newline at end of file diff --git a/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java b/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java index f927b00..fd4583d 100644 --- a/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java +++ b/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java @@ -2,15 +2,8 @@ import org.utplsql.api.ResourceUtil; -import java.io.IOException; -import java.io.InputStream; -import java.net.URISyntaxException; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.util.List; -import java.util.function.Consumer; public class CoverageHTMLReporter extends DefaultReporter { @@ -29,46 +22,14 @@ public CoverageHTMLReporter(String selfType, Object[] attributes) { super(selfType, attributes); } - /** - * Copies files from Classpath to a target directory. - * Can omit the first x folders of the asset-path when copying to the target directory - * - * @param assetPath Path of the asset in the classpath - * @param targetDirectory Target directory to copy the asset to - * @param filterNumOfFolders Omits the first x folders of the path when copying the asset to the target directory - * @throws IOException - */ - private static void copyFileFromClasspath(Path assetPath, Path targetDirectory, int filterNumOfFolders) throws IOException { - - Path assetStartPath = assetPath.subpath(filterNumOfFolders, assetPath.getNameCount()); - Path targetAssetPath = targetDirectory.resolve(Paths.get(assetStartPath.toString())); - - Files.createDirectories(targetAssetPath.getParent()); - - try (InputStream is = CoverageHTMLReporter.class.getClassLoader() - .getResourceAsStream(assetPath.toString()) - ) { - Files.copy(is, targetAssetPath, StandardCopyOption.REPLACE_EXISTING); - } - } - /** * Write the bundled assets necessary for the HTML Coverage report to a given targetPath * * @param targetDirectory Directory where the assets should be stored * @throws RuntimeException */ - public static void writeReportAssetsTo(Path targetDirectory) throws RuntimeException { - - try { - Files.createDirectories(targetDirectory); - - List paths = ResourceUtil.getListOfChildren(Paths.get("CoverageHTMLReporter"), true); - - paths.forEach((ThrowingConsumer) p -> copyFileFromClasspath(p, targetDirectory, 1)); - } catch (IOException | URISyntaxException e) { - throw new RuntimeException(e); - } + static void writeReportAssetsTo(Path targetDirectory) throws RuntimeException { + ResourceUtil.copyResources(Paths.get("CoverageHTMLReporter"), targetDirectory); } @Override @@ -116,23 +77,4 @@ public void setAssetsPath(String assetsPath) { this.assetsPath = assetsPath; } - /** - * Functional Interface just to throw Exception from Consumer - * - * @param - */ - @FunctionalInterface - public interface ThrowingConsumer extends Consumer { - - @Override - default void accept(final T elem) { - try { - acceptThrows(elem); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } - - void acceptThrows(T t) throws IOException; - } } diff --git a/src/test/java/org/utplsql/api/reporter/CoverageHTMLReporterAssetTest.java b/src/test/java/org/utplsql/api/reporter/CoverageHTMLReporterAssetTest.java index 41dd399..ee1af86 100644 --- a/src/test/java/org/utplsql/api/reporter/CoverageHTMLReporterAssetTest.java +++ b/src/test/java/org/utplsql/api/reporter/CoverageHTMLReporterAssetTest.java @@ -1,42 +1,25 @@ package org.utplsql.api.reporter; -import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.io.File; -import java.io.IOException; -import java.nio.file.*; -import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.Path; +import java.nio.file.Paths; import static org.junit.jupiter.api.Assertions.assertTrue; +@Tag("binary") class CoverageHTMLReporterAssetTest { private static final String TEST_FOLDER = "__testAssets"; - @AfterAll - static void clearTestAssetsFolder() { - try { - Files.walkFileTree(Paths.get(TEST_FOLDER), new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - e.printStackTrace(); - } - } + @TempDir + Path tempDir; private void testFileExists(Path filePath) { - File f = new File(filePath.toUri()); + File f = new File(tempDir.resolve(TEST_FOLDER).resolve(filePath).toUri()); assertTrue(f.exists(), () -> "File " + f.toString() + " does not exist"); } @@ -44,37 +27,37 @@ private void testFileExists(Path filePath) { @Test void writeReporterAssetsTo() throws RuntimeException { - Path targetPath = Paths.get(TEST_FOLDER); + Path targetPath = tempDir.resolve(TEST_FOLDER); // Act CoverageHTMLReporter.writeReportAssetsTo(targetPath); - testFileExists(targetPath.resolve(Paths.get("colorbox", "border.png"))); - testFileExists(targetPath.resolve(Paths.get("colorbox", "controls.png"))); - testFileExists(targetPath.resolve(Paths.get("colorbox", "loading.gif"))); - testFileExists(targetPath.resolve(Paths.get("colorbox", "loading_background.png"))); - - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_flat_0_aaaaaa_40x100.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_flat_75_ffffff_40x100.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_55_fbf9ee_1x400.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_65_ffffff_1x400.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_75_dadada_1x400.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_75_e6e6e6_1x400.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_glass_95_fef1ec_1x400.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-bg_highlight-soft_75_cccccc_1x100.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_2e83ff_256x240.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_222222_256x240.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_454545_256x240.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_888888_256x240.png"))); - testFileExists(targetPath.resolve(Paths.get("images", "ui-icons_cd0a0a_256x240.png"))); - - testFileExists(targetPath.resolve(Paths.get("application.css"))); - testFileExists(targetPath.resolve(Paths.get("application.js"))); - testFileExists(targetPath.resolve(Paths.get("favicon_green.png"))); - testFileExists(targetPath.resolve(Paths.get("favicon_red.png"))); - testFileExists(targetPath.resolve(Paths.get("favicon_yellow.png"))); - testFileExists(targetPath.resolve(Paths.get("loading.gif"))); - testFileExists(targetPath.resolve(Paths.get("magnify.png"))); + testFileExists(Paths.get("colorbox", "border.png")); + testFileExists(Paths.get("colorbox", "controls.png")); + testFileExists(Paths.get("colorbox", "loading.gif")); + testFileExists(Paths.get("colorbox", "loading_background.png")); + + testFileExists(Paths.get("images", "ui-bg_flat_0_aaaaaa_40x100.png")); + testFileExists(Paths.get("images", "ui-bg_flat_75_ffffff_40x100.png")); + testFileExists(Paths.get("images", "ui-bg_glass_55_fbf9ee_1x400.png")); + testFileExists(Paths.get("images", "ui-bg_glass_65_ffffff_1x400.png")); + testFileExists(Paths.get("images", "ui-bg_glass_75_dadada_1x400.png")); + testFileExists(Paths.get("images", "ui-bg_glass_75_e6e6e6_1x400.png")); + testFileExists(Paths.get("images", "ui-bg_glass_95_fef1ec_1x400.png")); + testFileExists(Paths.get("images", "ui-bg_highlight-soft_75_cccccc_1x100.png")); + testFileExists(Paths.get("images", "ui-icons_2e83ff_256x240.png")); + testFileExists(Paths.get("images", "ui-icons_222222_256x240.png")); + testFileExists(Paths.get("images", "ui-icons_454545_256x240.png")); + testFileExists(Paths.get("images", "ui-icons_888888_256x240.png")); + testFileExists(Paths.get("images", "ui-icons_cd0a0a_256x240.png")); + + testFileExists(Paths.get("application.css")); + testFileExists(Paths.get("application.js")); + testFileExists(Paths.get("favicon_green.png")); + testFileExists(Paths.get("favicon_red.png")); + testFileExists(Paths.get("favicon_yellow.png")); + testFileExists(Paths.get("loading.gif")); + testFileExists(Paths.get("magnify.png")); } }