Skip to content

Commit

Permalink
PDI-19719 - Fixed NPE if input stream is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
petrprochy committed Jan 31, 2023
1 parent 123cb2c commit 31dbde9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2022 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2002-2023 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand Down Expand Up @@ -30,22 +30,15 @@
//
//

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.google.common.annotations.VisibleForTesting;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LoggingObjectInterface;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
Expand All @@ -54,11 +47,16 @@
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.postgresql.PGConnection;
import org.postgresql.copy.PGCopyOutputStream;

import com.google.common.annotations.VisibleForTesting;

import org.postgresql.PGConnection;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Performs a bulk load to a postgres table.
Expand Down Expand Up @@ -492,13 +490,12 @@ public void dispose( StepMetaInterface smi, StepDataInterface sdi ) {
meta = (PGBulkLoaderMeta) smi;
data = (PGBulkLoaderData) sdi;

try {
pgCopyOut.close();
} catch ( IOException e ) {
logError( "Error while closing the Postgres Output Stream", e.getMessage() );
}

if ( data.db != null ) {
try {
pgCopyOut.close();
} catch ( IOException e ) {
logError( "Error while closing the Postgres Output Stream", e.getMessage() );
}
data.db.close();
}
super.dispose( smi, sdi );
Expand Down
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2017-2022 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2017-2023 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand All @@ -21,20 +21,6 @@
******************************************************************************/
package org.pentaho.di.trans.steps.pgbulkloader;

import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
Expand All @@ -58,10 +44,20 @@
import java.io.IOException;
import java.lang.reflect.Field;

import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.pentaho.di.core.row.ValueMetaInterface.TYPE_BOOLEAN;

public class PGBulkLoaderTest {
Expand Down Expand Up @@ -207,6 +203,20 @@ public void writeBooleanToPgOutput() throws Exception {
assertEquals( "false", "0" + Const.CR, out.toString() );
}

/**
* Regression test for PDI-18989
*/
@Test
public void emptyInput() throws Exception {
final PGBulkLoaderMeta meta = initMeta( "field" );
final PGBulkLoaderData data = initData();
final PGBulkLoader loader = spy( this.pgBulkLoader );
doReturn( null ).when( loader ).getRow();
loader.init( meta, data );
assertFalse( "Step finished", loader.processRow( meta, data ) );
loader.dispose( meta, data );
}

private ByteArrayOutputStream initPGCopyOutputStream() throws IOException, NoSuchFieldException, IllegalAccessException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PGCopyOutputStream pgCopy = mock( PGCopyOutputStream.class );
Expand Down

0 comments on commit 31dbde9

Please sign in to comment.