Search in sources :

Example 11 with PropertySet

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

the class ZeroDateTimeToNullValueFactoryTest method testBasics.

@Test
public void testBasics() {
    PropertySet pset = new DefaultPropertySet();
    pset.<PropertyDefinitions.ZeroDatetimeBehavior>getEnumProperty(PropertyKey.zeroDateTimeBehavior).setValue(ZeroDatetimeBehavior.CONVERT_TO_NULL);
    LocalDateTimeValueFactory vf = new LocalDateTimeValueFactory(pset);
    assertNull(vf.createFromDate(new InternalDate()));
    assertEquals(LocalDateTime.of(2018, 1, 1, 0, 0, 0, 0), vf.createFromDate(new InternalDate(2018, 1, 1)));
    assertNotNull(vf.createFromTime(new InternalTime()));
    assertEquals(LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC), vf.createFromTime(new InternalTime()));
    assertThrows(DataReadException.class, "The value '-1:00:00' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            vf.createFromTime(new InternalTime(-1, 0, 0, 0, 0));
            return null;
        }
    });
    assertThrows(DataReadException.class, "The value '44:00:00' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            vf.createFromTime(new InternalTime(44, 0, 0, 0, 0));
            return null;
        }
    });
    assertEquals(LocalDateTime.of(1970, 1, 1, 1, 1, 1, 1), vf.createFromTime(new InternalTime(1, 1, 1, 1, 9)));
    assertNull(vf.createFromTimestamp(new InternalTimestamp(0, 0, 0, 0, 0, 0, 0, 0)));
    assertThrows(DataReadException.class, "Zero date value prohibited", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            vf.createFromTimestamp(new InternalTimestamp(0, 0, 0, 1, 1, 1, 1, 9));
            return null;
        }
    });
    assertThrows(DateTimeException.class, "Invalid value for MonthOfYear \\(valid values 1 - 12\\): 0", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            assertEquals(LocalDateTime.of(0, 0, 1, 1, 1, 1, 1), vf.createFromTimestamp(new InternalTimestamp(0, 0, 1, 1, 1, 1, 1, 9)));
            return null;
        }
    });
    assertThrows(DateTimeException.class, "Invalid value for DayOfMonth \\(valid values 1 - 28/31\\): 0", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            assertEquals(LocalDateTime.of(0, 1, 0, 1, 1, 1, 1), vf.createFromTimestamp(new InternalTimestamp(0, 1, 0, 1, 1, 1, 1, 9)));
            return null;
        }
    });
    assertEquals(LocalDateTime.of(0, 1, 1, 1, 1, 1, 1), vf.createFromTimestamp(new InternalTimestamp(0, 1, 1, 1, 1, 1, 1, 9)));
    assertThrows(DateTimeException.class, "Invalid value for MonthOfYear \\(valid values 1 - 12\\): 0", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            assertEquals(LocalDateTime.of(2018, 0, 0, 1, 1, 1, 1), vf.createFromTimestamp(new InternalTimestamp(2018, 0, 0, 1, 1, 1, 1, 9)));
            return null;
        }
    });
    assertThrows(DateTimeException.class, "Invalid value for MonthOfYear \\(valid values 1 - 12\\): 0", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            assertEquals(LocalDateTime.of(2018, 0, 1, 1, 1, 1, 1), vf.createFromTimestamp(new InternalTimestamp(2018, 0, 1, 1, 1, 1, 1, 9)));
            return null;
        }
    });
    assertThrows(DateTimeException.class, "Invalid value for DayOfMonth \\(valid values 1 - 28/31\\): 0", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            assertEquals(LocalDateTime.of(2018, 1, 0, 1, 1, 1, 1), vf.createFromTimestamp(new InternalTimestamp(2018, 1, 0, 1, 1, 1, 1, 9)));
            return null;
        }
    });
    assertEquals(LocalDateTime.of(2018, 1, 1, 1, 1, 1, 1), vf.createFromTimestamp(new InternalTimestamp(2018, 1, 1, 1, 1, 1, 1, 9)));
    assertEquals("java.time.LocalDateTime", vf.getTargetTypeName());
}
Also used : DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) PropertyDefinitions(com.mysql.cj.conf.PropertyDefinitions) InternalDate(com.mysql.cj.protocol.InternalDate) PropertySet(com.mysql.cj.conf.PropertySet) DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) InternalTime(com.mysql.cj.protocol.InternalTime) DataReadException(com.mysql.cj.exceptions.DataReadException) DateTimeException(java.time.DateTimeException) InternalTimestamp(com.mysql.cj.protocol.InternalTimestamp) Test(org.junit.jupiter.api.Test)

Aggregations

PropertySet (com.mysql.cj.conf.PropertySet)11 DefaultPropertySet (com.mysql.cj.conf.DefaultPropertySet)8 HostInfo (com.mysql.cj.conf.HostInfo)2 PropertyKey (com.mysql.cj.conf.PropertyKey)2 Log (com.mysql.cj.log.Log)2 Properties (java.util.Properties)2 Test (org.junit.jupiter.api.Test)2 CoreSession (com.mysql.cj.CoreSession)1 Messages (com.mysql.cj.Messages)1 MysqlxSession (com.mysql.cj.MysqlxSession)1 NativeSession (com.mysql.cj.NativeSession)1 ConnectionUrl (com.mysql.cj.conf.ConnectionUrl)1 ConnectionUrlParser (com.mysql.cj.conf.ConnectionUrlParser)1 PropertyDefinitions (com.mysql.cj.conf.PropertyDefinitions)1 SslMode (com.mysql.cj.conf.PropertyDefinitions.SslMode)1 RuntimeProperty (com.mysql.cj.conf.RuntimeProperty)1 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)1 CJException (com.mysql.cj.exceptions.CJException)1 DataConversionException (com.mysql.cj.exceptions.DataConversionException)1 DataReadException (com.mysql.cj.exceptions.DataReadException)1