Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PDFNumberTestCase.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.IOException;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.junit.Assert.fail;
  24. /**
  25. * This test tests PDFNumber's doubleOut() methods.
  26. */
  27. public class PDFNumberTestCase extends PDFObjectTestCase {
  28. /**
  29. * Sets up the local variables, most of these are inherited from PDFObjectTestCase
  30. */
  31. @Before
  32. public void setUp() {
  33. pdfObjectUnderTest = new PDFNumber();
  34. pdfObjectUnderTest.setParent(parent);
  35. pdfObjectUnderTest.setDocument(doc);
  36. }
  37. /**
  38. * Tests PDFNumber.doubleOut().
  39. * @throws Exception if the test fails
  40. */
  41. @Test
  42. public void testDoubleOut1() throws Exception {
  43. //Default is 6 decimal digits
  44. assertEquals("0", PDFNumber.doubleOut(0.0f));
  45. assertEquals("0", PDFNumber.doubleOut(0.0000000000000000000123f));
  46. assertEquals("0.1", PDFNumber.doubleOut(0.1f));
  47. assertEquals("100", PDFNumber.doubleOut(100.0f));
  48. assertEquals("100", PDFNumber.doubleOut(99.99999999999999999999999f));
  49. //You'd expect 100.123456 here but DecimalFormat uses the BigDecimal.ROUND_HALF_EVEN
  50. //strategy. I don't know if that's a problem. The strange thing testDoubleOut2
  51. //seems to return the normally expected value. Weird.
  52. assertEquals("100.123459", PDFNumber.doubleOut(100.12345611111111f));
  53. assertEquals("-100.123459", PDFNumber.doubleOut(-100.12345611111111f));
  54. }
  55. /**
  56. * Tests PDFNumber.doubleOut().
  57. * @throws Exception if the test fails
  58. */
  59. public void testDoubleOut2() throws Exception {
  60. //4 decimal digits in this case
  61. assertEquals("0", PDFNumber.doubleOut(0.0f, 4));
  62. assertEquals("0", PDFNumber.doubleOut(0.0000000000000000000123f, 4));
  63. assertEquals("0.1", PDFNumber.doubleOut(0.1f, 4));
  64. assertEquals("100", PDFNumber.doubleOut(100.0f, 4));
  65. assertEquals("100", PDFNumber.doubleOut(99.99999999999999999999999f, 4));
  66. assertEquals("100.1234", PDFNumber.doubleOut(100.12341111111111f, 4));
  67. assertEquals("-100.1234", PDFNumber.doubleOut(-100.12341111111111f, 4));
  68. }
  69. /**
  70. * Tests PDFNumber.doubleOut().
  71. * @throws Exception if the test fails
  72. */
  73. public void testDoubleOut3() throws Exception {
  74. //0 decimal digits in this case
  75. assertEquals("0", PDFNumber.doubleOut(0.0f, 0));
  76. assertEquals("0", PDFNumber.doubleOut(0.1f, 0));
  77. assertEquals("1", PDFNumber.doubleOut(0.6f, 0));
  78. assertEquals("100", PDFNumber.doubleOut(100.1234f, 0));
  79. assertEquals("-100", PDFNumber.doubleOut(-100.1234f, 0));
  80. }
  81. /**
  82. * Tests PDFNumber.doubleOut(). Special cases (former bugs).
  83. * @throws Exception if the test fails
  84. */
  85. public void testDoubleOut4() throws Exception {
  86. double d = Double.parseDouble("5.7220458984375E-6");
  87. assertEquals("0.000006", PDFNumber.doubleOut(d));
  88. assertEquals("0", PDFNumber.doubleOut(d, 4));
  89. assertEquals("0.00000572", PDFNumber.doubleOut(d, 8));
  90. }
  91. /**
  92. * Tests PDFNumber.doubleOut(). Tests for wrong parameters.
  93. * @throws Exception if the test fails
  94. */
  95. public void testDoubleOutWrongParameters() throws Exception {
  96. try {
  97. PDFNumber.doubleOut(0.1f, -1);
  98. fail("IllegalArgument expected!");
  99. } catch (IllegalArgumentException iae) {
  100. //we want that
  101. }
  102. try {
  103. PDFNumber.doubleOut(0.1f, 17); //We support max 16 decimal digits
  104. fail("IllegalArgument expected!");
  105. } catch (IllegalArgumentException iae) {
  106. //we want that
  107. }
  108. try {
  109. PDFNumber.doubleOut(0.1f, 98274659);
  110. fail("IllegalArgument expected!");
  111. } catch (IllegalArgumentException iae) {
  112. //we want that
  113. }
  114. try {
  115. PDFNumber.doubleOut(null);
  116. fail("NullPointer expected!");
  117. } catch (NullPointerException e) {
  118. // PASS
  119. }
  120. }
  121. /**
  122. * Tests both getNumber() and setNumber() - basic getter/setter methods... Why there isn't a
  123. * constructor is beyond me...
  124. */
  125. public void testGetSetNumber() {
  126. PDFNumber pdfNum = new PDFNumber();
  127. // Check with a floating point number
  128. pdfNum.setNumber(1.111f);
  129. assertEquals(1.111f, pdfNum.getNumber());
  130. // try with an int
  131. pdfNum.setNumber(2);
  132. assertEquals(2, pdfNum.getNumber());
  133. // See what happens with a null... make sure it doesn't explode
  134. pdfNum.setNumber(null);
  135. assertEquals(null, pdfNum.getNumber());
  136. }
  137. /**
  138. * Tests toPDFString() - this serializes PDFNumber to PDF format.
  139. * @throws IOException error caused by I/O
  140. */
  141. public void testToPDFString() throws IOException {
  142. PDFNumber testSubject = new PDFNumber();
  143. testSubject.setNumber(1.0001);
  144. testOutputStreams("1.0001", testSubject);
  145. testSubject.setNumber(999);
  146. testOutputStreams("999", testSubject);
  147. }
  148. }