From 20d111a56270200da58a32584b06cb3bd7ffd9a1 Mon Sep 17 00:00:00 2001 From: Andrey Khayrutdinov Date: Mon, 11 Jan 2016 16:32:27 +0400 Subject: [PATCH] [PDI-13684] - Change in null behavior in 5.x version - use existing utility in DataGrid_EmptyStringVsNull_Test --- .../DataGrid_EmptyStringVsNull_Test.java | 52 +++++++++---------- .../org/pentaho/test/util/FieldAccessor.java | 29 +++++------ 2 files changed, 37 insertions(+), 44 deletions(-) diff --git a/engine/test-src/org/pentaho/di/trans/steps/datagrid/DataGrid_EmptyStringVsNull_Test.java b/engine/test-src/org/pentaho/di/trans/steps/datagrid/DataGrid_EmptyStringVsNull_Test.java index 5cf4aa0b2d89..10c22ffbae2d 100644 --- a/engine/test-src/org/pentaho/di/trans/steps/datagrid/DataGrid_EmptyStringVsNull_Test.java +++ b/engine/test-src/org/pentaho/di/trans/steps/datagrid/DataGrid_EmptyStringVsNull_Test.java @@ -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; /** @@ -55,30 +72,11 @@ public void emptyAndNullsAreDifferent() throws Exception { private void doTestEmptyStringVsNull( boolean diffProperty, List 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(); } } diff --git a/engine/test-src/org/pentaho/test/util/FieldAccessor.java b/engine/test-src/org/pentaho/test/util/FieldAccessor.java index 9b6b27e5e048..342d07d5d6e7 100644 --- a/engine/test-src/org/pentaho/test/util/FieldAccessor.java +++ b/engine/test-src/org/pentaho/test/util/FieldAccessor.java @@ -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 ); @@ -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 ); } }