SQL: Fix test when random JDK TZ doesn't exist in Joda (elastic/x-pack-elasticsearch#2903)

JodaTime timezone db can be out of date compared to that of the JDK which causes the JDBC Connection to fail when the randomized tests pick a timezone that's available in the JDK but not in Joda, like SystemV/PST8. This is happening because JdbcConnection configuration is using system default timezone and tries to pass it to Elasticsearch that is using joda. This commit, explicitly sets the time zone on JdbcConnection to a time zone randomly selected from a list of timezones that are known to both JDK and Joda.

relates elastic/x-pack-elasticsearch#2812

Original commit: elastic/x-pack-elasticsearch@b02e9794a8
This commit is contained in:
Igor Motov 2017-11-10 14:16:11 -05:00 committed by GitHub
parent b2285ae66e
commit 3c444c4719
2 changed files with 26 additions and 2 deletions

View File

@ -16,13 +16,22 @@ import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.qa.sql.embed.EmbeddedJdbcServer;
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConfiguration;
import org.joda.time.DateTimeZone;
import org.junit.ClassRule;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import static java.util.Collections.singletonMap;
@ -85,6 +94,21 @@ public abstract class JdbcIntegrationTestCase extends ESRestTestCase {
* The properties used to build the connection.
*/
protected Properties connectionProperties() {
return new Properties();
Properties connectionProperties = new Properties();
connectionProperties.put(JdbcConfiguration.TIME_ZONE, randomKnownTimeZone());
return connectionProperties;
}
public static String randomKnownTimeZone() {
// We use system default timezone for the connection that is selected randomly by TestRuleSetupAndRestoreClassEnv
// from all available JDK timezones. While Joda and JDK are generally in sync, some timezones might not be known
// to the current version of Joda and in this case the test might fail. To avoid that, we specify a timezone
// known for both Joda and JDK
Set<String> timeZones = new HashSet<>(DateTimeZone.getAvailableIDs());
timeZones.retainAll(Arrays.asList(TimeZone.getAvailableIDs()));
List<String> ids = new ArrayList<>(timeZones);
Collections.sort(ids);
return randomFrom(ids);
}
}

View File

@ -43,7 +43,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
// can be out/err/url
static final String DEBUG_OUTPUT_DEFAULT = "err";
static final String TIME_ZONE = "timezone";
public static final String TIME_ZONE = "timezone";
// follow the JDBC spec and use the JVM default...
// to avoid inconsistency, the default is picked up once at startup and reused across connections
// to cater to the principle of least surprise