/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2016 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* 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.pentaho.di.trans;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import junit.framework.TestCase;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.RowMetaAndData;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleValueException;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
public abstract class TransformationTestCase extends TestCase {
public TransformationTestCase() throws KettleException {
super();
KettleEnvironment.init( false );
}
public TransformationTestCase( String name ) throws KettleException {
super( name );
KettleEnvironment.init( false );
}
public RowMetaInterface createRowMetaInterface( ValueMetaInterface... valueMetas ) {
RowMetaInterface rm = new RowMeta();
for ( ValueMetaInterface vm : valueMetas ) {
rm.addValueMeta( vm );
}
return rm;
}
public List<RowMetaAndData> createData( RowMetaInterface rm, Object[][] rows ) {
List<RowMetaAndData> list = new ArrayList<RowMetaAndData>();
for ( Object[] row : rows ) {
list.add( new RowMetaAndData( rm, row ) );
}
return list;
}
/**
* Check the 2 lists comparing the rows in order. If they are not the same fail the test.
*/
public void checkRows( List<RowMetaAndData> rows1, List<RowMetaAndData> rows2 ) {
if ( rows1.size() != rows2.size() ) {
fail( "Number of rows is not the same: " + rows1.size() + " and " + rows2.size() );
}
ListIterator<RowMetaAndData> it1 = rows1.listIterator();
ListIterator<RowMetaAndData> it2 = rows2.listIterator();
while ( it1.hasNext() && it2.hasNext() ) {
RowMetaAndData rm1 = it1.next();
RowMetaAndData rm2 = it2.next();
Object[] r1 = rm1.getData();
Object[] r2 = rm2.getData();
if ( rm1.size() != rm2.size() ) {
fail( "row nr " + it1.nextIndex() + " is not equal" );
}
int[] fields = new int[r1.length];
for ( int ydx = 0; ydx < r1.length; ydx++ ) {
fields[ydx] = ydx;
}
try {
if ( rm1.getRowMeta().compare( r1, r2, fields ) != 0 ) {
fail( "row nr " + it1.nextIndex() + " is not equal" );
}
} catch ( KettleValueException e ) {
fail( "row nr " + it1.nextIndex() + " is not equal" );
}
}
}
}