Class UnicodeRoundTripTest


  • @SQLAction(provides="postgresql_unicodetest",install="SELECT CASE WHEN \'UTF8\' = current_setting(\'server_encoding\') THEN set_config(\'pljava.implementors\', \'postgresql_unicodetest,\' || current_setting(\'pljava.implementors\'), true) END") @SQLAction(requires="unicodetest fn",implementor="postgresql_unicodetest",install="   WITH     usable_codepoints ( cp ) AS (      SELECT generate_series(1,x\'d7ff\'::int)      UNION ALL      SELECT generate_series(x\'e000\'::int,x\'10ffff\'::int)     ),     test_inputs ( groupnum, cparray, s ) AS (      SELECT        cp / 1024 AS groupnum,        array_agg(cp ORDER BY cp), string_agg(chr(cp), \'\' ORDER BY cp)      FROM usable_codepoints      GROUP BY groupnum     ),     test_outputs AS (      SELECT groupnum, cparray, s, unicodetest(s, cparray) AS roundtrip      FROM test_inputs     ),     test_failures AS (      SELECT *      FROM test_outputs      WHERE       cparray != (roundtrip).cparray OR s != (roundtrip).s       OR NOT (roundtrip).matched     ),     test_summary ( n_failing_groups, first_failing_group ) AS (      SELECT count(*), min(groupnum) FROM test_failures     )    SELECT     CASE WHEN n_failing_groups > 0 THEN      javatest.logmessage(\'WARNING\', n_failing_groups ||       \' 1k codepoint ranges had mismatches, first is block starting 0x\' ||       to_hex(1024 * first_failing_group))     ELSE      javatest.logmessage(\'INFO\',         \'all Unicode codepoint ranges roundtripped successfully.\')     END     FROM test_summary")
    public class UnicodeRoundTripTest
    extends Object
    Test that strings containing characters from all Unicode planes are passed between PG and Java without alteration (issue 21).

    This function takes a string and an array of ints constructed in PG, such that PG believes the codepoints in the string to correspond exactly with the ints in the array. The function compares the two, generates a new array from the codepoints Java sees in the string and a new Java string from the original array, and returns a tuple (matched, cparray, s) where matched indicates whether the original array and string matched as seen by Java, and cparray and s are the new array and string generated in Java.

    The supplied test query generates all Unicode code points 1k at a time, calls this function on each (1k array, 1k string) pair, and counts a failure if matched is false or the original and returned arrays or strings do not match as seen in SQL.

    This example sets an implementor tag based on a PostgreSQL condition, as further explained in the ConditionalDDR example.

    • Method Detail

      • unicodetest

        @Function(out={"matched boolean","cparray integer[]","s text"},
                  provides="unicodetest fn")
        public static boolean unicodetest​(String s,
                                          int[] ints,
                                          ResultSet rs)
                                   throws SQLException
        This function takes a string and an array of ints constructed in PG, such that PG believes the codepoints in the string to correspond exactly with the ints in the array. The function compares the two, generates a new array from the codepoints Java sees in the string and a new Java string from the original array, and returns a tuple (matched, cparray, s) where matched indicates whether the original array and string matched as seen by Java, and cparray and s are the new array and string generated in Java.
        Parameters:
        s - A string, whose codepoints should match the entries of ints
        ints - Array of ints that should match the codepoints in s
        rs - OUT (matched, cparray, s) as described above
        Returns:
        true to indicate the OUT tuple is not null
        Throws:
        SQLException