/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.pdmodel.interactive.form;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;

import org.junit.Before;
import org.junit.Test;

/**
 * Test for the PDSignatureField class.
 *
 */
public class PDSignatureFieldTest
{
    private PDDocument document;
    private PDAcroForm acroForm;

    @Before
    public void setUp()
    {
        document = new PDDocument();
        acroForm = new PDAcroForm(document);
    }

    @Test
    public void createDefaultSignatureField()
    {
        PDSignatureField sigField = new PDSignatureField(acroForm);
        sigField.setPartialName("SignatureField");

        assertEquals(sigField.getFieldType(), sigField.getCOSObject().getNameAsString(COSName.FT));
        assertEquals("Sig", sigField.getFieldType());
        
        assertEquals(COSName.ANNOT, sigField.getCOSObject().getItem(COSName.TYPE));
        assertEquals(PDAnnotationWidget.SUB_TYPE, sigField.getCOSObject().getNameAsString(COSName.SUBTYPE));

        // Add the field to the acroform
        List<PDField> fields = new ArrayList<>();
        fields.add(sigField);
        this.acroForm.setFields(fields);

        assertNotNull(acroForm.getField("SignatureField"));
    }
    
    @Test(expected=UnsupportedOperationException.class)
    public void setValueForAbstractedSignatureField()
    {
        PDSignatureField sigField = new PDSignatureField(acroForm);
        sigField.setPartialName("SignatureField");

        sigField.setValue("Can't set value using String");
    }
}

@@ -860,93 +860,108 @@
 ort 
-static org.junit.Assert.assertEquals;%0Aimport static org.junit.As
+java.io.ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.nio.char
 se
-r
 t.
-assertNotNull
+StandardCharsets
 ;%0A
-%0A
 impo
@@ -1167,16 +1167,182 @@
 Widget;%0A
+import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;%0A%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull;
 %0Aimport 
@@ -2742,10 +2742,669 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * PDFBOX-4822: test get the signature contents.%0A     * %0A     * @throws IOException %0A     */%0A    @Test%0A    public void testGetContents() throws IOException%0A    %7B%0A        // Normally, range0 + range1 = position of %22%3C%22, and range2 = position after %22%3E%22%0A        PDSignature signature = new PDSignature();%0A        signature.setByteRange(new int%5B%5D%7B 0, 10, 30, 10%7D);%0A        byte%5B%5D by = %22AAAAAAAAAA%3C313233343536373839%3EBBBBBBBBBB%22.getBytes(StandardCharsets.ISO_8859_1);%0A        assertEquals(%22123456789%22, new String(signature.getContents(by)));%0A        assertEquals(%22123456789%22, new String(signature.getContents(new ByteArrayInputStream(by))));%0A    %7D%0A
 %7D%0A

add test