-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connection test #95
Merged
Merged
Connection test #95
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e6cdc81
datasource test
xiangyushawn 36045dc
added 2 more connection test cases
xiangyushawn 5e8f5f9
added WarningTest for connection test
xiangyushawn 83182ce
fix exceptions
xiangyushawn 6ebbb71
added ConnectionDriverTest
xiangyushawn 45e32da
added more test cases to ConnectionDriverTest test
xiangyushawn a8515a0
added Timeout test case to connection test
xiangyushawn 87c21a6
added more connection test cases
xiangyushawn 8b3d7c7
detect if server is azure or not
xiangyushawn c58bcba
all connection test cases
xiangyushawn f0ee8d1
clean up and fix failures for Azure SQL
xiangyushawn 625d21f
clean up test framework
xiangyushawn e0a6b36
changes based on code reviews
xiangyushawn 79bc298
use assumeTrue to skip test cases
xiangyushawn 896c1e4
added javadoc for tests that do not have a descriptive name
xiangyushawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
506 changes: 506 additions & 0 deletions
506
src/test/java/com/microsoft/sqlserver/jdbc/connection/ConnectionDriverTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/test/java/com/microsoft/sqlserver/jdbc/connection/DBMetadataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.microsoft.sqlserver.jdbc.connection; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.microsoft.sqlserver.jdbc.SQLServerDataSource; | ||
import com.microsoft.sqlserver.jdbc.SQLServerException; | ||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
import com.microsoft.sqlserver.testframework.DBTable; | ||
import com.microsoft.sqlserver.testframework.util.RandomUtil; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class DBMetadataTest extends AbstractTest { | ||
@Test | ||
public void testDatabaseMetaData() throws SQLException { | ||
String functionName = RandomUtil.getIdentifier("proc"); | ||
functionName = DBTable.escapeIdentifier(functionName); | ||
|
||
SQLServerDataSource ds = new SQLServerDataSource(); | ||
ds.setURL(connectionString); | ||
|
||
Connection con = ds.getConnection(); | ||
|
||
//drop function | ||
String sqlDropFunction = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo]." + functionName + "')" + "and xtype in (N'FN', N'IF', N'TF'))" | ||
+ "drop function " + functionName; | ||
con.createStatement().execute(sqlDropFunction); | ||
|
||
//create function | ||
String sqlCreateFunction = "CREATE FUNCTION " + functionName + " (@text varchar(8000), @delimiter varchar(20) = ' ') RETURNS @Strings TABLE " | ||
+ "(position int IDENTITY PRIMARY KEY, value varchar(8000)) AS BEGIN INSERT INTO @Strings VALUES ('DDD') RETURN END "; | ||
con.createStatement().execute(sqlCreateFunction); | ||
|
||
DatabaseMetaData md = con.getMetaData(); | ||
ResultSet arguments = md.getProcedureColumns(null, null, null, "@TABLE_RETURN_VALUE"); | ||
|
||
if (arguments.next()) { | ||
arguments.getString("COLUMN_NAME"); | ||
arguments.getString("DATA_TYPE"); // call this function to make sure it does not crash | ||
} | ||
|
||
con.createStatement().execute(sqlDropFunction); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/test/java/com/microsoft/sqlserver/jdbc/connection/NativeMSSQLDataSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.microsoft.sqlserver.jdbc.connection; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutput; | ||
import java.io.ObjectOutputStream; | ||
import java.io.PrintWriter; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.microsoft.sqlserver.jdbc.ISQLServerDataSource; | ||
import com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource; | ||
import com.microsoft.sqlserver.jdbc.SQLServerDataSource; | ||
import com.microsoft.sqlserver.jdbc.SQLServerException; | ||
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource; | ||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class NativeMSSQLDataSourceTest extends AbstractTest { | ||
|
||
@Test | ||
public void testNativeMSSQLDataSource() throws SQLException { | ||
SQLServerXADataSource ds = new SQLServerXADataSource(); | ||
ds.setLastUpdateCount(true); | ||
assertTrue(ds.getLastUpdateCount()); | ||
} | ||
|
||
@Test | ||
public void testSerialization() throws IOException { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
ObjectOutput objectOutput = new ObjectOutputStream(outputStream); | ||
|
||
SQLServerDataSource ds = new SQLServerDataSource(); | ||
ds.setLogWriter(new PrintWriter(new ByteArrayOutputStream())); | ||
|
||
objectOutput.writeObject(ds); | ||
objectOutput.flush(); | ||
} | ||
|
||
@Test | ||
public void testDSNormal() throws SQLServerException, ClassNotFoundException, IOException { | ||
SQLServerDataSource ds = new SQLServerDataSource(); | ||
ds.setURL(connectionString); | ||
Connection conn = ds.getConnection(); | ||
ds = testSerial(ds); | ||
conn = ds.getConnection(); | ||
} | ||
|
||
@Test | ||
public void testDSTSPassword() throws SQLServerException, ClassNotFoundException, IOException { | ||
SQLServerDataSource ds = new SQLServerDataSource(); | ||
System.setProperty("java.net.preferIPv6Addresses", "true"); | ||
ds.setURL(connectionString); | ||
ds.setTrustStorePassword("wrong_password"); | ||
Connection conn = ds.getConnection(); | ||
ds = testSerial(ds); | ||
try { | ||
conn = ds.getConnection(); | ||
} catch (SQLServerException e) { | ||
assertEquals("The DataSource trustStore password needs to be set.", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testInterfaceWrapping() throws ClassNotFoundException, SQLException { | ||
SQLServerDataSource ds = new SQLServerDataSource(); | ||
assertEquals(true, ds.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
assertEquals(true, ds.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDataSource"))); | ||
assertEquals(true, ds.isWrapperFor(Class.forName("javax.sql.CommonDataSource"))); | ||
ISQLServerDataSource ids = (ISQLServerDataSource) (ds.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
ids.setApplicationName("AppName"); | ||
|
||
SQLServerConnectionPoolDataSource poolDS = new SQLServerConnectionPoolDataSource(); | ||
assertEquals(true, poolDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
assertEquals(true, poolDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDataSource"))); | ||
assertEquals(true, poolDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource"))); | ||
assertEquals(true, poolDS.isWrapperFor(Class.forName("javax.sql.CommonDataSource"))); | ||
ISQLServerDataSource ids2 = (ISQLServerDataSource) (poolDS.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
ids2.setApplicationName("AppName"); | ||
|
||
SQLServerXADataSource xaDS = new SQLServerXADataSource(); | ||
assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDataSource"))); | ||
assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource"))); | ||
assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerXADataSource"))); | ||
assertEquals(true, xaDS.isWrapperFor(Class.forName("javax.sql.CommonDataSource"))); | ||
ISQLServerDataSource ids3 = (ISQLServerDataSource) (xaDS.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
ids3.setApplicationName("AppName"); | ||
} | ||
|
||
private SQLServerDataSource testSerial(SQLServerDataSource ds) throws IOException, ClassNotFoundException { | ||
java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream(); | ||
java.io.ObjectOutput objectOutput = new java.io.ObjectOutputStream(outputStream); | ||
objectOutput.writeObject(ds); | ||
objectOutput.flush(); | ||
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray())); | ||
SQLServerDataSource dtn; | ||
dtn = (SQLServerDataSource) in.readObject(); | ||
return dtn; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/test/java/com/microsoft/sqlserver/jdbc/connection/PoolingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.microsoft.sqlserver.jdbc.connection; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.UUID; | ||
|
||
import javax.sql.PooledConnection; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.microsoft.sqlserver.jdbc.ISQLServerConnection; | ||
import com.microsoft.sqlserver.jdbc.SQLServerException; | ||
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource; | ||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
import com.microsoft.sqlserver.testframework.DBConnection; | ||
import com.microsoft.sqlserver.testframework.DBTable; | ||
import com.microsoft.sqlserver.testframework.util.RandomUtil; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class PoolingTest extends AbstractTest { | ||
@Test | ||
public void testPooling() throws SQLException { | ||
assumeTrue(!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString)), "Skipping test case on Azure SQL."); | ||
|
||
String randomTableName = RandomUtil.getIdentifier("table"); | ||
|
||
// make the table a temporary table (will be created in tempdb database) | ||
String tempTableName = "#" + randomTableName; | ||
|
||
SQLServerXADataSource XADataSource1 = new SQLServerXADataSource(); | ||
XADataSource1.setURL(connectionString); | ||
XADataSource1.setDatabaseName("tempdb"); | ||
|
||
PooledConnection pc = XADataSource1.getPooledConnection(); | ||
Connection conn = pc.getConnection(); | ||
|
||
// create table in tempdb database | ||
conn.createStatement().execute("create table [" + tempTableName + "] (myid int)"); | ||
conn.createStatement().execute("insert into [" + tempTableName + "] values (1)"); | ||
conn.close(); | ||
|
||
conn = pc.getConnection(); | ||
|
||
boolean tempTableFileRemoved = false; | ||
try { | ||
conn.createStatement().executeQuery("select * from [" + tempTableName + "]"); | ||
} | ||
catch (SQLServerException e) { | ||
// make sure the temporary table is not found. | ||
if (e.getMessage().startsWith("Invalid object name")) { | ||
tempTableFileRemoved = true; | ||
} | ||
} | ||
assertTrue(tempTableFileRemoved, "Temporary table is not removed."); | ||
} | ||
|
||
@Test | ||
public void testConnectionPoolReget() throws SQLException { | ||
SQLServerXADataSource ds = new SQLServerXADataSource(); | ||
ds.setURL(connectionString); | ||
|
||
PooledConnection pc = ds.getPooledConnection(); | ||
Connection con = pc.getConnection(); | ||
|
||
// now reget a connection | ||
Connection con2 = pc.getConnection(); | ||
|
||
// assert that the first connection is closed. | ||
assertTrue(con.isClosed(), "First connection is not closed"); | ||
} | ||
|
||
@Test | ||
public void testConnectionPoolConnFunctions() throws SQLException { | ||
String tableName = RandomUtil.getIdentifier("table"); | ||
tableName = DBTable.escapeIdentifier(tableName); | ||
|
||
String sql1 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n" + "drop table " + tableName + "\n" + "create table " + tableName | ||
+ "\n" + "(\n" + "wibble_id int primary key not null,\n" + "counter int null\n" + ");"; | ||
String sql2 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n" + "drop table " + tableName + "\n"; | ||
|
||
SQLServerXADataSource ds = new SQLServerXADataSource(); | ||
ds.setURL(connectionString); | ||
|
||
PooledConnection pc = ds.getPooledConnection(); | ||
Connection con = pc.getConnection(); | ||
|
||
Statement statement = con.createStatement(); | ||
statement.execute(sql1); | ||
statement.execute(sql2); | ||
con.clearWarnings(); | ||
pc.close(); | ||
} | ||
|
||
@Test | ||
public void testConnectionPoolClose() throws SQLException { | ||
SQLServerXADataSource ds = new SQLServerXADataSource(); | ||
ds.setURL(connectionString); | ||
|
||
PooledConnection pc = ds.getPooledConnection(); | ||
Connection con = pc.getConnection(); | ||
|
||
pc.close(); | ||
// assert that the first connection is closed. | ||
assertTrue(con.isClosed(), "Connection is not closed with pool close"); | ||
} | ||
|
||
@Test | ||
public void testConnectionPoolClientConnectionId() throws SQLException { | ||
SQLServerXADataSource ds = new SQLServerXADataSource(); | ||
ds.setURL(connectionString); | ||
|
||
PooledConnection pc = ds.getPooledConnection(); | ||
ISQLServerConnection con = (ISQLServerConnection) pc.getConnection(); | ||
|
||
UUID Id1 = con.getClientConnectionId(); | ||
assertTrue(Id1 != null, "Unexecepted: ClientConnectionId is null from Pool"); | ||
con.close(); | ||
|
||
// now reget the connection | ||
ISQLServerConnection con2 = (ISQLServerConnection) pc.getConnection(); | ||
|
||
UUID Id2 = con2.getClientConnectionId(); | ||
con2.close(); | ||
|
||
assertEquals(Id1, Id2, "ClientConnection Ids from pool are not the same."); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this sql query for Drop Function is generic and should be reusable.