Skip to content

Commit

Permalink
- adds support for scriptable datasets;
Browse files Browse the repository at this point in the history
- removes windows special characters;
  • Loading branch information
rmpestano committed May 29, 2016
1 parent 5335f66 commit 010b65e
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ public void should_insert_special_entities_with_custom_end_line() throws Excepti
verify(connection.createStatement(), times(1)).execute(statementsCaptor.capture());
assertThat(statementsCaptor.getAllValues()).containsSequence(
"insert into useraccount (id, firstname, lastname, username, password)" +
" values (1, 'John', 'Smith & Company', 'doovde;;', '&test©')\nGO"
" values (1, 'John', 'Smith & Company', 'doovde;;', '&test©')" +
System.getProperty("line.separator") +
"GO"
);
}

Expand Down
6 changes: 6 additions & 0 deletions dbunit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.6</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.jboss.arquillian.persistence.dbunit.configuration.DBUnitConfiguration;
import org.jboss.arquillian.persistence.dbunit.configuration.DBUnitDataSeedStrategyProvider;
import org.jboss.arquillian.persistence.dbunit.dataset.DataSetRegister;
import org.jboss.arquillian.persistence.dbunit.dataset.scriptable.ScriptableDataSet;
import org.jboss.arquillian.persistence.dbunit.event.CompareDBUnitData;
import org.jboss.arquillian.persistence.dbunit.event.PrepareDBUnitData;
import org.jboss.arquillian.persistence.dbunit.exception.DBUnitConnectionException;
Expand Down Expand Up @@ -95,7 +96,11 @@ public void compare(@Observes CompareDBUnitData compareDataEvent) {
if (excludeTables.length != 0) {
currentDataSet = new FilteredDataSet(new ExcludeTableFilter(excludeTables), currentDataSet);
}
final IDataSet expectedDataSet = mergeDataSets(dataSetRegister.get().getExpected());
IDataSet expectedDataSet = mergeDataSets(dataSetRegister.get().getExpected());

if (dbunitConfigurationInstance.get().isScriptableDataSets()) {
expectedDataSet = new ScriptableDataSet(expectedDataSet);
}
final DataSetComparator dataSetComparator = new DataSetComparator(compareDataEvent.getSortByColumns(),
compareDataEvent.getColumnsToExclude(), compareDataEvent.getCustomColumnFilters());
dataSetComparator.compare(currentDataSet, expectedDataSet, assertionErrorCollector.get());
Expand Down Expand Up @@ -143,6 +148,10 @@ private void seedDatabase() throws Exception {
final ITableFilter databaseSequenceFilter = sequenceFilterProvider.provide(connection, initialDataSet.getTableNames());
initialDataSet = new FilteredDataSet(databaseSequenceFilter, initialDataSet);
}

if(dbunitConfigurationInstance.get().isScriptableDataSets()){
initialDataSet = new ScriptableDataSet(initialDataSet);
}
seedingStrategy().execute(connection, initialDataSet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public class DBUnitConfiguration extends Configuration {

private String customTableFilter;

private boolean scriptableDataSets = false;


public DBUnitConfiguration() {
super("persistence-dbunit", "arquillian.extension.persistence.dbunit.");
}
Expand Down Expand Up @@ -436,4 +439,16 @@ public String getCustomTableFilter() {
public void setCustomTableFilter(String customTableFilter) {
this.customTableFilter = customTableFilter;
}

public boolean isScriptableDataSets() {
return scriptableDataSets;
}

/**
* @param scriptableDataSets Enable or disable usage of script language in datasets.
* Default value is <code>false</code>
*/
public void setScriptableDataSets(boolean scriptableDataSets) {
this.scriptableDataSets = scriptableDataSets;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.arquillian.persistence.dbunit.dataset.scriptable;

import org.dbunit.dataset.AbstractDataSet;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITableIterator;

/**
* @author <a href="mailto:[email protected]">Rafael Pestano</a>
*
*/
public class ScriptableDataSet extends AbstractDataSet {

private IDataSet delegate;

public ScriptableDataSet(IDataSet delegate) {
this.delegate = delegate;
}

@Override
protected ITableIterator createIterator(boolean reversed) throws DataSetException {
return new ScriptableDataSetIterator(reversed ? delegate.reverseIterator() : delegate.iterator());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. 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.dataset.scriptable;

import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.ITableIterator;
import org.dbunit.dataset.ITableMetaData;

/**
* @author <a href="mailto:[email protected]">Rafael Pestano</a>
*
*/
public class ScriptableDataSetIterator implements ITableIterator{

private ITableIterator delegate;

public ScriptableDataSetIterator(ITableIterator delegate) {
this.delegate = delegate;
}

@Override
public boolean next() throws DataSetException {
return delegate.next();
}

@Override
public ITableMetaData getTableMetaData() throws DataSetException {
return delegate.getTableMetaData();
}

@Override
public ITable getTable() throws DataSetException {
return new ScriptableTable(delegate.getTable());
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. 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.dataset.scriptable;

import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.ITableMetaData;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
* @author <a href="mailto:[email protected]">Rafael Pestano</a>
* <p/>
* Adds support for script language (JSR 223) in table values.
*/
public class ScriptableTable implements ITable {

//any non digit char followed by ':' followed by 1 or more chars e.g: js: new Date().toString()
private final Pattern scriptEnginePattern = Pattern.compile(".*\\D.*:.+");

private static Logger log = Logger.getLogger(ScriptableTable.class.getName());

private Map<String, ScriptEngine> engines;

private ScriptEngineManager manager;

private ITable delegate;


public ScriptableTable(ITable delegate) {
this.delegate = delegate;
engines = new HashMap<String, ScriptEngine>();
manager = new ScriptEngineManager();
}

@Override
public ITableMetaData getTableMetaData() {
return delegate.getTableMetaData();
}

@Override
public int getRowCount() {
return delegate.getRowCount();
}

@Override
public Object getValue(int row, String column) throws DataSetException {
Object value = delegate.getValue(row, column);
if (value != null && scriptEnginePattern.matcher(value.toString()).matches()) {
ScriptEngine engine = getScriptEngine(value.toString().trim());
if (engine != null) {
Object scriptResult = getScriptResult(value.toString(), engine);
if (scriptResult != null) {
value = scriptResult;
} else {
throw new RuntimeException(String.format("Could not evaluate script expression for table '%s', column '%s'.", getTableMetaData().getTableName(), column));
}
}
}
return value;
}

/**
* Parses table cell to get script engine
*
* @param value the table cell
* @return scriptEngine
*/
private ScriptEngine getScriptEngine(String value) {
String engineName = value.substring(0, value.indexOf(":"));
if (engines.containsKey(engineName)) {
return engines.get(engineName);
} else {
ScriptEngine engine = manager.getEngineByName(engineName);
if (engine != null) {
engines.put(engineName, engine);
} else {
log.warning(String.format("Could not find script engine with name %s in classpath", engineName));
}
return engine;
}

}

/**
* Evaluates the script expression
*
* @return script expression result or null if any evaluation error
*/
private Object getScriptResult(String script, ScriptEngine engine) {
String scriptToExecute = script.substring(script.indexOf(":") + 1);
try {
return engine.eval(scriptToExecute);
} catch (Exception e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.arquillian.persistence.dbunit.deployment;

import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
import org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveAppender;
import org.jboss.arquillian.core.api.Instance;
Expand All @@ -25,13 +26,13 @@
import org.jboss.arquillian.persistence.dbunit.container.RemoteDBUnitExtension;
import org.jboss.arquillian.persistence.dbunit.filter.DefaultDatabaseSequenceFilterProvider;
import org.jboss.arquillian.persistence.dbunit.filter.OracleDatabaseSequenceFilterProvider;
import org.jboss.arquillian.persistence.dbunit.filter.TableFilterResolver;
import org.jboss.arquillian.persistence.spi.dbunit.filter.TableFilterProvider;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

import javax.script.ScriptEngineFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -62,6 +63,20 @@ public Archive<?> createAuxiliaryArchive() {
.addAsServiceProvider(RemoteLoadableExtension.class, RemoteDBUnitExtension.class)
.addAsServiceProvider(TableFilterProvider.class, DefaultDatabaseSequenceFilterProvider.class, OracleDatabaseSequenceFilterProvider.class);

if(dbunitConfigurationInstance.get().isScriptableDataSets()){
dbUnitExtensionArchive.addPackages(true,
Filters.exclude(".*/package-info.*"),
"groovy",
"org.codehaus.groovy",
"groovyjarjarcommonscli",
"groovyjarjarasm.asm",
"groovyjarjarantlr"
);
dbUnitExtensionArchive.
addAsServiceProvider(ScriptEngineFactory.class, GroovyScriptEngineFactory.class)
.addAsManifestResource("META-INF/dgminfo", "dgminfo")
.addAsManifestResource("META-INF/groovy-release-info.properties", "groovy-release-info.properties");
}
return dbUnitExtensionArchive;
}

Expand Down
Loading

0 comments on commit 010b65e

Please sign in to comment.