HHH-5693 Change way the target directory is discovered

Depending on wether the tests are run from the command line (Gradle) or IDE (Idea at least) the path will vary. For this reason one cannot just navigate back in the path. Instead I am assuming that there is a 'target' directory.
This commit is contained in:
Hardy Ferentschik 2010-10-26 12:03:09 +02:00
parent 2d501faa8a
commit e660463523
1 changed files with 28 additions and 29 deletions

View File

@ -66,33 +66,32 @@ import org.hibernate.ejb.test.pack.various.Seat;
public abstract class PackagingTestCase extends TestCase { public abstract class PackagingTestCase extends TestCase {
protected static ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); protected static ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
protected static ClassLoader bundleClassLoader; protected static ClassLoader bundleClassLoader;
protected static File targetDir; protected static File packageTargetDir;
static { static {
// get a URL reference to something we now is part of the classpath (us) // get a URL reference to something we now is part of the classpath (us)
URL myUrl = originalClassLoader.getResource( URL myUrl = originalClassLoader.getResource(
PackagingTestCase.class.getName().replace( '.', '/' ) + ".class" PackagingTestCase.class.getName().replace( '.', '/' ) + ".class"
); );
File myPath = new File( myUrl.getFile() );
// navigate back to '/target' // this is assuming that there is a target directory
targetDir = myPath int index = myUrl.getFile().lastIndexOf( "target" );
.getParentFile() // target/classes/test/org/hibernate/ejb/test/packaging if ( index == -1 ) {
.getParentFile() // target/classes/test/org/hibernate/ejb/test fail("Unable to setup packaging test");
.getParentFile() // target/classes/test/org/hibernate/ejb }
.getParentFile() // target/classes/test/org/hibernate
.getParentFile() // target/classes/test/org String baseDirPath = myUrl.getFile().substring( 0, index);
.getParentFile() // target/classes/test File baseDir = new File( baseDirPath );
.getParentFile() // target/classes
.getParentFile(); // target File testPackagesDir = new File( baseDir, "target/bundles" );
File testPackagesDir = new File( targetDir, "bundles" );
try { try {
bundleClassLoader = new URLClassLoader( new URL[] { testPackagesDir.toURL() }, originalClassLoader ); bundleClassLoader = new URLClassLoader( new URL[] { testPackagesDir.toURL() }, originalClassLoader );
} }
catch ( MalformedURLException e ) { catch ( MalformedURLException e ) {
fail( "Unable to build custom class loader" ); fail( "Unable to build custom class loader" );
} }
targetDir = new File( targetDir, "packages" ); packageTargetDir = new File( baseDir, "target/packages" );
targetDir.mkdirs(); packageTargetDir.mkdirs();
} }
@Override @Override
@ -154,7 +153,7 @@ public abstract class PackagingTestCase extends TestCase {
archive.addResource( "org/hibernate/ejb/test/pack/defaultpar/package-info.class", path ); archive.addResource( "org/hibernate/ejb/test/pack/defaultpar/package-info.class", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -183,7 +182,7 @@ public abstract class PackagingTestCase extends TestCase {
archive.addResource( "org/hibernate/ejb/test/pack/defaultpar_1_0/package-info.class", path ); archive.addResource( "org/hibernate/ejb/test/pack/defaultpar_1_0/package-info.class", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -206,7 +205,7 @@ public abstract class PackagingTestCase extends TestCase {
path = ArchivePaths.create( "META-INF/persistence.xml" ); path = ArchivePaths.create( "META-INF/persistence.xml" );
archive.addResource( "explicitpar/META-INF/persistence.xml", path ); archive.addResource( "explicitpar/META-INF/persistence.xml", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -228,8 +227,8 @@ public abstract class PackagingTestCase extends TestCase {
path = ArchivePaths.create( "org/hibernate/ejb/test/pack/explodedpar/package-info.class" ); path = ArchivePaths.create( "org/hibernate/ejb/test/pack/explodedpar/package-info.class" );
archive.addResource( "org/hibernate/ejb/test/pack/explodedpar/package-info.class", path ); archive.addResource( "org/hibernate/ejb/test/pack/explodedpar/package-info.class", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ExplodedExporter.class ).exportExploded( targetDir ); archive.as( ExplodedExporter.class ).exportExploded( packageTargetDir );
return testPackage; return testPackage;
} }
@ -249,7 +248,7 @@ public abstract class PackagingTestCase extends TestCase {
path = ArchivePaths.create( "org/hibernate/ejb/test/pack/excludehbmpar/Mouse.hbm.xml" ); path = ArchivePaths.create( "org/hibernate/ejb/test/pack/excludehbmpar/Mouse.hbm.xml" );
archive.addResource( "excludehbmpar/org/hibernate/ejb/test/pack/excludehbmpar/Mouse.hbm.xml", path ); archive.addResource( "excludehbmpar/org/hibernate/ejb/test/pack/excludehbmpar/Mouse.hbm.xml", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -268,7 +267,7 @@ public abstract class PackagingTestCase extends TestCase {
path = ArchivePaths.create( "org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml" ); path = ArchivePaths.create( "org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml" );
archive.addResource( "cfgxmlpar/org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml", path ); archive.addResource( "cfgxmlpar/org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -283,7 +282,7 @@ public abstract class PackagingTestCase extends TestCase {
ArchivePath path = ArchivePaths.create( "META-INF/persistence.xml" ); ArchivePath path = ArchivePaths.create( "META-INF/persistence.xml" );
archive.addResource( "space par/META-INF/persistence.xml", path ); archive.addResource( "space par/META-INF/persistence.xml", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -301,7 +300,7 @@ public abstract class PackagingTestCase extends TestCase {
path = ArchivePaths.create( "overridenpar.properties" ); path = ArchivePaths.create( "overridenpar.properties" );
archive.addResource( "overridenpar/overridenpar.properties", path ); archive.addResource( "overridenpar/overridenpar.properties", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -316,7 +315,7 @@ public abstract class PackagingTestCase extends TestCase {
ArchivePath path = ArchivePaths.create( "META-INF/orm.xml" ); ArchivePath path = ArchivePaths.create( "META-INF/orm.xml" );
archive.addResource( "externaljar/META-INF/orm.xml", path ); archive.addResource( "externaljar/META-INF/orm.xml", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -343,7 +342,7 @@ public abstract class PackagingTestCase extends TestCase {
path = ArchivePaths.create( "WEB-INF/classes/org/hibernate/ejb/test/pack/war/Mouse.hbm.xml" ); path = ArchivePaths.create( "WEB-INF/classes/org/hibernate/ejb/test/pack/war/Mouse.hbm.xml" );
archive.addResource( "war/WEB-INF/classes/org/hibernate/ejb/test/pack/war/Mouse.hbm.xml", path ); archive.addResource( "war/WEB-INF/classes/org/hibernate/ejb/test/pack/war/Mouse.hbm.xml", path );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -353,7 +352,7 @@ public abstract class PackagingTestCase extends TestCase {
JavaArchive archive = Archives.create( fileName, JavaArchive.class ); JavaArchive archive = Archives.create( fileName, JavaArchive.class );
archive.addResource( includeFile ); archive.addResource( includeFile );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ZipExporter.class ).exportZip( testPackage, true ); archive.as( ZipExporter.class ).exportZip( testPackage, true );
return testPackage; return testPackage;
} }
@ -363,8 +362,8 @@ public abstract class PackagingTestCase extends TestCase {
JavaArchive archive = Archives.create( fileName, JavaArchive.class ); JavaArchive archive = Archives.create( fileName, JavaArchive.class );
archive.addResource( includeFile ); archive.addResource( includeFile );
File testPackage = new File( targetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ExplodedExporter.class ).exportExploded( targetDir ); archive.as( ExplodedExporter.class ).exportExploded( packageTargetDir );
return testPackage; return testPackage;
} }
} }