HHH-9699 - Re-work the hibernate-osgi integration tests using managed Karaf

This commit is contained in:
Steve Ebersole 2015-05-05 17:18:16 -05:00
parent fde734e828
commit 10922ba279
4 changed files with 213 additions and 86 deletions

View File

@ -101,6 +101,7 @@ class HibernateBuildPlugin implements Plugin<Project> {
def applyJavaTarget(JavaTargetExtension javaTargetExtension, Project project, Jvm java6Home) {
project.logger.info( "Setting target Java version : ${javaTargetExtension.version} (${project.name})" )
project.properties.put( 'sourceCompatibility', "${javaTargetExtension.version}" )
project.properties.put( 'targetCompatibility', "${javaTargetExtension.version}" )
@ -125,47 +126,49 @@ class HibernateBuildPlugin implements Plugin<Project> {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Apply to compile task (just for main sourceSet)
// Apply to compile task
SourceSet mainSourceSet = project.getConvention().findPlugin( JavaPluginConvention.class ).sourceSets.findByName( "main" )
JavaCompile compileTask = project.tasks.findByName( mainSourceSet.compileJavaTaskName ) as JavaCompile
project.getConvention().findPlugin( JavaPluginConvention.class ).sourceSets.each { sourceSet ->
JavaCompile javaCompileTask = project.tasks.findByName( sourceSet.compileJavaTaskName ) as JavaCompile
// NOTE : this aptDir stuff is needed until we can have IntelliJ run annotation processors for us
// which cannot happen until we can fold hibernate-testing back into hibernate-core/src/test
// which cannot happen until... ugh
File aptDir = project.file( "${project.buildDir}/generated-src/apt/main" )
mainSourceSet.allJava.srcDir( aptDir )
// NOTE : this aptDir stuff is needed until we can have IntelliJ run annotation processors for us
// which cannot happen until we can fold hibernate-testing back into hibernate-core/src/test
// which cannot happen until... ugh
File aptDir = project.file( "${project.buildDir}/generated-src/apt/main" )
sourceSet.allJava.srcDir( aptDir )
compileTask.options.compilerArgs += [
"-nowarn",
"-encoding", "UTF-8",
"-s", "${aptDir.absolutePath}"
]
compileTask.doFirst {
aptDir.mkdirs()
}
if ( javaTargetExtension.version.java8Compatible ) {
compileTask.options.compilerArgs += [
"-source", '1.8',
"-target", '1.8'
]
}
else {
compileTask.options.compilerArgs += [
"-source", '1.6',
"-target", '1.6'
javaCompileTask.options.compilerArgs += [
"-nowarn",
"-encoding", "UTF-8",
"-s", "${aptDir.absolutePath}"
]
javaCompileTask.doFirst {
aptDir.mkdirs()
}
if ( java6Home != null ) {
if ( javaTargetExtension.shouldApplyTargetToCompile ) {
// Technically we need only one here between:
// 1) setting the javac executable
// 2) setting the bootClasspath
// However, (1) requires fork=true whereas (2) does not.
// compileTask.options.fork = true
// compileTask.options.forkOptions.executable = java6Home.javacExecutable
compileTask.options.bootClasspath = java6Home.runtimeJar.absolutePath
if ( javaTargetExtension.version.java8Compatible ) {
javaCompileTask.options.compilerArgs += [
"-source", '1.8',
"-target", '1.8'
]
}
else {
javaCompileTask.options.compilerArgs += [
"-source", '1.6',
"-target", '1.6'
]
if ( java6Home != null ) {
if ( javaTargetExtension.shouldApplyTargetToCompile ) {
// Technically we need only one here between:
// 1) setting the javac executable
// 2) setting the bootClasspath
// However, (1) requires fork=true whereas (2) does not.
// javaCompileTask.options.fork = true
// javaCompileTask.options.forkOptions.executable = java6Home.javacExecutable
javaCompileTask.options.bootClasspath = java6Home.runtimeJar.absolutePath
}
}
}
}

View File

@ -1,9 +1,5 @@
apply plugin: 'karaf-featuresgen'
ext {
paxExamVersion = '4.0.0'
karafVersion = '3.0.3'
}
configurations {
aries {
@ -59,6 +55,12 @@ configurations {
//}
dependencies {
ext {
paxExamVersion = '4.4.0'
paxExamUrlVersion = '2.4.1'
karafVersion = '3.0.3'
}
compile( project( ':hibernate-core' ) )
compile( project( ':hibernate-entitymanager' ) )
// MUST use 4.3.1! 4.3.0 was compiled with "-target jsr14".
@ -69,13 +71,23 @@ dependencies {
// PaxExam and friends. Not entirely sure yet of proper configuration
testCompile "org.ops4j.pax.exam:pax-exam:${paxExamVersion}"
testCompile "org.ops4j.pax.exam:pax-exam-container-karaf:${paxExamVersion}"
testCompile( "org.apache.karaf:apache-karaf:${karafVersion}@tar.gz" ) {
transitive = false
}
testCompile "org.ops4j.pax.exam:pax-exam-junit4:${paxExamVersion}"
testCompile 'org.ops4j.pax.url:pax-url-aether:1.6.0'
testCompile 'javax.inject:javax.inject:1'
testRuntime "org.apache.karaf:apache-karaf:${karafVersion}@tar.gz"
testRuntime "org.ops4j.pax.exam:pax-exam-link-mvn:${paxExamVersion}"
testRuntime "org.ops4j.pax.url:pax-url-aether:${paxExamUrlVersion}"
testRuntime "org.ops4j.pax.url:pax-url-reference:${paxExamUrlVersion}"
testRuntime "org.ops4j.pax.url:pax-url-wrap:${paxExamUrlVersion}"
// testCompile( "org.apache.karaf:apache-karaf:${karafVersion}@tar.gz" ) {
// transitive = false
// }
karafFeaturesBundles "org.apache.logging.log4j:log4j-api:2.0"
// karafFeaturesBundles libraries.log4j
// karafFeaturesBundles libraries.slf4j_api
// karafFeaturesBundles libraries.slf4j_log4j
//
//
//
@ -349,6 +361,7 @@ karafFeatures {
}
test.dependsOn felixProperties
//test.dependsOn testClientBundleJar
test.dependsOn jar

View File

@ -24,6 +24,7 @@
package org.hibernate.osgi.test;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.MalformedURLException;
@ -44,7 +45,9 @@
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.osgi.OsgiSessionFactoryService;
import org.hibernate.osgi.test.client.DataPoint;
import org.hibernate.osgi.test.client.OsgiTestActivator;
import org.hibernate.osgi.test.client.SomeService;
import org.hibernate.osgi.test.client.SomeServiceContributor;
import org.hibernate.osgi.test.client.TestIntegrator;
import org.hibernate.osgi.test.client.TestStrategyRegistrationProvider;
import org.hibernate.osgi.test.client.TestTypeContributor;
@ -59,6 +62,8 @@
import org.osgi.framework.ServiceReference;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Customizer;
import org.ops4j.pax.exam.MavenUtils;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.ProbeBuilder;
import org.ops4j.pax.exam.TestProbeBuilder;
@ -74,8 +79,13 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.ops4j.pax.exam.CoreOptions.bundle;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.repositories;
import static org.ops4j.pax.exam.CoreOptions.repository;
import static org.ops4j.pax.exam.CoreOptions.when;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.debugConfiguration;
@ -94,11 +104,10 @@
@ExamReactorStrategy( PerClass.class )
public class OsgiIntegrationTest {
private static final boolean DEBUG = false;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Prepare the Karaf container
@Inject
@SuppressWarnings("UnusedDeclaration")
private BundleContext bundleContext;
private static final boolean DEBUG = false;
@Configuration
public Option[] config() throws Exception {
@ -111,31 +120,111 @@ public Option[] config() throws Exception {
return options(
when( debug ).useOptions( debugConfiguration( "5005", true ) ),
karafDistributionConfiguration()
.frameworkUrl(
maven()
.groupId( "org.apache.karaf" )
.artifactId( "apache-karaf" )
.type( "tar.gz" )
.versionAsInProject()
)
.unpackDirectory( new File( "target/exam" ) )
.karafVersion( karafVersion() )
.frameworkUrl( karafDistributionUrl() )
.unpackDirectory( new File( "target/karaf" ) )
.useDeployFolder( false ),
configureConsole()
.ignoreLocalConsole()
.ignoreRemoteShell(),
repositories(
repository( "https://repository.jboss.org/nexus/content/groups/public-jboss/" )
.id( "jboss-nexus" )
.allowSnapshots()
),
// features( karafStandardRepo(), "standard,scr" ),
// features( karafFrameworkRepo(), "framework" ),
configureConsole().ignoreLocalConsole().ignoreRemoteShell(),
when( debug ).useOptions( keepRuntimeFolder() ),
logLevel( LogLevelOption.LogLevel.INFO ),
// avoiding additional boot features; specifically "enterprise"
editConfigurationFilePut(
"etc/org.apache.karaf.features.cfg",
"featuresBoot",
"standard"
),
features( hibernateKarafFeatureFile(), "hibernate-core", "hibernate-entitymanager", "hibernate-osgi" )
// editConfigurationFilePut(
// "etc/org.apache.karaf.features.cfg",
// "featuresBoot",
// "standard,karaf-framework"
// ),
mavenBundle().groupId( "com.h2database" ).artifactId( "h2" ).version( "1.3.170" ).start(),
junitBundles(),
features( hibernateKarafFeatureFile(), "hibernate-native", "hibernate-jpa" ),
new Customizer() {
@Override
public void customizeEnvironment(File workingFolder) {
super.customizeEnvironment( workingFolder );
Locale.setDefault( Locale.ENGLISH );
}
@Override
public InputStream customizeTestProbe(InputStream testProbe) throws Exception {
return org.ops4j.pax.tinybundles.core.TinyBundles.bundle()
.read( super.customizeTestProbe( testProbe ) )
.add( "/hibernate.cfg.xml", probeCfgXmlFile().toURI().toURL() )
.add( "/META-INF/persistence.xml", probePersistenceXmlFile().toURI().toURL() )
.add( "/OSGI-INF/blueprint/blueprint.xml", probeBlueprintXmlFile().toURI().toURL() )
.add( DataPoint.class )
.add( OsgiTestActivator.class )
.add( SomeService.class )
.add( SomeServiceContributor.class )
.add( TestIntegrator.class )
.add( TestStrategyRegistrationProvider.class )
.add( TestTypeContributor.class )
.build();
}
}
);
}
private MavenUrlReference karafDistributionUrl() {
return maven()
.groupId( "org.apache.karaf" )
.artifactId( "apache-karaf" )
.version( karafVersion() )
.type( "tar.gz" );
}
private String karafVersion() {
return MavenUtils.getArtifactVersion( "org.apache.karaf", "apache-karaf" );
}
private MavenUrlReference karafStandardRepo() {
return maven()
.groupId( "org.apache.karaf.features" )
.artifactId( "standard" )
.version( karafVersion() )
.classifier( "features" )
.type( "xml" );
}
private MavenUrlReference karafFrameworkRepo() {
return maven()
.groupId( "org.apache.karaf.features" )
.artifactId( "framework" )
.version( karafVersion() )
.classifier( "features" )
.type( "xml" );
}
private UrlReference hibernateKarafFeatureFile() throws Exception {
File dir = resolveBuildDir();
// go to the `karafFeatures` dir...
dir = new File( dir, "karafFeatures" );
// and look for the feature file there...
final String featureFileName = String.format(
Locale.ENGLISH,
"hibernate-osgi-%s-karaf.xml",
determineProjectVersion()
);
final URL url = new File( dir, featureFileName ).toURI().toURL();
System.out.println( "hibernate-osgi karaf feature file for Pax-Exam testing : " + url.toExternalForm() );
return new UrlReference() {
@Override
public String getURL() {
return url.toExternalForm();
}
};
}
private static File resolveBuildDir() {
// get a URL reference to something we now is part of the classpath (us)
final URL classUrl = OsgiSessionFactoryService.class.getClassLoader().getResource(
OsgiSessionFactoryService.class.getName().replace( '.', '/' ) + ".class"
@ -157,26 +246,9 @@ private UrlReference hibernateKarafFeatureFile() throws Exception {
dir = dir.getParentFile();
}
// dir now points to the root classes output dir, go up one more...
dir = dir.getParentFile();
// go to the `karafFeatures` dir...
dir = new File( dir, "karafFeatures" );
// and look for the feature file there...
final String featureFileName = String.format(
Locale.ENGLISH,
"hibernate-osgi-%s-karaf.xml",
determineProjectVersion()
);
final URL url = new File( dir, featureFileName ).toURI().toURL();
return new UrlReference() {
@Override
public String getURL() {
return url.toExternalForm();
}
};
// dir now points to the root classes output dir, go up 2 more...
dir = dir.getParentFile().getParentFile();
return dir;
}
private String determineProjectVersion() throws Exception {
@ -187,8 +259,37 @@ private String determineProjectVersion() throws Exception {
return String.valueOf( buffer, 0, count );
}
private static File probeCfgXmlFile() {
File dir = resolveBuildDir();
dir = new File( dir, "resources" );
dir = new File( dir, "test" );
return new File( dir, "hibernate.cfg.xml" );
}
private static File probePersistenceXmlFile() {
File dir = resolveBuildDir();
dir = new File( dir, "resources" );
dir = new File( dir, "test" );
dir = new File( dir, "META-INF" );
return new File( dir, "persistence.xml" );
}
private static File probeBlueprintXmlFile() {
File dir = resolveBuildDir();
dir = new File( dir, "resources" );
dir = new File( dir, "test" );
dir = new File( dir, "OSGI-INF" );
dir = new File( dir, "blueprint" );
return new File( dir, "blueprint.xml" );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Prepare the PaxExam probe (the bundle to deploy)
@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
System.out.println( "Configuring probe..." );
// attempt to override PaxExam's default of dynamically importing everything
probe.setHeader( Constants.DYNAMICIMPORT_PACKAGE, "" );
// and use defined imports instead
@ -219,6 +320,14 @@ public static void setLocaleToEnglish() {
Locale.setDefault( Locale.ENGLISH );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// The tests
@Inject
@SuppressWarnings("UnusedDeclaration")
private BundleContext bundleContext;
@Test
public void testJpa() throws Exception {
final ServiceReference serviceReference = bundleContext.getServiceReference( PersistenceProvider.class.getName() );

View File

@ -56,7 +56,9 @@ ext {
// javax
jpa: 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final',
jta: 'org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.0.0.Final',
// There is a bug in the OSGi information in the JBoss one. See https://issues.jboss.org/browse/JBEE-160
//jta: 'org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.0.0.Final',
jta: 'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1',
validation: 'javax.validation:validation-api:1.1.0.Final',
jacc: 'org.jboss.spec.javax.security.jacc:jboss-jacc-api_1.4_spec:1.0.2.Final',