mirror of
https://github.com/apache/struts.git
synced 2026-08-06 07:06:58 +00:00
satisfy coveralls via testing with a not URLClassLoader
See also WW-4845
(cherry picked from commit 02abab3)
This commit is contained in:
@@ -19,11 +19,17 @@
|
||||
package com.opensymphony.xwork2.util;
|
||||
|
||||
import com.opensymphony.xwork2.XWorkTestCase;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
|
||||
public class ClassPathFinderTest extends XWorkTestCase {
|
||||
|
||||
|
||||
public void testFinder() {
|
||||
ClassPathFinder finder = new ClassPathFinder();
|
||||
finder.setPattern("**/xwork-test-wildcard-*.xml");
|
||||
@@ -52,4 +58,46 @@ public class ClassPathFinderTest extends XWorkTestCase {
|
||||
|
||||
}
|
||||
|
||||
public void testFinderNotURLClassLoader() throws Exception {
|
||||
NotURLClassLoader loader = new NotURLClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
Thread.currentThread().setContextClassLoader(loader);
|
||||
|
||||
Class<?> clazz = loader.loadClass(ClassPathFinderTest.class.getName());
|
||||
Object test = clazz.getConstructor().newInstance();
|
||||
|
||||
clazz.getMethod("testFinder").invoke(test);
|
||||
|
||||
Thread.currentThread().setContextClassLoader(loader.parentClassLoader);
|
||||
}
|
||||
|
||||
|
||||
private class NotURLClassLoader extends ClassLoader {
|
||||
private Map<String, Class<?>> loadedClasses = new HashMap<>();
|
||||
private ClassLoader parentClassLoader;
|
||||
|
||||
NotURLClassLoader(ClassLoader parentClassLoader) {
|
||||
super(null);
|
||||
this.parentClassLoader = parentClassLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
if (!loadedClasses.containsKey(name)) {
|
||||
try {
|
||||
byte[] classBits = IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream(
|
||||
name.replace('.', '/') + ".class"));
|
||||
loadedClasses.put(name, defineClass(name, classBits, 0, classBits.length));
|
||||
} catch (IOException e) {
|
||||
throw new ClassNotFoundException("class " + name + " is not findable", e);
|
||||
}
|
||||
}
|
||||
|
||||
return loadedClasses.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Enumeration<URL> findResources(String name) throws IOException {
|
||||
return parentClassLoader.getResources(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.finder.ClassLoaderInterface;
|
||||
import com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate;
|
||||
import com.opensymphony.xwork2.util.fs.DefaultFileManager;
|
||||
import com.sun.net.httpserver.HttpsParameters;
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.struts2.dispatcher.HttpParameters;
|
||||
@@ -46,11 +45,7 @@ import javax.servlet.Servlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
|
||||
@@ -231,6 +226,19 @@ public class EmbeddedJSPResultTest extends TestCase {
|
||||
assertEquals("WhoamI?", StringUtils.deleteWhitespace(response.getContentAsString()));
|
||||
}
|
||||
|
||||
public void testNotURLClassLoader() throws Exception {
|
||||
ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
NotURLClassLoader loader = new NotURLClassLoader(parentClassLoader);
|
||||
Thread.currentThread().setContextClassLoader(loader);
|
||||
|
||||
result.setLocation("org/apache/struts2/tag0.jsp");
|
||||
result.execute(null);
|
||||
|
||||
assertEquals("Thissessionisnotsecure.OtherText", StringUtils.deleteWhitespace(response.getContentAsString()));
|
||||
|
||||
Thread.currentThread().setContextClassLoader(parentClassLoader);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@@ -374,3 +382,10 @@ class CountingClassLoaderInterface extends ClassLoaderInterfaceDelegate {
|
||||
return super.getResourceAsStream(name);
|
||||
}
|
||||
}
|
||||
|
||||
class NotURLClassLoader extends ClassLoader {
|
||||
|
||||
NotURLClassLoader(ClassLoader parentClassLoader) {
|
||||
super(parentClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user