@Test public void jdbcCompliant_exception() throws SQLException { Driver driver = new MockDriver() { @Override public boolean jdbcCompliant() { throw new NullPointerException(); } }; HiveDriver d = new HiveDriver(getMockUtil(getMockShimWithDriver(driver))); // should return false if there is an exception assertFalse(d.jdbcCompliant()); }
@Test public void getMinorVersion_exception() { Driver driver = new MockDriver() { @Override public int getMinorVersion() { throw new NullPointerException(); } }; HiveDriver d = new HiveDriver(getMockUtil(getMockShimWithDriver(driver))); // If an exception is thrown the version returned should be -1 assertEquals(-1, d.getMinorVersion()); }
@Test public void jdbcCompliant() throws SQLException { final AtomicBoolean called = new AtomicBoolean(false); Driver driver = new MockDriver() { @Override public boolean jdbcCompliant() { called.set(true); return false; } }; HiveDriver d = new HiveDriver(getMockUtil(getMockShimWithDriver(driver))); d.jdbcCompliant(); assertTrue(called.get()); }
@Test public void getMinorVersion() throws SQLException { final AtomicBoolean called = new AtomicBoolean(false); Driver driver = new MockDriver() { @Override public int getMinorVersion() { called.set(true); return 0; } }; HiveDriver d = new HiveDriver(getMockUtil(getMockShimWithDriver(driver))); d.getMinorVersion(); assertTrue(called.get()); }
@Test public void acceptsURL() throws SQLException { final AtomicBoolean called = new AtomicBoolean(false); Driver driver = new MockDriver() { @Override public boolean acceptsURL(String url) throws SQLException { called.set(true); return false; } }; HiveDriver d = new HiveDriver(getMockUtil(getMockShimWithDriver(driver))); d.acceptsURL(null); assertTrue(called.get()); }
@Test public void connect() throws SQLException { final AtomicBoolean called = new AtomicBoolean(false); Driver driver = new MockDriver() { @Override public Connection connect(String url, Properties info) throws SQLException { called.set(true); return null; } }; HiveDriver d = new HiveDriver(getMockUtil(getMockShimWithDriver(driver))); d.connect(null, null); assertTrue(called.get()); }
@Test public void getActiveDriver() throws SQLException { final AtomicBoolean called = new AtomicBoolean(false); HadoopShim shim = new MockHadoopShim() { public java.sql.Driver getJdbcDriver(String scheme) { if (scheme.equalsIgnoreCase("hive")) { called.set(true); return new MockDriver(); } else return null; } }; HiveDriver d = new HiveDriver(getMockUtil(shim)); d.getActiveDriver(); assertTrue("Shim's getJdbcDriver(\"hive\") not called", called.get()); }
@Test public void getActiveDriver_null_driver() { HadoopShim shim = new MockHadoopShim() { public java.sql.Driver getJdbcDriver(String scheme) { return null; } }; HiveDriver d = new HiveDriver(getMockUtil(shim)); try { d.getActiveDriver(); fail("Expected exception"); } catch (SQLException ex) { assertNull(ex.getCause()); assertEquals( "The active Hadoop configuration does not contain a Hive JDBC driver", ex.getMessage()); } }
@Test public void getActiveDriver_same_driver() { HadoopShim shim = new MockHadoopShim() { public java.sql.Driver getJdbcDriver(String scheme) { // Return another shim driver. This should fail when called since the // classes are the same return (scheme.equalsIgnoreCase("hive")) ? new HiveDriver() : null; } }; HiveDriver d = new HiveDriver(getMockUtil(shim)); try { d.getActiveDriver(); fail("Expected exception"); } catch (SQLException ex) { assertNull(ex.getCause()); assertEquals( "The active Hadoop configuration does not contain a Hive JDBC driver", ex.getMessage()); } }
@Test public void getActiveDriver_exception_in_getJdbcDriver() { HadoopShim shim = new MockHadoopShim() { public java.sql.Driver getJdbcDriver(String scheme) { if (scheme.equalsIgnoreCase("hive")) { throw new RuntimeException(); } else { return null; } } }; HiveDriver d = new HiveDriver(getMockUtil(shim)); try { d.getActiveDriver(); fail("Expected exception"); } catch (SQLException ex) { assertEquals(InvocationTargetException.class, ex.getCause().getClass()); assertEquals( "Unable to load Hive JDBC driver for the currently active Hadoop configuration", ex.getMessage()); } }