-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds support for scriptable datasets;
- removes windows special characters;
- Loading branch information
Showing
20 changed files
with
500 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...in/java/org/jboss/arquillian/persistence/dbunit/dataset/scriptable/ScriptableDataSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...org/jboss/arquillian/persistence/dbunit/dataset/scriptable/ScriptableDataSetIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
|
||
} |
118 changes: 118 additions & 0 deletions
118
...main/java/org/jboss/arquillian/persistence/dbunit/dataset/scriptable/ScriptableTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.