Refactor tests to hoist common methods into RefineTest

Moves the invoke() method and the associated fields into
the super class and deletes the redundant implementations.
This commit is contained in:
Tom Morris 2020-08-22 17:33:40 -04:00
parent 9ac54edbba
commit 392a64b25e
20 changed files with 34 additions and 563 deletions

View File

@ -43,6 +43,7 @@ import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.powermock.modules.testng.PowerMockTestCase; import org.powermock.modules.testng.PowerMockTestCase;
@ -57,6 +58,8 @@ import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.IntNode; import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode; import com.fasterxml.jackson.databind.node.TextNode;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.importers.SeparatorBasedImporter; import com.google.refine.importers.SeparatorBasedImporter;
import com.google.refine.importing.ImportingJob; import com.google.refine.importing.ImportingJob;
import com.google.refine.importing.ImportingManager; import com.google.refine.importing.ImportingManager;
@ -75,6 +78,8 @@ import edu.mit.simile.butterfly.ButterflyModule;
*/ */
public class RefineTest extends PowerMockTestCase { public class RefineTest extends PowerMockTestCase {
protected static Properties bindings = null;
protected Logger logger; protected Logger logger;
boolean testFailed; boolean testFailed;
@ -308,6 +313,30 @@ public class RefineTest extends PowerMockTestCase {
verify(options, times(1)).get(name); verify(options, times(1)).get(name);
} }
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
protected static Object invoke(String name, Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (bindings == null) {
bindings = new Properties();
}
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@AfterMethod
public void TearDown() throws Exception {
bindings = null;
}
protected ButterflyModule getCoreModule() { protected ButterflyModule getCoreModule() {
ButterflyModule coreModule = mock(ButterflyModule.class); ButterflyModule coreModule = mock(ButterflyModule.class);
when(coreModule.getName()).thenReturn("core"); when(coreModule.getName()).thenReturn("core");

View File

@ -44,15 +44,11 @@ import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.Coalesce;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class CoalesceTests extends RefineTest { public class CoalesceTests extends RefineTest {
private static Properties bindings;
private static final Integer[] ZERO_TO_TWO = new Integer[] {0, 1, 2}; private static final Integer[] ZERO_TO_TWO = new Integer[] {0, 1, 2};
@Override @Override
@ -71,22 +67,6 @@ public class CoalesceTests extends RefineTest {
bindings = null; bindings = null;
} }
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testCoalesceInvalidParams() { public void testCoalesceInvalidParams() {
Assert.assertTrue(invoke("coalesce") instanceof EvalError); Assert.assertTrue(invoke("coalesce") instanceof EvalError);

View File

@ -41,8 +41,6 @@ import com.google.refine.expr.EvalError;
import com.google.refine.expr.HasFieldsListImpl; import com.google.refine.expr.HasFieldsListImpl;
import com.google.refine.expr.WrappedCell; import com.google.refine.expr.WrappedCell;
import com.google.refine.expr.WrappedRow; import com.google.refine.expr.WrappedRow;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.model.Row; import com.google.refine.model.Row;
@ -52,7 +50,6 @@ import com.google.refine.util.TestUtils;
* Test cases for cross function. * Test cases for cross function.
*/ */
public class CrossTests extends RefineTest { public class CrossTests extends RefineTest {
static Properties bindings;
private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
@Override @Override
@ -374,22 +371,6 @@ public class CrossTests extends RefineTest {
"cross expects a cell or value, a project name to look up (optional), and a column name in that project (optional)"); "cross expects a cell or value, a project name to look up (optional), and a column name in that project (optional)");
} }
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void serializeCross() { public void serializeCross() {
String json = "{\"description\":\"Looks up the given value in the target column of the target project, returns an array of matched rows. Two values match if and only if they have the same string representation. " + String json = "{\"description\":\"Looks up the given value in the target column of the target project, returns an array of matched rows. Two values match if and only if they have the same string representation. " +

View File

@ -42,14 +42,10 @@ import java.util.Properties;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.Type;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class TypeTests extends RefineTest { public class TypeTests extends RefineTest {
private static Properties bindings;
static final List<String> listArray = Arrays.asList("v1", "v2", "v3"); static final List<String> listArray = Arrays.asList("v1", "v2", "v3");
private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
@ -69,22 +65,6 @@ public class TypeTests extends RefineTest {
bindings = null; bindings = null;
} }
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testTypeInvalidParams() { public void testTypeInvalidParams() {
Assert.assertTrue(invoke("type") instanceof EvalError); Assert.assertTrue(invoke("type") instanceof EvalError);

View File

@ -32,16 +32,11 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.arrays.InArray;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class InArrayTests extends RefineTest { public class InArrayTests extends RefineTest {
@ -51,16 +46,6 @@ public class InArrayTests extends RefineTest {
static final String stringArray[] = {"v1","v2","v3"}; static final String stringArray[] = {"v1","v2","v3"};
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
@Test @Test
public void serializeInArray() { public void serializeInArray() {
String json = "{\"description\":\"Checks if array a contains string s\",\"params\":\"array a, string s\",\"returns\":\"boolean\"}"; String json = "{\"description\":\"Checks if array a contains string s\",\"params\":\"array a, string s\",\"returns\":\"boolean\"}";
@ -93,18 +78,5 @@ public class InArrayTests extends RefineTest {
Assert.assertTrue((boolean) invoke("inArray", arrayNode, "v1")); Assert.assertTrue((boolean) invoke("inArray", arrayNode, "v1"));
Assert.assertFalse((boolean) invoke("inArray", arrayNode, "v4")); Assert.assertFalse((boolean) invoke("inArray", arrayNode, "v4"));
} }
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
} }

View File

@ -33,24 +33,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.expr.functions.booleans; package com.google.refine.expr.functions.booleans;
import java.io.IOException;
import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.model.Cell;
import com.google.refine.model.ModelException;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
public class BooleanTests extends RefineTest { public class BooleanTests extends RefineTest {
@ -72,54 +61,12 @@ public class BooleanTests extends RefineTest {
{"xor","false","true","false","true"}, {"xor","false","true","false","true"},
}; };
static private Properties bindings;
private Project project;
@Override @Override
@BeforeTest @BeforeTest
public void init() { public void init() {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void SetUp() throws IOException, ModelException {
bindings = new Properties();
project = createProjectWithColumns("BooleanTests", "Column A");
bindings.put("project", project);
// Five rows of a's and five of 1s
for (int i = 0; i < 10; i++) {
Row row = new Row(1);
row.setCell(0, new Cell(i < 5 ? "a":new Integer(1), null));
project.rows.add(row);
}
}
@AfterMethod
public void TearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testInvalidParams() { public void testInvalidParams() {
for (String op : new String[] {"and","or","xor"}) { for (String op : new String[] {"and","or","xor"}) {

View File

@ -35,15 +35,10 @@ import java.util.TimeZone;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.functions.date.DatePart;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
@ -57,32 +52,6 @@ public class DatePartTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSSSSSX"); private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSSSSSX");
@Test @Test

View File

@ -29,25 +29,18 @@ package com.google.refine.expr.functions.date;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.date.Inc;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class IncTests extends RefineTest { public class IncTests extends RefineTest {
private static Properties bindings;
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSSSSSX"); private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSSSSSX");
@Override @Override
@ -55,33 +48,7 @@ public class IncTests extends RefineTest {
public void init() { public void init() {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void setUp() {
bindings = new Properties();
}
@AfterMethod
public void tearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testInc() { public void testInc() {
OffsetDateTime source = OffsetDateTime.parse("20180510-23:55:44.000789000Z", OffsetDateTime source = OffsetDateTime.parse("20180510-23:55:44.000789000Z",

View File

@ -28,24 +28,17 @@ package com.google.refine.expr.functions.date;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.functions.date.Now;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class NowTests extends RefineTest { public class NowTests extends RefineTest {
private static Properties bindings;
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSSSSSX"); private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSSSSSX");
@Override @Override
@ -53,33 +46,7 @@ public class NowTests extends RefineTest {
public void init() { public void init() {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void setUp() {
bindings = new Properties();
}
@AfterMethod
public void tearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testNow() { public void testNow() {
// 2018-4-30 23:55:44 // 2018-4-30 23:55:44

View File

@ -33,15 +33,10 @@ import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.html.ParseHtml;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class ParseHtmlTests extends RefineTest { public class ParseHtmlTests extends RefineTest {
@ -65,32 +60,6 @@ public class ParseHtmlTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void serializeParseHtml() { public void serializeParseHtml() {
String json = "{\"description\":\"Parses a string as HTML\",\"params\":\"string s\",\"returns\":\"HTML object\"}"; String json = "{\"description\":\"Parses a string as HTML\",\"params\":\"string s\",\"returns\":\"HTML object\"}";

View File

@ -29,31 +29,15 @@ package com.google.refine.expr.functions.math;
import java.util.Properties; import java.util.Properties;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.expr.functions.math.RandomNumber;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.arrays.InArray;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class RandomNumberTests extends RefineTest { public class RandomNumberTests extends RefineTest {
static Properties bindings; static Properties bindings;
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
@Test @Test
public void serializeRandomNumber() { public void serializeRandomNumber() {
String json = "{\"description\":\"Returns a pseudo-random integer between the lower and upper bound (inclusive)\",\"params\":\"number lower bound, number upper bound\",\"returns\":\"number\"}"; String json = "{\"description\":\"Returns a pseudo-random integer between the lower and upper bound (inclusive)\",\"params\":\"number lower bound, number upper bound\",\"returns\":\"number\"}";
@ -73,18 +57,5 @@ public class RandomNumberTests extends RefineTest {
Object a = invoke("randomNumber", 1, 10); Object a = invoke("randomNumber", 1, 10);
Assert.assertTrue((int) a < 11 && (int) a > 0); Assert.assertTrue((int) a < 11 && (int) a > 0);
} }
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
} }

View File

@ -26,33 +26,20 @@
******************************************************************************/ ******************************************************************************/
package com.google.refine.expr.functions.strings; package com.google.refine.expr.functions.strings;
import org.testng.annotations.Test;
import com.google.refine.expr.functions.strings.Contains;
import com.google.refine.util.TestUtils;
import java.util.Properties;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineServlet;
import com.google.refine.RefineServletStub;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.util.TestUtils;
import com.google.refine.grel.Function;
/** /**
* Test cases for find function. * Test cases for find function.
*/ */
public class ContainsTests extends RefineTest { public class ContainsTests extends RefineTest {
static Properties bindings;
@Override @Override
@BeforeTest @BeforeTest
@ -60,21 +47,6 @@ public class ContainsTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
// dependencies
RefineServlet servlet;
@BeforeMethod
public void SetUp() {
bindings = new Properties();
servlet = new RefineServletStub();
}
@AfterMethod
public void TearDown() {
}
@Test @Test
public void testContainsFunction() { public void testContainsFunction() {
String value = "rose is a rose"; String value = "rose is a rose";
@ -84,27 +56,11 @@ public class ContainsTests extends RefineTest {
Assert.assertEquals(invoke("contains", value, "$"),false); Assert.assertEquals(invoke("contains", value, "$"),false);
Assert.assertEquals(invoke("contains", value, "r.se"),false); Assert.assertEquals(invoke("contains", value, "r.se"),false);
Assert.assertEquals(invoke("contains", value, "\\s+"),false); Assert.assertEquals(invoke("contains", value, "\\s+"),false);
// Input regex pattern in UI with : "/ /" , is intepreted as Pattern // Input regex pattern in UI with : "/ /" , is interpreted as Pattern
Assert.assertEquals(invoke("contains", value, Pattern.compile("$")),true); Assert.assertEquals(invoke("contains", value, Pattern.compile("$")),true);
Assert.assertEquals(invoke("contains", value, Pattern.compile("\\s+")),true); Assert.assertEquals(invoke("contains", value, Pattern.compile("\\s+")),true);
} }
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void serializeContains() { public void serializeContains() {
String json = "{\"description\":\"Returns whether s contains frag\",\"params\":\"string s, string frag\",\"returns\":\"boolean\"}"; String json = "{\"description\":\"Returns whether s contains frag\",\"params\":\"string s, string frag\",\"returns\":\"boolean\"}";

View File

@ -33,16 +33,11 @@ import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.strings.Diff;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
@ -73,32 +68,6 @@ public class DiffTests extends RefineTest {
odt8 = OffsetDateTime.of(1923, 4, 21, 12, 0, 0, 0, ZoneOffset.UTC); odt8 = OffsetDateTime.of(1923, 4, 21, 12, 0, 0, 0, ZoneOffset.UTC);
} }
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testDiffInvalidParams() { public void testDiffInvalidParams() {
Assert.assertTrue(invoke("diff") instanceof EvalError); Assert.assertTrue(invoke("diff") instanceof EvalError);

View File

@ -31,17 +31,11 @@ import java.util.regex.Pattern;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineServlet; import com.google.refine.RefineServlet;
import com.google.refine.RefineServletStub;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.functions.strings.Find;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
/** /**
@ -59,18 +53,6 @@ public class FindTests extends RefineTest {
// dependencies // dependencies
RefineServlet servlet; RefineServlet servlet;
@BeforeMethod
public void SetUp() {
bindings = new Properties();
servlet = new RefineServletStub();
}
@AfterMethod
public void TearDown() {
}
@Test @Test
public void findFunctionFindAllTest() throws Exception { public void findFunctionFindAllTest() throws Exception {
String[] matches = (String[]) invoke("find", "This is a test string for testing find.", "test"); String[] matches = (String[]) invoke("find", "This is a test string for testing find.", "test");
@ -96,20 +78,4 @@ public class FindTests extends RefineTest {
String json = "{\"description\":\"Returns all the occurances of match given regular expression\",\"params\":\"string or regexp\",\"returns\":\"array of strings\"}"; String json = "{\"description\":\"Returns all the occurances of match given regular expression\",\"params\":\"string or regexp\",\"returns\":\"array of strings\"}";
TestUtils.isSerializedTo(new Find(), json); TestUtils.isSerializedTo(new Find(), json);
} }
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
} }

View File

@ -35,15 +35,10 @@ import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.functions.strings.Fingerprint;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
@ -71,32 +66,6 @@ public class FingerprintTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testInvalidParams() { public void testInvalidParams() {
Assert.assertNull(invoke("fingerprint")); Assert.assertNull(invoke("fingerprint"));

View File

@ -26,20 +26,13 @@
******************************************************************************/ ******************************************************************************/
package com.google.refine.expr.functions.strings; package com.google.refine.expr.functions.strings;
import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.strings.Range;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
/** /**
@ -47,8 +40,6 @@ import com.google.refine.util.TestUtils;
*/ */
public class RangeTests extends RefineTest { public class RangeTests extends RefineTest {
private static Properties bindings;
private static final Integer[] EMPTY_ARRAY = new Integer[0]; private static final Integer[] EMPTY_ARRAY = new Integer[0];
private static final Integer[] ONE_AND_THREE = new Integer[] {1, 3}; private static final Integer[] ONE_AND_THREE = new Integer[] {1, 3};
@ -67,32 +58,6 @@ public class RangeTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void setUp() {
bindings = new Properties();
}
@AfterMethod
public void tearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function " + name);
}
if (args == null) {
return function.call(bindings, new Object[0]);
} else {
return function.call(bindings, args);
}
}
@Test @Test
public void testRangeInvalidParams() { public void testRangeInvalidParams() {
// Test number of arguments // Test number of arguments

View File

@ -30,16 +30,11 @@ import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.strings.Trim;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
@ -85,32 +80,6 @@ public class TrimTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testInvalidParams() { public void testInvalidParams() {
Assert.assertTrue(invoke("trim") instanceof EvalError); Assert.assertTrue(invoke("trim") instanceof EvalError);

View File

@ -34,15 +34,10 @@ import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.xml.ParseXml;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
@ -72,32 +67,6 @@ public class ParseXmlTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void serializeParseXml() { public void serializeParseXml() {
String json = "{\"description\":\"Parses a string as XML\",\"params\":\"string s\",\"returns\":\"XML object\"}"; String json = "{\"description\":\"Parses a string as XML\",\"params\":\"string s\",\"returns\":\"XML object\"}";

View File

@ -54,8 +54,6 @@ import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.browsing.Engine; import com.google.refine.browsing.Engine;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
import com.google.refine.model.ModelException; import com.google.refine.model.ModelException;
import com.google.refine.model.Project; import com.google.refine.model.Project;
@ -63,12 +61,9 @@ import com.google.refine.model.Row;
public class FunctionTests extends RefineTest { public class FunctionTests extends RefineTest {
static Properties bindings;
Project project; Project project;
Engine engine; Engine engine;
@Override @Override
@BeforeTest @BeforeTest
public void init() { public void init() {
@ -77,11 +72,10 @@ public class FunctionTests extends RefineTest {
@BeforeMethod @BeforeMethod
public void SetUp() throws IOException, ModelException { public void SetUp() throws IOException, ModelException {
project = createProjectWithColumns("FunctionTests", "Column A"); project = createProjectWithColumns("FunctionTests", "Column A");
bindings = new Properties(); bindings = new Properties();
bindings.put("project", project); bindings.put("project", project);
// Five rows of a's and five of 1s // Five rows of a's and five of 1s
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
Row row = new Row(1); Row row = new Row(1);
@ -95,31 +89,14 @@ public class FunctionTests extends RefineTest {
public void TearDown() { public void TearDown() {
bindings = null; bindings = null;
} }
/**
* Lookup a control function by name and invoke it with a variable number of args
*/
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
@Test @Test
public void testInvalidParams() { public void testInvalidParams() {
Assert.assertTrue(invoke("facetCount") instanceof EvalError); Assert.assertTrue(invoke("facetCount") instanceof EvalError);
Assert.assertTrue(invoke("facetCount", "one","two","three") instanceof EvalError); Assert.assertTrue(invoke("facetCount", "one","two","three") instanceof EvalError);
Assert.assertTrue(invoke("facetCount", "one","bad(","Column A") instanceof EvalError); Assert.assertTrue(invoke("facetCount", "one","bad(","Column A") instanceof EvalError);
} }
@Test @Test
public void testFacetCount() { public void testFacetCount() {
Assert.assertEquals(invoke("facetCount", "a", "value", "Column A"),Integer.valueOf(5)); Assert.assertEquals(invoke("facetCount", "a", "value", "Column A"),Integer.valueOf(5));

View File

@ -49,7 +49,6 @@ import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest; import com.google.refine.RefineTest;
import com.google.refine.importers.JsonImporter;
import com.google.refine.importers.JsonImporter.JSONTreeReader; import com.google.refine.importers.JsonImporter.JSONTreeReader;
import com.google.refine.importers.XmlImporter.XmlParser; import com.google.refine.importers.XmlImporter.XmlParser;
import com.google.refine.importers.tree.ImportColumn; import com.google.refine.importers.tree.ImportColumn;