/*
 * Copyright 2016 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 org.apache.pdfbox.util;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 *
 * @author Michael Doswald
 */
public class TestHexUtil extends TestCase
{
    
    /**
     * Test conversion from short to char[]
     */
    public void testGetCharsFromShortWithoutPassingInABuffer()
    {
        assertArrayEquals(new char[]{'0','0','0','0'}, Hex.getChars((short)0x0000));
        assertArrayEquals(new char[]{'0','0','0','F'}, Hex.getChars((short)0x000F));
        assertArrayEquals(new char[]{'A','B','C','D'}, Hex.getChars((short)0xABCD));
        assertArrayEquals(new char[]{'B','A','B','E'}, Hex.getChars((short)0xCAFEBABE));
    }

    /**
     * Check conversion from String to a char[] which contains the UTF16-BE encoded
     * bytes of the string as hex digits
     *
     */
    public void testGetCharsUTF16BE()
    {
        assertArrayEquals(new char[]{'0','0','6','1','0','0','6','2'}, Hex.getCharsUTF16BE("ab"));
        assertArrayEquals(new char[]{'5','E','2','E','5','2','A','9'}, Hex.getCharsUTF16BE("帮助"));
    }

    private void assertArrayEquals(char[] expected, char[] actual)
    {
        assertEquals("Length of char array not equal", expected.length, actual.length);
        for (int idx = 0; idx < expected.length; idx++)
        {
            if (expected[idx] != actual[idx])
            {
                fail(String.format("Character at index %d not equal. Expected '%c' but got '%c'", 
                        idx, expected[idx], actual[idx]));
            }
        }
    }

    /**
     * Set the tests in the suite for this test class.
     *
     * @return the Suite.
     */
    public static Test suite()
    {
        return new TestSuite(TestHexUtil.class);
    }

    /**
     * Command line execution.
     *
     * @param args Command line arguments.
     */
    public static void main(String[] args)
    {
        String[] arg =
        {
            TestHexUtil.class.getName()
        };
        junit.textui.TestRunner.main(arg);
    }
}

@@ -642,16 +642,83 @@
 .util;%0A%0A
+import java.nio.charset.StandardCharsets;%0Aimport java.util.Locale;%0A
 import j
@@ -804,16 +804,66 @@
 stSuite;
+%0Aimport static org.junit.Assert.assertArrayEquals;
 %0A%0A/**%0A *
@@ -1828,163 +1828,151 @@
     
-private void assertArrayEquals(char%5B%5D expected, char%5B%5D actual)%0A    %7B%0A        assertEquals(%22Length of char array not equal%22, expected.length, actual.length)
+/**%0A     * Test getBytes() and getString() and decodeHex()%0A     */%0A    public void testMisc()%0A    %7B%0A        byte%5B%5D byteSrcArray = new byte%5B256%5D
 ;%0A  
@@ -1991,44 +1991,26 @@
 nt i
-dx
  = 0; i
-dx
  %3C 
-expected.length; idx
+256; 
 ++
+i
 )%0A  
@@ -2033,222 +2033,744 @@
     
-if (expected%5Bidx%5D != actual%5Bidx%5D)%0A            %7B%0A                fail(String.format(%22Character at index %25d not equal. Expected '%25c' but got '%25c'%22, %0A                        idx
+byteSrcArray%5Bi%5D = (byte) i;%0A%0A            byte%5B%5D bytes = Hex.getBytes((byte) i);%0A            assertEquals(2, bytes.length);%0A            String s2 = String.format(Locale.US, %22%2502X%22, i);%0A            assertArrayEquals(s2.getBytes(StandardCharsets.US_ASCII), bytes);%0A            s2 = Hex.getString((byte) i);%0A            assertArrayEquals(s2.getBytes(StandardCharsets.US_ASCII), bytes);%0A            %0A            assertArrayEquals(new byte%5B%5D%7B(byte) i%7D
 , 
+H
 ex
-pected%5Bidx%5D, actual%5Bidx%5D)
+.decodeHex(s2));%0A        %7D%0A        byte%5B%5D byteDstArray = Hex.getBytes(byteSrcArray);%0A        assertEquals(byteDstArray.length, byteSrcArray.length * 2);%0A%0A        String dstString = Hex.getString(byteSrcArray);%0A        assertEquals(dstString.length(), byteSrcArray.length * 2
 );%0A
+%0A
         
     
@@ -2769,23 +2769,163 @@
     
-    %7D%0A        %7D
+assertArrayEquals(dstString.getBytes(StandardCharsets.US_ASCII), byteDstArray);%0A        %0A        assertArrayEquals(byteSrcArray, Hex.decodeHex(dstString));
 %0A   

increase code coverage