com.github.searls.jasmine.thirdpartylibs.WebJarResourceHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jasmine-maven-plugin Show documentation
Show all versions of jasmine-maven-plugin Show documentation
A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports
package com.github.searls.jasmine.thirdpartylibs;
import org.webjars.WebJarAssetLocator;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class WebJarResourceHandler extends AbstractThirdPartyLibsResourceHandler {
private static final Pattern WILDCARD = Pattern.compile(".*");
private final List webJarAssetLocators;
private final ClassLoader projectClassLoader;
public WebJarResourceHandler(ClassLoader projectClassLoader) {
this.projectClassLoader = projectClassLoader;
this.webJarAssetLocators = createWebJarAssetLocators(
WebJarResourceHandler.class.getClassLoader(),
projectClassLoader
);
}
@Override
protected InputStream findResource(String resourcePath) {
String fullPath = findFullPath(resourcePath);
return fullPath != null ? projectClassLoader.getResourceAsStream(fullPath) : null;
}
private String findFullPath(String resourcePath) {
String fullPath = null;
for (WebJarAssetLocator locator : webJarAssetLocators) {
fullPath = findFullPath(locator, resourcePath);
if (fullPath != null) {
break;
}
}
return fullPath;
}
private String findFullPath(WebJarAssetLocator locator, String resourcePath) {
String fullPath = null;
try {
fullPath = locator.getFullPath(resourcePath);
} catch (Exception e) {
fullPath = findFullPathByPartial(locator, resourcePath);
}
return fullPath;
}
private String findFullPathByPartial(WebJarAssetLocator locator, String resourcePath) {
String fullPath = null;
int splitAt = resourcePath.indexOf("/");
if (splitAt > 0) {
String webjar = resourcePath.substring(0, splitAt);
String partialPath = resourcePath.substring(splitAt + 1);
try {
fullPath = locator.getFullPath(webjar, partialPath);
} catch (Exception e) {
// TODO: at least log this exception
}
}
return fullPath;
}
private List createWebJarAssetLocators(ClassLoader ... classLoaders) {
List locators = new ArrayList();
for (ClassLoader classLoader : classLoaders) {
locators.add(new WebJarAssetLocator(WebJarAssetLocator.getFullPathIndex(WILDCARD, classLoader)));
}
return locators;
}
}