Search in sources :

Example 16 with RowAdapter

use of org.pentaho.di.trans.step.RowAdapter in project pentaho-kettle by pentaho.

the class CalculatorBackwardCompatibilityUnitTest method assertRoundStd.

public void assertRoundStd(final double expectedResult, final double value) throws KettleException {
    RowMeta inputRowMeta = new RowMeta();
    ValueMetaNumber valueMeta = new ValueMetaNumber("Value");
    inputRowMeta.addValueMeta(valueMeta);
    RowSet inputRowSet = smh.getMockInputRowSet(new Object[] { value });
    inputRowSet.setRowMeta(inputRowMeta);
    Calculator calculator = new Calculator(smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init(smh.initStepMetaInterface, smh.initStepDataInterface);
    CalculatorMeta meta = new CalculatorMeta();
    meta.setCalculation(new CalculatorMetaFunction[] { new CalculatorMetaFunction("test", CalculatorMetaFunction.CALC_ROUND_STD_1, "Value", null, null, ValueMetaInterface.TYPE_NUMBER, 2, 0, false, "", "", "", "") });
    // Verify output
    try {
        calculator.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {
                assertEquals(expectedResult, row[1]);
            }
        });
        calculator.processRow(meta, new CalculatorData());
    } catch (KettleException ke) {
        ke.printStackTrace();
        fail();
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) RowSet(org.pentaho.di.core.RowSet) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) RowAdapter(org.pentaho.di.trans.step.RowAdapter)

Example 17 with RowAdapter

use of org.pentaho.di.trans.step.RowAdapter in project pentaho-kettle by pentaho.

the class CalculatorBackwardCompatibilityUnitTest method assertRoundStd2.

public void assertRoundStd2(final double expectedResult, final double value, final long precision) throws KettleException {
    RowMeta inputRowMeta = new RowMeta();
    ValueMetaNumber valueMeta = new ValueMetaNumber("Value");
    ValueMetaInteger precisionMeta = new ValueMetaInteger("Precision");
    inputRowMeta.addValueMeta(valueMeta);
    inputRowMeta.addValueMeta(precisionMeta);
    RowSet inputRowSet = smh.getMockInputRowSet(new Object[] { value, precision });
    inputRowSet.setRowMeta(inputRowMeta);
    Calculator calculator = new Calculator(smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init(smh.initStepMetaInterface, smh.initStepDataInterface);
    CalculatorMeta meta = new CalculatorMeta();
    meta.setCalculation(new CalculatorMetaFunction[] { new CalculatorMetaFunction("test", CalculatorMetaFunction.CALC_ROUND_STD_2, "Value", "Precision", null, ValueMetaInterface.TYPE_NUMBER, 2, 0, false, "", "", "", "") });
    // Verify output
    try {
        calculator.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {
                assertEquals(expectedResult, row[2]);
            }
        });
        calculator.processRow(meta, new CalculatorData());
    } catch (KettleException ke) {
        ke.printStackTrace();
        fail();
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) RowSet(org.pentaho.di.core.RowSet) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) RowAdapter(org.pentaho.di.trans.step.RowAdapter) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger)

Example 18 with RowAdapter

use of org.pentaho.di.trans.step.RowAdapter in project pentaho-kettle by pentaho.

the class CalculatorUnitTest method assertRoundGeneral.

public void assertRoundGeneral(final Object expectedResult, final int calcFunction, final Number value, final Long precision, final Long roundingMode, final int valueDataType, final int functionDataType) throws KettleException {
    final String msg = getKettleTypeName(valueDataType) + "->" + getKettleTypeName(functionDataType) + " ";
    final RowMeta inputRowMeta = new RowMeta();
    final List<Object> inputValues = new ArrayList<Object>(3);
    final String fieldValue = "Value";
    final ValueMetaInterface valueMeta;
    switch(valueDataType) {
        case ValueMetaInterface.TYPE_BIGNUMBER:
            valueMeta = new ValueMetaBigNumber(fieldValue);
            break;
        case ValueMetaInterface.TYPE_NUMBER:
            valueMeta = new ValueMetaNumber(fieldValue);
            break;
        case ValueMetaInterface.TYPE_INTEGER:
            valueMeta = new ValueMetaInteger(fieldValue);
            break;
        default:
            throw new IllegalArgumentException(msg + "Unexpected value dataType: " + value.getClass().getName() + ". Long, Double or BigDecimal expected.");
    }
    inputRowMeta.addValueMeta(valueMeta);
    inputValues.add(value);
    final String fieldPrecision;
    final ValueMetaInteger precisionMeta;
    if (precision == null) {
        fieldPrecision = null;
        precisionMeta = null;
    } else {
        fieldPrecision = "Precision";
        precisionMeta = new ValueMetaInteger(fieldPrecision);
        inputRowMeta.addValueMeta(precisionMeta);
        inputValues.add(precision);
    }
    final String fieldRoundingMode;
    final ValueMetaInteger roundingModeMeta;
    if (roundingMode == null) {
        fieldRoundingMode = null;
        roundingModeMeta = null;
    } else {
        fieldRoundingMode = "RoundingMode";
        roundingModeMeta = new ValueMetaInteger(fieldRoundingMode);
        inputRowMeta.addValueMeta(roundingModeMeta);
        inputValues.add(roundingMode);
    }
    RowSet inputRowSet = smh.getMockInputRowSet(inputValues.toArray());
    inputRowSet.setRowMeta(inputRowMeta);
    final String fieldA = inputRowMeta.size() > 0 ? inputRowMeta.getValueMetaList().get(0).getName() : null;
    final String fieldB = inputRowMeta.size() > 1 ? inputRowMeta.getValueMetaList().get(1).getName() : null;
    final String fieldC = inputRowMeta.size() > 2 ? inputRowMeta.getValueMetaList().get(2).getName() : null;
    final int resultDataType = functionDataType;
    final String fieldResult = "test";
    final int expectedResultRowSize = inputRowMeta.size() + 1;
    Calculator calculator = new Calculator(smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init(smh.initStepMetaInterface, smh.initStepDataInterface);
    CalculatorMeta meta = new CalculatorMeta();
    meta.setCalculation(new CalculatorMetaFunction[] { new CalculatorMetaFunction(fieldResult, calcFunction, fieldA, fieldB, fieldC, resultDataType, 2, 0, false, "", "", "", "") });
    // Verify output
    try {
        calculator.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {
                assertEquals(msg + " resultRowSize", expectedResultRowSize, rowMeta.size());
                final int fieldResultIndex = rowMeta.size() - 1;
                assertEquals(msg + " fieldResult", fieldResult, rowMeta.getValueMeta(fieldResultIndex).getName());
                assertEquals(msg, expectedResult, row[fieldResultIndex]);
            }
        });
        calculator.processRow(meta, new CalculatorData());
    } catch (KettleException ke) {
        ke.printStackTrace();
        fail(msg + ke.getMessage());
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) ArrayList(java.util.ArrayList) RowSet(org.pentaho.di.core.RowSet) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Matchers.anyString(org.mockito.Matchers.anyString) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) RowAdapter(org.pentaho.di.trans.step.RowAdapter) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger)

Example 19 with RowAdapter

use of org.pentaho.di.trans.step.RowAdapter in project pentaho-kettle by pentaho.

the class CalculatorUnitTest method assertCalculatorReminder.

private void assertCalculatorReminder(final Object expectedResult, final Object[] values, final int[] types) throws Exception {
    RowMeta inputRowMeta = new RowMeta();
    for (int i = 0; i < types.length; i++) {
        switch(types[i]) {
            case ValueMetaInterface.TYPE_BIGNUMBER:
                inputRowMeta.addValueMeta(new ValueMetaBigNumber("f" + i));
                break;
            case ValueMetaInterface.TYPE_NUMBER:
                inputRowMeta.addValueMeta(new ValueMetaNumber("f" + i));
                break;
            case ValueMetaInterface.TYPE_INTEGER:
                inputRowMeta.addValueMeta(new ValueMetaInteger("f" + i));
                break;
            default:
                throw new IllegalArgumentException("Unexpected value dataType: " + types[i] + ". Long, Double or BigDecimal expected.");
        }
    }
    RowSet inputRowSet = null;
    try {
        inputRowSet = smh.getMockInputRowSet(new Object[][] { { values[0], values[1] } });
    } catch (Exception pe) {
        pe.printStackTrace();
        fail();
    }
    inputRowSet.setRowMeta(inputRowMeta);
    Calculator calculator = new Calculator(smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init(smh.initStepMetaInterface, smh.initStepDataInterface);
    CalculatorMeta meta = new CalculatorMeta();
    meta.setCalculation(new CalculatorMetaFunction[] { new CalculatorMetaFunction("res", CalculatorMetaFunction.CALC_REMAINDER, "f0", "f1", null, ValueMetaInterface.TYPE_NUMBER, 0, 0, false, "", "", "", "") });
    // Verify output
    try {
        calculator.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {
                try {
                    assertEquals(expectedResult, row[2]);
                } catch (Exception pe) {
                    throw new KettleStepException(pe);
                }
            }
        });
        calculator.processRow(meta, new CalculatorData());
    } catch (KettleException ke) {
        ke.printStackTrace();
        fail();
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) RowSet(org.pentaho.di.core.RowSet) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) KettleException(org.pentaho.di.core.exception.KettleException) ParseException(java.text.ParseException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) RowAdapter(org.pentaho.di.trans.step.RowAdapter) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger)

Example 20 with RowAdapter

use of org.pentaho.di.trans.step.RowAdapter in project pentaho-kettle by pentaho.

the class CalculatorUnitTest method testAddSeconds.

@Test
public void testAddSeconds() throws KettleException {
    RowMeta inputRowMeta = new RowMeta();
    ValueMetaDate dayMeta = new ValueMetaDate("Day");
    inputRowMeta.addValueMeta(dayMeta);
    ValueMetaInteger secondsMeta = new ValueMetaInteger("Seconds");
    inputRowMeta.addValueMeta(secondsMeta);
    RowSet inputRowSet = null;
    try {
        inputRowSet = smh.getMockInputRowSet(new Object[][] { { new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-01 00:00:00"), new Long(10) }, { new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-10-31 23:59:50"), new Long(30) } });
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail();
    }
    inputRowSet.setRowMeta(inputRowMeta);
    Calculator calculator = new Calculator(smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init(smh.initStepMetaInterface, smh.initStepDataInterface);
    CalculatorMeta meta = new CalculatorMeta();
    meta.setCalculation(new CalculatorMetaFunction[] { new CalculatorMetaFunction("new_day", CalculatorMetaFunction.CALC_ADD_SECONDS, "Day", "Seconds", null, ValueMetaInterface.TYPE_DATE, 0, 0, false, "", "", "", "") });
    // Verify output
    try {
        calculator.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {
                try {
                    assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-01 00:00:10"), row[2]);
                } catch (ParseException pe) {
                    throw new KettleStepException(pe);
                }
            }
        });
        calculator.processRow(meta, new CalculatorData());
    } catch (KettleException ke) {
        ke.printStackTrace();
        fail();
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) RowSet(org.pentaho.di.core.RowSet) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) RowAdapter(org.pentaho.di.trans.step.RowAdapter) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ParseException(java.text.ParseException) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

RowAdapter (org.pentaho.di.trans.step.RowAdapter)24 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)22 KettleStepException (org.pentaho.di.core.exception.KettleStepException)21 KettleException (org.pentaho.di.core.exception.KettleException)16 RowSet (org.pentaho.di.core.RowSet)9 RowMeta (org.pentaho.di.core.row.RowMeta)9 Trans (org.pentaho.di.trans.Trans)7 StepInterface (org.pentaho.di.trans.step.StepInterface)7 ValueMetaNumber (org.pentaho.di.core.row.value.ValueMetaNumber)6 Test (org.junit.Test)5 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)5 ArrayList (java.util.ArrayList)4 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)4 KettleValueException (org.pentaho.di.core.exception.KettleValueException)4 StepMeta (org.pentaho.di.trans.step.StepMeta)4 IOException (java.io.IOException)3 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)3 TransMeta (org.pentaho.di.trans.TransMeta)3 ParseException (java.text.ParseException)2 ValueMetaBigNumber (org.pentaho.di.core.row.value.ValueMetaBigNumber)2