PdfCollectionSort extends PdfObjectWrapper<PdfDictionary> { public PdfCollectionSort setSortOrder(boolean ascending) { PdfObject obj = getPdfObject().get(PdfName.S); if (obj.isName()) { getPdfObject().put(PdfName.A, PdfBoolean.valueOf(ascending)); } else { throw new PdfException(PdfException.YouHaveToDefineABooleanArrayForThisCollectionSortDictionary); } return this; } PdfCollectionSort(PdfDictionary pdfObject);  PdfCollectionSort(String key);  PdfCollectionSort(String[] keys); PdfCollectionSort setSortOrder(boolean ascending); PdfCollectionSort setSortOrder(boolean[] ascending);  }
@Test public void sortOrderForOneKeyTest() { final String key = "testKey"; final boolean testAscending = true; PdfCollectionSort sort = new PdfCollectionSort(key); Assert.assertNull(sort.getPdfObject().get(PdfName.A)); sort.setSortOrder(testAscending); Assert.assertTrue(sort.getPdfObject().getAsBool(PdfName.A)); }
@Test public void incorrectSortOrderForOneKeyTest() { junitExpectedException.expect(PdfException.class); junitExpectedException.expectMessage(PdfException.YouNeedASingleBooleanForThisCollectionSortDictionary); final String key = "testKey"; final boolean[] testAscendings = {true, false}; PdfCollectionSort sort = new PdfCollectionSort(key); sort.setSortOrder(testAscendings); Assert.assertTrue(sort.getPdfObject().getAsBool(PdfName.A)); }
@Test public void sortOrderForMultipleKeysTest() { final String[] keys = { "testKey1", "testKey2", "testKey3"}; final boolean[] testAscendings = {true, false, true}; PdfCollectionSort sort = new PdfCollectionSort(keys); sort.setSortOrder(testAscendings); for(int i = 0; i < testAscendings.length; i++) { Assert.assertEquals(testAscendings[i], sort.getPdfObject().getAsArray(PdfName.A).getAsBoolean(i).getValue()); } }
@Test public void singleSortOrderForMultipleKeysTest() { junitExpectedException.expect(PdfException.class); junitExpectedException.expectMessage(PdfException.YouHaveToDefineABooleanArrayForThisCollectionSortDictionary); final String[] keys = { "testKey1", "testKey2", "testKey3"}; final boolean testAscending = true; PdfCollectionSort sort = new PdfCollectionSort(keys); sort.setSortOrder(testAscending); }
@Test public void incorrectMultipleSortOrderForMultipleKeysTest() { junitExpectedException.expect(PdfException.class); junitExpectedException.expectMessage(PdfException.NumberOfBooleansInTheArrayDoesntCorrespondWithTheNumberOfFields); final String[] keys = { "testKey1", "testKey2", "testKey3"}; final boolean[] testAscendings = {true, false}; PdfCollectionSort sort = new PdfCollectionSort(keys); sort.setSortOrder(testAscendings); }