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.

PSTTFGlyphOutputStreamTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 static org.mockito.Mockito.inOrder;
  20. import static org.mockito.Mockito.mock;
  21. import static org.mockito.Mockito.times;
  22. import static org.mockito.Mockito.verify;
  23. import java.io.IOException;
  24. import junit.framework.TestCase;
  25. import org.mockito.InOrder;
  26. /**
  27. * Test class for PSTTFGlyphOutputStream
  28. */
  29. public class PSTTFGlyphOutputStreamTest extends TestCase {
  30. private PSTTFGenerator mockGen;
  31. private PSTTFGlyphOutputStream glyphOut;
  32. @Override
  33. public void setUp() {
  34. mockGen = mock(PSTTFGenerator.class);
  35. glyphOut = new PSTTFGlyphOutputStream(mockGen);
  36. }
  37. /**
  38. * Test startGlyphStream() - test that startGlyphStream() invokes reset() and startString() in
  39. * PSTTFGenerator.
  40. * @exception IOException file write error
  41. */
  42. public void testStartGlyphStream() throws IOException {
  43. glyphOut.startGlyphStream();
  44. verify(mockGen).startString();
  45. }
  46. /**
  47. * Test streamGlyph(byte[],int,int) - tests several paths:
  48. * 1) strings are properly appended
  49. * 2) when total strings size > PSTTFGenerator.MAX_BUFFER_SIZE, the strings is closed and a new
  50. * strings is started.
  51. * 3) if a glyph of size > PSTTFGenerator.MAX_BUFFER_SIZE is attempted, an exception is thrown.
  52. * @throws IOException file write error.
  53. */
  54. public void testStreamGlyph() throws IOException {
  55. int byteArraySize = 10;
  56. byte[] byteArray = new byte[byteArraySize];
  57. int runs = 100;
  58. for (int i = 0; i < runs; i++) {
  59. glyphOut.streamGlyph(byteArray, 0, byteArraySize);
  60. }
  61. verify(mockGen, times(runs)).streamBytes(byteArray, 0, byteArraySize);
  62. /*
  63. * We want to run this for MAX_BUFFER_SIZE / byteArraySize so that go over the string
  64. * boundary and enforce the ending and starting of a new string. Using mockito to ensure
  65. * that this behaviour is performed in order (since this is an integral behavioural aspect)
  66. */
  67. int stringLimit = PSTTFGenerator.MAX_BUFFER_SIZE / byteArraySize;
  68. for (int i = 0; i < stringLimit; i++) {
  69. glyphOut.streamGlyph(byteArray, 0, byteArraySize);
  70. }
  71. InOrder inOrder = inOrder(mockGen);
  72. inOrder.verify(mockGen, times(stringLimit)).streamBytes(byteArray, 0, byteArraySize);
  73. inOrder.verify(mockGen).endString();
  74. inOrder.verify(mockGen).startString();
  75. inOrder.verify(mockGen, times(runs)).streamBytes(byteArray, 0, byteArraySize);
  76. try {
  77. glyphOut.streamGlyph(byteArray, 0, PSTTFGenerator.MAX_BUFFER_SIZE + 1);
  78. fail("Shouldn't allow a length > PSTTFGenerator.MAX_BUFFER_SIZE");
  79. } catch (UnsupportedOperationException e) {
  80. // PASS
  81. }
  82. }
  83. /**
  84. * Test endGlyphStream() - tests that PSTTFGenerator.endString() is invoked when this method
  85. * is called.
  86. * @throws IOException file write exception
  87. */
  88. public void testEndGlyphStream() throws IOException {
  89. glyphOut.endGlyphStream();
  90. verify(mockGen).endString();
  91. }
  92. }