diff --git a/core/src/main/java/org/pentaho/di/core/database/Database.java b/core/src/main/java/org/pentaho/di/core/database/Database.java index ed519e108a3b..4694c0996818 100644 --- a/core/src/main/java/org/pentaho/di/core/database/Database.java +++ b/core/src/main/java/org/pentaho/di/core/database/Database.java @@ -53,11 +53,11 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang.StringUtils; import org.apache.commons.vfs2.FileObject; import org.pentaho.di.core.Const; -import org.pentaho.di.core.logging.SimpleLoggingObject; import org.pentaho.di.core.plugins.PluginTypeListener; import org.pentaho.di.core.row.value.ValueMetaPluginType; import org.pentaho.di.core.util.Utils; @@ -3950,7 +3950,15 @@ public String[] getTablenames( String schemanamein, boolean includeSchema, Map l ) { + if ( l == null ) { + return null; + } + List sortedList = l.stream().sorted().collect( Collectors.toList() ); + return sortedList.toArray( new String[sortedList.size()] ); } public Map> getTableMap() throws KettleDatabaseException { diff --git a/core/src/test/java/org/pentaho/di/core/database/DatabaseTest.java b/core/src/test/java/org/pentaho/di/core/database/DatabaseTest.java index aeba5a42630f..5056934c14fb 100644 --- a/core/src/test/java/org/pentaho/di/core/database/DatabaseTest.java +++ b/core/src/test/java/org/pentaho/di/core/database/DatabaseTest.java @@ -59,6 +59,7 @@ import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.sql.Types; +import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.logging.Logger; @@ -842,6 +843,26 @@ public void testGetTablenames() throws SQLException, KettleDatabaseException { assertEquals( tableNames.length, 1 ); } + @Test + public void testGetTablenamesMultipleTables() throws SQLException, KettleDatabaseException { + when(rs.next()).thenReturn(true, true, true, false); + when(rs.getString("TABLE_NAME")).thenReturn("K_TABLE","A_TABLE", "Z_TABLE"); + when(dbMetaMock.getTables( + same(dbMetaDataMock), or(anyString(), eq(null)), or(anyString(), eq(null)), any() + )).thenReturn(rs); + when(dbMetaMock.getQuotedSchemaTableCombination(eq(null), anyString())).thenReturn( + "K_TABLE", "A_TABLE", "Z_TABLE" + ); + Database db = new Database(log, dbMetaMock); + db.setConnection(mockConnection(dbMetaDataMock)); + + String[] tableNames = db.getTablenames(); + assertEquals(3, tableNames.length); + assertEquals("A_TABLE", tableNames[0]); + assertEquals("K_TABLE", tableNames[1]); + assertEquals("Z_TABLE", tableNames[2]); + } + @Test public void testCheckTableExistsNoProperty() throws Exception { DatabaseMeta databaseMeta = new DatabaseMeta();