Skip to content

Commit

Permalink
fix: sort the results of java.sql.DatabaseMetaData#getCatalogs as per…
Browse files Browse the repository at this point in the history
… its specification

This is an follow-up to #2953
See #2949
  • Loading branch information
vlsi committed Nov 16, 2023
1 parent de74292 commit 69debfa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,8 @@ public ResultSet getSchemas(@Nullable String catalog, @Nullable String schemaPat
@Override
public ResultSet getCatalogs() throws SQLException {
String sql = "SELECT datname AS TABLE_CAT FROM pg_catalog.pg_database"
+ " WHERE datallowconn = true";
+ " WHERE datallowconn = true"
+ " ORDER BY datname";
return createMetaDataStatement().executeQuery(sql);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

package org.postgresql.test.jdbc2;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -16,6 +19,7 @@
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4.BinaryMode;

import org.hamcrest.MatcherAssert;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
Expand Down Expand Up @@ -1064,27 +1068,23 @@ public void testProcedures() throws SQLException {
@Test
public void testCatalogs() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getCatalogs();

boolean foundDefault = false;
boolean foundTest = false;
int count;

for (count = 0; rs.next(); count++) {
String catalog = rs.getString("TABLE_CAT");
if ("test".equals(catalog)) {
foundTest = true;
}
if ("postgres".equals(catalog)) {
foundDefault = true;
try (ResultSet rs = dbmd.getCatalogs();) {
List<String> catalogs = new ArrayList<>();
while (rs.next()) {
catalogs.add(rs.getString("TABLE_CAT"));
}
List<String> sortedCatalogs = new ArrayList<>(catalogs);
Collections.sort(sortedCatalogs);

MatcherAssert.assertThat(
catalogs,
allOf(
hasItem("test"),
hasItem("postgres"),
equalTo(sortedCatalogs)
)
);
}

rs.close();

assertTrue(count >= 2);
assertTrue(foundDefault);
assertTrue(foundTest);
}

@Test
Expand Down

0 comments on commit 69debfa

Please sign in to comment.