Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Removes script replacer in favor of scriptable dataset so the script evaluation happens for each row instead of each replacement.
  • Loading branch information
rmpestano committed Oct 19, 2017
1 parent 9aa2054 commit c72a6d7
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.database.rider.core.api.dataset;

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

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,37 @@
package com.github.database.rider.core.api.dataset;

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,100 @@
package com.github.database.rider.core.api.dataset;

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

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

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

//any non digit char (except 'regex') followed by ':' followed by 1 or more chars e.g: js: new Date().toString()
private static final Pattern scriptEnginePattern = Pattern.compile("^(?!regex)[a-zA-Z]+:.+");

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<>();
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) {
try {
return getScriptResult(value.toString(), engine);
} catch (Exception e) {
log.log(Level.WARNING,String.format("Could not evaluate script expression for table '%s', column '%s'. The original value will be used.", getTableMetaData().getTableName(), column),e);
}
}
}
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 {
throw new RuntimeException(String.format("Could not find script engine with name %s in classpath", engineName));
}
return engine;
}

}

/**
* Evaluates the script expression
*
* @return script expression result
*/
private Object getScriptResult(String script, ScriptEngine engine) throws ScriptException {
String scriptToExecute = script.substring(script.indexOf(":") + 1);
return engine.eval(scriptToExecute);
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
package com.github.database.rider.core.dataset;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.JarURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import com.github.database.rider.core.api.connection.ConnectionHolder;
import com.github.database.rider.core.api.dataset.DataSetExecutor;
import com.github.database.rider.core.api.dataset.JSONDataSet;
import com.github.database.rider.core.api.dataset.ScriptableDataSet;
import com.github.database.rider.core.api.dataset.YamlDataSet;
import com.github.database.rider.core.assertion.DataSetAssertion;
import com.github.database.rider.core.configuration.ConnectionConfig;
import com.github.database.rider.core.configuration.DBUnitConfig;
import com.github.database.rider.core.configuration.DataSetConfig;
import com.github.database.rider.core.connection.ConnectionHolderImpl;
import com.github.database.rider.core.connection.RiderDataSource;
import com.github.database.rider.core.exception.DataBaseSeedingException;
import com.github.database.rider.core.replacer.DateTimeReplacer;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.AmbiguousTableNameException;
import org.dbunit.database.DatabaseSequenceFilter;
import org.dbunit.dataset.CompositeDataSet;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.FilteredDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.*;
import org.dbunit.dataset.csv.CsvDataSet;
import org.dbunit.dataset.excel.XlsDataSet;
import org.dbunit.dataset.filter.DefaultColumnFilter;
Expand All @@ -39,19 +27,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.database.rider.core.api.connection.ConnectionHolder;
import com.github.database.rider.core.api.dataset.DataSetExecutor;
import com.github.database.rider.core.api.dataset.JSONDataSet;
import com.github.database.rider.core.api.dataset.YamlDataSet;
import com.github.database.rider.core.assertion.DataSetAssertion;
import com.github.database.rider.core.configuration.ConnectionConfig;
import com.github.database.rider.core.configuration.DBUnitConfig;
import com.github.database.rider.core.configuration.DataSetConfig;
import com.github.database.rider.core.connection.ConnectionHolderImpl;
import com.github.database.rider.core.connection.RiderDataSource;
import com.github.database.rider.core.exception.DataBaseSeedingException;
import com.github.database.rider.core.replacer.DateTimeReplacer;
import com.github.database.rider.core.replacer.ScriptReplacer;
import java.io.*;
import java.net.JarURLConnection;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Created by pestano on 26/07/15.
Expand Down Expand Up @@ -192,24 +178,24 @@ public IDataSet loadDataSet(String name) throws DataSetException, IOException {
String extension = dataSetName.substring(dataSetName.lastIndexOf('.') + 1).toLowerCase();
switch (extension) {
case "yml": {
target = new YamlDataSet(getDataSetStream(dataSetName), dbUnitConfig);
target = new ScriptableDataSet(new YamlDataSet(getDataSetStream(dataSetName), dbUnitConfig));
break;
}
case "xml": {
target = new FlatXmlDataSetBuilder().build(getDataSetStream(dataSetName));
target = new ScriptableDataSet(new FlatXmlDataSetBuilder().build(getDataSetStream(dataSetName)));
break;
}
case "csv": {
target = new CsvDataSet(
new File(getClass().getClassLoader().getResource(dataSetName).getFile()).getParentFile());
target = new ScriptableDataSet(new CsvDataSet(
new File(getClass().getClassLoader().getResource(dataSetName).getFile()).getParentFile()));
break;
}
case "xls": {
target = new XlsDataSet(getDataSetStream(dataSetName));
target = new ScriptableDataSet(new XlsDataSet(getDataSetStream(dataSetName)));
break;
}
case "json": {
target = new JSONDataSet(getDataSetStream(dataSetName));
target = new ScriptableDataSet(new JSONDataSet(getDataSetStream(dataSetName)));
break;
}
default:
Expand Down Expand Up @@ -389,7 +375,6 @@ public void executeStatements(String... statements) {

private IDataSet performReplacements(IDataSet dataSet) {
IDataSet replace = DateTimeReplacer.replace(dataSet);
replace = ScriptReplacer.replace(replace);
return replace;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.github.database.rider.core.replacer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.github.database.rider.core.api.replacer.*;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ReplacementDataSet;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* based on: http://marcin-michalski.pl/2012/10/22/decorating-dbunit-datasets-power-of-replacementdataset/
*/
Expand Down

This file was deleted.

Loading

0 comments on commit c72a6d7

Please sign in to comment.