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.

PSTTFGlyphOutputStreamTestCase.java 3.9KB

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