Skip to content
Permalink
Browse files

Custom params for @testdata methods can be contributed via TestDataSe…

…tupAccessorFactories now
  • Loading branch information...
Alex Stockinger
Alex Stockinger committed Sep 7, 2017
1 parent 8426656 commit 8deb94cbcf5f4cc4dfdc91fb9ff04e0c449874f6
@@ -1,5 +1,6 @@
Version 0.6.0
- Hibernate support
- Custom params for @TestData methods can be contributed via TestDataSetupAccessorFactories now

Version 0.5.3
- Fixed issue with globally enabled interceptors and mocks
@@ -0,0 +1,22 @@
/*
* 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 org.jboss.weld.bootstrap.api.ServiceRegistry;

public interface TestDataSetupAccessorFactory {
Object createTestDataSetupAccessor(final ServiceRegistry serviceRegistry);
}
@@ -0,0 +1,36 @@
/*
* 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.jdbc;

import fi.testee.spi.TestDataSetupAccessorFactory;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.injection.spi.ResourceInjectionServices;

import javax.sql.DataSource;

public class JdbcTestDataSetupAccessorFactory implements TestDataSetupAccessorFactory {
@Override
public Object createTestDataSetupAccessor(final ServiceRegistry serviceRegistry) {
return testDataSources(serviceRegistry.get(ResourceInjectionServices.class));
}

private static TestDataSources testDataSources(final ResourceInjectionServices resourceInjectionServices) {
return mappedName -> (DataSource) resourceInjectionServices
.registerResourceInjectionPoint(null, mappedName)
.createResource()
.getInstance();
}
}
@@ -0,0 +1,37 @@
/*
* 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.jpa;

import fi.testee.services.JpaInjectionServicesImpl;
import fi.testee.spi.TestDataSetupAccessorFactory;
import org.jboss.weld.bootstrap.api.ServiceRegistry;

import javax.persistence.EntityManager;

public class JpaTestDataSetupAccessorFactory implements TestDataSetupAccessorFactory {
@Override
public Object createTestDataSetupAccessor(final ServiceRegistry serviceRegistry) {
// TODO it's somewhat hacky to require the actual implementation instead of the interface here
return testPersistenceUnits(serviceRegistry.get(JpaInjectionServicesImpl.class));
}

private static TestPersistenceUnits testPersistenceUnits(final JpaInjectionServicesImpl jpaInjectionServices) {
return unitName -> (EntityManager) jpaInjectionServices
.registerPersistenceContextInjectionPoint(unitName)
.createResource()
.getInstance();
}
}
@@ -17,16 +17,14 @@

import fi.testee.exceptions.TestEEfiException;
import fi.testee.jdbc.TestData;
import fi.testee.jdbc.TestDataSources;
import fi.testee.jpa.TestPersistenceUnits;
import fi.testee.services.JpaInjectionServicesImpl;
import fi.testee.spi.TestDataSetupAccessorFactory;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.injection.spi.ResourceInjectionServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -36,7 +34,6 @@
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toSet;

@@ -45,39 +42,22 @@
*
* @author Alex Stockinger, IT-Stockinger
*/
final class TestDataSetup {
public class TestDataSetup {
private static final Logger LOG = LoggerFactory.getLogger(TestDataSetup.class);

private TestDataSetup() {
}
@Inject
@Any
private Instance<TestDataSetupAccessorFactory> providers;

static void setupTestData(
public void setupTestData(
final Class<?> setupClass,
final ServiceRegistry serviceRegistry
) {
final JpaInjectionServicesImpl jpaInjectionServices = serviceRegistry.get(JpaInjectionServicesImpl.class);
final ResourceInjectionServices resourceInjectionServices = serviceRegistry.get(ResourceInjectionServices.class);
final Set<Object> testDataSetupAccessors = new HashSet<>(asList(
testDataSources(resourceInjectionServices),
testPersistenceUnits(jpaInjectionServices)
));
final Set<Object> testDataSetupAccessors = new HashSet<>();
providers.forEach(it -> testDataSetupAccessors.add(it.createTestDataSetupAccessor(serviceRegistry)));
setupTestData(setupClass, testDataSetupAccessors);
}

private static TestPersistenceUnits testPersistenceUnits(final JpaInjectionServicesImpl jpaInjectionServices) {
return unitName -> (EntityManager) jpaInjectionServices
.registerPersistenceContextInjectionPoint(unitName)
.createResource()
.getInstance();
}

private static TestDataSources testDataSources(final ResourceInjectionServices resourceInjectionServices) {
return mappedName -> (DataSource) resourceInjectionServices
.registerResourceInjectionPoint(null, mappedName)
.createResource()
.getInstance();
}

public static void setupTestData(final Class<?> setupClass, final Collection<Object> testDataSetupAccessors) {
Class<?> currentClass = setupClass;
final List<Method> methodsToInvoke = new ArrayList<>();
@@ -29,6 +29,7 @@
import fi.testee.spi.ReleaseCallbackHandler;
import fi.testee.spi.Releaser;
import fi.testee.spi.ResourceProvider;
import fi.testee.spi.TestDataSetupAccessorFactory;
import fi.testee.spi.scope.TestSetupScope;
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
@@ -115,7 +116,7 @@ private void setupTestData(final ResourceProvider setupResources) {
final Set<DataSourceMigrator> migrators = getInstancesOf(DataSourceMigrator.class, testDataReleaser);
final ServiceRegistry serviceRegistry = context.getDependencyInjection().getServiceRegistry();
DatabaseMigration.migrateDataSources(setupClass, migrators, serviceRegistry);
TestDataSetup.setupTestData(setupClass, serviceRegistry);
getInstanceOf(TestDataSetup.class, testDataReleaser).setupTestData(setupClass, serviceRegistry);
context.flushEntityManagers();
} finally {
testDataReleaser.release();

0 comments on commit 8deb94c

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