Skip to content

Commit

Permalink
[PDI-13684] - Change in null behavior in 5.x version
Browse files Browse the repository at this point in the history
- use existing utility in DataGrid_EmptyStringVsNull_Test
  • Loading branch information
Andrey Khayrutdinov committed Jan 12, 2016
1 parent 881feef commit 20d111a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 44 deletions.
@@ -1,24 +1,41 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2015 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.steps.datagrid;

import org.junit.BeforeClass;
import org.junit.Test;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.row.value.ValueMetaBase;
import org.pentaho.di.core.row.value.ValueMetaFactory;
import org.pentaho.di.trans.TransTestingUtil;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.steps.StepMockUtil;
import org.pentaho.di.trans.steps.mock.StepMockHelper;
import org.pentaho.test.util.FieldAccessor;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;

import static java.util.Arrays.asList;
import static org.apache.commons.lang.reflect.FieldUtils.*;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -55,30 +72,11 @@ public void emptyAndNullsAreDifferent() throws Exception {


private void doTestEmptyStringVsNull( boolean diffProperty, List<Object[]> expected ) throws Exception {
final String fieldName = "EMPTY_STRING_AND_NULL_ARE_DIFFERENT";

final Field metaBaseField = getField( ValueMetaBase.class, fieldName );
final Object metaBaseValue = readStaticField( metaBaseField );

final Field metaField = getField( ValueMeta.class, fieldName );
final Object metaValue = readStaticField( metaField );

final Field modifiers = getField( Field.class, "modifiers", true );
writeField( modifiers, metaBaseField, metaBaseField.getModifiers() & ~Modifier.FINAL, true );
writeField( modifiers, metaField, metaField.getModifiers() & ~Modifier.FINAL, true );

Field.setAccessible( new AccessibleObject[] { metaBaseField, metaField }, true );
writeStaticField( metaBaseField, diffProperty );
writeStaticField( metaField, diffProperty );

FieldAccessor.ensureEmptyStringIsNotNull( diffProperty );
try {
executeAndAssertResults( expected );
} finally {
writeStaticField( metaBaseField, metaBaseValue );
writeStaticField( metaField, metaValue );

writeField( modifiers, metaBaseField, metaBaseField.getModifiers() & Modifier.FINAL, true );
writeField( modifiers, metaField, metaField.getModifiers() & Modifier.FINAL, true );
FieldAccessor.resetEmptyStringIsNotNull();
}
}

Expand Down
29 changes: 12 additions & 17 deletions engine/test-src/org/pentaho/test/util/FieldAccessor.java
Expand Up @@ -37,6 +37,8 @@ public class FieldAccessor {
private static final boolean ValueMeta_EMPTY_STRING_AND_NULL_ARE_DIFFERENT =
ValueMeta.EMPTY_STRING_AND_NULL_ARE_DIFFERENT;

private static final String EMPTY_AND_NULL_ARE_DIFF_FIELD = "EMPTY_STRING_AND_NULL_ARE_DIFFERENT";

public static void ensureBooleanStaticFieldVal( Field f, boolean newValue ) throws NoSuchFieldException,
SecurityException, IllegalArgumentException, IllegalAccessException {
boolean value = f.getBoolean( null );
Expand All @@ -60,38 +62,31 @@ public static void ensureBooleanStaticFieldVal( Field f, boolean newValue ) thro
if ( modifiersBak != modifiersNew ) {
modifiersField.setInt( f, modifiersBak );
if ( !modifAccessibleBak ) {
modifiersField.setAccessible( modifAccessibleBak );
modifiersField.setAccessible( false );
}
}
if ( !fieldAccessibleBak ) {
Field.setAccessible( new Field[] { f }, fieldAccessibleBak );
Field.setAccessible( new Field[] { f }, false );
}
}
}

public static void ensureEmptyStringIsNotNull( boolean newValue ) {
try {
ensureBooleanStaticFieldVal( ValueMetaBase.class.getField( "EMPTY_STRING_AND_NULL_ARE_DIFFERENT" ), newValue );
ensureBooleanStaticFieldVal( ValueMeta.class.getField( "EMPTY_STRING_AND_NULL_ARE_DIFFERENT" ), newValue );
} catch ( NoSuchFieldException e ) {
throw new RuntimeException( e );
} catch ( SecurityException e ) {
throw new RuntimeException( e );
} catch ( IllegalArgumentException e ) {
throw new RuntimeException( e );
} catch ( IllegalAccessException e ) {
ensureBooleanStaticFieldVal( ValueMetaBase.class.getField( EMPTY_AND_NULL_ARE_DIFF_FIELD ), newValue );
ensureBooleanStaticFieldVal( ValueMeta.class.getField( EMPTY_AND_NULL_ARE_DIFF_FIELD ), newValue );
} catch ( Exception e ) {
throw new RuntimeException( e );
}
Assert.assertEquals( "ValueMetaBase.EMPTY_STRING_AND_NULL_ARE_DIFFERENT", newValue,
ValueMetaBase.EMPTY_STRING_AND_NULL_ARE_DIFFERENT );
Assert.assertEquals( "ValueMeta.EMPTY_STRING_AND_NULL_ARE_DIFFERENT", newValue,
ValueMeta.EMPTY_STRING_AND_NULL_ARE_DIFFERENT );

Assert.assertEquals( "ValueMetaBase", newValue, ValueMetaBase.EMPTY_STRING_AND_NULL_ARE_DIFFERENT );
Assert.assertEquals( "ValueMeta", newValue, ValueMeta.EMPTY_STRING_AND_NULL_ARE_DIFFERENT );
}

public static void resetEmptyStringIsNotNull() throws NoSuchFieldException, IllegalAccessException {
ensureBooleanStaticFieldVal( ValueMetaBase.class.getField( "EMPTY_STRING_AND_NULL_ARE_DIFFERENT" ),
ensureBooleanStaticFieldVal( ValueMetaBase.class.getField( EMPTY_AND_NULL_ARE_DIFF_FIELD ),
ValueMetaBase_EMPTY_STRING_AND_NULL_ARE_DIFFERENT );
ensureBooleanStaticFieldVal( ValueMeta.class.getField( "EMPTY_STRING_AND_NULL_ARE_DIFFERENT" ),
ensureBooleanStaticFieldVal( ValueMeta.class.getField( EMPTY_AND_NULL_ARE_DIFF_FIELD ),
ValueMeta_EMPTY_STRING_AND_NULL_ARE_DIFFERENT );
}
}

0 comments on commit 20d111a

Please sign in to comment.