📄 datasourceregressiontest.java
字号:
assertTrue("Datasource not bound", boundDs != null); Connection dsCon = null; Statement dsStmt = null; try { dsCon = boundDs.getPooledConnection().getConnection(); dsStmt = dsCon.createStatement(); dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920"); dsStmt .executeUpdate("CREATE TABLE testBug3920 (field1 varchar(32))"); assertTrue( "Connection can not be obtained from data source", dsCon != null); } finally { dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920"); dsStmt.close(); dsCon.close(); } } finally { if (boundDs != null) { this.ctx.unbind(jndiName); } } } } /** * Tests fix for BUG#19169 - ConnectionProperties (and thus some * subclasses) are not serializable, even though some J2EE containers * expect them to be. * * @throws Exception if the test fails. */ public void testBug19169() throws Exception { MysqlDataSource toSerialize = new MysqlDataSource(); toSerialize.setZeroDateTimeBehavior("convertToNull"); boolean testBooleanFlag = !toSerialize.getAllowLoadLocalInfile(); toSerialize.setAllowLoadLocalInfile(testBooleanFlag); int testIntFlag = toSerialize.getBlobSendChunkSize() + 1; toSerialize.setBlobSendChunkSize(String.valueOf(testIntFlag)); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ObjectOutputStream objOut = new ObjectOutputStream(bOut); objOut.writeObject(toSerialize); objOut.flush(); ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray())); MysqlDataSource thawedDs = (MysqlDataSource)objIn.readObject(); assertEquals("convertToNull", thawedDs.getZeroDateTimeBehavior()); assertEquals(testBooleanFlag, thawedDs.getAllowLoadLocalInfile()); assertEquals(testIntFlag, thawedDs.getBlobSendChunkSize()); } /** * Tests fix for BUG#20242 - MysqlValidConnectionChecker for JBoss doesn't * work with MySQLXADataSources. * * @throws Exception if the test fails. */ public void testBug20242() throws Exception { if (versionMeetsMinimum(5, 0)) { try { Class.forName("org.jboss.resource.adapter.jdbc.ValidConnectionChecker"); } catch (Exception ex) { return; // class not available for testing } MysqlXADataSource xaDs = new MysqlXADataSource(); xaDs.setUrl(dbUrl); MysqlValidConnectionChecker checker = new MysqlValidConnectionChecker(); assertNull(checker.isValidConnection(xaDs.getXAConnection().getConnection())); } } private void bindDataSource(String name, DataSource ds) throws Exception { this.ctx.bind(this.tempDir.getAbsolutePath() + name, ds); } /** * This method is separated from the rest of the example since you normally * would NOT register a JDBC driver in your code. It would likely be * configered into your naming and directory service using some GUI. * * @throws Exception * if an error occurs */ private void createJNDIContext() throws Exception { this.tempDir = File.createTempFile("jnditest", null); this.tempDir.delete(); this.tempDir.mkdir(); this.tempDir.deleteOnExit(); MysqlConnectionPoolDataSource ds; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); this.ctx = new InitialContext(env); assertTrue("Naming Context not created", this.ctx != null); ds = new MysqlConnectionPoolDataSource(); ds.setUrl(dbUrl); // from BaseTestCase ds.setDatabaseName("test"); this.ctx.bind(this.tempDir.getAbsolutePath() + "/test", ds); } private DataSource lookupDatasourceInJNDI(String jndiName) throws Exception { NameParser nameParser = this.ctx.getNameParser(""); Name datasourceName = nameParser.parse(this.tempDir.getAbsolutePath() + jndiName); Object obj = this.ctx.lookup(datasourceName); DataSource boundDs = null; if (obj instanceof DataSource) { boundDs = (DataSource) obj; } else if (obj instanceof Reference) { // // For some reason, this comes back as a Reference // instance under CruiseControl !? // Reference objAsRef = (Reference) obj; ObjectFactory factory = (ObjectFactory) Class.forName( objAsRef.getFactoryClassName()).newInstance(); boundDs = (DataSource) factory.getObjectInstance(objAsRef, datasourceName, this.ctx, new Hashtable()); } return boundDs; } public void testCSC4616() throws Exception { MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); ds.setURL(BaseTestCase.dbUrl); PooledConnection pooledConn = ds.getPooledConnection(); Connection physConn = pooledConn.getConnection(); Statement physStatement = physConn.createStatement(); Method enableStreamingResultsMethodStmt = Class.forName( "com.mysql.jdbc.jdbc2.optional.StatementWrapper").getMethod( "enableStreamingResults", new Class[0]); enableStreamingResultsMethodStmt.invoke(physStatement, new Class[0]); this.rs = physStatement.executeQuery("SELECT 1"); try { physConn.createStatement().executeQuery("SELECT 2"); fail("Should have caught a streaming exception here"); } catch (SQLException sqlEx) { assertTrue(sqlEx.getMessage() != null && sqlEx.getMessage().indexOf("Streaming") != -1); } finally { if (this.rs != null) { this.rs.close(); this.rs = null; } } PreparedStatement physPrepStmt = physConn.prepareStatement("SELECT 1"); Method enableStreamingResultsMethodPstmt = Class.forName( "com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper") .getMethod("enableStreamingResults", new Class[0]); enableStreamingResultsMethodPstmt.invoke(physPrepStmt, new Class[0]); this.rs = physPrepStmt.executeQuery(); try { physConn.createStatement().executeQuery("SELECT 2"); fail("Should have caught a streaming exception here"); } catch (SQLException sqlEx) { assertTrue(sqlEx.getMessage() != null && sqlEx.getMessage().indexOf("Streaming") != -1); } finally { if (this.rs != null) { this.rs.close(); this.rs = null; } } } /** * Tests fix for BUG#16791 - NullPointerException in MysqlDataSourceFactory * due to Reference containing RefAddrs with null content. * * @throws Exception if the test fails */ public void testBug16791() throws Exception { MysqlDataSource myDs = new MysqlDataSource(); myDs.setUrl(dbUrl); Reference asRef = myDs.getReference(); System.out.println(asRef); removeFromRef(asRef, "port"); removeFromRef(asRef, NonRegisteringDriver.USER_PROPERTY_KEY); removeFromRef(asRef, NonRegisteringDriver.PASSWORD_PROPERTY_KEY); removeFromRef(asRef, "serverName"); removeFromRef(asRef, "databaseName"); MysqlDataSource newDs = (MysqlDataSource)new MysqlDataSourceFactory().getObjectInstance(asRef, null, null, null); } private void removeFromRef(Reference ref, String key) { int size = ref.size(); for (int i = 0; i < size; i++) { RefAddr refAddr = ref.get(i); if (refAddr.getType().equals(key)) { ref.remove(i); break; } } } /** * Tests fix for BUG#32101 - When using a connection from our ConnectionPoolDataSource, * some Connection.prepareStatement() methods would return null instead of * a prepared statement. * * @throws Exception */ public void testBug32101() throws Exception { MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); ds.setURL(BaseTestCase.dbUrl); PooledConnection pc = ds.getPooledConnection(); assertNotNull(pc.getConnection().prepareStatement("SELECT 1")); assertNotNull(pc.getConnection().prepareStatement("SELECT 1", Statement.RETURN_GENERATED_KEYS)); assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new int[0])); assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new String[0])); assertNotNull(pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); assertNotNull(pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)); } public void testBug35810() throws Exception { int defaultConnectTimeout = ((ConnectionProperties) this.conn).getConnectTimeout(); int nonDefaultConnectTimeout = defaultConnectTimeout + 1000 * 2; MysqlConnectionPoolDataSource cpds = new MysqlConnectionPoolDataSource(); String dsUrl = BaseTestCase.dbUrl; if (dsUrl.indexOf("?") == -1) { dsUrl += "?"; } else { dsUrl += "&"; } dsUrl += "connectTimeout=" + nonDefaultConnectTimeout; cpds.setUrl(dsUrl); Connection dsConn = cpds.getPooledConnection().getConnection(); int configuredConnectTimeout = ((ConnectionProperties) dsConn).getConnectTimeout(); assertEquals("Connect timeout spec'd by URL didn't take", nonDefaultConnectTimeout, configuredConnectTimeout); assertFalse("Connect timeout spec'd by URL didn't take", defaultConnectTimeout == configuredConnectTimeout); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -