Skip to content

Instantly share code, notes, and snippets.

@mgalushka
Created December 4, 2014 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgalushka/faceb697807483e5927c to your computer and use it in GitHub Desktop.
Save mgalushka/faceb697807483e5927c to your computer and use it in GitHub Desktop.
Unsafe code in OpenRefine
import com.google.refine.expr.CellTuple;
import com.google.refine.expr.Evaluable;
import com.google.refine.jython.JythonEvaluable;
import com.google.refine.model.Cell;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Properties;
/**
* @author mgalushka
*/
public class JythonThreadUnsafe {
@Test
public void testJythonContextSafety() {
Properties props = new Properties();
Project project = new Project();
Row row = new Row(2);
row.setCell(0, new Cell("one", null));
row.setCell(1, new Cell("1", null));
props.put("columnName", "_ - value");
props.put("true", "true");
props.put("rowIndex", "0");
props.put("value", 1);
props.put("project", project);
props.put("call", "some cell");
props.put("false", "false");
props.put("cells", new CellTuple(project, row));
props.put("PI", "3.141592653589793");
props.put("row", row);
// Evaluate expression first time
Evaluable eval1 = new JythonEvaluable("a = value\nreturn a * 2");
Long value1 = (Long) eval1.evaluate(props);
// Just create some other(!!) non-related evaluable...
new JythonEvaluable("a = value\nreturn a * 3");
// Repeat same evaluation - obviously the result MUST be the same
Long value2 = (Long) eval1.evaluate(props);
// Check if 1st value == 2nd value
Assert.assertEquals(value1, value2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment