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.

PSTTFGeneratorTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.render.ps.fonts;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import junit.framework.TestCase;
  22. import org.apache.xmlgraphics.ps.PSGenerator;
  23. /**
  24. * The test class for org.apache.fop.render.ps.fonts.PSGenerator
  25. */
  26. public class PSTTFGeneratorTest extends TestCase {
  27. private PSTTFGenerator ttfGen;
  28. private ByteArrayOutputStream out = new ByteArrayOutputStream();
  29. private PSGenerator gen = new PSGenerator(out);
  30. private byte[] byteArray;
  31. /**
  32. * Constructor
  33. */
  34. public PSTTFGeneratorTest() {
  35. byteArray = new byte[65536];
  36. for (int i = 0; i < 65536; i++) {
  37. byteArray[i] = (byte) i;
  38. }
  39. }
  40. @Override
  41. public void setUp() {
  42. ttfGen = new PSTTFGenerator(gen);
  43. }
  44. /**
  45. * Tests startString() - starts the string in an appropriate way for a PostScript file.
  46. * @exception IOException write error
  47. */
  48. public void testStartString() throws IOException {
  49. ttfGen.startString();
  50. assertEquals("<\n", out.toString());
  51. }
  52. /**
  53. * Test streamBytes() - tests that strings are written to file in the proper format.
  54. * @throws IOException write error.
  55. */
  56. public void testStreamBytes() throws IOException {
  57. ttfGen.streamBytes(byteArray, 0, 16);
  58. assertEquals("000102030405060708090A0B0C0D0E0F", out.toString());
  59. /*
  60. * 65520 is the closes multiple of 80 to 65535 (max string size in PS document) and since
  61. * one byte takes up two characters, 65520 / 2 - 16 (16 bytes already written)= 32744.
  62. */
  63. ttfGen.streamBytes(byteArray, 0, 32744);
  64. // Using a regex to ensure that the format is correct
  65. assertTrue(out.toString().matches("([0-9A-F]{80}\n){819}"));
  66. try {
  67. ttfGen.streamBytes(byteArray, 0, PSTTFGenerator.MAX_BUFFER_SIZE + 1);
  68. fail("Shouldn't be able to write more than MAX_BUFFER_SIZE to a PS document");
  69. } catch (UnsupportedOperationException e) {
  70. // PASS
  71. }
  72. }
  73. /**
  74. * Test reset() - reset should reset the line counter such that when reset() is invoked the
  75. * following string streamed to the PS document should be 80 chars long.
  76. * @throws IOException file write error.
  77. */
  78. public void testReset() throws IOException {
  79. ttfGen.streamBytes(byteArray, 0, 40);
  80. assertTrue(out.toString().matches("([0-9A-F]{80}\n)"));
  81. ttfGen.streamBytes(byteArray, 0, 40);
  82. assertTrue(out.toString().matches("([0-9A-F]{80}\n){2}"));
  83. }
  84. /**
  85. * Test endString() - ensures strings are ended in the PostScript document in the correct
  86. * format, a "00" needs to be appended to the end of a string.
  87. * @throws IOException file write error
  88. */
  89. public void testEndString() throws IOException {
  90. ttfGen.endString();
  91. assertEquals("00\n> ", out.toString());
  92. out.reset();
  93. // we need to check that this doesn't write more than 80 chars per line
  94. ttfGen.streamBytes(byteArray, 0, 40);
  95. ttfGen.endString();
  96. assertTrue(out.toString().matches("([0-9A-F]{80}\n)00\n> "));
  97. }
  98. }