Search in sources :

Example 26 with MysqlConnection

use of com.mysql.cj.MysqlConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionTest method testLocalInfileWithUrl.

/**
 * Tests functionality of using URLs in 'LOAD DATA LOCAL INFILE' statements.
 *
 * @throws Exception
 */
@Test
public void testLocalInfileWithUrl() throws Exception {
    assumeTrue(supportsLoadLocalInfile(this.stmt), "This test requires the server started with --local-infile=ON");
    File infile = File.createTempFile("foo", "txt");
    infile.deleteOnExit();
    String url = infile.toURI().toURL().toExternalForm();
    FileWriter output = new FileWriter(infile);
    output.write("Test");
    output.flush();
    output.close();
    createTable("testLocalInfileWithUrl", "(field1 LONGTEXT)");
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.allowLoadLocalInfile.getKeyName(), "true");
    props.setProperty(PropertyKey.allowUrlInLocalInfile.getKeyName(), "true");
    Connection loadConn = getConnectionWithProps(props);
    Statement loadStmt = loadConn.createStatement();
    String charset = " CHARACTER SET " + CharsetMappingWrapper.getStaticMysqlCharsetForJavaEncoding(((MysqlConnection) loadConn).getPropertySet().getStringProperty(PropertyKey.characterEncoding).getValue(), ((JdbcConnection) loadConn).getServerVersion());
    try {
        loadStmt.execute("LOAD DATA LOCAL INFILE '" + url + "' INTO TABLE testLocalInfileWithUrl" + charset);
    } catch (SQLException sqlEx) {
        sqlEx.printStackTrace();
        throw sqlEx;
    }
    this.rs = this.stmt.executeQuery("SELECT * FROM testLocalInfileWithUrl");
    assertTrue(this.rs.next());
    assertTrue("Test".equals(this.rs.getString(1)));
    int count = this.stmt.executeUpdate("DELETE FROM testLocalInfileWithUrl");
    assertTrue(count == 1);
    StringBuilder escapedPath = new StringBuilder();
    String path = infile.getCanonicalPath();
    for (int i = 0; i < path.length(); i++) {
        char c = path.charAt(i);
        if (c == '\\') {
            escapedPath.append('\\');
        }
        escapedPath.append(c);
    }
    loadStmt.execute("LOAD DATA LOCAL INFILE '" + escapedPath.toString() + "' INTO TABLE testLocalInfileWithUrl" + charset);
    this.rs = this.stmt.executeQuery("SELECT * FROM testLocalInfileWithUrl");
    assertTrue(this.rs.next());
    assertTrue("Test".equals(this.rs.getString(1)));
    try {
        loadStmt.execute("LOAD DATA LOCAL INFILE 'foo:///' INTO TABLE testLocalInfileWithUrl" + charset);
    } catch (SQLException sqlEx) {
        assertTrue(sqlEx.getMessage() != null);
        assertTrue(sqlEx.getMessage().indexOf("FileNotFoundException") != -1);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) Statement(java.sql.Statement) FileWriter(java.io.FileWriter) Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) MysqlConnection(com.mysql.cj.MysqlConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) Properties(java.util.Properties) File(java.io.File) Savepoint(java.sql.Savepoint) Test(org.junit.jupiter.api.Test)

Example 27 with MysqlConnection

use of com.mysql.cj.MysqlConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionTest method testNonVerifyServerCert.

@Test
public void testNonVerifyServerCert() throws Exception {
    assumeTrue((((MysqlConnection) this.conn).getSession().getServerSession().getCapabilities().getCapabilityFlags() & NativeServerSession.CLIENT_SSL) != 0, "This test requires server with SSL support.");
    assumeTrue(supportsTLSv1_2(((MysqlConnection) this.conn).getSession().getServerSession().getServerVersion()), "This test requires server with TLSv1.2+ support.");
    assumeTrue(supportsTestCertificates(this.stmt), "This test requires the server configured with SSL certificates from ConnectorJ/src/test/config/ssl-test-certs");
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.REQUIRED.name());
    getConnectionWithProps(props);
}
Also used : MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 28 with MysqlConnection

use of com.mysql.cj.MysqlConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionTest method testIsolationLevel.

/**
 * Tests isolation level functionality
 *
 * @throws Exception
 */
@Test
public void testIsolationLevel() throws Exception {
    // Check initial transaction isolation level
    ((MysqlConnection) this.conn).getPropertySet().getBooleanProperty(PropertyKey.useLocalSessionState).setValue(true);
    int initialTransactionIsolation = this.conn.getTransactionIsolation();
    ((MysqlConnection) this.conn).getPropertySet().getBooleanProperty(PropertyKey.useLocalSessionState).setValue(false);
    int actualTransactionIsolation = this.conn.getTransactionIsolation();
    assertEquals(actualTransactionIsolation, initialTransactionIsolation, "Inital transaction isolation level doesn't match the server's");
    // Check setting all allowed transaction isolation levels
    String[] isoLevelNames = new String[] { "Connection.TRANSACTION_NONE", "Connection.TRANSACTION_READ_COMMITTED", "Connection.TRANSACTION_READ_UNCOMMITTED", "Connection.TRANSACTION_REPEATABLE_READ", "Connection.TRANSACTION_SERIALIZABLE" };
    int[] isolationLevels = new int[] { Connection.TRANSACTION_NONE, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_REPEATABLE_READ, Connection.TRANSACTION_SERIALIZABLE };
    DatabaseMetaData dbmd = this.conn.getMetaData();
    for (int i = 0; i < isolationLevels.length; i++) {
        if (dbmd.supportsTransactionIsolationLevel(isolationLevels[i])) {
            this.conn.setTransactionIsolation(isolationLevels[i]);
            assertTrue(this.conn.getTransactionIsolation() == isolationLevels[i] || this.conn.getTransactionIsolation() > isolationLevels[i], "Transaction isolation level that was set (" + isoLevelNames[i] + ") was not returned, nor was a more restrictive isolation level used by the server");
        }
    }
}
Also used : MysqlConnection(com.mysql.cj.MysqlConnection) DatabaseMetaData(java.sql.DatabaseMetaData) Savepoint(java.sql.Savepoint) Test(org.junit.jupiter.api.Test)

Example 29 with MysqlConnection

use of com.mysql.cj.MysqlConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionTest method testSslConnectionOptions.

/**
 * Tests that given SSL/TLS related connection properties values are processed as expected.
 *
 * @throws Exception
 */
@Test
public void testSslConnectionOptions() throws Exception {
    assumeTrue((((MysqlConnection) this.conn).getSession().getServerSession().getCapabilities().getCapabilityFlags() & NativeServerSession.CLIENT_SSL) != 0, "This test requires server with SSL support.");
    assumeTrue(supportsTLSv1_2(((MysqlConnection) this.conn).getSession().getServerSession().getServerVersion()), "This test requires server with TLSv1.2+ support.");
    assumeTrue(supportsTestCertificates(this.stmt), "This test requires the server configured with SSL certificates from ConnectorJ/src/test/config/ssl-test-certs");
    Connection testConn;
    JdbcPropertySet propSet;
    testConn = getConnectionWithProps(this.sslFreeBaseUrl, "");
    propSet = ((JdbcConnection) testConn).getPropertySet();
    assertEquals(SslMode.PREFERRED, propSet.getProperty(PropertyKey.sslMode).getValue());
    assertNull(propSet.getProperty(PropertyKey.trustCertificateKeyStoreUrl).getValue());
    assertEquals("JKS", propSet.getProperty(PropertyKey.trustCertificateKeyStoreType).getValue());
    assertNull(propSet.getProperty(PropertyKey.trustCertificateKeyStorePassword).getValue());
    assertTrue(propSet.getBooleanProperty(PropertyKey.fallbackToSystemTrustStore).getValue());
    assertNull(propSet.getProperty(PropertyKey.clientCertificateKeyStoreUrl).getValue());
    assertEquals("JKS", propSet.getProperty(PropertyKey.clientCertificateKeyStoreType).getValue());
    assertNull(propSet.getProperty(PropertyKey.clientCertificateKeyStorePassword).getValue());
    assertTrue(propSet.getBooleanProperty(PropertyKey.fallbackToSystemKeyStore).getValue());
    testConn.close();
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.toString());
    props.setProperty(PropertyKey.trustCertificateKeyStoreUrl.getKeyName(), "trust-cert-keystore-url");
    props.setProperty(PropertyKey.trustCertificateKeyStoreType.getKeyName(), "trust-cert-keystore-type");
    props.setProperty(PropertyKey.trustCertificateKeyStorePassword.getKeyName(), "trust-cert-keystore-pwd");
    props.setProperty(PropertyKey.fallbackToSystemTrustStore.getKeyName(), "false");
    props.setProperty(PropertyKey.clientCertificateKeyStoreUrl.getKeyName(), "client-cert-keystore-url");
    props.setProperty(PropertyKey.clientCertificateKeyStoreType.getKeyName(), "client-cert-keystore-type");
    props.setProperty(PropertyKey.clientCertificateKeyStorePassword.getKeyName(), "client-cert-keystore-pwd");
    props.setProperty(PropertyKey.fallbackToSystemKeyStore.getKeyName(), "false");
    testConn = getConnectionWithProps(this.sslFreeBaseUrl, props);
    propSet = ((JdbcConnection) testConn).getPropertySet();
    assertEquals(SslMode.DISABLED, propSet.getProperty(PropertyKey.sslMode).getValue());
    assertEquals("trust-cert-keystore-url", propSet.getProperty(PropertyKey.trustCertificateKeyStoreUrl).getValue());
    assertEquals("trust-cert-keystore-type", propSet.getProperty(PropertyKey.trustCertificateKeyStoreType).getValue());
    assertEquals("trust-cert-keystore-pwd", propSet.getProperty(PropertyKey.trustCertificateKeyStorePassword).getValue());
    assertFalse(propSet.getBooleanProperty(PropertyKey.fallbackToSystemTrustStore).getValue());
    assertEquals("client-cert-keystore-url", propSet.getProperty(PropertyKey.clientCertificateKeyStoreUrl).getValue());
    assertEquals("client-cert-keystore-type", propSet.getProperty(PropertyKey.clientCertificateKeyStoreType).getValue());
    assertEquals("client-cert-keystore-pwd", propSet.getProperty(PropertyKey.clientCertificateKeyStorePassword).getValue());
    assertFalse(propSet.getBooleanProperty(PropertyKey.fallbackToSystemKeyStore).getValue());
    testConn.close();
    props.setProperty(PropertyKey.fallbackToSystemTrustStore.getKeyName(), "true");
    props.setProperty(PropertyKey.fallbackToSystemKeyStore.getKeyName(), "true");
    testConn = getConnectionWithProps(this.sslFreeBaseUrl, props);
    propSet = ((JdbcConnection) testConn).getPropertySet();
    assertEquals(SslMode.DISABLED, propSet.getProperty(PropertyKey.sslMode).getValue());
    assertEquals("trust-cert-keystore-url", propSet.getProperty(PropertyKey.trustCertificateKeyStoreUrl).getValue());
    assertEquals("trust-cert-keystore-type", propSet.getProperty(PropertyKey.trustCertificateKeyStoreType).getValue());
    assertEquals("trust-cert-keystore-pwd", propSet.getProperty(PropertyKey.trustCertificateKeyStorePassword).getValue());
    assertTrue(propSet.getBooleanProperty(PropertyKey.fallbackToSystemTrustStore).getValue());
    assertEquals("client-cert-keystore-url", propSet.getProperty(PropertyKey.clientCertificateKeyStoreUrl).getValue());
    assertEquals("client-cert-keystore-type", propSet.getProperty(PropertyKey.clientCertificateKeyStoreType).getValue());
    assertEquals("client-cert-keystore-pwd", propSet.getProperty(PropertyKey.clientCertificateKeyStorePassword).getValue());
    assertTrue(propSet.getBooleanProperty(PropertyKey.fallbackToSystemKeyStore).getValue());
    testConn.close();
}
Also used : Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) JdbcPropertySet(com.mysql.cj.jdbc.JdbcPropertySet) Test(org.junit.jupiter.api.Test)

Example 30 with MysqlConnection

use of com.mysql.cj.MysqlConnection in project aws-mysql-jdbc by awslabs.

the class DateTimeTest method testSqlTimestampSetters.

@Test
public void testSqlTimestampSetters() throws Exception {
    // fractional seconds are not supported in previous versions
    boolean withFract = versionMeetsMinimum(5, 6, 4);
    createTable(tYear, "(id INT, d YEAR)");
    createTable(tDate, "(id INT, d DATE)");
    createTable(tTime, withFract ? "(id INT, d TIME(6))" : "(id INT, d TIME)");
    createTable(tDatetime, withFract ? "(id INT, d DATETIME(6))" : "(id INT, d DATETIME)");
    createTable(tTimestamp, withFract ? "(id INT, d TIMESTAMP(6))" : "(id INT, d TIMESTAMP)");
    createTable(tVarchar, "(id INT, d VARCHAR(30))");
    id = 0;
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.cacheDefaultTimeZone.getKeyName(), "false");
    props.setProperty(PropertyKey.connectionTimeZone.getKeyName(), "SERVER");
    TimeZone serverTz;
    try (Connection testConn = getConnectionWithProps(props)) {
        serverTz = ((MysqlConnection) testConn).getSession().getServerSession().getSessionTimeZone();
    }
    Calendar cal_02 = GregorianCalendar.getInstance(tz_plus_02_00);
    for (TimeZone senderTz : this.senderTimeZones) {
        try {
            for (String connectionTZ : this.connectionTimeZones) {
                initConnections(senderTz, connectionTZ);
                for (boolean forceConnectionTimeZoneToSession : new boolean[] { false, true }) {
                    for (boolean preserveInstants : new boolean[] { false, true }) {
                        for (boolean useSSPS : new boolean[] { false, true }) {
                            for (boolean sendFractionalSeconds : new boolean[] { false, true }) {
                                for (boolean sendTimeFract : new boolean[] { false, true }) {
                                    System.out.println("connTimeZone=" + connectionTZ + "; forceConnTimeZoneToSession=" + forceConnectionTimeZoneToSession + "; preserveInstants=" + preserveInstants + "; useServerPrepStmts=" + useSSPS + "; sendFractSeconds=" + sendFractionalSeconds + "; sendFractSecondsForTime=" + sendTimeFract);
                                    if (connectionTZ == null) {
                                        props.remove(PropertyKey.connectionTimeZone.getKeyName());
                                    } else {
                                        props.setProperty(PropertyKey.connectionTimeZone.getKeyName(), connectionTZ);
                                    }
                                    props.setProperty(PropertyKey.forceConnectionTimeZoneToSession.getKeyName(), "" + forceConnectionTimeZoneToSession);
                                    props.setProperty(PropertyKey.preserveInstants.getKeyName(), "" + preserveInstants);
                                    props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "" + useSSPS);
                                    props.setProperty(PropertyKey.sendFractionalSecondsForTime.getKeyName(), "" + sendTimeFract);
                                    props.setProperty(PropertyKey.sendFractionalSeconds.getKeyName(), "" + sendFractionalSeconds);
                                    TimeZone connTz = connectionTZ == null || "LOCAL".equals(connectionTZ) ? senderTz : ("SERVER".equals(connectionTZ) ? serverTz : TimeZone.getTimeZone(connectionTZ));
                                    TimeZone sessionTz = forceConnectionTimeZoneToSession ? connTz : serverTz;
                                    DateTimeFormatter dateTimeFmt = sendFractionalSeconds ? (withFract ? DATETIME_FORMATTER_WITH_MICROS_NO_OFFCET : TimeUtil.DATETIME_FORMATTER_NO_FRACT_NO_OFFSET) : TimeUtil.DATETIME_FORMATTER_NO_FRACT_NO_OFFSET;
                                    DateTimeFormatter timeFmtsendFractionalSeconds = sendFractionalSeconds ? (withFract ? TIME_FORMATTER_WITH_MICROS_NO_OFFCET : TimeUtil.TIME_FORMATTER_NO_FRACT_NO_OFFSET) : TimeUtil.TIME_FORMATTER_NO_FRACT_NO_OFFSET;
                                    ZonedDateTime zdt_20200101_120000_123456_at_senderTz = (withFract ? ldt_20200101_120000_123456 : ldt_20200101_120000_123456.withNano(0)).atZone(senderTz.toZoneId());
                                    ZonedDateTime zdt_20200101_120000_123456_at_calendarTz = zdt_20200101_120000_123456_at_senderTz.withZoneSameInstant(tz_plus_02_00.toZoneId());
                                    ZonedDateTime zdt_20200101_120000_123456_senderTz_to_connTz = preserveInstants && !(connectionTZ == null || "LOCAL".equals(connectionTZ)) ? zdt_20200101_120000_123456_at_senderTz.withZoneSameInstant(connTz.toZoneId()) : zdt_20200101_120000_123456_at_senderTz;
                                    java.sql.Timestamp ts = java.sql.Timestamp.from(zdt_20200101_120000_123456_at_senderTz.toInstant());
                                    ZonedDateTime zdt_20200101_120000_123456_on_wire = zdt_20200101_120000_123456_at_senderTz.withZoneSameLocal(sessionTz.toZoneId());
                                    ZonedDateTime zdt_no_date_120000_123456_on_wire = LocalDate.now(sessionTz.toZoneId()).atTime(zdt_20200101_120000_123456_at_senderTz.toLocalTime()).atZone(sessionTz.toZoneId());
                                    ZonedDateTime zdt_calendar_on_wire = zdt_20200101_120000_123456_at_calendarTz.withZoneSameLocal(sessionTz.toZoneId());
                                    ZonedDateTime zdt_20200101_no_time_on_wire = zdt_20200101_120000_123456_on_wire.withHour(0).withMinute(0).withSecond(0).withNano(0);
                                    ZonedDateTime zdt_TS_on_wire = zdt_20200101_120000_123456_senderTz_to_connTz.withZoneSameLocal(sessionTz.toZoneId());
                                    String expYear = zdt_20200101_120000_123456_on_wire.format(YEAR_FORMATTER);
                                    String expYearDef = zdt_no_date_120000_123456_on_wire.format(YEAR_FORMATTER);
                                    String expYearCal = zdt_calendar_on_wire.format(YEAR_FORMATTER);
                                    String expYearTS = zdt_TS_on_wire.format(YEAR_FORMATTER);
                                    String expDate = zdt_20200101_120000_123456_on_wire.format(TimeUtil.DATE_FORMATTER);
                                    String expDateDef = zdt_no_date_120000_123456_on_wire.format(useSSPS ? TimeUtil.DATE_FORMATTER : DateTimeFormatter.ofPattern("20HH-mm-ss"));
                                    String expDateCal = zdt_calendar_on_wire.format(TimeUtil.DATE_FORMATTER);
                                    String expDateTS = zdt_TS_on_wire.format(TimeUtil.DATE_FORMATTER);
                                    String expTimeNoMs = zdt_20200101_120000_123456_on_wire.format(TimeUtil.TIME_FORMATTER_NO_FRACT_NO_OFFSET);
                                    String expTime = zdt_20200101_120000_123456_on_wire.format(timeFmtsendFractionalSeconds);
                                    String expTimeCal = zdt_calendar_on_wire.format(timeFmtsendFractionalSeconds);
                                    String expTimeTS = zdt_TS_on_wire.format(timeFmtsendFractionalSeconds);
                                    String expDatetime = zdt_20200101_120000_123456_on_wire.format(dateTimeFmt);
                                    String expDatetimeTS = zdt_TS_on_wire.format(dateTimeFmt);
                                    String expDatetimeDef = LocalDate.now(sessionTz.toZoneId()).atTime(zdt_20200101_120000_123456_on_wire.toLocalTime()).atZone(sessionTz.toZoneId()).format(useSSPS ? dateTimeFmt : DateTimeFormatter.ofPattern("20HH-mm-ss 00:00:00"));
                                    String expDatetimeCal = zdt_calendar_on_wire.format(dateTimeFmt);
                                    String expDatetimeNoTime = zdt_20200101_120000_123456_on_wire.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
                                    String expTimestamp = zdt_20200101_120000_123456_on_wire.withZoneSameInstant(tz_UTC.toZoneId()).format(dateTimeFmt);
                                    String expTimestampTS = zdt_TS_on_wire.withZoneSameInstant(tz_UTC.toZoneId()).format(dateTimeFmt);
                                    String expDefTimestamp = zdt_no_date_120000_123456_on_wire.withZoneSameInstant(tz_UTC.toZoneId()).format(dateTimeFmt);
                                    String expCalTimestamp = zdt_calendar_on_wire.withZoneSameInstant(tz_UTC.toZoneId()).format(dateTimeFmt);
                                    String expTimestampNoTime = zdt_20200101_no_time_on_wire.withZoneSameInstant(tz_UTC.toZoneId()).format(dateTimeFmt);
                                    String expDefUnixTs = zdt_no_date_120000_123456_on_wire.toEpochSecond() + (sendFractionalSeconds && zdt_no_date_120000_123456_on_wire.getNano() > 0 ? "." + TimeUtil.formatNanos(zdt_no_date_120000_123456_on_wire.getNano(), 6) : "");
                                    String expCalUnixTs = zdt_calendar_on_wire.toEpochSecond() + (sendFractionalSeconds && zdt_calendar_on_wire.getNano() > 0 ? "." + TimeUtil.formatNanos(zdt_calendar_on_wire.getNano(), 6) : "");
                                    String expFullUnixTs = zdt_20200101_120000_123456_on_wire.toEpochSecond() + (sendFractionalSeconds && zdt_20200101_120000_123456_on_wire.getNano() > 0 ? "." + TimeUtil.formatNanos(zdt_20200101_120000_123456_on_wire.getNano(), 6) : "");
                                    String expFullUnixTsTS = zdt_TS_on_wire.toEpochSecond() + (sendFractionalSeconds && zdt_TS_on_wire.getNano() > 0 ? "." + TimeUtil.formatNanos(zdt_TS_on_wire.getNano(), 6) : "");
                                    String expUnixTsFromDate = zdt_20200101_no_time_on_wire.toEpochSecond() + "";
                                    if (useSSPS && withFract) {
                                        setObjectFromTz(props, tYear, ts, null, senderTz, expYearCal, null, UseMethod.setTimestamp, cal_02);
                                        setObjectFromTz(props, tYear, ts, null, senderTz, expYear, null, UseMethod.setTimestamp);
                                        setObjectFromTz(props, tYear, ts, null, senderTz, expYear);
                                        setObjectFromTz(props, tYear, ts, MysqlType.DATE, senderTz, expYear);
                                        setObjectFromTz(props, tYear, ts, MysqlType.TIME, senderTz, expYearDef);
                                        setObjectFromTz(props, tYear, ts, MysqlType.DATETIME, senderTz, expYear);
                                        setObjectFromTz(props, tYear, ts, MysqlType.TIMESTAMP, senderTz, expYearTS);
                                    } else {
                                        assertThrows(props, tYear, ts, null, senderTz, null, null, UseMethod.setTimestamp, cal_02, dataTruncatedErr);
                                        assertThrows(props, tYear, ts, null, senderTz, null, null, UseMethod.setTimestamp, dataTruncatedErr);
                                        assertThrows(props, tYear, ts, null, senderTz, dataTruncatedErr);
                                        assertThrows(props, tYear, ts, MysqlType.DATE, senderTz, dataTruncatedErr);
                                        assertThrows(props, tYear, ts, MysqlType.TIME, senderTz, dataTruncatedErr);
                                        assertThrows(props, tYear, ts, MysqlType.DATETIME, senderTz, dataTruncatedErr);
                                        assertThrows(props, tYear, ts, MysqlType.TIMESTAMP, senderTz, dataTruncatedErr);
                                    }
                                    assertThrows(props, tYear, ts, MysqlType.CHAR, senderTz, dataTruncatedErr);
                                    assertThrows(props, tYear, ts, MysqlType.VARCHAR, senderTz, dataTruncatedErr);
                                    assertThrows(props, tYear, ts, MysqlType.TINYTEXT, senderTz, dataTruncatedErr);
                                    assertThrows(props, tYear, ts, MysqlType.TEXT, senderTz, dataTruncatedErr);
                                    assertThrows(props, tYear, ts, MysqlType.MEDIUMTEXT, senderTz, dataTruncatedErr);
                                    assertThrows(props, tYear, ts, MysqlType.LONGTEXT, senderTz, dataTruncatedErr);
                                    setObjectFromTz(props, tYear, ts, MysqlType.YEAR, senderTz, expYear);
                                    /* Into DATE field */
                                    setObjectFromTz(props, tDate, ts, null, senderTz, expDateCal, null, UseMethod.setTimestamp, cal_02);
                                    setObjectFromTz(props, tDate, ts, null, senderTz, expDateTS, null, UseMethod.setTimestamp);
                                    setObjectFromTz(props, tDate, ts, null, senderTz, expDateTS);
                                    setObjectFromTz(props, tDate, ts, MysqlType.DATE, senderTz, expDate);
                                    setObjectFromTz(props, tDate, ts, MysqlType.CHAR, senderTz, expDate);
                                    setObjectFromTz(props, tDate, ts, MysqlType.VARCHAR, senderTz, expDate);
                                    setObjectFromTz(props, tDate, ts, MysqlType.TINYTEXT, senderTz, expDate);
                                    setObjectFromTz(props, tDate, ts, MysqlType.TEXT, senderTz, expDate);
                                    setObjectFromTz(props, tDate, ts, MysqlType.MEDIUMTEXT, senderTz, expDate);
                                    setObjectFromTz(props, tDate, ts, MysqlType.LONGTEXT, senderTz, expDate);
                                    if (useSSPS) {
                                        if (withFract) {
                                            setObjectFromTz(props, tDate, ts, MysqlType.TIME, senderTz, expDateDef);
                                        } else {
                                            assertThrows(props, tDate, ts, MysqlType.TIME, senderTz, dataTruncatedErr);
                                        }
                                    } else {
                                        assertThrows(props, tDate, ts, MysqlType.TIME, senderTz, incorrectDateErr.replace("X", expTime));
                                    }
                                    setObjectFromTz(props, tDate, ts, MysqlType.DATETIME, senderTz, expDate);
                                    setObjectFromTz(props, tDate, ts, MysqlType.TIMESTAMP, senderTz, expDateTS);
                                    assertThrows(props, tDate, ts, MysqlType.YEAR, senderTz, incorrectDateErr.replace("X", s_2020));
                                    /* Into TIME field */
                                    setObjectFromTz(props, tTime, ts, null, senderTz, expTimeCal, null, UseMethod.setTimestamp, cal_02);
                                    setObjectFromTz(props, tTime, ts, null, senderTz, expTimeTS, null, UseMethod.setTimestamp);
                                    setObjectFromTz(props, tTime, ts, null, senderTz, expTimeTS);
                                    if (useSSPS) {
                                        setObjectFromTz(props, tTime, ts, MysqlType.DATE, senderTz, s_000000);
                                    } else {
                                        assertThrows(props, tTime, ts, MysqlType.DATE, senderTz, incorrectTimeErr.replace("X", expDate));
                                    }
                                    setObjectFromTz(props, tTime, ts, MysqlType.CHAR, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.VARCHAR, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.TINYTEXT, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.TEXT, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.MEDIUMTEXT, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.LONGTEXT, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.TIME, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.DATETIME, senderTz, expTime);
                                    setObjectFromTz(props, tTime, ts, MysqlType.TIMESTAMP, senderTz, expTimeTS);
                                    // TIME takes numbers as a short notation, thus it works here
                                    setObjectFromTz(props, tTime, ts, MysqlType.YEAR, senderTz, expYear);
                                    setObjectFromTz(props, tTime, ts, MysqlType.YEAR, senderTz, // TIME takes numbers as a short notation, thus it works here
                                    "00:" + expYear.substring(0, 2) + ":" + expYear.substring(2));
                                    /* Into DATETIME field */
                                    setObjectFromTz(props, tDatetime, ts, null, senderTz, expDatetimeCal, null, UseMethod.setTimestamp, cal_02);
                                    setObjectFromTz(props, tDatetime, ts, null, senderTz, expDatetimeTS, null, UseMethod.setTimestamp);
                                    setObjectFromTz(props, tDatetime, ts, null, senderTz, expDatetimeTS);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.DATE, senderTz, expDatetimeNoTime);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.CHAR, senderTz, expDatetime);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.VARCHAR, senderTz, expDatetime);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.TINYTEXT, senderTz, expDatetime);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.TEXT, senderTz, expDatetime);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.MEDIUMTEXT, senderTz, expDatetime);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.LONGTEXT, senderTz, expDatetime);
                                    if (useSSPS) {
                                        if (withFract) {
                                            setObjectFromTz(props, tDatetime, ts, MysqlType.TIME, senderTz, expDatetimeDef);
                                        } else {
                                            assertThrows(props, tDatetime, ts, MysqlType.TIME, senderTz, dataTruncatedErr);
                                        }
                                    } else {
                                        assertThrows(props, tDatetime, ts, MysqlType.TIME, senderTz, incorrectDatetimeErr.replace("X", expTime));
                                    }
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.DATETIME, senderTz, expDatetime);
                                    setObjectFromTz(props, tDatetime, ts, MysqlType.TIMESTAMP, senderTz, expDatetimeTS);
                                    assertThrows(props, tDatetime, ts, MysqlType.YEAR, senderTz, incorrectDatetimeErr.replace("X", expYear));
                                    /* Into TIMESTAMP field */
                                    setObjectFromTz(props, tTimestamp, ts, null, senderTz, expCalTimestamp, expCalUnixTs, UseMethod.setTimestamp, cal_02);
                                    setObjectFromTz(props, tTimestamp, ts, null, senderTz, expTimestampTS, expFullUnixTsTS, UseMethod.setTimestamp);
                                    setObjectFromTz(props, tTimestamp, ts, null, senderTz, expTimestampTS, expFullUnixTsTS);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.DATE, senderTz, expTimestampNoTime, expUnixTsFromDate);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.CHAR, senderTz, expTimestamp, expFullUnixTs);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.VARCHAR, senderTz, expTimestamp, expFullUnixTs);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.TINYTEXT, senderTz, expTimestamp, expFullUnixTs);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.TEXT, senderTz, expTimestamp, expFullUnixTs);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.MEDIUMTEXT, senderTz, expTimestamp, expFullUnixTs);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.LONGTEXT, senderTz, expTimestamp, expFullUnixTs);
                                    if (useSSPS && withFract) {
                                        setObjectFromTz(props, tTimestamp, ts, MysqlType.TIME, senderTz, expDefTimestamp, expDefUnixTs);
                                    } else {
                                        assertThrows(props, tTimestamp, ts, MysqlType.TIME, senderTz, incorrectDatetimeErr.replace("X", expTime));
                                    }
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.DATETIME, senderTz, expTimestamp, expFullUnixTs);
                                    setObjectFromTz(props, tTimestamp, ts, MysqlType.TIMESTAMP, senderTz, expTimestampTS, expFullUnixTsTS);
                                    assertThrows(props, tTimestamp, ts, MysqlType.YEAR, senderTz, incorrectDatetimeErr.replace("X", expYear));
                                    /* Into VARCHAR field */
                                    String expDatetimeCal2 = useSSPS ? zdt_calendar_on_wire.format(TimeUtil.DATETIME_FORMATTER_NO_FRACT_NO_OFFSET) : // TODO milliseconds are ignored by server. Bug ?
                                    expDatetimeCal;
                                    String expDatetime2 = useSSPS ? zdt_20200101_120000_123456_on_wire.format(TimeUtil.DATETIME_FORMATTER_NO_FRACT_NO_OFFSET) : // TODO milliseconds are ignored by server. Bug ?
                                    expDatetime;
                                    // TODO milliseconds are ignored by server. Bug ?
                                    String expTime2 = useSSPS ? expTimeNoMs : expTime;
                                    // TODO milliseconds are ignored by server. Bug ?
                                    String expDatetimeTS2 = useSSPS ? zdt_TS_on_wire.format(TimeUtil.DATETIME_FORMATTER_NO_FRACT_NO_OFFSET) : expDatetimeTS;
                                    setObjectFromTz(props, tVarchar, ts, null, senderTz, expDatetimeCal2, null, UseMethod.setTimestamp, cal_02);
                                    setObjectFromTz(props, tVarchar, ts, null, senderTz, expDatetimeTS2, null, UseMethod.setTimestamp);
                                    setObjectFromTz(props, tVarchar, ts, null, senderTz, expDatetimeTS2);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.DATETIME, senderTz, expDatetime2);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.TIMESTAMP, senderTz, expDatetimeTS2);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.TIME, senderTz, expTime2);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.DATE, senderTz, expDate);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.CHAR, senderTz, expDatetime);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.VARCHAR, senderTz, expDatetime);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.TINYTEXT, senderTz, expDatetime);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.TEXT, senderTz, expDatetime);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.MEDIUMTEXT, senderTz, expDatetime);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.LONGTEXT, senderTz, expDatetime);
                                    setObjectFromTz(props, tVarchar, ts, MysqlType.YEAR, senderTz, expYear);
                                }
                            }
                        }
                    }
                }
                closeConnections();
            }
        } finally {
            closeConnections();
        }
    }
}
Also used : TimeZone(java.util.TimeZone) ZonedDateTime(java.time.ZonedDateTime) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Timestamp(java.sql.Timestamp) Connection(java.sql.Connection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.junit.jupiter.api.Test)

Aggregations

MysqlConnection (com.mysql.cj.MysqlConnection)70 Test (org.junit.jupiter.api.Test)65 Properties (java.util.Properties)57 Connection (java.sql.Connection)55 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)42 TimeZone (java.util.TimeZone)26 XAConnection (javax.sql.XAConnection)24 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)23 PooledConnection (javax.sql.PooledConnection)22 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)21 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)21 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)21 ZonedDateTime (java.time.ZonedDateTime)21 SQLException (java.sql.SQLException)18 PreparedStatement (java.sql.PreparedStatement)16 Statement (java.sql.Statement)16 CommunicationsException (com.mysql.cj.jdbc.exceptions.CommunicationsException)14 IOException (java.io.IOException)14 ExecutionException (java.util.concurrent.ExecutionException)14 TimeoutException (java.util.concurrent.TimeoutException)14