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

Tests | Add logging stream handler for tests #1027

Merged
merged 11 commits into from
May 3, 2019
1 change: 0 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* Various driver utilities.
*
*/

final class Util {
final static String SYSTEM_SPEC_VERSION = System.getProperty("java.specification.version");
final static char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
Expand Down
33 changes: 26 additions & 7 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.sql.CallableStatement;
import java.sql.Connection;
Expand All @@ -21,6 +23,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.logging.LogManager;

import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.Constants;
Expand Down Expand Up @@ -71,14 +74,15 @@ public class TestUtils {
private static Boolean isAzureDW = null;
private static Boolean isAzureMI = null;

/*
* SERVERPROPERTY('EngineEdition') can be used to determine whether the db server is SQL Azure.
* It should return 6 for SQL Azure DW. This is more reliable than @@version or
* serverproperty('edition').
* Reference: http://msdn.microsoft.com/en-us/library/ee336261.aspx
/**
* SERVERPROPERTY('EngineEdition') can be used to determine whether the db server is SQL Azure. It should return 6
* for SQL Azure DW. This is more reliable than @@version or serverproperty('edition'). Reference:
* http://msdn.microsoft.com/en-us/library/ee336261.aspx
*
* SERVERPROPERTY('EngineEdition') means Database Engine edition of the instance of SQL Server installed on the
* server.
*
* SERVERPROPERTY('EngineEdition') means
* Database Engine edition of the instance of SQL Server installed on the server.
* <pre>
lilgreenbird marked this conversation as resolved.
Show resolved Hide resolved
* 1 = Personal or Desktop Engine (Not available for SQL Server.)
* 2 = Standard (This is returned for Standard and Workgroup.)
* 3 = Enterprise (This is returned for Enterprise, Enterprise Evaluation, and Developer.)
Expand All @@ -87,6 +91,7 @@ public class TestUtils {
* 6 = SQL Azure DW
* 8 = Managed Instance
* Base data type: int
* </pre>
*/
public static boolean isAzure(Connection con) {
if (null == isAzure) {
Expand Down Expand Up @@ -701,4 +706,18 @@ public static boolean supportJDBC43(Connection con) throws SQLException {
public static String escapeSingleQuotes(String name) {
return name.replace("'", "''");
}

/**
* Enables Activity Tracing
*
* @throws IOException
* @throws SecurityException
*/
public static void setActivityTraceOn() throws SecurityException, IOException {
String activityIDTraceOn = Util.ACTIVITY_ID_TRACE_PROPERTY + "=on";
try (InputStream is = new ByteArrayInputStream(activityIDTraceOn.getBytes());) {
LogManager lm = LogManager.getLogManager();
lm.readConfiguration(is);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.microsoft.sqlserver.testframework;

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -15,6 +16,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -253,23 +255,32 @@ public static void invokeLogging() {
try {
if (Constants.LOGGING_HANDLER_CONSOLE.equalsIgnoreCase(loggingHandler)) {
handler = new ConsoleHandler();
handler.setFormatter(new SimpleFormatter());
} else if (Constants.LOGGING_HANDLER_FILE.equalsIgnoreCase(loggingHandler)) {
handler = new FileHandler(Constants.DEFAULT_DRIVER_LOG);
handler.setFormatter(new SimpleFormatter());
System.out.println("Look for Driver.log file in your classpath for detail logs");
} else if (Constants.LOGGING_HANDLER_STREAM.equalsIgnoreCase(loggingHandler)) {
handler = new StreamHandler(new PrintStream(Constants.LOGGING_STREAM), new SimpleFormatter());
lilgreenbird marked this conversation as resolved.
Show resolved Hide resolved
}

if (handler != null) {
handler.setFormatter(new SimpleFormatter());
handler.setLevel(Level.FINEST);
Logger.getLogger(Constants.MSSQL_JDBC_LOGGING_HANDLER).addHandler(handler);
}
// By default, Loggers also send their output to their parent logger.
// Typically the root Logger is configured with a set of Handlers that essentially act as default handlers
// for all loggers.

/*
* By default, Loggers also send their output to their parent logger. Typically the root Logger is
* configured with a set of Handlers that essentially act as default handlers for all loggers.
*/
Logger logger = Logger.getLogger(Constants.MSSQL_JDBC_PACKAGE);
logger.setLevel(Level.FINEST);

// enable activity trace
TestUtils.setActivityTraceOn();
lilgreenbird marked this conversation as resolved.
Show resolved Hide resolved

} catch (Exception e) {
System.err.println("Some how could not invoke logging: " + e.getMessage());
System.err.println("Could not invoke logging: " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.microsoft.sqlserver.testframework;

import java.io.ByteArrayOutputStream;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.UUID;
Expand Down Expand Up @@ -63,6 +64,8 @@ public class Constants {

public static final String LOGGING_HANDLER_FILE = "file";
public static final String LOGGING_HANDLER_CONSOLE = "console";
public static final String LOGGING_HANDLER_STREAM = "stream";
public static final ByteArrayOutputStream LOGGING_STREAM = new ByteArrayOutputStream();

public final static int ENGINE_EDITION_FOR_SQL_AZURE = 5;
public final static int ENGINE_EDITION_FOR_SQL_AZURE_DW = 6;
Expand Down