Skip to content

Commit

Permalink
Ensure that RunnerClassloader can read directory resources
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed May 21, 2020
1 parent 9ef7784 commit 56b097a
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
Expand Up @@ -104,6 +104,10 @@ protected Enumeration<URL> findResources(String name) throws IOException {
} else {
resources = resourceDirectoryMap.get(dirName);
}
if (resources == null) {
// the resource could itself be a directory
resources = resourceDirectoryMap.get(name);
}
if (resources == null) {
return null;
}
Expand Down
Expand Up @@ -2,10 +2,16 @@

import static io.quarkus.maven.it.ApplicationNameAndVersionTestUtil.assertApplicationPropertiesSetCorrectly;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -111,9 +117,34 @@ public void testThatFastJarFormatWorks() throws MavenInvocationException, IOExce

// test that the application name and version are properly set
assertApplicationPropertiesSetCorrectly();

assertResourceReadingFromClassPathWorksCorrectly();
} finally {
process.destroy();
}

}

static void assertResourceReadingFromClassPathWorksCorrectly() {
try {
URL url = new URL("http://localhost:8080/app/classpathResources");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.2
connection.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2");
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
failResourcesFromTheClasspath();
}
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
String output = br.readLine();
assertThat(output).isEqualTo("success");
}
} catch (IOException e) {
failResourcesFromTheClasspath();
}
}

private static void failResourcesFromTheClasspath() {
fail("Failed to assert that the application properly reads resources from the classpath");
}
}
Expand Up @@ -40,6 +40,11 @@
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down
@@ -0,0 +1,158 @@
package org.acme;

import org.apache.commons.io.IOUtils;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.function.Supplier;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

@Path("/classpathResources")
public class ClasspathResources {

private static final String SUCCESS = "success";

@GET
public String readClassPathResources() {
return runAssertions(
() -> assertInvalidExactFileLocation(),
() -> assertCorrectExactFileLocation(),
() -> assertInvalidDirectory(),
() -> assertCorrectDirectory()
);
}

private String runAssertions(Supplier<String>... assertions) {
String result;
for (Supplier<String> assertion : assertions) {
result = assertion.get();
if (!SUCCESS.equals(result)) {
return result;
}
}
return SUCCESS;
}

private String assertInvalidExactFileLocation() {
final String testType = "invalid-exact-location";
try {
Enumeration<URL> exactFileLocationEnumeration = this.getClass().getClassLoader().getResources("db/location/test2.sql");
List<URL> exactFileLocationList = urlList(exactFileLocationEnumeration);
if (exactFileLocationList.size() != 0) {
return errorResult(testType, "wrong number of urls");
}
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return errorResult(testType, "exception during resolution of resource");
}
}

private String assertCorrectExactFileLocation() {
final String testType = "correct-exact-location";
try {
Enumeration<URL> exactFileLocationEnumeration = this.getClass().getClassLoader().getResources("db/location/test.sql");
List<URL> exactFileLocationList = urlList(exactFileLocationEnumeration);
if (exactFileLocationList.size() != 1) {
return errorResult(testType, "wrong number of urls");
}
String fileContent = IOUtils.toString(exactFileLocationList.get(0).toURI(), StandardCharsets.UTF_8);
if (!fileContent.contains("CREATE TABLE")) {
return errorResult(testType, "wrong file content");
}
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return errorResult(testType, "exception during resolution of resource");
}
}

private String assertInvalidDirectory() {
final String testType = "invalid-directory";
try {
Enumeration<URL> exactFileLocationEnumeration = this.getClass().getClassLoader().getResources("db/location2");
List<URL> exactFileLocationList = urlList(exactFileLocationEnumeration);
if (exactFileLocationList.size() != 0) {
return errorResult(testType, "wrong number of urls");
}
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return errorResult(testType, "exception during resolution of resource");
}
}

private String assertCorrectDirectory() {
final String testType = "correct-directory";
try {
Enumeration<URL> directoryEnumeration = this.getClass().getClassLoader().getResources("db/location");
List<URL> directoryURLList = urlList(directoryEnumeration);
if (directoryURLList.size() != 1) {
return errorResult(testType, "wrong number of directory urls");
}

URL singleURL = directoryURLList.get(0);

int separatorIndex = singleURL.getPath().lastIndexOf('!');
String jarPath = singleURL.getPath().substring(0, separatorIndex);
String directoryName = singleURL.getPath().substring(separatorIndex + 2) + "/";

try (JarFile jarFile = new JarFile(Paths.get(new URI(jarPath)).toFile())) {
Enumeration<JarEntry> entries = jarFile.entries();
List<JarEntry> entriesInDirectory = new ArrayList<>();
while (entries.hasMoreElements()) {
JarEntry currentEntry = entries.nextElement();
String entryName = currentEntry.getName();
if (entryName.startsWith(directoryName) && !entryName.equals(directoryName)) {
entriesInDirectory.add(currentEntry);
}
}

if (entriesInDirectory.size() != 1) {
return errorResult(testType, "wrong number of entries in jar directory");
}

try (InputStream is = jarFile.getInputStream(entriesInDirectory.get(0))) {
String fileContent = IOUtils.toString(is, StandardCharsets.UTF_8);
if (!fileContent.contains("CREATE TABLE")) {
return errorResult(testType, "wrong file content");
}
return SUCCESS;
}
}


} catch (Exception e) {
e.printStackTrace();
return errorResult(testType, "exception during resolution of resource");
}
}

private List<URL> urlList(Enumeration<URL> enumeration) {
if (enumeration == null) {
return Collections.emptyList();
}
List<URL> result = new ArrayList<>();
while (enumeration.hasMoreElements()) {
result.add(enumeration.nextElement());
}
return result;
}

private String errorResult(String testType, String message) {
return testType + " / " + message;
}
}
@@ -0,0 +1,7 @@
CREATE TABLE TEST_SCHEMA.quarkus_table2
(
id INT,
name VARCHAR(20)
);
INSERT INTO TEST_SCHEMA.quarkus_table2(id, name)
VALUES (1, '1.0.1 QUARKED');

0 comments on commit 56b097a

Please sign in to comment.