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.

FontFileReaderTest.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.fonts.truetype;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.EOFException;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Arrays;
  24. import junit.framework.TestCase;
  25. /**
  26. * A test class for org.apache.fop.truetype.FontFileReader
  27. */
  28. public class FontFileReaderTest extends TestCase {
  29. private FontFileReader fontReader;
  30. private final InputStream in;
  31. private final byte[] byteArray;
  32. /**
  33. * Constructor - initialises an array that only needs to be created once. It creates a byte[]
  34. * of form { 0x00, 0x01, 0x02, 0x03..., 0xff};
  35. */
  36. public FontFileReaderTest() {
  37. byteArray = new byte[256];
  38. for (int i = 0; i < 256; i++) {
  39. byteArray[i] = (byte) i;
  40. }
  41. in = new ByteArrayInputStream(byteArray);
  42. }
  43. /**
  44. * sets up the test subject object for testing.
  45. */
  46. public void setUp() {
  47. try {
  48. fontReader = new FontFileReader(in);
  49. } catch (Exception e) {
  50. fail("Error: " + e.getMessage());
  51. }
  52. }
  53. /**
  54. * the "destructor" method.
  55. *
  56. */
  57. public void tearDown() {
  58. fontReader = null;
  59. }
  60. /**
  61. * Test readTTFByte()
  62. * @throws IOException exception
  63. */
  64. public void testReadTTFByte() throws IOException {
  65. for (int i = 0; i < 256; i++) {
  66. assertEquals((byte) i, fontReader.readTTFByte());
  67. }
  68. }
  69. /**
  70. * Test seekSet() - check that it moves to the correct position and enforce a failure case.
  71. * @throws IOException exception
  72. */
  73. public void testSeekSet() throws IOException {
  74. fontReader.seekSet(10);
  75. assertEquals(10, fontReader.readTTFByte());
  76. try {
  77. fontReader.seekSet(257);
  78. fail("FileFontReaderTest Failed testSeekSet");
  79. } catch (IOException e) {
  80. // Passed
  81. }
  82. }
  83. /**
  84. * Test skip() - check that it moves to the correct position and enforce a failure case.
  85. * @throws IOException exception
  86. */
  87. public void testSkip() throws IOException {
  88. fontReader.skip(100);
  89. assertEquals(100, fontReader.readTTFByte());
  90. try {
  91. // 100 (seekAdd) + 1 (read() = 1 byte) + 156 = 257
  92. fontReader.skip(156);
  93. fail("FileFontReaderTest Failed testSkip");
  94. } catch (IOException e) {
  95. // Passed
  96. }
  97. }
  98. /**
  99. * Test getCurrentPos() - 3 checks:
  100. * 1) test with seekSet(int)
  101. * 2) test with skip(int)
  102. * 3) test with a readTTFByte() (this moves the position by the size of the data being read)
  103. * @throws IOException exception
  104. */
  105. public void testGetCurrentPos() throws IOException {
  106. fontReader.seekSet(10);
  107. fontReader.skip(100);
  108. assertEquals(110, fontReader.getCurrentPos());
  109. fontReader.readTTFByte();
  110. assertEquals(111, fontReader.getCurrentPos());
  111. }
  112. /**
  113. * Test getFileSize()
  114. */
  115. public void testGetFileSize() {
  116. assertEquals(256, fontReader.getFileSize());
  117. }
  118. /**
  119. * Test readTTFUByte()
  120. * @throws IOException exception
  121. */
  122. public void testReadTTFUByte() throws IOException {
  123. for (int i = 0; i < 256; i++) {
  124. assertEquals(i, fontReader.readTTFUByte());
  125. }
  126. }
  127. /**
  128. * Test readTTFShort() - Test positive and negative numbers (two's compliment).
  129. * @throws IOException exception
  130. */
  131. public void testReadTTFShort() throws IOException {
  132. // 0x0001 = 1
  133. assertEquals("Should have been 1 (0x0001)", 1, fontReader.readTTFShort());
  134. // 0x0203 = 515
  135. assertEquals(515, fontReader.readTTFShort());
  136. // now test negative numbers
  137. fontReader.seekSet(250);
  138. // 0xfafb
  139. assertEquals(-1285, fontReader.readTTFShort());
  140. }
  141. /**
  142. * Test readTTFUShort() - Test positive and potentially negative numbers (two's compliment).
  143. * @throws IOException exception
  144. */
  145. public void testReadTTFUShort() throws IOException {
  146. // 0x0001
  147. assertEquals(1, fontReader.readTTFUShort());
  148. // 0x0203
  149. assertEquals(515, fontReader.readTTFUShort());
  150. // test potential negatives
  151. fontReader.seekSet(250);
  152. // 0xfafb
  153. assertEquals((250 << 8) + 251, fontReader.readTTFUShort());
  154. }
  155. /**
  156. * Test readTTFShort(int) - test reading ahead of current position and behind current position
  157. * and in both cases ensure that our current position isn't changed.
  158. * @throws IOException exception
  159. */
  160. public void testReadTTFShortWithArg() throws IOException {
  161. // 0x6465
  162. assertEquals(25701, fontReader.readTTFShort(100));
  163. assertEquals(0, fontReader.getCurrentPos());
  164. // read behind current position (and negative)
  165. fontReader.seekSet(255);
  166. // 0xfafb
  167. assertEquals(-1285, fontReader.readTTFShort(250));
  168. assertEquals(255, fontReader.getCurrentPos());
  169. }
  170. /**
  171. * Test readTTFUShort(int arg) - test reading ahead of current position and behind current
  172. * position and in both cases ensure that our current position isn't changed.
  173. * @throws IOException exception
  174. */
  175. public void testReadTTFUShortWithArg() throws IOException {
  176. // 0x6465
  177. assertEquals(25701, fontReader.readTTFUShort(100));
  178. assertEquals(0, fontReader.getCurrentPos());
  179. // read behind current position (and potential negative)
  180. fontReader.seekSet(255);
  181. // 0xfafb
  182. assertEquals(64251, fontReader.readTTFUShort(250));
  183. assertEquals(255, fontReader.getCurrentPos());
  184. }
  185. /**
  186. * Test readTTFLong()
  187. * @throws IOException exception
  188. */
  189. public void testReadTTFLong() throws IOException {
  190. // 0x00010203
  191. assertEquals(66051, fontReader.readTTFLong());
  192. // test negative numbers
  193. fontReader.seekSet(250);
  194. // 0xf0f1f2f3
  195. assertEquals(-84148995, fontReader.readTTFLong());
  196. }
  197. /**
  198. * Test readTTFULong()
  199. * @throws IOException exception
  200. */
  201. public void testReadTTFULong() throws IOException {
  202. // 0x00010203
  203. assertEquals(66051, fontReader.readTTFULong());
  204. // test negative numbers
  205. fontReader.seekSet(250);
  206. // 0xfafbfcfd
  207. assertEquals(4210818301L, fontReader.readTTFULong());
  208. }
  209. /**
  210. * Test readTTFString() - there are two paths to test here:
  211. * 1) A null terminated string
  212. * 2) A string not terminated with a null (we expect this to throw an EOFException)
  213. * @throws IOException exception
  214. */
  215. public void testReadTTFString() throws IOException {
  216. byte[] strByte = {(byte)'t', (byte)'e', (byte)'s', (byte)'t', 0x00};
  217. fontReader = new FontFileReader(new ByteArrayInputStream(strByte));
  218. assertEquals("test", fontReader.readTTFString());
  219. try {
  220. // not NUL terminated
  221. byte[] strByteNoNull = {(byte)'t', (byte)'e', (byte)'s', (byte)'t'};
  222. fontReader = new FontFileReader(new ByteArrayInputStream(strByteNoNull));
  223. assertEquals("test", fontReader.readTTFString());
  224. fail("FontFileReaderTest testReadTTFString Fails.");
  225. } catch (EOFException e) {
  226. // Pass
  227. }
  228. }
  229. /**
  230. * Test readTTFString(int arg)
  231. * @throws IOException exception
  232. */
  233. public void testReadTTFStringIntArg() throws IOException {
  234. byte[] strByte = {(byte)'t', (byte)'e', (byte)'s', (byte)'t'};
  235. fontReader = new FontFileReader(new ByteArrayInputStream(strByte));
  236. assertEquals("test", fontReader.readTTFString(4));
  237. try {
  238. fontReader = new FontFileReader(new ByteArrayInputStream(strByte));
  239. assertEquals("test", fontReader.readTTFString(5));
  240. fail("FontFileReaderTest testReadTTFStringIntArg Fails.");
  241. } catch (EOFException e) {
  242. // Pass
  243. }
  244. }
  245. /**
  246. * Test readTTFString(int arg1, int arg2)
  247. */
  248. public void testReadTTFString2IntArgs() {
  249. // currently the same as above
  250. }
  251. /**
  252. * Test getBytes()
  253. * @throws IOException exception
  254. */
  255. public void testGetBytes() throws IOException {
  256. byte[] retrievedBytes = fontReader.getBytes(0, 256);
  257. assertTrue(Arrays.equals(byteArray, retrievedBytes));
  258. }
  259. }