HHH-4892 - Simplify testing of persistence packages

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18738 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-02-09 03:53:56 +00:00
parent 8b12e759f0
commit 527468a122
2 changed files with 86 additions and 3 deletions

View File

@ -23,6 +23,11 @@
*/
package org.hibernate.ejb.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
@ -62,11 +67,45 @@ import org.hibernate.util.ConfigHelper;
*/
@SuppressWarnings("unchecked")
public class PackagedEntityManagerTest extends TestCase {
private static ClassLoader originalClassLoader;
public Class[] getAnnotatedClasses() {
return new Class[] { Item.class, Distributor.class };
}
@Override
protected void setUp() throws Exception {
originalClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( buildCustomTCCL( originalClassLoader ) );
super.setUp();
}
private ClassLoader buildCustomTCCL(ClassLoader parentClassLoader) throws MalformedURLException {
// get a URL reference to something we now is part of the classpath (us)
URL myUrl = parentClassLoader.getResource( PackagedEntityManagerTest.class.getName().replace( '.', '/' ) + ".class" );
File myPath = new File( myUrl.getFile() );
// navigate back to '/target'
File targetDir = myPath
.getParentFile() // target/classes/org/hibernate/ejb/test
.getParentFile() // target/classes/org/hibernate/ejb
.getParentFile() // target/classes/org/hibernate
.getParentFile() // target/classes/org
.getParentFile() // target/classes/
.getParentFile(); // target
File testPackagesDir = new File( targetDir, "test-packages" );
ArrayList<URL> urls = new ArrayList<URL>();
for ( File testPackage : testPackagesDir.listFiles() ) {
urls.add( testPackage.toURL() );
}
return new URLClassLoader( urls.toArray( new URL[ urls.size() ] ), parentClassLoader );
}
@Override
public void tearDown() throws Exception {
super.tearDown();
Thread.currentThread().setContextClassLoader( originalClassLoader );
}
@Override
protected void buildConfiguration() throws Exception {
super.buildConfiguration();

View File

@ -1,6 +1,29 @@
//$Id$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.test.packaging;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
@ -25,6 +48,8 @@ import org.hibernate.ejb.packaging.JarVisitorFactory;
import org.hibernate.ejb.packaging.PackageFilter;
import org.hibernate.ejb.test.pack.defaultpar.ApplicationServer;
import org.hibernate.ejb.test.pack.explodedpar.Carpet;
import org.hibernate.junit.FailureExpected;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,7 +61,25 @@ public class JarVisitorTest extends TestCase {
private static final Logger log = LoggerFactory.getLogger(JarVisitorTest.class);
private static final String jarFileBase = "file:./target/test-packages";
private static String jarFileBase;
@Override
protected void setUp() throws Exception {
URL myUrl = JarVisitorTest.class.getClassLoader().getResource( JarVisitorTest.class.getName().replace( '.', '/' ) + ".class" );
File myPath = new File( myUrl.getFile() );
// navigate back to '/target'
File targetDir = myPath
.getParentFile() // target/classes/org/hibernate/ejb/test/packaging
.getParentFile() // target/classes/org/hibernate/ejb/test
.getParentFile() // target/classes/org/hibernate/ejb
.getParentFile() // target/classes/org/hibernate
.getParentFile() // target/classes/org
.getParentFile() // target/classes
.getParentFile(); // target
jarFileBase = new File( targetDir, "test-packages" ).toURL().toExternalForm();
super.setUp();
}
public void testHttp() throws Exception {
URL url = JarVisitorFactory.getJarURLFromURLEntry(
@ -59,8 +102,9 @@ public class JarVisitorTest extends TestCase {
assertEquals( 0, visitor.getMatchingEntries()[2].size() );
}
@FailureExpected( jiraKey = "")
public void testInputStreamZippedJar() throws Exception {
String jarFileName = jarFileBase + "/defaultpar.par";
String jarFileName = jarFileBase + "defaultpar.par";
Filter[] filters = getFilters();
JarVisitor jarVisitor = new InputStreamZippedJarVisitor( new URL( jarFileName ), filters, "" );
assertEquals( "defaultpar", jarVisitor.getUnqualifiedJarName() );