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.

FontFileReaderTestCase.java 9.3KB

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