Skip to content
Permalink
Browse files

Hibernate support

  • Loading branch information...
Alex Stockinger
Alex Stockinger committed Aug 16, 2017
1 parent d9c690c commit 25c4999c4d7045d2be5b11841ba971e54fe70343
@@ -154,6 +154,7 @@ ext {
libCucumberJava = 'info.cukes:cucumber-java:1.2.5'
libHamcrest = 'org.hamcrest:hamcrest-all:1.3'
libEclipseLink = 'org.eclipse.persistence:eclipselink:2.6.0'
libHibernate = 'org.hibernate:hibernate-core:5.2.10.Final'
libH2 = 'com.h2database:h2:1.4.196'
libGroovy = 'org.codehaus.groovy:groovy-all:2.4.12'
libPsql = 'org.postgresql:postgresql:42.1.3'
@@ -1,4 +1,5 @@
Version 0.6.0
- Hibernate support
- Fixed classpath entry merging issues with Gradle 4 (found and fixed with 4.1)
- Using Gradle 4.1 for build

@@ -0,0 +1,25 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fi.testee.spi;

import java.util.Properties;

public interface PersistenceUnitPropertyContributor {
void contribute(
final Properties properties,
final String provider
);
}
@@ -15,6 +15,7 @@
*/
package fi.testee.jpa;

import fi.testee.spi.PersistenceUnitPropertyContributor;
import fi.testee.classpath.ClasspathResource;
import fi.testee.classpath.JavaArchive;
import fi.testee.deployment.BeanArchiveDiscovery;
@@ -63,16 +64,21 @@
public class PersistenceUnitDiscovery {
private static final Logger LOG = LoggerFactory.getLogger(PersistenceUnitDiscovery.class);
private static final DocumentBuilder BUILDER = createDocumentBuilder();

private final PersistenceUnitPropertyContributor contributor;
private final BeanArchiveDiscovery beanArchiveDiscovery;
private final ResourceInjectionServices resourceInjectionServices;

private Map<String, ? extends PersistenceUnitInfo> units;
private BeanArchiveDiscovery beanArchiveDiscovery;
private ResourceInjectionServices resourceInjectionServices;

public PersistenceUnitDiscovery(
final BeanArchiveDiscovery beanArchiveDiscovery,
final ResourceInjectionServices resourceInjectionServices
final ResourceInjectionServices resourceInjectionServices,
final PersistenceUnitPropertyContributor contributor
) {
this.beanArchiveDiscovery = beanArchiveDiscovery;
this.resourceInjectionServices = resourceInjectionServices;
this.contributor = contributor;
}

private static DocumentBuilder createDocumentBuilder() {
@@ -128,6 +134,7 @@ private PersistenceUnitInfoImpl createUnitInfo(
final String propValue = prop.getAttribute("value");
properties.put(propName, propValue);
}
contributor.contribute(properties, provider);
final List<URL> jarFileUrls = collectStringListElements(unit, xpath, "jar-file").stream()
.map(UrlUtils::toUrl)
.collect(toList());
@@ -31,14 +31,18 @@
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.injection.spi.ResourceInjectionServices;
import org.jboss.weld.transaction.spi.TransactionServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.transaction.Synchronization;
import javax.transaction.UserTransaction;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import static fi.testee.deployment.DeploymentImpl.UNMODIFIED;
import static fi.testee.runtime.ManualResourceProviderBuilder.manualResourceProvider;
@@ -123,17 +127,45 @@ private void setupTestData(final ResourceProvider setupResources) {
}
}

private ResourceProvider createSetupResources(final boolean rollbackTransactions) {
private ResourceProvider createSetupResources(
final boolean rollbackTransactions
) {
return manualResourceProvider()
.put("testeefi/setup/class", setupClass)
.put("testeefi/setup/beanArchiveDiscovery", runtime.getBeanArchiveDiscorvery())
.put("testeefi/setup/connectionFactoryManager", (ConnectionFactoryManager) this::connectionFactoryManager)
.put("testeefi/setup/dependencyInjection", testSetupDependencyInjection())
.put("testeefi/setup/annotationScanner", (AnnotationScanner) runtime.getBeanArchiveDiscorvery()::getClassesWith)
.put("testeefi/setup/rollbackTransactions", rollbackTransactions)
.put("testeefi/setup/transactionServices", transactionServices())
.build();
}

private TransactionServices transactionServices() {
final Supplier<TransactionServices> delegate = () -> getServiceRegistry().get(TransactionServices.class);
return new TransactionServices() {
@Override
public void registerSynchronization(final Synchronization synchronizedObserver) {
delegate.get().registerSynchronization(synchronizedObserver);
}

@Override
public boolean isTransactionActive() {
return delegate.get().isTransactionActive();
}

@Override
public UserTransaction getUserTransaction() {
return delegate.get().getUserTransaction();
}

@Override
public void cleanup() {
delegate.get().cleanup();
}
};
}

private DependencyInjection testSetupDependencyInjection() {
return new DeferredDependencyInjection(() -> this);
}
@@ -30,9 +30,10 @@
import fi.testee.services.ResourceInjectionServicesImpl;
import fi.testee.services.SecurityServicesImpl;
import fi.testee.services.TransactionServicesImpl;
import fi.testee.spi.DynamicArchiveContributor;
import fi.testee.spi.BeansXmlModifier;
import fi.testee.spi.DependencyInjection;
import fi.testee.spi.DynamicArchiveContributor;
import fi.testee.spi.PersistenceUnitPropertyContributor;
import fi.testee.spi.ReleaseCallbackHandler;
import fi.testee.spi.Releaser;
import fi.testee.spi.ResourceProvider;
@@ -94,6 +95,9 @@
@Inject
@Any
private Instance<ResourceProvider> resourceProvidersInstance;
@Inject
@Any
private Instance<PersistenceUnitPropertyContributor> propertyContributorInstance;

private DependencyInjectionRealm realm;
private EjbContainer ejbContainer;
@@ -121,8 +125,8 @@ public void initialize(
beanArchiveDiscovery,
sessionBeanAlternatives,
ejbContainer::lookupDescriptor,
ejbContainer::createInstance
);
ejbContainer::createInstance,
propertyContributor());
realm = new DependencyInjectionRealm().init(
instanceServiceRegistry,
beanArchiveDiscovery,
@@ -228,7 +232,8 @@ private static ServiceRegistry createInstanceServiceRegistry(
final BeanArchiveDiscovery beanArchiveDiscovery,
final SessionBeanAlternatives alternatives,
final EjbInjectionServicesImpl.EjbLookup ejbLookup,
final EjbInjectionServicesImpl.EjbFactory ejbFactory
final EjbInjectionServicesImpl.EjbFactory ejbFactory,
final PersistenceUnitPropertyContributor propertyContributor
) {
final ServiceRegistry serviceRegistry = new SimpleServiceRegistry();
// Resource injection
@@ -239,7 +244,8 @@ private static ServiceRegistry createInstanceServiceRegistry(
// JPA injection
JpaInjectionServicesImpl jpaInjectionService = createJpaInjectionService(
beanArchiveDiscovery,
serviceRegistry.get(ResourceInjectionServices.class)
serviceRegistry.get(ResourceInjectionServices.class),
propertyContributor
);
serviceRegistry.add(JpaInjectionServices.class, jpaInjectionService);
serviceRegistry.add(JpaInjectionServicesImpl.class, jpaInjectionService);
@@ -259,15 +265,23 @@ private static ServiceRegistry createInstanceServiceRegistry(

private static JpaInjectionServicesImpl createJpaInjectionService(
final BeanArchiveDiscovery beanArchiveDiscovery,
final ResourceInjectionServices resourceInjectionServices
final ResourceInjectionServices resourceInjectionServices,
final PersistenceUnitPropertyContributor propertyContributor
) {
final PersistenceUnitDiscovery persistenceUnitDiscovery = new PersistenceUnitDiscovery(
beanArchiveDiscovery,
resourceInjectionServices
resourceInjectionServices,
propertyContributor
);
return new JpaInjectionServicesImpl(persistenceUnitDiscovery);
}

private PersistenceUnitPropertyContributor propertyContributor() {
final Collection<PersistenceUnitPropertyContributor> contributors = new HashSet<>();
propertyContributorInstance.forEach(contributors::add);
return (properties, provider) -> contributors.forEach(it -> it.contribute(properties, provider));
}

public <T> T run(final TransactionRunnable<T> runnable) {
return runnable.run(testSetupClass, realm);
}
@@ -15,6 +15,7 @@
*/
package fi.testee.services;

import fi.testee.exceptions.TestEEfiException;
import org.jboss.weld.transaction.spi.TransactionServices;

import javax.transaction.Synchronization;
@@ -40,7 +41,7 @@ public boolean isTransactionActive() {
@Override
public UserTransaction getUserTransaction() {
// TODO figure out what to do here
return null;
throw new AssertionError("getUserTransaction");
}

@Override
@@ -0,0 +1,6 @@
apply plugin: 'java'

dependencies {
compile project(":core-spi")
compile libHibernate
}
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fi.testee.hibernate;

import org.hibernate.boot.archive.spi.ArchiveContext;
import org.hibernate.boot.archive.spi.ArchiveDescriptor;

import java.util.Collection;

public class CompositeArchiveDescriptor implements ArchiveDescriptor {
private final Collection<ArchiveDescriptor> archiveDescriptors;

public CompositeArchiveDescriptor(final Collection<ArchiveDescriptor> archiveDescriptors) {
this.archiveDescriptors = archiveDescriptors;
}

@Override
public void visitArchive(final ArchiveContext archiveContext) {
archiveDescriptors.forEach(it -> it.visitArchive(archiveContext));
}
}
Oops, something went wrong.

0 comments on commit 25c4999

Please sign in to comment.
You can’t perform that action at this time.