-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for scriptable datasets via Java scripting api (JSR 233) #47
Open
rmpestano
wants to merge
6
commits into
arquillian:master
Choose a base branch
from
rmpestano:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
010b65e
- adds support for scriptable datasets;
rmpestano 8977d21
- code format
rmpestano 35f988e
adds notes to jsr223 support.
rmpestano 47de511
- script result exception handling should not rely on null result.
rmpestano c971946
- removed unnecessary null check
rmpestano d7fd3b1
simplified script engine regex pattern
rmpestano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions
38
...ava/org/jboss/arquillian/persistence/core/exception/ScriptableDataSetEngineException.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,38 @@ | ||
/* | ||
* 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.core.exception; | ||
|
||
public class ScriptableDataSetEngineException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 508713667493794528L; | ||
|
||
public ScriptableDataSetEngineException() { | ||
} | ||
|
||
public ScriptableDataSetEngineException(String message) { | ||
super(message); | ||
} | ||
|
||
public ScriptableDataSetEngineException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public ScriptableDataSetEngineException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...org/jboss/arquillian/persistence/core/exception/ScriptableDataSetEvaluationException.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,38 @@ | ||
/* | ||
* 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.core.exception; | ||
|
||
public class ScriptableDataSetEvaluationException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 508713667493794528L; | ||
|
||
public ScriptableDataSetEvaluationException() { | ||
} | ||
|
||
public ScriptableDataSetEvaluationException(String message) { | ||
super(message); | ||
} | ||
|
||
public ScriptableDataSetEvaluationException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public ScriptableDataSetEvaluationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
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 javax.script.ScriptException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.jboss.arquillian.persistence.core.exception.ScriptableDataSetEvaluationException; | ||
import org.jboss.arquillian.persistence.core.exception.ScriptableDataSetEngineException; | ||
|
||
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 followed by ':' followed by 1 or more chars e.g: js: new Date().toString() | ||
private final Pattern scriptEnginePattern = Pattern.compile("^[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<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) { | ||
try { | ||
return getScriptResult(value.toString(), engine); | ||
} catch (Exception e) { | ||
throw new ScriptableDataSetEvaluationException(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 { | ||
throw new ScriptableDataSetEngineException(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); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the version to the property like for other artifacts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure