Skip to content
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 15 commits into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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'))"
Copy link
Contributor

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.

+ "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,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 src/test/java/com/microsoft/sqlserver/jdbc/connection/PoolingTest.java
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.");
}
}
Loading