|
| 1 | +package org.postgresql.test.jdbc2; |
| 2 | + |
| 3 | +import org.postgresql.test.TestUtil; |
| 4 | +import junit.framework.TestCase; |
| 5 | +import java.sql.*; |
| 6 | + |
| 7 | +/* |
| 8 | + * TestCase to test the internal functionality of |
| 9 | + * org.postgresql.jdbc2.DatabaseMetaData's various properties. |
| 10 | + * Methods which return a ResultSet are tested elsewhere. |
| 11 | + * This avoids a complicated setUp/tearDown for something like |
| 12 | + * assertTrue(dbmd.nullPlusNonNullIsNull()); |
| 13 | + */ |
| 14 | + |
| 15 | +public class DatabaseMetaDataPropertiesTest extends TestCase |
| 16 | +{ |
| 17 | + |
| 18 | + private Connection con; |
| 19 | + /* |
| 20 | + * Constructor |
| 21 | + */ |
| 22 | + public DatabaseMetaDataPropertiesTest(String name) |
| 23 | + { |
| 24 | + super(name); |
| 25 | + } |
| 26 | + |
| 27 | + protected void setUp() throws Exception |
| 28 | + { |
| 29 | + con = TestUtil.openDB(); |
| 30 | + } |
| 31 | + protected void tearDown() throws Exception |
| 32 | + { |
| 33 | + TestUtil.closeDB( con ); |
| 34 | + } |
| 35 | + |
| 36 | + /* |
| 37 | + * The spec says this may return null, but we always do! |
| 38 | + */ |
| 39 | + public void testGetMetaData() |
| 40 | + { |
| 41 | + try |
| 42 | + { |
| 43 | + |
| 44 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 45 | + assertNotNull(dbmd); |
| 46 | + |
| 47 | + } |
| 48 | + catch (SQLException ex) |
| 49 | + { |
| 50 | + fail(ex.getMessage()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /* |
| 55 | + * Test default capabilities |
| 56 | + */ |
| 57 | + public void testCapabilities() |
| 58 | + { |
| 59 | + try |
| 60 | + { |
| 61 | + |
| 62 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 63 | + assertNotNull(dbmd); |
| 64 | + |
| 65 | + assertTrue(dbmd.allProceduresAreCallable()); |
| 66 | + assertTrue(dbmd.allTablesAreSelectable()); // not true all the time |
| 67 | + |
| 68 | + // This should always be false for postgresql (at least for 7.x) |
| 69 | + assertTrue(!dbmd.isReadOnly()); |
| 70 | + |
| 71 | + // does the backend support this yet? The protocol does... |
| 72 | + assertTrue(!dbmd.supportsMultipleResultSets()); |
| 73 | + |
| 74 | + // yes, as multiple backends can have transactions open |
| 75 | + assertTrue(dbmd.supportsMultipleTransactions()); |
| 76 | + |
| 77 | + assertTrue(dbmd.supportsMinimumSQLGrammar()); |
| 78 | + assertTrue(!dbmd.supportsCoreSQLGrammar()); |
| 79 | + assertTrue(!dbmd.supportsExtendedSQLGrammar()); |
| 80 | + if (TestUtil.haveMinimumServerVersion(con,"7.3")) |
| 81 | + assertTrue(dbmd.supportsANSI92EntryLevelSQL()); |
| 82 | + else |
| 83 | + assertTrue(!dbmd.supportsANSI92EntryLevelSQL()); |
| 84 | + assertTrue(!dbmd.supportsANSI92IntermediateSQL()); |
| 85 | + assertTrue(!dbmd.supportsANSI92FullSQL()); |
| 86 | + |
| 87 | + assertTrue(!dbmd.supportsIntegrityEnhancementFacility()); |
| 88 | + |
| 89 | + } |
| 90 | + catch (SQLException ex) |
| 91 | + { |
| 92 | + fail(ex.getMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + public void testJoins() |
| 98 | + { |
| 99 | + try |
| 100 | + { |
| 101 | + |
| 102 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 103 | + assertNotNull(dbmd); |
| 104 | + |
| 105 | + assertTrue(dbmd.supportsOuterJoins()); |
| 106 | + assertTrue(dbmd.supportsFullOuterJoins()); |
| 107 | + assertTrue(dbmd.supportsLimitedOuterJoins()); |
| 108 | + |
| 109 | + } |
| 110 | + catch (SQLException ex) |
| 111 | + { |
| 112 | + fail(ex.getMessage()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void testCursors() |
| 117 | + { |
| 118 | + try |
| 119 | + { |
| 120 | + |
| 121 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 122 | + assertNotNull(dbmd); |
| 123 | + |
| 124 | + assertTrue(!dbmd.supportsPositionedDelete()); |
| 125 | + assertTrue(!dbmd.supportsPositionedUpdate()); |
| 126 | + |
| 127 | + } |
| 128 | + catch (SQLException ex) |
| 129 | + { |
| 130 | + fail(ex.getMessage()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public void testValues() |
| 135 | + { |
| 136 | + try { |
| 137 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 138 | + assertNotNull(dbmd); |
| 139 | + int indexMaxKeys = dbmd.getMaxColumnsInIndex(); |
| 140 | + if (TestUtil.haveMinimumServerVersion(con,"7.3")) { |
| 141 | + assertEquals(indexMaxKeys,32); |
| 142 | + } else { |
| 143 | + assertEquals(indexMaxKeys,16); |
| 144 | + } |
| 145 | + } catch (SQLException sqle) { |
| 146 | + fail(sqle.getMessage()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public void testNulls() |
| 151 | + { |
| 152 | + try |
| 153 | + { |
| 154 | + |
| 155 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 156 | + assertNotNull(dbmd); |
| 157 | + |
| 158 | + assertTrue(!dbmd.nullsAreSortedAtStart()); |
| 159 | + assertTrue( dbmd.nullsAreSortedAtEnd() != TestUtil.haveMinimumServerVersion(con,"7.2")); |
| 160 | + assertTrue( dbmd.nullsAreSortedHigh() == TestUtil.haveMinimumServerVersion(con,"7.2")); |
| 161 | + assertTrue(!dbmd.nullsAreSortedLow()); |
| 162 | + |
| 163 | + assertTrue(dbmd.nullPlusNonNullIsNull()); |
| 164 | + |
| 165 | + assertTrue(dbmd.supportsNonNullableColumns()); |
| 166 | + |
| 167 | + } |
| 168 | + catch (SQLException ex) |
| 169 | + { |
| 170 | + fail(ex.getMessage()); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public void testLocalFiles() |
| 175 | + { |
| 176 | + try |
| 177 | + { |
| 178 | + |
| 179 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 180 | + assertNotNull(dbmd); |
| 181 | + |
| 182 | + assertTrue(!dbmd.usesLocalFilePerTable()); |
| 183 | + assertTrue(!dbmd.usesLocalFiles()); |
| 184 | + |
| 185 | + } |
| 186 | + catch (SQLException ex) |
| 187 | + { |
| 188 | + fail(ex.getMessage()); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public void testIdentifiers() |
| 193 | + { |
| 194 | + try |
| 195 | + { |
| 196 | + |
| 197 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 198 | + assertNotNull(dbmd); |
| 199 | + |
| 200 | + assertTrue(!dbmd.supportsMixedCaseIdentifiers()); // always false |
| 201 | + assertTrue(dbmd.supportsMixedCaseQuotedIdentifiers()); // always true |
| 202 | + |
| 203 | + assertTrue(!dbmd.storesUpperCaseIdentifiers()); // always false |
| 204 | + assertTrue(dbmd.storesLowerCaseIdentifiers()); // always true |
| 205 | + assertTrue(!dbmd.storesUpperCaseQuotedIdentifiers()); // always false |
| 206 | + assertTrue(!dbmd.storesLowerCaseQuotedIdentifiers()); // always false |
| 207 | + assertTrue(!dbmd.storesMixedCaseQuotedIdentifiers()); // always false |
| 208 | + |
| 209 | + assertTrue(dbmd.getIdentifierQuoteString().equals("\"")); |
| 210 | + |
| 211 | + |
| 212 | + } |
| 213 | + catch (SQLException ex) |
| 214 | + { |
| 215 | + fail(ex.getMessage()); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + public void testTables() |
| 220 | + { |
| 221 | + try |
| 222 | + { |
| 223 | + |
| 224 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 225 | + assertNotNull(dbmd); |
| 226 | + |
| 227 | + // we can add columns |
| 228 | + assertTrue(dbmd.supportsAlterTableWithAddColumn()); |
| 229 | + |
| 230 | + // we can only drop columns in >= 7.3 |
| 231 | + if (TestUtil.haveMinimumServerVersion(con,"7.3")) { |
| 232 | + assertTrue(dbmd.supportsAlterTableWithDropColumn()); |
| 233 | + } else { |
| 234 | + assertTrue(!dbmd.supportsAlterTableWithDropColumn()); |
| 235 | + } |
| 236 | + } |
| 237 | + catch (SQLException ex) |
| 238 | + { |
| 239 | + fail(ex.getMessage()); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + public void testSelect() |
| 244 | + { |
| 245 | + try |
| 246 | + { |
| 247 | + |
| 248 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 249 | + assertNotNull(dbmd); |
| 250 | + |
| 251 | + // yes we can?: SELECT col a FROM a; |
| 252 | + assertTrue(dbmd.supportsColumnAliasing()); |
| 253 | + |
| 254 | + // yes we can have expressions in ORDERBY |
| 255 | + assertTrue(dbmd.supportsExpressionsInOrderBy()); |
| 256 | + |
| 257 | + // Yes, an ORDER BY clause can contain columns that are not in the |
| 258 | + // SELECT clause. |
| 259 | + assertTrue(dbmd.supportsOrderByUnrelated()); |
| 260 | + |
| 261 | + assertTrue(dbmd.supportsGroupBy()); |
| 262 | + assertTrue(dbmd.supportsGroupByUnrelated()); |
| 263 | + assertTrue(dbmd.supportsGroupByBeyondSelect()); // needs checking |
| 264 | + |
| 265 | + } |
| 266 | + catch (SQLException ex) |
| 267 | + { |
| 268 | + fail(ex.getMessage()); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + public void testDBParams() |
| 273 | + { |
| 274 | + try |
| 275 | + { |
| 276 | + |
| 277 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 278 | + assertNotNull(dbmd); |
| 279 | + |
| 280 | + assertTrue(dbmd.getURL().equals(TestUtil.getURL())); |
| 281 | + assertTrue(dbmd.getUserName().equals(TestUtil.getUser())); |
| 282 | + |
| 283 | + } |
| 284 | + catch (SQLException ex) |
| 285 | + { |
| 286 | + fail(ex.getMessage()); |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + public void testDbProductDetails() |
| 291 | + { |
| 292 | + try |
| 293 | + { |
| 294 | + assertTrue(con instanceof org.postgresql.PGConnection); |
| 295 | + org.postgresql.jdbc2.AbstractJdbc2Connection pc = (org.postgresql.jdbc2.AbstractJdbc2Connection) con; |
| 296 | + |
| 297 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 298 | + assertNotNull(dbmd); |
| 299 | + |
| 300 | + assertTrue(dbmd.getDatabaseProductName().equals("PostgreSQL")); |
| 301 | + //The test below doesn't make sense to me, it tests that |
| 302 | + //the version of the driver = the version of the database it is connected to |
| 303 | + //since the driver should be backwardly compatible this test is commented out |
| 304 | + //assertTrue(dbmd.getDatabaseProductVersion().startsWith( |
| 305 | + // Integer.toString(pc.getDriver().getMajorVersion()) |
| 306 | + // + "." |
| 307 | + // + Integer.toString(pc.getDriver().getMinorVersion()))); |
| 308 | + assertTrue(dbmd.getDriverName().equals("PostgreSQL Native Driver")); |
| 309 | + |
| 310 | + } |
| 311 | + catch (SQLException ex) |
| 312 | + { |
| 313 | + fail(ex.getMessage()); |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + public void testDriverVersioning() |
| 318 | + { |
| 319 | + try |
| 320 | + { |
| 321 | + assertTrue(con instanceof org.postgresql.PGConnection); |
| 322 | + org.postgresql.jdbc2.AbstractJdbc2Connection pc = (org.postgresql.jdbc2.AbstractJdbc2Connection) con; |
| 323 | + |
| 324 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 325 | + assertNotNull(dbmd); |
| 326 | + |
| 327 | + assertTrue(dbmd.getDriverVersion().equals(pc.getDriver().getVersion())); |
| 328 | + assertTrue(dbmd.getDriverMajorVersion() == pc.getDriver().getMajorVersion()); |
| 329 | + assertTrue(dbmd.getDriverMinorVersion() == pc.getDriver().getMinorVersion()); |
| 330 | + |
| 331 | + |
| 332 | + } |
| 333 | + catch (SQLException ex) |
| 334 | + { |
| 335 | + fail(ex.getMessage()); |
| 336 | + } |
| 337 | + } |
| 338 | +} |
| 339 | + |
0 commit comments