mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
@@ -0,0 +1,114 @@
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
|
||||
/**
|
||||
* This ensures that the ClassPathSearch supports normal file:file.jar files as well as jar/war files
|
||||
* with bang paths. Bang paths typically look like this as a url:
|
||||
*
|
||||
* jar:file:/path/to/file.war!/WEB-INF/classes
|
||||
*
|
||||
* author: Richard Vowles - http://plus.google.com/RichardVowles
|
||||
*/
|
||||
public class ClassPathSearchTests {
|
||||
|
||||
private static final String WEB_INF_CLASSES = "WEB-INF/classes/";
|
||||
|
||||
private void runAsserts(List<Class<?>> found, URLClassLoader cl, ClassPathSearch search,
|
||||
URL jar, URL jarBang) throws ClassNotFoundException {
|
||||
|
||||
assert found.size() == 2;
|
||||
assert found.contains(cl.loadClass(SimpleJarBangClass.class.getName()));
|
||||
assert found.contains(cl.loadClass(SimpleJarClass.class.getName()));
|
||||
|
||||
assert search.getJarHits().contains(new File(jar.getFile()).getName());
|
||||
assert search.getJarHits().contains(new File(jarBang.getFile().split("!")[0]).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ensureClassPathFindsClassesInBangPaths() throws IOException, ClassNotFoundException {
|
||||
URL jar = setupJar();
|
||||
URL jarBang = setupJarBang();
|
||||
|
||||
URLClassLoader cl = new URLClassLoader(new URL[] { jar, jarBang});
|
||||
|
||||
ClassPathSearchFilter filter = new ClassPathSearchFilter();
|
||||
filter.includePackage("com.avaje.ebeaninternal.server");
|
||||
filter.includeJar("WEB-INF");
|
||||
|
||||
ClassPathSearch search = new ClassPathSearch(cl, filter, new ClassPathSearchMatcher() {
|
||||
@Override
|
||||
public boolean isMatch(Class<?> cls) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
List<Class<?>> found = search.findClasses();
|
||||
|
||||
Assert.assertEquals(1, found.size());
|
||||
assert found.contains(cl.loadClass(SimpleJarBangClass.class.getName()));
|
||||
|
||||
assert search.getJarHits().contains(new File(jarBang.getFile().split("!")[0]).getName());
|
||||
|
||||
filter = new ClassPathSearchFilter();
|
||||
filter.includePackage("com.avaje.ebeaninternal.server");
|
||||
filter.includeJar("bang");
|
||||
|
||||
search = new ClassPathSearch(cl, filter, new ClassPathSearchMatcher() {
|
||||
@Override
|
||||
public boolean isMatch(Class<?> cls) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
runAsserts(search.findClasses(), cl, search, jar, jarBang);
|
||||
}
|
||||
|
||||
private URL createJar(Class clazz, File jarFile, String offset) throws IOException {
|
||||
FileOutputStream stream = new FileOutputStream(jarFile);
|
||||
JarOutputStream jarOutputStream = new JarOutputStream(stream);
|
||||
String clazzPath = clazz.getPackage().getName().replace(".", "/") + "/" + clazz.getSimpleName() + ".class";
|
||||
JarEntry entry = new JarEntry(offset + clazzPath);
|
||||
jarOutputStream.putNextEntry(entry);
|
||||
InputStream classStream = getClass().getResourceAsStream("/" + clazzPath);
|
||||
IOUtils.copy(classStream, jarOutputStream);
|
||||
|
||||
if (offset != null) { // copy the same file in, with a different offset
|
||||
entry = new JarEntry("random/" + clazzPath);
|
||||
jarOutputStream.putNextEntry(entry);
|
||||
classStream = getClass().getResourceAsStream("/" + clazzPath);
|
||||
IOUtils.copy(classStream, jarOutputStream);
|
||||
}
|
||||
|
||||
jarOutputStream.close();
|
||||
stream.close();
|
||||
|
||||
if (offset.length() > 0) {
|
||||
return new URL("jar:" + jarFile.toURI().toString() + "!/" + offset);
|
||||
} else {
|
||||
return jarFile.toURI().toURL();
|
||||
}
|
||||
}
|
||||
|
||||
private URL setupJarBang() throws IOException {
|
||||
File jarFile = File.createTempFile("bang", ".war");
|
||||
return createJar(SimpleJarBangClass.class, jarFile, WEB_INF_CLASSES);
|
||||
}
|
||||
|
||||
private URL setupJar() throws IOException {
|
||||
File jarFile = File.createTempFile("nobang", ".jar");
|
||||
return createJar(SimpleJarClass.class, jarFile, "");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
/**
|
||||
* Exists only for including in the jar for the Class Path Search Tests.
|
||||
*
|
||||
* author: Richard Vowles - http://plus.google.com/RichardVowles
|
||||
*/
|
||||
public class SimpleJarBangClass {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
/**
|
||||
* Exists only for the purpose of putting in a jar for the ClassPathSearchTests.
|
||||
*
|
||||
* author: Richard Vowles - http://plus.google.com/RichardVowles
|
||||
*/
|
||||
public class SimpleJarClass {
|
||||
}
|
||||
@@ -1,14 +1,28 @@
|
||||
package com.avaje.tests.unitinternal;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebeaninternal.server.core.XmlConfigLoader;
|
||||
import com.avaje.ebeaninternal.server.lib.util.Dnode;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
|
||||
/**
|
||||
* Tests basic xml loading functionality across multiple jars and inside our own test structure.
|
||||
*
|
||||
* @author Rob Bygrave
|
||||
* @author Richard Vowles - http://plus.google.com/RichardVowles
|
||||
*/
|
||||
public class TestXmlConfigLoader extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@@ -16,10 +30,62 @@ public class TestXmlConfigLoader extends BaseTestCase {
|
||||
|
||||
XmlConfigLoader xmlConfigLoader = new XmlConfigLoader(null);
|
||||
|
||||
List<Dnode> ebeanOrmXml = xmlConfigLoader.search("META-INF/ebean-orm.xml");
|
||||
List<Dnode> ebeanOrmXml = xmlConfigLoader.search(META_INF_EBEAN_ORM_XML);
|
||||
|
||||
Assert.assertNotNull(ebeanOrmXml);
|
||||
Assert.assertTrue("Found ebean-orm.xml", ebeanOrmXml.size() > 0);
|
||||
|
||||
}
|
||||
|
||||
private static final String META_INF_EBEAN_ORM_XML = "META-INF/ebean-orm.xml";
|
||||
private static final String META_INF_ORM_XML = "META-INF/orm.xml";
|
||||
private static final String WEB_INF_CLASSES = "WEB-INF/classes/";
|
||||
|
||||
@Test
|
||||
public void ensureEbeanOrmStillLoads() throws IOException {
|
||||
URL jar = setupJar();
|
||||
URL jarBang = setupJarBang();
|
||||
|
||||
URLClassLoader cl = new URLClassLoader(new URL[] { jar, jarBang}, getClass().getClassLoader());
|
||||
|
||||
XmlConfigLoader loader = new XmlConfigLoader(cl);
|
||||
|
||||
Assert.assertEquals(3, loader.search(META_INF_EBEAN_ORM_XML).size()); // 1 in parent, 2 in ours
|
||||
Assert.assertEquals(2, loader.search(META_INF_ORM_XML).size()); // 2 in ours
|
||||
|
||||
}
|
||||
|
||||
private URL createJar(File jarFile, String offset) throws IOException {
|
||||
FileOutputStream stream = new FileOutputStream(jarFile);
|
||||
JarOutputStream jarOutputStream = new JarOutputStream(stream);
|
||||
|
||||
JarEntry entry = new JarEntry(offset + META_INF_EBEAN_ORM_XML);
|
||||
jarOutputStream.putNextEntry(entry);
|
||||
InputStream classStream = getClass().getResourceAsStream("/" + META_INF_EBEAN_ORM_XML);
|
||||
IOUtils.copy(classStream, jarOutputStream);
|
||||
|
||||
entry = new JarEntry(offset + META_INF_ORM_XML);
|
||||
jarOutputStream.putNextEntry(entry);
|
||||
classStream = getClass().getResourceAsStream("/" + META_INF_EBEAN_ORM_XML);
|
||||
IOUtils.copy(classStream, jarOutputStream);
|
||||
|
||||
jarOutputStream.close();
|
||||
stream.close();
|
||||
|
||||
if (offset.length() > 0) {
|
||||
return new URL("jar:" + jarFile.toURI().toString() + "!/" + offset);
|
||||
} else {
|
||||
return jarFile.toURI().toURL();
|
||||
}
|
||||
}
|
||||
|
||||
private URL setupJarBang() throws IOException {
|
||||
File jarFile = File.createTempFile("bang", ".war");
|
||||
return createJar(jarFile, WEB_INF_CLASSES);
|
||||
}
|
||||
|
||||
private URL setupJar() throws IOException {
|
||||
File jarFile = File.createTempFile("nobang", ".jar");
|
||||
return createJar(jarFile, "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user