Permalink
Browse files

[ARQ-1355] Introduced injectable dbunit DatabaseConnection through @A…

…rquillianResource annotation.
  • Loading branch information...
1 parent d34bbbb commit bd1725843bdbca908bc42caeb95b20cfd1fcb366 @bartoszmajsak bartoszmajsak committed Mar 17, 2013
@@ -74,7 +74,7 @@ private void serializeFieldsToProperties(final OutputStream output)
private Map<String, String> mapFieldsToProperties() throws IllegalArgumentException, IllegalAccessException
{
final Map<String,String> extractedValues = new HashMap<String, String>();
- final List<Field> fields = SecurityActions.getAccessibleFields(configuration.getClass());
+ final List<Field> fields = ReflectionHelper.getAccessibleFields(configuration.getClass());
for (Field field : fields)
{
@@ -137,7 +137,7 @@ private String convertFromPropertyKey(String key)
private void createConfiguration(final Map<String, String> fieldsWithValues)
{
final ConfigurationTypeConverter typeConverter = new ConfigurationTypeConverter();
- final Collection<Field> fields = SecurityActions.getAccessibleFields(configuration.getClass());
+ final Collection<Field> fields = ReflectionHelper.getAccessibleFields(configuration.getClass());
for (Field field : fields)
{
@@ -35,7 +35,7 @@
*
* @version $Revision: $
*/
-final class SecurityActions {
+final class ReflectionHelper {
// -------------------------------------------------------------------------------||
// Constructor
@@ -45,7 +45,7 @@
/**
* No instantiation
*/
- private SecurityActions() {
+ private ReflectionHelper() {
throw new UnsupportedOperationException("No instantiation");
}
@@ -27,11 +27,13 @@
import org.jboss.arquillian.persistence.core.lifecycle.SchemaCreationScriptsExecutor;
import org.jboss.arquillian.persistence.dbunit.DBUnitDataHandler;
import org.jboss.arquillian.persistence.dbunit.DBUnitDataStateLogger;
+import org.jboss.arquillian.persistence.dbunit.DBUnitDatabaseConnectionProvider;
import org.jboss.arquillian.persistence.dbunit.DBUnitPersistenceTestLifecycleHandler;
import org.jboss.arquillian.persistence.dbunit.configuration.DBUnitConfigurationRemoteProducer;
import org.jboss.arquillian.persistence.jpa.cache.JpaCacheEvictionHandler;
import org.jboss.arquillian.persistence.script.configuration.ScriptingConfigurationRemoteProducer;
import org.jboss.arquillian.persistence.transaction.PersistenceExtensionConventionTransactionEnabler;
+import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;
import org.jboss.arquillian.transaction.spi.provider.TransactionEnabler;
/**
@@ -63,7 +65,8 @@ private void registerDBUnitHandlers(ExtensionBuilder builder)
builder.observer(DBUnitDataHandler.class)
.observer(DBUnitConfigurationRemoteProducer.class)
.observer(DBUnitPersistenceTestLifecycleHandler.class)
- .observer(DBUnitDataStateLogger.class);
+ .observer(DBUnitDataStateLogger.class)
+ .service(ResourceProvider.class, DBUnitDatabaseConnectionProvider.class);
}
private void registerTestLifecycleHandlers(ExtensionBuilder builder)
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.arquillian.persistence.dbunit;
+
+import java.lang.annotation.Annotation;
+
+import org.dbunit.database.DatabaseConnection;
+import org.jboss.arquillian.core.api.Instance;
+import org.jboss.arquillian.core.api.annotation.Inject;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;
+
+/**
+ * Injects DBUnit database connection through {@link ArquillianResource} annotation to the test class instance.
+ *
+ * @author <a href="mailto:bartosz.majsak@gmail.com">Bartosz Majsak</a>
+ *
+ */
+public class DBUnitDatabaseConnectionProvider implements ResourceProvider
+{
+
+ @Inject
+ private Instance<DatabaseConnection> databaseConnectionInstance;
+
+ /* (non-Javadoc)
+ * @see org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider#canProvide(java.lang.Class)
+ */
+ @Override
+ public boolean canProvide(Class<?> type)
+ {
+ return DatabaseConnection.class.isAssignableFrom(type);
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider#lookup(org.jboss.arquillian.test.api.ArquillianResource, java.lang.annotation.Annotation[])
+ */
+ @Override
+ public Object lookup(ArquillianResource resource, Annotation... qualifiers)
+ {
+ return databaseConnectionInstance.get();
+ }
+
+}
@@ -47,11 +47,10 @@
import org.jboss.arquillian.persistence.dbunit.exception.DBUnitInitializationException;
import org.jboss.arquillian.test.spi.annotation.ClassScoped;
import org.jboss.arquillian.test.spi.annotation.TestScoped;
-import org.jboss.arquillian.test.spi.event.suite.TestEvent;
/**
*
- * @author Bartosz Majsak
+ * @author <a href="mailto:bartosz.majsak@gmail.com">Bartosz Majsak</a>
*
*/
public class DBUnitPersistenceTestLifecycleHandler
@@ -81,12 +80,20 @@
public void provideDatabaseConnectionAroundBeforePersistenceTest(@Observes(precedence = 100000) EventContext<BeforePersistenceTest> context)
{
- provideDatabaseConnection(context);
+ createDatabaseConnection();
+ context.proceed();
}
public void provideDatabaseConnectionAroundAfterPersistenceTest(@Observes(precedence = 100000) EventContext<AfterPersistenceTest> context)
{
- provideDatabaseConnection(context);
+ try
+ {
+ context.proceed();
+ }
+ finally
+ {
+ closeDatabaseConnection();
+ }
}
public void createDatasets(@Observes(precedence = 1000) EventContext<BeforePersistenceTest> context)
@@ -110,29 +117,11 @@ public void createDatasets(@Observes(precedence = 1000) EventContext<BeforePersi
// ------------------------------------------------------------------------------------------------
- private void provideDatabaseConnection(EventContext<? extends TestEvent> context)
- {
- createDatabaseConnection();
- try
- {
- context.proceed();
- }
- finally
- {
- closeDatabaseConnection();
- }
- }
-
private void createDatabaseConnection()
{
try
{
- if (databaseConnectionProducer.get() != null && !databaseConnectionProducer.get().getConnection().isClosed())
- {
- closeDatabaseConnection();
- }
-
DataSource dataSource = dataSourceInstance.get();
final String schema = dbUnitConfigurationInstance.get().getSchema();
DatabaseConnection databaseConnection = null;
Oops, something went wrong.

0 comments on commit bd17258

Please sign in to comment.