You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFNameTestCase.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.fail;
  25. import org.apache.commons.io.output.CountingOutputStream;
  26. /**
  27. * Test class for {@link PDFName}.
  28. */
  29. public class PDFNameTestCase extends PDFObjectTestCase {
  30. private PDFName pdfName;
  31. /**
  32. * Sets up the local variables
  33. */
  34. @Before
  35. public void setUp() {
  36. pdfName = new PDFName("TestName");
  37. pdfName.setParent(parent);
  38. pdfName.setDocument(doc);
  39. pdfObjectUnderTest = pdfName;
  40. }
  41. /**
  42. * Tests escapeName() - tests that this method escapes the necessary characters.
  43. */
  44. @Test
  45. public void testEscapeName() {
  46. try {
  47. // Test for null, this is a programming error thus the NPE
  48. PDFName.escapeName(null);
  49. fail("NPE not thrown when null object given to escapeName()");
  50. } catch (NullPointerException e) {
  51. // PASS
  52. }
  53. // All names are prefixed by "/", check the PDF spec for further details.
  54. assertEquals("/Test", PDFName.escapeName("Test"));
  55. // Check that if the name is already prefixed with "/" it doens't do it twice
  56. assertEquals("/Test", PDFName.escapeName("/Test"));
  57. // Test with a space in the middle
  58. assertEquals("/Test#20test", PDFName.escapeName("Test test"));
  59. // Test that all chars apart from ASCII '!' --> '~' are escaped
  60. nonEscapedCharactersTests();
  61. escapedCharactersTests();
  62. }
  63. private void escapedCharactersTests() {
  64. for (char i = 0; i < '!'; i++) {
  65. String str = Integer.toHexString(i >>> 4 & 0x0f).toUpperCase();
  66. str += Integer.toHexString(i & 0x0f).toUpperCase();
  67. assertEquals("/#" + str, PDFName.escapeName(String.valueOf(i)));
  68. }
  69. for (char i = '~' + 1; i < 256; i++) {
  70. String str = Integer.toHexString(i >>> 4 & 0x0f).toUpperCase();
  71. str += Integer.toHexString(i & 0x0f).toUpperCase();
  72. assertEquals("/#" + str, PDFName.escapeName(String.valueOf(i)));
  73. }
  74. checkCharacterIsEscaped('#');
  75. checkCharacterIsEscaped('%');
  76. checkCharacterIsEscaped('(');
  77. checkCharacterIsEscaped(')');
  78. checkCharacterIsEscaped('<');
  79. checkCharacterIsEscaped('>');
  80. checkCharacterIsEscaped('[');
  81. checkCharacterIsEscaped(']');
  82. checkCharacterIsEscaped('>');
  83. }
  84. private void checkCharacterIsEscaped(char c) {
  85. String str = Integer.toHexString(c >>> 4 & 0x0f).toUpperCase();
  86. str += Integer.toHexString(c & 0x0f).toUpperCase();
  87. assertEquals("/#" + str, PDFName.escapeName(String.valueOf(c)));
  88. }
  89. private void nonEscapedCharactersTests() {
  90. charactersNotEscapedBetween('!', '"');
  91. charactersNotEscapedBetween('*', ';');
  92. charactersNotEscapedBetween('?', 'Z');
  93. charactersNotEscapedBetween('^', '~');
  94. }
  95. private void charactersNotEscapedBetween(char c1, char c2) {
  96. for (char i = c1; i <= c2; i++) {
  97. String str = String.valueOf(i);
  98. String expected = !str.equals("/") ? "/" + str : str;
  99. assertEquals(expected, PDFName.escapeName(str));
  100. }
  101. }
  102. /**
  103. * Tests toString() - this has been overridden to return the String that PDFName wraps.
  104. */
  105. @Test
  106. public void testToString() {
  107. // The escape characters have already been tested in testEscapeName() so this doesn't need
  108. // to be done twice.
  109. PDFName test1 = new PDFName("test1");
  110. assertEquals("/test1", test1.toString());
  111. PDFName test2 = new PDFName("another test");
  112. assertEquals("/another#20test", test2.toString());
  113. try {
  114. new PDFName(null);
  115. fail("NPE not thrown when null passed to constructor");
  116. } catch (NullPointerException e) {
  117. // PASS
  118. }
  119. }
  120. /**
  121. * Tests output() - check that this object can stream itself in the correct format.
  122. * @throws IOException error caused by I/O
  123. */
  124. @Test
  125. public void testOutput() throws IOException {
  126. testOutputStreams("/TestName", pdfName);
  127. testOutputStreams("/test#20test", new PDFName("test test"));
  128. }
  129. /**
  130. * Test outputInline() - this writes the object reference if it is a direct object (has an
  131. * object number), or writes the String representation if there is no object number.
  132. */
  133. @Test
  134. public void testOutputInline() {
  135. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  136. CountingOutputStream cout = new CountingOutputStream(outStream);
  137. StringBuilder textBuffer = new StringBuilder();
  138. try {
  139. // test with no object number set.
  140. pdfName.outputInline(outStream, textBuffer);
  141. PDFDocument.flushTextBuffer(textBuffer, cout);
  142. assertEquals("/TestName", outStream.toString());
  143. outStream.reset();
  144. // test with object number set
  145. pdfName.setObjectNumber(1);
  146. pdfName.outputInline(outStream, textBuffer);
  147. PDFDocument.flushTextBuffer(textBuffer, cout);
  148. assertEquals("1 0 R", outStream.toString());
  149. } catch (IOException e) {
  150. fail("IOException: " + e.getMessage());
  151. }
  152. }
  153. }