|
| 1 | +package dev.mongocamp.driver.mongodb.jdbc |
| 2 | + |
| 3 | +import dev.mongocamp.driver.mongodb.* |
| 4 | +import dev.mongocamp.driver.mongodb.jdbc.statement.MongoPreparedStatement |
| 5 | + |
| 6 | +import java.sql.{Connection, DriverManager, SQLFeatureNotSupportedException, SQLWarning, Savepoint} |
| 7 | +import java.util.Properties |
| 8 | +import java.util.concurrent.Executor |
| 9 | + |
| 10 | +class ConnectionSuite extends BaseJdbcSuite { |
| 11 | + |
| 12 | + test("getDatabaseProvider should return the database provider") { |
| 13 | + val driver = new MongoJdbcDriver() |
| 14 | + val connectionUrl = "jdbc:mongodb://localhost:27017/mongocamp-unit-test?retryWrites=true&loadBalanced=false&serverSelectionTimeoutMS=5000&connectTimeoutMS=10000" |
| 15 | + val propertiesInfo = driver.getPropertyInfo(connectionUrl, new Properties()) |
| 16 | + assertEquals(propertiesInfo.length, 5) |
| 17 | + } |
| 18 | + |
| 19 | + test("getDatabaseProvider should return the database provider") { |
| 20 | + assertEquals(connection.asInstanceOf[MongoJdbcConnection].getDatabaseProvider.collections().results().isEmpty, false) |
| 21 | + } |
| 22 | + |
| 23 | + test("createStatement should return a MongoPreparedStatement") { |
| 24 | + assert(connection.createStatement().isInstanceOf[MongoPreparedStatement]) |
| 25 | + assert(connection.createStatement(0, 0).isInstanceOf[MongoPreparedStatement]) |
| 26 | + assert(connection.createStatement(0, 0, 0).isInstanceOf[MongoPreparedStatement]) |
| 27 | + } |
| 28 | + |
| 29 | + test("prepareStatement should return a MongoPreparedStatement") { |
| 30 | + assert(connection.prepareStatement("SELECT * FROM people").isInstanceOf[MongoPreparedStatement]) |
| 31 | + assert(connection.prepareStatement("SELECT * FROM people", 0).isInstanceOf[MongoPreparedStatement]) |
| 32 | + assert(connection.prepareStatement("SELECT * FROM people", 0, 0).isInstanceOf[MongoPreparedStatement]) |
| 33 | + assert(connection.prepareStatement("SELECT * FROM people", 0, 0, 0).isInstanceOf[MongoPreparedStatement]) |
| 34 | + assert(connection.prepareStatement("SELECT * FROM people", Array[Int]()).isInstanceOf[MongoPreparedStatement]) |
| 35 | + assert(connection.prepareStatement("SELECT * FROM people", Array[String]()).isInstanceOf[MongoPreparedStatement]) |
| 36 | + } |
| 37 | + |
| 38 | + test("prepareCall should return a MongoPreparedStatement") { |
| 39 | + assert(connection.prepareCall("SELECT * FROM people").isInstanceOf[MongoPreparedStatement]) |
| 40 | + assert(connection.prepareCall("SELECT * FROM people", 0, 0).isInstanceOf[MongoPreparedStatement]) |
| 41 | + assert(connection.prepareCall("SELECT * FROM people", 0, 0, 0).isInstanceOf[MongoPreparedStatement]) |
| 42 | + } |
| 43 | + |
| 44 | + test("nativeSQL should return the same SQL string") { |
| 45 | + val sql = "SELECT * FROM people" |
| 46 | + assertEquals(connection.nativeSQL(sql), sql) |
| 47 | + } |
| 48 | + |
| 49 | + test("setAutoCommit should not throw an exception") { |
| 50 | + connection.setAutoCommit(true) |
| 51 | + } |
| 52 | + |
| 53 | + test("getAutoCommit should return true") { |
| 54 | + assert(connection.getAutoCommit) |
| 55 | + } |
| 56 | + |
| 57 | + test("commit should not throw an exception") { |
| 58 | + connection.commit() |
| 59 | + } |
| 60 | + |
| 61 | + test("rollback should not throw an exception") { |
| 62 | + connection.rollback() |
| 63 | + } |
| 64 | + |
| 65 | + test("getMetaData should return MongoDatabaseMetaData") { |
| 66 | + assert(connection.getMetaData.isInstanceOf[MongoDatabaseMetaData]) |
| 67 | + } |
| 68 | + |
| 69 | + test("setReadOnly should set the connection to read-only") { |
| 70 | + connection.setReadOnly(true) |
| 71 | + assert(connection.isReadOnly) |
| 72 | + } |
| 73 | + |
| 74 | + test("setCatalog should not throw an exception") { |
| 75 | + connection.setCatalog("testCatalog") |
| 76 | + } |
| 77 | + |
| 78 | + test("getCatalog should return null") { |
| 79 | + assertEquals(connection.getCatalog, null) |
| 80 | + } |
| 81 | + |
| 82 | + test("intercept not implemented sql features") { |
| 83 | + intercept[SQLFeatureNotSupportedException](connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED)) |
| 84 | + intercept[SQLFeatureNotSupportedException](connection.createNClob()) |
| 85 | + intercept[SQLFeatureNotSupportedException](connection.createBlob()) |
| 86 | + intercept[SQLFeatureNotSupportedException](connection.createClob()) |
| 87 | + intercept[SQLFeatureNotSupportedException](connection.createSQLXML()) |
| 88 | + intercept[SQLFeatureNotSupportedException](connection.createStruct("", null)) |
| 89 | + intercept[SQLFeatureNotSupportedException](connection.createArrayOf("typeName: String", null)) |
| 90 | + } |
| 91 | + |
| 92 | + test("getTransactionIsolation should return TRANSACTION_NONE") { |
| 93 | + assertEquals(connection.getTransactionIsolation, Connection.TRANSACTION_NONE) |
| 94 | + } |
| 95 | + |
| 96 | + test("getWarnings should return null") { |
| 97 | + assertEquals(connection.getWarnings, null) |
| 98 | + } |
| 99 | + |
| 100 | + test("clearWarnings should not throw an exception") { |
| 101 | + connection.clearWarnings() |
| 102 | + } |
| 103 | + |
| 104 | + test("getTypeMap should return null") { |
| 105 | + assertEquals(connection.getTypeMap, null) |
| 106 | + } |
| 107 | + |
| 108 | + test("setTypeMap should not throw an exception") { |
| 109 | + connection.setTypeMap(new java.util.HashMap[String, Class[_]]()) |
| 110 | + } |
| 111 | + |
| 112 | + test("setHoldability should not throw an exception") { |
| 113 | + connection.setHoldability(0) |
| 114 | + } |
| 115 | + |
| 116 | + test("getHoldability should return 0") { |
| 117 | + assertEquals(connection.getHoldability, 0) |
| 118 | + } |
| 119 | + |
| 120 | + test("setSavepoint should return null") { |
| 121 | + assertEquals(connection.setSavepoint(), null) |
| 122 | + } |
| 123 | + |
| 124 | + test("setSavepoint with name should return null") { |
| 125 | + assertEquals(connection.setSavepoint("savepoint"), null) |
| 126 | + } |
| 127 | + |
| 128 | + test("rollback with savepoint should not throw an exception") { |
| 129 | + connection.rollback(null.asInstanceOf[Savepoint]) |
| 130 | + } |
| 131 | + |
| 132 | + test("releaseSavepoint should not throw an exception") { |
| 133 | + connection.releaseSavepoint(null.asInstanceOf[Savepoint]) |
| 134 | + } |
| 135 | + |
| 136 | + test("isValid should return true") { |
| 137 | + assert(connection.isValid(0)) |
| 138 | + } |
| 139 | + |
| 140 | + test("setClientInfo with name and value should not throw an exception") { |
| 141 | + connection.setClientInfo("ApplicationName", "testApp") |
| 142 | + } |
| 143 | + |
| 144 | + test("setClientInfo with properties should not throw an exception") { |
| 145 | + val properties = new Properties() |
| 146 | + properties.setProperty("ApplicationName", "testApp") |
| 147 | + connection.setClientInfo(properties) |
| 148 | + } |
| 149 | + |
| 150 | + test("getClientInfo with name should return the application name") { |
| 151 | + connection.setClientInfo("ApplicationName", "testApp") |
| 152 | + assertEquals(connection.getClientInfo("ApplicationName"), "testApp") |
| 153 | + } |
| 154 | + |
| 155 | + test("getClientInfo should return properties with application name") { |
| 156 | + connection.setClientInfo("ApplicationName", "testApp") |
| 157 | + val properties = connection.getClientInfo |
| 158 | + assertEquals(properties.getProperty("ApplicationName"), "testApp") |
| 159 | + } |
| 160 | + |
| 161 | + test("getSchema should return the default database name") { |
| 162 | + assertEquals(connection.getSchema, "mongocamp-unit-test") |
| 163 | + } |
| 164 | + |
| 165 | + test("setSchema should not throw an exception") { |
| 166 | + connection.setSchema("testSchema") |
| 167 | + } |
| 168 | + |
| 169 | + test("abort should not throw an exception") { |
| 170 | + connection.abort(null.asInstanceOf[Executor]) |
| 171 | + } |
| 172 | + |
| 173 | + test("setNetworkTimeout should not throw an exception") { |
| 174 | + connection.setNetworkTimeout(null.asInstanceOf[Executor], 0) |
| 175 | + } |
| 176 | + |
| 177 | + test("getNetworkTimeout should return 0") { |
| 178 | + assertEquals(connection.getNetworkTimeout, 0) |
| 179 | + } |
| 180 | + |
| 181 | + test("unwrap should return null") { |
| 182 | + assertEquals(connection.unwrap(classOf[Connection]), null) |
| 183 | + } |
| 184 | + |
| 185 | + test("isWrapperFor should return false") { |
| 186 | + assert(!connection.isWrapperFor(classOf[Connection])) |
| 187 | + } |
| 188 | + |
| 189 | + test("close should close the connection") { |
| 190 | + connection.close() |
| 191 | + assertEquals(connection.isClosed, true) |
| 192 | + } |
| 193 | +} |
0 commit comments