private void convertTypeTest(String convertType, Class resultClass) throws Throwable { JdbcDataSource dataSource = new JdbcDataSource(); Properties p = new Properties(); p.put("driver", "org.apache.derby.jdbc.EmbeddedDriver"); p.put("url", "jdbc:derby:memory:tempDB;create=true;territory=en_US"); p.put("convertType", convertType); List<Map<String, String>> flds = new ArrayList<>(); Map<String, String> f = new HashMap<>(); f.put("column", "some_i"); f.put("type", "long"); flds.add(f); Context c = getContext(null, null, dataSource, Context.FULL_DUMP, flds, null); dataSource.init(c, p); Iterator<Map<String, Object>> i = dataSource.getData( "select 1 as id, CAST(9999 AS DECIMAL) as \"some_i\" from sysibm.sysdummy1"); assertTrue(i.hasNext()); Map<String, Object> map = i.next(); Object val = map.get("some_i"); assertEquals(resultClass, val.getClass()); dataSource.close(); }
@Test @Ignore("Needs a Mock database server to work") public void testBasic() throws Exception { JdbcDataSource dataSource = new JdbcDataSource(); Properties p = new Properties(); p.put("driver", "com.mysql.jdbc.Driver"); p.put("url", "jdbc:mysql://localhost/autos"); p.put("user", "root"); p.put("password", ""); List<Map<String, String>> flds = new ArrayList<Map<String, String>>(); Map<String, String> f = new HashMap<String, String>(); f.put("column", "trim_id"); f.put("type", "long"); flds.add(f); f = new HashMap<String, String>(); f.put("column", "msrp"); f.put("type", "float"); flds.add(f); Context c = getContext(null, null, dataSource, Context.FULL_DUMP, flds, null); dataSource.init(c, p); Iterator<Map<String, Object>> i = dataSource.getData( "select make,model,year,msrp,trim_id from atrimlisting where make='Acura'"); int count = 0; Object msrp = null; Object trim_id = null; while (i.hasNext()) { Map<String, Object> map = i.next(); msrp = map.get("msrp"); trim_id = map.get("trim_id"); count++; } assertEquals(5, count); assertEquals(Float.class, msrp.getClass()); assertEquals(Long.class, trim_id.getClass()); }
@Test public void testRetrieveFromJndi() throws Exception { MockInitialContextFactory.bind("java:comp/env/jdbc/JndiDB", dataSource); props.put(JdbcDataSource.JNDI_NAME, "java:comp/env/jdbc/JndiDB"); EasyMock.expect(dataSource.getConnection()).andReturn(connection); connection.setAutoCommit(false); // connection.setHoldability(1); mockControl.replay(); Connection conn = jdbcDataSource.createConnectionFactory(context, props).call(); mockControl.verify(); assertSame("connection", conn, connection); }
@Test public void testRetrieveFromDriverManager() throws Exception { DriverManager.registerDriver(driver); EasyMock.expect(driver.connect((String) EasyMock.notNull(), (Properties) EasyMock.notNull())) .andReturn(connection); connection.setAutoCommit(false); connection.setHoldability(1); props.put(JdbcDataSource.DRIVER, driver.getClass().getName()); props.put(JdbcDataSource.URL, "jdbc:fakedb"); props.put("holdability", "HOLD_CURSORS_OVER_COMMIT"); mockControl.replay(); Connection conn = jdbcDataSource.createConnectionFactory(context, props).call(); mockControl.verify(); assertSame("connection", conn, connection); }
@Test public void testRetrieveFromJndiWithCredentials() throws Exception { MockInitialContextFactory.bind("java:comp/env/jdbc/JndiDB", dataSource); props.put(JdbcDataSource.JNDI_NAME, "java:comp/env/jdbc/JndiDB"); props.put("user", "Fred"); props.put("password", "4r3d"); props.put("holdability", "HOLD_CURSORS_OVER_COMMIT"); EasyMock.expect(dataSource.getConnection("Fred", "4r3d")).andReturn(connection); connection.setAutoCommit(false); connection.setHoldability(1); mockControl.replay(); Connection conn = jdbcDataSource.createConnectionFactory(context, props).call(); mockControl.verify(); assertSame("connection", conn, connection); }