SmokeTest.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright Materialize, Inc. and contributors. All rights reserved.
  2. //
  3. // Use of this software is governed by the Business Source License
  4. // included in the LICENSE file.
  5. //
  6. // As of the Change Date specified in that file, in accordance with
  7. // the Business Source License, use of this software will be governed
  8. // by the Apache License, Version 2.0.
  9. import java.sql.Connection;
  10. import java.sql.DatabaseMetaData;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.sql.Types;
  17. import org.junit.jupiter.api.BeforeEach;
  18. import org.junit.jupiter.api.Test;
  19. import org.junit.jupiter.api.Assertions;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.postgresql.jdbc.PgConnection;
  22. import org.postgresql.jdbc.TypeInfoCache;
  23. class SmokeTest {
  24. private Connection conn;
  25. String connUrl() {
  26. String host = System.getenv("PGHOST");
  27. if (host == null)
  28. host = "localhost";
  29. String port = System.getenv("PGPORT");
  30. if (port == null)
  31. port = "6875";
  32. return String.format("jdbc:postgresql://%s:%s/materialize", host, port);
  33. }
  34. @BeforeEach
  35. void setUp() throws SQLException, java.lang.ClassNotFoundException {
  36. String url = connUrl();
  37. conn = DriverManager.getConnection(url, "materialize", null);
  38. }
  39. @AfterEach
  40. void tearDown() throws SQLException, java.lang.ClassNotFoundException {
  41. conn.close();
  42. }
  43. @Test
  44. void testConnectionOptions() throws SQLException {
  45. // Test with an escaped =.
  46. {
  47. String url = connUrl() + "?options=--cluster%3Da_cluster";
  48. Connection optionsConn = DriverManager.getConnection(url, "materialize", null);
  49. PreparedStatement stmt = optionsConn.prepareStatement("SHOW cluster");
  50. ResultSet rs = stmt.executeQuery();
  51. Assertions.assertTrue(rs.next());
  52. Assertions.assertEquals("a_cluster", rs.getString(1));
  53. rs.close();
  54. stmt.close();
  55. optionsConn.close();
  56. }
  57. // Test without an escaped =.
  58. {
  59. String url = connUrl() + "?options=--cluster=b_cluster";
  60. Connection optionsConn = DriverManager.getConnection(url, "materialize", null);
  61. PreparedStatement stmt = optionsConn.prepareStatement("SHOW cluster");
  62. ResultSet rs = stmt.executeQuery();
  63. Assertions.assertTrue(rs.next());
  64. Assertions.assertEquals("b_cluster", rs.getString(1));
  65. rs.close();
  66. stmt.close();
  67. optionsConn.close();
  68. }
  69. // Test multiple options in a single query param.
  70. {
  71. String url = connUrl() + "?options=--cluster%3Db_cluster\\\\ -ccluster%3Dc_cluster";
  72. Connection optionsConn = DriverManager.getConnection(url, "materialize", null);
  73. PreparedStatement stmt = optionsConn.prepareStatement("SHOW cluster");
  74. ResultSet rs = stmt.executeQuery();
  75. Assertions.assertTrue(rs.next());
  76. Assertions.assertEquals("c_cluster", rs.getString(1));
  77. rs.close();
  78. stmt.close();
  79. optionsConn.close();
  80. }
  81. // Test multiple options in multiple query params.
  82. {
  83. String url = connUrl() + "?options=--cluster%3Db_cluster\\\\ -ccluster%3Dc_cluster&options=--cluster=d_cluster";
  84. Connection optionsConn = DriverManager.getConnection(url, "materialize", null);
  85. PreparedStatement stmt = optionsConn.prepareStatement("SHOW cluster");
  86. ResultSet rs = stmt.executeQuery();
  87. Assertions.assertTrue(rs.next());
  88. Assertions.assertEquals("d_cluster", rs.getString(1));
  89. rs.close();
  90. stmt.close();
  91. optionsConn.close();
  92. }
  93. }
  94. @Test
  95. void testParamString() throws SQLException {
  96. PreparedStatement stmt = conn.prepareStatement("SELECT ?");
  97. stmt.setString(1, "foo");
  98. ResultSet rs = stmt.executeQuery();
  99. Assertions.assertTrue(rs.next());
  100. Assertions.assertEquals("foo", rs.getString(1));
  101. rs.close();
  102. stmt.close();
  103. }
  104. @Test
  105. void testParamInt() throws SQLException {
  106. PreparedStatement stmt = conn.prepareStatement("SELECT ?");
  107. stmt.setInt(1, 42);
  108. ResultSet rs = stmt.executeQuery();
  109. Assertions.assertTrue(rs.next());
  110. Assertions.assertEquals("42", rs.getString(1));
  111. Assertions.assertEquals(42, rs.getInt(1));
  112. rs.close();
  113. stmt.close();
  114. }
  115. // Regression for materialize#4117.
  116. @Test
  117. void testBinaryTimestamp() throws SQLException, ClassNotFoundException {
  118. Class.forName("org.postgresql.jdbc.PgConnection");
  119. conn.unwrap(org.postgresql.jdbc.PgConnection.class).setForceBinary(true);
  120. PreparedStatement stmt = conn.prepareStatement("SELECT '2010-01-02'::timestamp");
  121. ResultSet rs = stmt.executeQuery();
  122. Assertions.assertTrue(rs.next());
  123. Assertions.assertEquals("2010-01-02 00:00:00", rs.getString(1));
  124. rs.close();
  125. stmt.close();
  126. }
  127. @Test
  128. void testGetSqlType() throws SQLException, ClassNotFoundException {
  129. Class.forName("org.postgresql.core.BaseConnection");
  130. TypeInfoCache ic = new TypeInfoCache(conn.unwrap(org.postgresql.core.BaseConnection.class), 0);
  131. Assertions.assertEquals(ic.getSQLType("int2"), Types.SMALLINT);
  132. Assertions.assertEquals(ic.getSQLType("_int2"), Types.ARRAY);
  133. }
  134. @Test
  135. void testPgJDBCgetColumns() throws SQLException, ClassNotFoundException {
  136. Statement stmt = conn.createStatement();
  137. stmt.execute("CREATE TABLE materialize.public.getcols (a INT, b STRING)");
  138. stmt.close();
  139. ResultSet columns = conn.getMetaData().getColumns("materialize", "public", "getcols", null);
  140. Assertions.assertTrue(columns.next());
  141. Assertions.assertEquals("a", columns.getString("COLUMN_NAME"));
  142. Assertions.assertTrue(columns.next());
  143. Assertions.assertEquals("b", columns.getString("COLUMN_NAME"));
  144. Assertions.assertFalse(columns.next());
  145. columns.close();
  146. stmt = conn.createStatement();
  147. stmt.execute("DROP TABLE materialize.public.getcols");
  148. stmt.close();
  149. }
  150. @Test
  151. void testPgJDBCgetPrimaryKeys() throws SQLException, ClassNotFoundException {
  152. Statement stmt = conn.createStatement();
  153. stmt.execute("CREATE TABLE materialize.public.getpks (a INT, b STRING)");
  154. stmt.close();
  155. ResultSet columns = conn.getMetaData().getPrimaryKeys("materialize", "public", "getpks");
  156. Assertions.assertFalse(columns.next());
  157. columns.close();
  158. stmt = conn.createStatement();
  159. stmt.execute("DROP TABLE materialize.public.getpks");
  160. stmt.close();
  161. }
  162. }