/*   Copyright 2004 The Apache Software Foundation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package misc.checkin;

import net.sf.saxon.str.StringView;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.HexBinaryValue;
import org.apache.xmlbeans.impl.util.HexBin;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Locale;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class HexBinTest {

    @Test
    void testSurrogate() throws Exception {
        String exp = "ABC\ud83c\udf09123";
        String enc = HexBin.encode(exp);
        String dec = HexBin.decode(enc);
        assertEquals(exp, dec);
    }

    @Test
    void testInvalidHighByte() {
        // 0xFF is not a hex digit; decode must report it like any other invalid
        // input (null) instead of indexing past the lookup table.
        assertNull(HexBin.decode(new byte[]{(byte) 0xFF, (byte) 0xFF}));
        assertNull(HexBin.decode(new String(new byte[]{(byte) 0xFF, (byte) 0xFF},
                StandardCharsets.ISO_8859_1)));
    }

    @Test
    void knownValue() throws XPathException {
        // I've looked for comparison values but the following definition seems to be wrong,
        // because Saxon also returns the same encoded value
        // see http://books.xmlschemata.org/relaxng/ch19-77143.html
        // "Relax NG" by Eric van der Vlist

        String in = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>a";

        String exp = "3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d225554462d38223f3e61";
        String enc = HexBin.encode(in);

        HexBinaryValue val = new HexBinaryValue(StringView.tidy(enc));
        String saxIn = new String(val.getBinaryValue(), StandardCharsets.UTF_8);

        assertEquals(exp, enc.toLowerCase(Locale.ROOT));
        assertEquals(in, saxIn);
    }
}
