You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2020/11/20 18:56:13 UTC

svn commit: r1883672 - in /pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form: TestCheckBox.java TestListBox.java

Author: msahyoun
Date: Fri Nov 20 18:56:13 2020
New Revision: 1883672

URL: http://svn.apache.org/viewvc?rev=1883672&view=rev
Log:
PDFBOX-5017: Sonar fixes; split long method into individual tests

Modified:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java?rev=1883672&r1=1883671&r2=1883672&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java Fri Nov 20 18:56:13 2020
@@ -72,12 +72,12 @@ class TestCheckBox
 
             // assert that the values have been correctly set
             assertNotNull(checkBox.getCOSObject().getItem(COSName.OPT));
-            assertEquals(optItem.size(),2);
+            assertEquals(2, optItem.size());
             assertEquals(options.get(0), optItem.getString(0));
             
             // assert that the values can be retrieved correctly
             List<String> retrievedOptions = checkBox.getExportValues();
-            assertEquals(retrievedOptions.size(),2);
+            assertEquals(2, retrievedOptions.size());
             assertEquals(retrievedOptions, options);
 
             // assert that the Opt entry is removed

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java?rev=1883672&r1=1883671&r2=1883672&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java Fri Nov 20 18:56:13 2020
@@ -19,7 +19,7 @@ package org.apache.pdfbox.pdmodel.intera
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -28,6 +28,7 @@ import java.util.List;
 
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDResources;
@@ -35,6 +36,8 @@ import org.apache.pdfbox.pdmodel.common.
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -42,194 +45,222 @@ import org.junit.jupiter.api.Test;
  */
 class TestListBox
 {
-    
-    /**
-     * This will test the list box PDModel.
-     *
-     * @throws IOException If there is an error creating the field.
-     */
-    @Test
-    void testListboxPDModel() throws IOException
-    {
 
-        /*
-         * Set up two data lists which will be used for the tests
-         */
+    private List<String> exportValues;
+    private List<String> displayValues;
+    private PDDocument doc;
+    PDListBox choice;
 
+    @BeforeEach
+    void setUp() throws IOException
+    {
         // export values
-        List<String> exportValues = new ArrayList<>();
+        exportValues = new ArrayList<>();
         exportValues.add("export01");
         exportValues.add("export02");
         exportValues.add("export03");
 
         // display values, not sorted on purpose as this
         // will be used to test the sort option of the list box
-        List<String> displayValues = new ArrayList<>();
+        displayValues = new ArrayList<>();
         displayValues.add("display02");
         displayValues.add("display01");
         displayValues.add("display03");
+        
+        doc = new PDDocument();
+        PDPage page = new PDPage(PDRectangle.A4);
+        doc.addPage(page);
+        PDAcroForm form = new PDAcroForm( doc );
+        
+        // Adobe Acrobat uses Helvetica as a default font and 
+        // stores that under the name '/Helv' in the resources dictionary
+        PDFont font = PDType1Font.HELVETICA;
+        PDResources resources = new PDResources();
+        resources.put(COSName.getPDFName("Helv"), font);
+        
+        // Add and set the resources and default appearance at the form level
+        form.setDefaultResources(resources);
+        
+        // Acrobat sets the font size on the form level to be
+        // auto sized as default. This is done by setting the font size to '0'
+        String defaultAppearanceString = "/Helv 0 Tf 0 g";
+        form.setDefaultAppearance(defaultAppearanceString);
+        
+        // the choice field for testing
+        choice = new PDListBox(form);
+        
+        choice.setDefaultAppearance("/Helv 12 Tf 0g");
+        
+        
+        // Specify the annotation associated with the field
+        PDAnnotationWidget widget = choice.getWidgets().get(0);
+        PDRectangle rect = new PDRectangle(50, 750, 200, 50);
+        widget.setRectangle(rect);
+        widget.setPage(page);
+        
+        // Add the annotation to the page
+        page.getAnnotations().add(widget);
+    }
 
-        try (PDDocument doc = new PDDocument())
-        {
-            PDPage page = new PDPage(PDRectangle.A4);
-            doc.addPage(page);
-            PDAcroForm form = new PDAcroForm( doc );
-            
-            // Adobe Acrobat uses Helvetica as a default font and 
-            // stores that under the name '/Helv' in the resources dictionary
-            PDFont font = PDType1Font.HELVETICA;
-            PDResources resources = new PDResources();
-            resources.put(COSName.getPDFName("Helv"), font);
-            
-            // Add and set the resources and default appearance at the form level
-            form.setDefaultResources(resources);
-            
-            // Acrobat sets the font size on the form level to be
-            // auto sized as default. This is done by setting the font size to '0'
-            String defaultAppearanceString = "/Helv 0 Tf 0 g";
-            form.setDefaultAppearance(defaultAppearanceString);
-            
-            PDListBox choice = new PDListBox(form);
-            
-            choice.setDefaultAppearance("/Helv 12 Tf 0g");
-            
-            
-            // Specify the annotation associated with the field
-            PDAnnotationWidget widget = choice.getWidgets().get(0);
-            PDRectangle rect = new PDRectangle(50, 750, 200, 50);
-            widget.setRectangle(rect);
-            widget.setPage(page);
-            
-            // Add the annotation to the page
-            page.getAnnotations().add(widget);
-            
-            
-            // test that there are no nulls returned for an empty field
-            // only specific methods are tested here
-            assertNotNull(choice.getOptions());
-            assertNotNull(choice.getValue());
-            
-            /*
-             * Tests for setting the export values
-             */
-
-            // setting/getting option values - the dictionaries Opt entry
-            choice.setOptions(exportValues);
-            assertEquals(exportValues,choice.getOptionsDisplayValues());
-            assertEquals(exportValues,choice.getOptionsExportValues());
-
-            // Test bug 1 of PDFBOX-4252 when top index is not null
-            choice.setTopIndex(1);
-            choice.setValue(exportValues.get(2));
-            assertEquals(exportValues.get(2), choice.getValue().get(0));
-            choice.setTopIndex(null); // reset
-
-            // assert that the option values have been correctly set
-            COSArray optItem = (COSArray) choice.getCOSObject().getItem(COSName.OPT);
-            assertNotNull(choice.getCOSObject().getItem(COSName.OPT));
-            assertEquals(optItem.size(),exportValues.size());
-            assertEquals(exportValues.get(0), optItem.getString(0));
-            
-            // assert that the option values can be retrieved correctly
-            List<String> retrievedOptions = choice.getOptions();
-            assertEquals(retrievedOptions.size(),exportValues.size());
-            assertEquals(retrievedOptions, exportValues);
-
-            /*
-             * Tests for setting the field values
-             */
-
-            // assert that the field value can be set
-            choice.setValue("export01");
-            assertEquals("export01", choice.getValue().get(0));
-            
-            // ensure that the choice field doesn't allow multiple selections
-            choice.setMultiSelect(false);
-            
-            // without multiselect setting multiple items shall fail
-            try
-            {
-                choice.setValue(exportValues);
-                fail( "Missing IllegalArgumentException" );
-            }
-            catch( IllegalArgumentException e )
-            {
-                assertEquals("The list box does not allow multiple selections.",e.getMessage() );
-            }            
-            
-            // ensure that the choice field does allow multiple selections
-            choice.setMultiSelect(true);
-            // now this call must succeed
+    @Test
+    void testNoNullsReturned()
+    {
+        // test that there are no nulls returned for an empty field
+        // only specific methods are tested here
+        assertNotNull(choice.getOptions());
+        assertNotNull(choice.getValue());
+    }
+
+    /*
+     * Tests for setting the export values
+     */
+    @Test
+    void testExportValuesGetterSetter() throws IOException
+    {
+        // setting/getting option values - the dictionaries Opt entry
+        choice.setOptions(exportValues);
+        assertEquals(exportValues,choice.getOptionsDisplayValues());
+        assertEquals(exportValues,choice.getOptionsExportValues());
+
+        // Test bug 1 of PDFBOX-4252 when top index is not null
+        choice.setTopIndex(1);
+        choice.setValue(exportValues.get(2));
+        assertEquals(exportValues.get(2), choice.getValue().get(0));
+        choice.setTopIndex(null); // reset
+
+        // assert that the option values have been correctly set
+        COSArray optItem = (COSArray) choice.getCOSObject().getItem(COSName.OPT);
+        assertNotNull(choice.getCOSObject().getItem(COSName.OPT));
+        assertEquals(optItem.size(),exportValues.size());
+        assertEquals(exportValues.get(0), optItem.getString(0));
+            
+        // assert that the option values can be retrieved correctly
+        List<String> retrievedOptions = choice.getOptions();
+        assertEquals(retrievedOptions.size(),exportValues.size());
+        assertEquals(retrievedOptions, exportValues);
+        // assert that the field value can be set
+    }
+
+    /*
+     * Test for setting the field value
+     */
+    @Test
+    void testFieldValueSetterGetter() throws IOException
+    {
+        // add test data
+        choice.setOptions(exportValues);
+        choice.setMultiSelect(true);
+        choice.setValue(exportValues);
+
+        // assert that the option values have been correctly set
+        System.out.println(choice.getCOSObject());
+
+        COSArray valueItems = (COSArray) choice.getCOSObject().getItem(COSName.V);
+        assertNotNull(valueItems);
+        assertEquals(valueItems.size(),exportValues.size());
+        assertEquals(exportValues.get(0), valueItems.getString(0));
+            
+        // assert that the index values have been correctly set
+        COSArray indexItems = (COSArray) choice.getCOSObject().getItem(COSName.I);
+        assertNotNull(indexItems);
+        assertEquals(indexItems.size(),exportValues.size());
+            
+        // setting a single value shall remove the indices
+        choice.setValue("export01");
+        indexItems = (COSArray) choice.getCOSObject().getItem(COSName.I);
+        assertNull(indexItems);        
+    }
+
+    @Test
+    void testMultiselect() throws IOException
+    {
+        // add test data
+        choice.setOptions(exportValues);
+
+        // ensure that the choice field doesn't allow multiple selections
+        choice.setMultiSelect(false);
+
+        // without multiselect setting multiple items shall fail
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> {
             choice.setValue(exportValues);
+        });
+
+	    assertEquals("The list box does not allow multiple selections.", exception.getMessage());      
             
-            // assert that the option values have been correctly set
-            COSArray valueItems = (COSArray) choice.getCOSObject().getItem(COSName.V);
-            assertNotNull(valueItems);
-            assertEquals(valueItems.size(),exportValues.size());
-            assertEquals(exportValues.get(0), valueItems.getString(0));
-            
-            // assert that the index values have been correctly set
-            COSArray indexItems = (COSArray) choice.getCOSObject().getItem(COSName.I);
-            assertNotNull(indexItems);
-            assertEquals(indexItems.size(),exportValues.size());
-            
-            // setting a single value shall remove the indices
-            choice.setValue("export01");
-            indexItems = (COSArray) choice.getCOSObject().getItem(COSName.I);
-            assertNull(indexItems);
-
-            // assert that the Opt entry is removed
-            choice.setOptions(null);
-            assertNull(choice.getCOSObject().getItem(COSName.OPT));
-            // if there is no Opt entry an empty List shall be returned
-            assertEquals(Collections.emptyList(), choice.getOptions());
-            
-            /*
-             * Test for setting export and display values
-             */
-            
-            // setting display and export value
-            choice.setOptions(exportValues, displayValues);
-            assertEquals(displayValues,choice.getOptionsDisplayValues());
-            assertEquals(exportValues,choice.getOptionsExportValues());
-            
-            /*
-             * Testing the sort option
-             */
-            assertEquals("display02", choice.getOptionsDisplayValues().get(0));
-            choice.setSort(true);
+        // ensure that the choice field does allow multiple selections
+        choice.setMultiSelect(true);
+        // now this call must succeed
+        choice.setValue(exportValues);
+    }
+
+    @Test
+    void testOptIsRemovedForNull()
+    {
+        // add test data
+        choice.setOptions(exportValues);
+        assertNotNull(choice.getCOSObject().getItem(COSName.OPT));
+        // assert that the Opt entry is removed
+        choice.setOptions(null);
+        assertNull(choice.getCOSObject().getItem(COSName.OPT));
+        // if there is no Opt entry an empty List shall be returned
+        assertEquals(Collections.emptyList(), choice.getOptions());
+    }
+
+    @Test
+    void testSetExportAndDisplay()
+    {
+        // setting display and export value
+        choice.setOptions(exportValues, displayValues);
+        assertEquals(displayValues,choice.getOptionsDisplayValues());
+        assertEquals(exportValues,choice.getOptionsExportValues());
+    }
+
+    @Test
+    void testSortOption()
+    {
+        // add test data
+        choice.setOptions(exportValues, displayValues);
+        assertEquals("display02", choice.getOptionsDisplayValues().get(0));
+
+        // test the sort option
+        choice.setSort(true);
+        choice.setOptions(exportValues, displayValues);
+        assertEquals("display01", choice.getOptionsDisplayValues().get(0));
+        assertEquals("display02", choice.getOptionsDisplayValues().get(1));
+        assertEquals("display03", choice.getOptionsDisplayValues().get(2));
+    }
+
+    @Test
+    void testEmptyOptionsNotNull()
+    {
+        // assert that the Opt entry is removed
+        choice.setOptions(null, displayValues);
+        assertNull(choice.getCOSObject().getItem(COSName.OPT));
+            
+        // if there is no Opt entry an empty list shall be returned
+        assertEquals(Collections.emptyList(), choice.getOptions());
+        assertEquals(Collections.emptyList(), choice.getOptionsDisplayValues());
+        assertEquals(Collections.emptyList(), choice.getOptionsExportValues());
+    }
+
+    @Test
+    void testExceptionForDifferentNumberOfEntries()
+    {
+        // test that an IllegalArgumentException is thrown when export and display 
+        // value lists have different sizes
+        exportValues.remove(1);
+
+        // without multiselect setting multiple items shall fail
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> {
             choice.setOptions(exportValues, displayValues);
-            assertEquals("display01", choice.getOptionsDisplayValues().get(0));
-            assertEquals("display02", choice.getOptionsDisplayValues().get(1));
-            assertEquals("display03", choice.getOptionsDisplayValues().get(2));
-
-            /*
-             * Setting options with an empty list
-             */
-            // assert that the Opt entry is removed
-            choice.setOptions(null, displayValues);
-            assertNull(choice.getCOSObject().getItem(COSName.OPT));
-            
-            // if there is no Opt entry an empty list shall be returned
-            assertEquals(Collections.emptyList(), choice.getOptions());
-            assertEquals(Collections.emptyList(), choice.getOptionsDisplayValues());
-            assertEquals(Collections.emptyList(), choice.getOptionsExportValues());
-            
-            // test that an IllegalArgumentException is thrown when export and display 
-            // value lists have different sizes
-            exportValues.remove(1);
-            
-            try
-            {
-                choice.setOptions(exportValues, displayValues);
-                fail( "Missing exception" );
-            }
-            catch( IllegalArgumentException e )
-            {
-                assertEquals( 
-                        "The number of entries for exportValue and displayValue shall be the same.",
-                        e.getMessage() );
-            }
-        }
+        });
+        
+        assertEquals("The number of entries for exportValue and displayValue shall be the same.", exception.getMessage());  
+    }
+
+    @AfterEach
+    void tearDown()
+    {
+        IOUtils.closeQuietly(doc);
     }
 }
\ No newline at end of file