-
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
Connection test #95
Changes from 12 commits
e6cdc81
36045dc
5e8f5f9
83182ce
6ebbb71
45e32da
a8515a0
87c21a6
8b3d7c7
c58bcba
f0ee8d1
625d21f
e0a6b36
79bc298
896c1e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
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(true == ds.getLastUpdateCount()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No need to compare There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
@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(); | ||
try { | ||
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"); | ||
} catch (UnsupportedOperationException e) { | ||
assertEquals("This operation is not supported.", e.getMessage()); | ||
} | ||
|
||
SQLServerConnectionPoolDataSource poolDS = new SQLServerConnectionPoolDataSource(); | ||
try { | ||
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 ids = (ISQLServerDataSource) (poolDS.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
ids.setApplicationName("AppName"); | ||
} catch (UnsupportedOperationException e) { | ||
assertEquals("This operation is not supported.", e.getMessage()); | ||
} | ||
|
||
SQLServerXADataSource xaDS = new SQLServerXADataSource(); | ||
try { | ||
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 ids = (ISQLServerDataSource) (xaDS.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource"))); | ||
ids.setApplicationName("AppName"); | ||
} catch (UnsupportedOperationException e) { | ||
assertEquals("This operation is not supported.", e.getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some java docs related to which Unsupported Operation you are expecting in this unit test case? |
||
} | ||
} | ||
|
||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.microsoft.sqlserver.jdbc.connection; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
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 { | ||
if (!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use No need to use This will give us indications about how many test case gets skipped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
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. | ||
assertEquals(true, 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. | ||
assertEquals(true, 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."); | ||
} | ||
} |
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.