Fix | Thai locale bug in JUnit tests
Fix | Bug where locale would affect the Timestamp format returned in string form
package com.microsoft.sqlserver.jdbc.datatypes;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -17,6 +18,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
+import java.text.DateFormatSymbols;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.EnumSet;
@@ -1094,7 +1096,6 @@ public class DataTypesTest extends AbstractTest {
@Test
public void testResultSetMetaData() throws Exception {
try (Connection conn = DriverManager.getConnection(connectionString)) {
- TestValue v[] = TestValue.values();
for (TestValue value : TestValue.values())
value.sqlValue.verifyResultSetMetaData(conn);
}
@@ -1378,9 +1379,21 @@ public class DataTypesTest extends AbstractTest {
rs.next();
// compare these separately since there may be an extra space between the 2
- assertEquals("Jan 1 1970", rs.getString(1).substring(0, 11));
- assertEquals(timeFormat.format(ts.getTime()),
- rs.getString(1).substring(rs.getString(1).length() - 7).trim());
+ assertTrue(rs.getString(1).startsWith("Jan 1 1970"));
+
+ /*
+ * Timestamp returned from varchar column may be wrongly formatted. E.g: 12:12PM will not pass if
+ * compared to 12:12p.m.
+ */
+ DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
+ symbols.setAmPmStrings(new String[] {"am", "pm"});
+ timeFormat.setDateFormatSymbols(symbols);
+ String expectedTimePortion = timeFormat.format(ts);
+ String recievedTimePortion = rs.getString(1).substring(rs.getString(1).length() - 7).trim();
+ assertTrue(
+ "Timestamp mismatch, expected: " + expectedTimePortion + " but recieved: "
+ + recievedTimePortion + ".",
+ expectedTimePortion.equalsIgnoreCase(recievedTimePortion));
}
// Test PreparedStatement with Date
