Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PDI-16553] Sort a list of tables fetched from DB. #8954

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions core/src/main/java/org/pentaho/di/core/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -3950,7 +3950,15 @@ public String[] getTablenames( String schemanamein, boolean includeSchema, Map<S
}
}
}
return res.toArray( new String[ res.size() ] );
return createSortedArray( res );
}

private static String[] createSortedArray( List<String> l ) {
if ( l == null ) {
return null;
}
List<String> sortedList = l.stream().sorted().collect( Collectors.toList() );
return sortedList.toArray( new String[sortedList.size()] );
}

public Map<String, Collection<String>> getTableMap() throws KettleDatabaseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,30 @@ public void testDeprecatedTargetFilenameExtension() {
public void testExecute_simpleConfiguration() throws IOException {
String validURL = HTTP_SERVER_BASEURL;
String invalidURL = "http://www.www.www.www";
File tempTargetFile = File.createTempFile( "targetFile", ".tmp" );
tempTargetFile.deleteOnExit();
Result result = new Result();
JobEntryHTTP basicHttpJobEntry = new JobEntryHTTP();
basicHttpJobEntry.setTargetFilename( tempTargetFile.getAbsolutePath() );
basicHttpJobEntry.setAddFilenameToResult( false );

// Test valid URL
File tempTargetFile1 = File.createTempFile( "targetFile", ".tmp" );
tempTargetFile1.deleteOnExit();
basicHttpJobEntry.setTargetFilename( tempTargetFile1.getAbsolutePath() );
basicHttpJobEntry.setUrl( validURL );
basicHttpJobEntry.execute( result, 0 );
assertEquals( 0L, result.getNrErrors() );
assertEquals( HttpStatus.SC_OK, basicHttpJobEntry.getResponseStatusCode() );
assertTrue( FileUtils.sizeOf( tempTargetFile ) > 0 );
assertTrue( FileUtils.sizeOf( tempTargetFile1 ) > 0 );

// Test invalid URL
result = new Result();
File tempTargetFile2 = File.createTempFile( "targetFile", ".tmp" );
tempTargetFile2.deleteOnExit();
basicHttpJobEntry.setTargetFilename( tempTargetFile2.getAbsolutePath() );
basicHttpJobEntry.setUrl( invalidURL );
basicHttpJobEntry.execute( result, 0 );
assertEquals( 1L, result.getNrErrors() );
assertEquals( 0, basicHttpJobEntry.getResponseStatusCode() );
assertTrue( FileUtils.sizeOf( tempTargetFile ) == 0 );
assertTrue( FileUtils.sizeOf( tempTargetFile2 ) == 0 );
}

@Test
Expand Down