Skip to content

Commit

Permalink
Merge pull request #32 from kbss-cvut/31-fix-loading-from-jars
Browse files Browse the repository at this point in the history
[#31] fix loading from jars
  • Loading branch information
psiotwo committed Mar 18, 2021
2 parents 092f1a0 + 26bff47 commit 68287d4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static URI getUrlAsUri(URL url) {
}
}

private void processJarFile(URL jarResource, String packageName) {
protected void processJarFile(URL jarResource, String packageName) {
final String relPath = packageName.replace('.', '/');
final String jarPath = jarResource.getPath().replaceFirst("[.]jar[!].*", JAR_FILE_SUFFIX)
.replaceFirst("file:", "");
Expand All @@ -114,9 +114,11 @@ private void processJarFile(URL jarResource, String packageName) {
if (shouldSkipEntry(entryName)) {
continue;
}
if (entryName.endsWith(CLASS_FILE_SUFFIX) && entryName.startsWith(relPath)) {
if (entryName.endsWith(CLASS_FILE_SUFFIX) && entryName.contains(relPath)) {
// Remove prefix from multi-release JAR class names
className = entryName.replaceFirst("META-INF/versions/[1-9][0-9]*/", "");
className = className.replaceFirst("WEB-INF/classes/", "");
className = className.replaceFirst("BOOT-INF/classes/", "");
className = className.replace('/', '.').replace('\\', '.');
className = className.substring(0, className.length() - CLASS_FILE_SUFFIX.length());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details. You should have received a copy of the GNU General
* Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package cz.cvut.kbss.jsonld.deserialization.util;

import static org.junit.jupiter.api.Assertions.fail;


import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;

class ClasspathScannerTest {
Expand All @@ -28,4 +34,31 @@ void processClassesSupportsMultiReleaseJarFiles() {
});
sut.processClasses(null);
}

@Test
void processClassesSupportsWarFiles() throws IOException {
processClassesSupportsJarFiles("testjar.war", "cz.cvut.kbss.testjar.model");
}

@Test
void processClassesSupportsSpringBootJarFiles() throws IOException, ClassNotFoundException {
processClassesSupportsJarFiles("testjar.jar", "cz.cvut.kbss.testjar.model");
}

void processClassesSupportsJarFiles(String jarFile, String pkg)
throws IOException {
// Empty consumer
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResources("./bug-31/" + jarFile).nextElement();
ClassLoader classLoader = new URLClassLoader(new URL[] {url});
Thread.currentThread().setContextClassLoader(classLoader);
AtomicBoolean a = new AtomicBoolean(false);
final ClasspathScanner sut = new ClasspathScanner(cls -> {
a.set(true);
});
sut.processJarFile(url, pkg);
if (!a.get()) {
fail();
}
}
}
11 changes: 11 additions & 0 deletions src/test/java/cz/cvut/kbss/testjar/model/TestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cz.cvut.kbss.testjar.model;

/**
* This serves for {@link cz.cvut.kbss.jsonld.deserialization.util.ClasspathScannerTest} to catch
* Class.forName call in ClasspathScanner.
*
* Tried https://maven.apache.org/surefire/maven-surefire-plugin/examples/configuring-classpath.html
* but wasn't working.
*/
public class TestClass {
}
Binary file added src/test/resources/bug-31/testjar.jar
Binary file not shown.
Binary file added src/test/resources/bug-31/testjar.war
Binary file not shown.

0 comments on commit 68287d4

Please sign in to comment.