Skip to content
Snippets Groups Projects
Commit 2b70212b authored by felmer's avatar felmer
Browse files

LMS-1186 unit tests for filter framework

SVN: 12754
parent 47c0e8ea
No related branches found
No related tags found
No related merge requests found
...@@ -31,8 +31,8 @@ import org.apache.commons.lang.StringUtils; ...@@ -31,8 +31,8 @@ import org.apache.commons.lang.StringUtils;
*/ */
public final class StandardFunctions public final class StandardFunctions
{ {
private static final Double DOUBLE_DEFAULT_VALUE = new Double(-Double.MAX_VALUE); static final Double DOUBLE_DEFAULT_VALUE = new Double(-Double.MAX_VALUE);
private static final Integer INTEGER_DEFAULT_VALUE = new Integer(Integer.MIN_VALUE); static final Integer INTEGER_DEFAULT_VALUE = new Integer(Integer.MIN_VALUE);
/** /**
* Returns the specified value as an integer. Returns the smallest integer if <code>value</code> * Returns the specified value as an integer. Returns the smallest integer if <code>value</code>
......
/*
* Copyright 2009 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.openbis.generic.client.web.server.calculator;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.columns.framework.AbstractColumnDefinition;
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ParameterWithValue;
import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
/**
* @author Franz-Josef Elmer
*/
public class RowCalculatorTest extends AssertJUnit
{
private Set<IColumnDefinition<Data>> availableColumns;
private static final class Data
{
private double value;
public void setValue(double value)
{
this.value = value;
}
public double getValue()
{
return value;
}
}
@BeforeMethod
public void setUp()
{
availableColumns = new LinkedHashSet<IColumnDefinition<Data>>();
availableColumns.add(new AbstractColumnDefinition<Data>("header", 100, true)
{
public String getIdentifier()
{
return "VALUE";
}
@Override
protected String tryGetValue(Data entity)
{
return Double.toString(entity.getValue());
}
@Override
public Comparable<?> getComparableValue(Data rowModel)
{
return rowModel.getValue();
}
});
}
@Test
public void testEvalToDoubleWithStrangeParameterName()
{
Set<ParameterWithValue> parameters = createParameters("f.*r", "42");
RowCalculator<Data> calculator = createCalculator(parameters, "row.col('VALUE') * ${f.*r}");
calculator.setRowData(createData(2.5));
assertEquals(105.0, calculator.evalToDouble());
calculator.setRowData(createData(10));
assertEquals(420.0, calculator.evalToDouble());
}
@Test
public void testEvalToDoubleWithTwoParameters()
{
Set<ParameterWithValue> parameters = createParameters("x", "10");
parameters.addAll(createParameters("y", "1"));
RowCalculator<Data> calculator = createCalculator(parameters, "${x} * row.col('VALUE') + ${y}");
calculator.setRowData(createData(2.5));
assertEquals(26.0, calculator.evalToDouble());
calculator.setRowData(createData(10));
assertEquals(101.0, calculator.evalToDouble());
}
@Test
public void testEvalToBoolean()
{
Set<ParameterWithValue> parameters = createParameters("x", "42");
RowCalculator<Data> calculator = createCalculator(parameters, "row.col('VALUE') < ${x}");
calculator.setRowData(createData(2.5));
assertEquals(true, calculator.evalToBoolean());
calculator.setRowData(createData(43));
assertEquals(false, calculator.evalToBoolean());
}
@Test
public void testEvalToInt()
{
Set<ParameterWithValue> parameters = createParameters("x", "42");
RowCalculator<Data> calculator =
createCalculator(parameters, "int(row.col('VALUE') * ${x})");
calculator.setRowData(createData(2.5));
assertEquals(105, calculator.evalToInt());
calculator.setRowData(createData(10));
assertEquals(420, calculator.evalToInt());
}
@Test
public void testEvalToBigInt()
{
Set<ParameterWithValue> parameters = createParameters("x", "10");
RowCalculator<Data> calculator =
createCalculator(parameters, "${x} ** int(row.col('VALUE'))");
calculator.setRowData(createData(10));
assertEquals(new BigInteger("10000000000"), calculator.evalToBigInt());
}
@Test
public void testEvalAsString()
{
Set<ParameterWithValue> parameters = createParameters("x", "42");
RowCalculator<Data> calculator =
createCalculator(parameters, "str(${x} + row.col('VALUE'))");
calculator.setRowData(createData(10));
assertEquals("52.0", calculator.evalAsString());
calculator.setRowData(createData(-10));
assertEquals("32.0", calculator.evalAsString());
}
@Test
public void testMathFunctionAndStandardFunction()
{
Set<ParameterWithValue> parameters = createParameters("x", "0.0");
RowCalculator<Data> calculator =
createCalculator(parameters, "ifThenElse(row.col('VALUE') < PI, cos(${x}), ${x})");
calculator.setRowData(createData(3.14));
assertEquals(1.0, calculator.evalToDouble());
calculator.setRowData(createData(3.15));
assertEquals(0.0, calculator.evalToDouble());
}
private RowCalculator<Data> createCalculator(Set<ParameterWithValue> parameters,
String expression)
{
return new RowCalculator<Data>(availableColumns, expression, parameters);
}
private Set<ParameterWithValue> createParameters(String name, String value)
{
ParameterWithValue parameter = new ParameterWithValue();
parameter.setParameter(name);
parameter.setValue(value);
return new LinkedHashSet<ParameterWithValue>(Arrays.asList(parameter));
}
private Data createData(double value)
{
Data data = new Data();
data.setValue(value);
return data;
}
}
/*
* Copyright 2009 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.openbis.generic.client.web.server.calculator;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.INTEGER_DEFAULT_VALUE;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.DOUBLE_DEFAULT_VALUE;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.ifThenElse;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.mean;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.median;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.minimum;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.maximum;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.toInteger;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.toFloat;
import java.util.Arrays;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
/**
*
*
* @author Franz-Josef Elmer
*/
public class StandardFunctionsTest extends AssertJUnit
{
@Test
public void testToIntegerWithNullArgument()
{
assertEquals(INTEGER_DEFAULT_VALUE, toInteger(null));
assertEquals(42, toInteger(null, 42).intValue());
}
@Test
public void testToIntegerWithBlankArgument()
{
assertEquals(INTEGER_DEFAULT_VALUE, toInteger(" "));
assertEquals(42, toInteger(" ", 42).intValue());
}
@Test
public void testToIntegerWithNumberArgument()
{
assertEquals(42, toInteger(42).intValue());
assertEquals(42, toInteger(42, 4711).intValue());
}
@Test
public void testToIntegerWithParsableArgument()
{
assertEquals(42, toInteger("42").intValue());
assertEquals(42, toInteger("42", 4711).intValue());
}
@Test
public void testToIntegerWithUnParsableArgument()
{
try
{
toInteger("abc");
fail("NumberFormatException expected");
} catch (NumberFormatException ex)
{
assertEquals("For input string: \"abc\"", ex.getMessage());
}
}
@Test
public void testToFloatWithNullArgument()
{
assertEquals(DOUBLE_DEFAULT_VALUE, toFloat(null));
assertEquals(42.5, toFloat(null, 42.5).doubleValue());
}
@Test
public void testToFloatWithBlankArgument()
{
assertEquals(DOUBLE_DEFAULT_VALUE, toFloat(" "));
assertEquals(42.5, toFloat(" ", 42.5).doubleValue());
}
@Test
public void testToFloatWithNumberArgument()
{
assertEquals(42.5, toFloat(42.5).doubleValue());
assertEquals(42.5, toFloat(42.5, 4711.0).doubleValue());
}
@Test
public void testToFloatWithParsableArgument()
{
assertEquals(42.5, toFloat("42.5").doubleValue());
assertEquals(42.5, toFloat("42.5", 4711.0).doubleValue());
}
@Test
public void testToFloatWithUnParsableArgument()
{
try
{
toFloat("abc");
fail("NumberFormatException expected");
} catch (NumberFormatException ex)
{
assertEquals("For input string: \"abc\"", ex.getMessage());
}
}
@Test
public void testIfThenElse()
{
assertEquals("no", ifThenElse(null, "yes", "no"));
assertEquals("no", ifThenElse(false, "yes", "no"));
assertEquals("yes", ifThenElse(true, "yes", "no"));
}
@Test
public void testMean()
{
assertEquals(1.5, mean(Arrays.<Object>asList(1.5)));
assertEquals(5.0, mean(Arrays.<Object>asList(1, 4, 10)));
assertEquals(5.5, mean(Arrays.<Object>asList(null, 1, 10)));
assertEquals(5.5, mean(Arrays.<Object>asList(" ", 1, "10")));
try
{
mean(Arrays.<Object>asList());
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex)
{
assertEquals("Argument of function 'mean' is an empty array.", ex.getMessage());
}
try
{
mean(Arrays.<Object>asList("a"));
fail("NumberFormatException expected");
} catch (NumberFormatException ex)
{
// ignored
}
}
@Test
public void testMedian()
{
assertEquals(1.5, median(Arrays.<Object>asList(1.5)));
assertEquals(4.0, median(Arrays.<Object>asList(4, 1, 10)));
assertEquals(5.5, median(Arrays.<Object>asList(null, 1, 10)));
assertEquals(5.5, median(Arrays.<Object>asList(" ", 1, "10")));
try
{
median(Arrays.<Object>asList());
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex)
{
assertEquals("Argument of function 'median' is an empty array.", ex.getMessage());
}
try
{
median(Arrays.<Object>asList("a"));
fail("NumberFormatException expected");
} catch (NumberFormatException ex)
{
// ignored
}
}
@Test
public void testMinimum()
{
assertEquals(1.5, minimum(Arrays.<Object>asList(1.5)));
assertEquals(1.0, minimum(Arrays.<Object>asList(4, 1, 10)));
assertEquals(1.0, minimum(Arrays.<Object>asList(null, 1, 10)));
assertEquals(1.0, minimum(Arrays.<Object>asList(" ", 1, "10")));
try
{
minimum(Arrays.<Object>asList());
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex)
{
assertEquals("Argument of function 'minimum' is an empty array.", ex.getMessage());
}
try
{
minimum(Arrays.<Object>asList("a"));
fail("NumberFormatException expected");
} catch (NumberFormatException ex)
{
// ignored
}
}
@Test
public void testMaximum()
{
assertEquals(1.5, maximum(Arrays.<Object>asList(1.5)));
assertEquals(10.0, maximum(Arrays.<Object>asList(4, 1, 10)));
assertEquals(10.0, maximum(Arrays.<Object>asList(null, 1, 10)));
assertEquals(10.0, maximum(Arrays.<Object>asList(" ", 1, "10")));
try
{
maximum(Arrays.<Object>asList());
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex)
{
assertEquals("Argument of function 'maximum' is an empty array.", ex.getMessage());
}
try
{
maximum(Arrays.<Object>asList("a"));
fail("NumberFormatException expected");
} catch (NumberFormatException ex)
{
// ignored
}
}
}
/*
* Copyright 2009 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.openbis.generic.client.web.server.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.columns.framework.AbstractColumnDefinition;
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.CustomFilterInfo;
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ParameterWithValue;
import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
/**
*
*
* @author Franz-Josef Elmer
*/
public class FilterUtilsTest extends AssertJUnit
{
private static final class Data
{
private double value;
public void setValue(double value)
{
this.value = value;
}
public double getValue()
{
return value;
}
}
@Test
public void test()
{
List<Data> filterdList = new ArrayList<Data>();
CustomFilterInfo<Data> filterInfo = new CustomFilterInfo<Data>();
filterInfo.setExpression("row.col('VALUE') < ${threshold}");
ParameterWithValue parameter = new ParameterWithValue();
parameter.setParameter("threshold");
parameter.setValue("42");
filterInfo.setParameters(new LinkedHashSet<ParameterWithValue>(Arrays.asList(parameter)));
Set<IColumnDefinition<Data>> availableColumns = new LinkedHashSet<IColumnDefinition<Data>>();
availableColumns.add(new AbstractColumnDefinition<Data>("header", 100, true)
{
public String getIdentifier()
{
return "VALUE";
}
@Override
protected String tryGetValue(Data entity)
{
return Double.toString(entity.getValue());
}
@Override
public Comparable<?> getComparableValue(Data rowModel)
{
return rowModel.getValue();
}
});
FilterUtils.applyCustomFilter(createData(57, 34), availableColumns, filterInfo, filterdList);
assertEquals(1, filterdList.size());
assertEquals(34.0, filterdList.get(0).getValue());
}
private List<Data> createData(double... values)
{
ArrayList<Data> list = new ArrayList<Data>();
for (double value : values)
{
Data data = new Data();
data.setValue(value);
list.add(data);
}
return list;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment