Skip to content

Commit

Permalink
added support for config.properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed Nov 22, 2019
1 parent 549c6ac commit cdaa085
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 43 deletions.
33 changes: 0 additions & 33 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,39 +119,6 @@ public static boolean isAEv2(Connection con) {
return ((SQLServerConnection) con).isAEv2();
}


/**
* Read variable from property files if found null try to read from env.
*
* @param key
* @return Value
*/
public static String getConfiguredProperty(String key) {
String value = System.getProperty(key);

if (value == null) {
value = System.getenv(key);
}

return value;
}

/**
* Convenient method for {@link #getConfiguredProperty(String)}
*
* @param key
* @return Value
*/
public static String getConfiguredProperty(String key, String defaultValue) {
String value = getConfiguredProperty(key);

if (value == null) {
value = defaultValue;
}

return value;
}

/**
*
* @param javatype
Expand Down
101 changes: 91 additions & 10 deletions src/test/java/com/microsoft/sqlserver/testframework/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
package com.microsoft.sqlserver.testframework;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
Expand Down Expand Up @@ -79,6 +83,7 @@ public abstract class AbstractTest {
*/
private static ByteArrayOutputStream logOutputStream = null;
private static PrintStream logPrintStream = null;
private static Properties configProperties = null;

/**
* This will take care of all initialization before running the Test Suite.
Expand All @@ -91,14 +96,22 @@ public static void setup() throws Exception {
// Invoke fine logging...
invokeLogging();

connectionString = TestUtils.getConfiguredProperty(Constants.MSSQL_JDBC_TEST_CONNECTION_PROPERTIES);
// get Properties from config file
try (InputStream input = new FileInputStream(Constants.CONFIG_PROPERTIES_FILE)) {
configProperties = new Properties();
configProperties.load(input);
} catch (FileNotFoundException | SecurityException e) {
// no config file used
}

connectionString = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_TEST_CONNECTION_PROPERTIES);
connectionStringNTLM = connectionString;

applicationClientID = System.getProperty("applicationClientID");
applicationKey = System.getProperty("applicationKey");
applicationClientID = getConfiguredProperty("applicationClientID");
applicationKey = getConfiguredProperty("applicationKey");
javaKeyPath = TestUtils.getCurrentClassPath() + Constants.JKS_NAME;
keyIDs = System.getProperty("keyID", "").split(Constants.SEMI_COLON);
windowsKeyPath = System.getProperty("windowsKeyPath");
keyIDs = getConfiguredProperty("keyID", "").split(Constants.SEMI_COLON);
windowsKeyPath = getConfiguredProperty("windowsKeyPath");

Map<String, SQLServerColumnEncryptionKeyStoreProvider> map = new HashMap<String, SQLServerColumnEncryptionKeyStoreProvider>();
if (null == jksProvider) {
Expand All @@ -118,9 +131,9 @@ public static void setup() throws Exception {
}

// if these properties are defined then NTLM is desired, modify connection string accordingly
String domain = System.getProperty("domainNTLM");
String user = System.getProperty("userNTLM");
String password = System.getProperty("passwordNTLM");
String domain = getConfiguredProperty("domainNTLM");
String user = getConfiguredProperty("userNTLM");
String password = getConfiguredProperty("passwordNTLM");

if (null != domain) {
connectionStringNTLM = TestUtils.addOrOverrideProperty(connectionStringNTLM, "domain", domain);
Expand Down Expand Up @@ -309,14 +322,14 @@ public static void invokeLogging() {
Handler handler = null;

// enable logging to stream by default for tests
String enableLogging = TestUtils.getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING, Boolean.TRUE.toString());
String enableLogging = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_LOGGING, Boolean.TRUE.toString());

// If logging is not enable then return.
if (!Boolean.TRUE.toString().equalsIgnoreCase(enableLogging)) {
return;
}

String loggingHandler = TestUtils.getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING_HANDLER,
String loggingHandler = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_LOGGING_HANDLER,
Constants.LOGGING_HANDLER_STREAM);

try {
Expand Down Expand Up @@ -390,4 +403,72 @@ private static void isSqlAzureOrAzureDW(Connection con) throws SQLException {
determinedSqlAzureOrSqlServer = true;
}
}

/**
* Read property from system or config properties file
*
* @param key
* @return property value
*/
private static String getConfiguredProperty(String key) {
String value = System.getProperty(key);

if (null == value && null != configProperties) {
return configProperties.getProperty(key);
}

return value;
}

/**
* Read property from system or config properties file or read from env var
*
* @param key
* @return property value
*/
private static String getConfiguredPropertyOrEnv(String key) {
String value = getConfiguredProperty(key);

if (null == value) {
return System.getenv(key);
}

return value;
}

/**
* Read property from system or config properties file if not set return default value
*
* @param key
* @return property value or default value
*/
private static String getConfiguredProperty(String key, String defaultValue) {
String value = getConfiguredProperty(key);

if (null == value) {
return defaultValue;
}

return value;
}

/**
* Read property from system or config properties file or env var if not set return default value
*
* @param key
* @return property value or default value
*/
private static String getConfiguredPropertyOrEnv(String key, String defaultValue) {
String value = getConfiguredProperty(key);

if (null == value) {
value = System.getenv(key);
}

if (null == value) {
value = defaultValue;
}

return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ private Constants() {}
public static final String ENCLAVE_ATTESTATIONURL = "enclaveAttestationUrl";
public static final String ENCLAVE_ATTESTATIONPROTOCOL = "enclaveAttestationProtocol";

public static final String CONFIG_PROPERTIES_FILE = "config.properties";

public enum LOB {
CLOB,
NCLOB,
Expand Down

0 comments on commit cdaa085

Please sign in to comment.