|
1 | 1 | package com.clickhouse.jdbc; |
2 | 2 |
|
3 | 3 | import java.sql.Array; |
| 4 | +import java.sql.Connection; |
4 | 5 | import java.sql.ResultSet; |
5 | 6 | import java.sql.SQLException; |
6 | 7 | import java.sql.Statement; |
@@ -69,4 +70,110 @@ public void testNonExistDatabase() throws Exception { |
69 | 70 | } |
70 | 71 | Assert.assertNotNull(exp, "Should not have SQLException because the database has been created"); |
71 | 72 | } |
| 73 | + |
| 74 | + @Test(groups = "integration") |
| 75 | + public void testReadOnly() throws SQLException { |
| 76 | + Properties props = new Properties(); |
| 77 | + props.setProperty("user", "dba"); |
| 78 | + props.setProperty("password", "dba"); |
| 79 | + try (Connection conn = newConnection(props); Statement stmt = conn.createStatement()) { |
| 80 | + Assert.assertFalse(conn.isReadOnly(), "Connection should NOT be readonly"); |
| 81 | + Assert.assertFalse(stmt.execute( |
| 82 | + "drop table if exists test_readonly; drop user if exists readonly1; drop user if exists readonly2; " |
| 83 | + + "create table test_readonly(id String)engine=Memory; " |
| 84 | + + "create user readonly1 IDENTIFIED WITH no_password SETTINGS readonly=1; " |
| 85 | + + "create user readonly2 IDENTIFIED WITH no_password SETTINGS readonly=2; " |
| 86 | + + "grant insert on test_readonly TO readonly1, readonly2")); |
| 87 | + conn.setReadOnly(false); |
| 88 | + Assert.assertFalse(conn.isReadOnly(), "Connection should NOT be readonly"); |
| 89 | + conn.setReadOnly(true); |
| 90 | + Assert.assertTrue(conn.isReadOnly(), "Connection should be readonly"); |
| 91 | + |
| 92 | + try (Statement s = conn.createStatement()) { |
| 93 | + SQLException exp = null; |
| 94 | + try { |
| 95 | + s.execute("insert into test_readonly values('readonly1')"); |
| 96 | + } catch (SQLException e) { |
| 97 | + exp = e; |
| 98 | + } |
| 99 | + Assert.assertNotNull(exp, "Should fail with SQL exception"); |
| 100 | + Assert.assertEquals(exp.getErrorCode(), 164); |
| 101 | + } |
| 102 | + |
| 103 | + conn.setReadOnly(false); |
| 104 | + Assert.assertFalse(conn.isReadOnly(), "Connection should NOT be readonly"); |
| 105 | + |
| 106 | + try (Statement s = conn.createStatement()) { |
| 107 | + Assert.assertFalse(s.execute("insert into test_readonly values('readonly1')")); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + props.clear(); |
| 112 | + props.setProperty("user", "readonly1"); |
| 113 | + try (Connection conn = newConnection(props); Statement stmt = conn.createStatement()) { |
| 114 | + Assert.assertTrue(conn.isReadOnly(), "Connection should be readonly"); |
| 115 | + conn.setReadOnly(true); |
| 116 | + Assert.assertTrue(conn.isReadOnly(), "Connection should be readonly"); |
| 117 | + SQLException exp = null; |
| 118 | + try { |
| 119 | + stmt.execute("insert into test_readonly values('readonly1')"); |
| 120 | + } catch (SQLException e) { |
| 121 | + exp = e; |
| 122 | + } |
| 123 | + Assert.assertNotNull(exp, "Should fail with SQL exception"); |
| 124 | + Assert.assertEquals(exp.getErrorCode(), 164); |
| 125 | + |
| 126 | + exp = null; |
| 127 | + try { |
| 128 | + conn.setReadOnly(true); |
| 129 | + stmt.execute("set max_result_rows=5; select 1"); |
| 130 | + } catch (SQLException e) { |
| 131 | + exp = e; |
| 132 | + } |
| 133 | + Assert.assertNotNull(exp, "Should fail with SQL exception"); |
| 134 | + Assert.assertEquals(exp.getErrorCode(), 164); |
| 135 | + } |
| 136 | + |
| 137 | + props.setProperty("user", "readonly2"); |
| 138 | + try (Connection conn = newConnection(props); Statement stmt = conn.createStatement()) { |
| 139 | + Assert.assertTrue(conn.isReadOnly(), "Connection should be readonly"); |
| 140 | + Assert.assertTrue(stmt.execute("set max_result_rows=5; select 1")); |
| 141 | + |
| 142 | + Assert.assertThrows(SQLException.class, () -> conn.setReadOnly(false)); |
| 143 | + Assert.assertTrue(conn.isReadOnly(), "Connection should be readonly"); |
| 144 | + |
| 145 | + SQLException exp = null; |
| 146 | + try (Statement s = conn.createStatement()) { |
| 147 | + Assert.assertFalse(s.execute("insert into test_readonly values('readonly2')")); |
| 148 | + } catch (SQLException e) { |
| 149 | + exp = e; |
| 150 | + } |
| 151 | + Assert.assertNotNull(exp, "Should fail with SQL exception"); |
| 152 | + Assert.assertEquals(exp.getErrorCode(), 164); |
| 153 | + |
| 154 | + conn.setReadOnly(true); |
| 155 | + Assert.assertTrue(conn.isReadOnly(), "Connection should be readonly"); |
| 156 | + } |
| 157 | + |
| 158 | + props.setProperty(ClickHouseClientOption.SERVER_TIME_ZONE.getKey(), "UTC"); |
| 159 | + props.setProperty(ClickHouseClientOption.SERVER_VERSION.getKey(), "21.8"); |
| 160 | + try (Connection conn = newConnection(props); Statement stmt = conn.createStatement()) { |
| 161 | + Assert.assertFalse(conn.isReadOnly(), "Connection should NOT be readonly"); |
| 162 | + Assert.assertTrue(stmt.execute("set max_result_rows=5; select 1")); |
| 163 | + |
| 164 | + conn.setReadOnly(true); |
| 165 | + Assert.assertTrue(conn.isReadOnly(), "Connection should be readonly"); |
| 166 | + conn.setReadOnly(false); |
| 167 | + Assert.assertFalse(conn.isReadOnly(), "Connection should NOT be readonly"); |
| 168 | + |
| 169 | + SQLException exp = null; |
| 170 | + try (Statement s = conn.createStatement()) { |
| 171 | + Assert.assertFalse(s.execute("insert into test_readonly values('readonly2')")); |
| 172 | + } catch (SQLException e) { |
| 173 | + exp = e; |
| 174 | + } |
| 175 | + Assert.assertNotNull(exp, "Should fail with SQL exception"); |
| 176 | + Assert.assertEquals(exp.getErrorCode(), 164); |
| 177 | + } |
| 178 | + } |
72 | 179 | } |
0 commit comments