|
22 | 22 | import java.net.URLDecoder;
|
23 | 23 | import java.util.Arrays;
|
24 | 24 | import java.util.List;
|
| 25 | +import java.util.Iterator; |
| 26 | +import java.sql.Connection; |
| 27 | +import java.sql.Statement; |
| 28 | +import java.sql.SQLException; |
25 | 29 |
|
26 | 30 | import lombok.extern.slf4j.Slf4j;
|
27 | 31 |
|
|
47 | 51 | import org.sakaiproject.tool.api.Session;
|
48 | 52 | import org.sakaiproject.tool.api.SessionManager;
|
49 | 53 | import org.sakaiproject.util.BasicConfigItem;
|
| 54 | +import org.sakaiproject.db.api.SqlService; |
50 | 55 |
|
51 | 56 | @FixMethodOrder(NAME_ASCENDING)
|
52 | 57 | @Slf4j
|
@@ -264,4 +269,89 @@ public void testMimeDetection() throws Exception {
|
264 | 269 | stream.close();
|
265 | 270 | }
|
266 | 271 | }
|
| 272 | + |
| 273 | + @Test |
| 274 | + public void testSqlSanity() { |
| 275 | + ContentHostingService ch = getService(ContentHostingService.class); |
| 276 | + SqlService m_sqlService = getService(SqlService.class); |
| 277 | + |
| 278 | + // Make sure that the structure of the CONTENT_ tables passes the "modern schema" test |
| 279 | + try { |
| 280 | + Connection connection = m_sqlService.borrowConnection(); |
| 281 | + Statement statement = connection.createStatement(); |
| 282 | + |
| 283 | + try { |
| 284 | + statement.execute("select BINARY_ENTITY from CONTENT_COLLECTION where COLLECTION_ID = 'does-not-exist' " ); |
| 285 | + } catch ( Exception ex ) { |
| 286 | + Assert.fail(); |
| 287 | + } |
| 288 | + try { |
| 289 | + statement.execute("select XML from CONTENT_COLLECTION where COLLECTION_ID = 'does-not-exist' "); |
| 290 | + } catch ( Exception ex ) { |
| 291 | + Assert.fail(); |
| 292 | + } |
| 293 | + |
| 294 | + try { |
| 295 | + statement.execute("select BINARY_ENTITY from CONTENT_RESOURCE where RESOURCE_ID = 'does-not-exist' " ); |
| 296 | + } catch ( Exception ex ) { |
| 297 | + Assert.fail(); |
| 298 | + } |
| 299 | + try { |
| 300 | + statement.execute("select XML from CONTENT_RESOURCE where RESOURCE_ID = 'does-not-exist' "); |
| 301 | + Assert.fail(); |
| 302 | + } catch ( Exception ex ) { |
| 303 | + // Pass |
| 304 | + } |
| 305 | + try { |
| 306 | + statement.execute("select BINARY_ENTITY from CONTENT_RESOURCE_DELETE where RESOURCE_ID = 'does-not-exist' " ); |
| 307 | + } catch ( Exception ex ) { |
| 308 | + Assert.fail(); |
| 309 | + } |
| 310 | + try { |
| 311 | + statement.execute("select XML from CONTENT_RESOURCE_DELETE where RESOURCE_ID = 'does-not-exist' "); |
| 312 | + Assert.fail(); |
| 313 | + } catch ( Exception ex ) { |
| 314 | + // pass |
| 315 | + } |
| 316 | + |
| 317 | + // Yes, these are a bit less than exciting, post autoDDL - but what they heck - |
| 318 | + // they should not fail and be correct |
| 319 | + checkCount("select count(*) from CONTENT_COLLECTION where BINARY_ENTITY IS NULL ", 0); |
| 320 | + checkCount("select count(*) from CONTENT_RESOURCE where BINARY_ENTITY IS NULL ", 0); |
| 321 | + checkCount("select count(*) from CONTENT_RESOURCE_DELETE where BINARY_ENTITY IS NULL ", 0); |
| 322 | + |
| 323 | + checkCount("select count(*) from CONTENT_COLLECTION where XML IS NOT NULL ", 0); |
| 324 | + |
| 325 | + } catch ( Exception ex ) { |
| 326 | + Assert.fail(); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + protected void checkCount(String sql, int expected) throws Exception |
| 331 | + { |
| 332 | + |
| 333 | + SqlService m_sqlService = getService(SqlService.class); |
| 334 | + List list = m_sqlService.dbRead(sql, null, null); |
| 335 | + if (list == null) Assert.fail("Nothing returned for: "+sql); |
| 336 | + |
| 337 | + Iterator iter = list.iterator(); |
| 338 | + if (iter.hasNext()) |
| 339 | + { |
| 340 | + Object val = null; |
| 341 | + try |
| 342 | + { |
| 343 | + val = iter.next(); |
| 344 | + Integer found = Integer.parseInt((String) val); |
| 345 | + if ( found == expected ) return; |
| 346 | + Assert.fail("Mismatch expecting: " + expected + " found: " + found + " sql: "+sql); |
| 347 | + } |
| 348 | + catch (Exception ignore) |
| 349 | + { |
| 350 | + Assert.fail("Bad integer: " + val + " from: "+sql); |
| 351 | + } |
| 352 | + } |
| 353 | + Assert.fail("Empty list for: "+sql); |
| 354 | + return; |
| 355 | + } |
| 356 | + |
267 | 357 | }
|
0 commit comments