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.

AFPResourceUtilTestCase.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.afp;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.Arrays;
  24. import org.junit.Test;
  25. import static org.junit.Assert.assertTrue;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.fop.afp.util.AFPResourceUtil;
  28. /**
  29. * Tests the {@link AFPResourceUtil} class.
  30. */
  31. public class AFPResourceUtilTestCase {
  32. private static final String RESOURCE_FILENAME = "expected_resource.afp";
  33. private static final String NAMED_RESOURCE_FILENAME = "expected_named_resource.afp";
  34. private static final String RESOURCE_ANY_NAME = "resource_any_name.afp";
  35. private static final String RESOURCE_NAME_MATCH = "resource_name_match.afp";
  36. private static final String RESOURCE_NAME_MISMATCH = "resource_name_mismatch.afp";
  37. private static final String RESOURCE_NO_END_NAME = "resource_no_end_name.afp";
  38. private static final String PSEG_A = "XFEATHER";
  39. private static final String PSEG_B = "S1CODEQR";
  40. /**
  41. * Tests copyResourceFile()
  42. * @throws Exception -
  43. */
  44. @Test
  45. public void testCopyResourceFile() throws Exception {
  46. compareResources(new ResourceCopier() {
  47. public void copy(InputStream in, OutputStream out) throws IOException {
  48. AFPResourceUtil.copyResourceFile(in, out);
  49. }
  50. }, RESOURCE_FILENAME, RESOURCE_FILENAME);
  51. }
  52. /**
  53. * Tests copyNamedResource()
  54. * @throws Exception -
  55. */
  56. @Test
  57. public void testCopyNamedResource() throws Exception {
  58. compareResources(new ResourceCopier() {
  59. public void copy(InputStream in, OutputStream out) throws IOException {
  60. AFPResourceUtil.copyNamedResource(PSEG_A, in, out);
  61. }
  62. }, RESOURCE_FILENAME, NAMED_RESOURCE_FILENAME);
  63. }
  64. private void compareResources(ResourceCopier copyResource, String resourceA, String resourceB)
  65. throws IOException {
  66. ByteArrayOutputStream baos = copyResource(resourceA, copyResource);
  67. byte[] expectedBytes = resourceAsByteArray(resourceB);
  68. assertTrue(Arrays.equals(expectedBytes, baos.toByteArray()));
  69. }
  70. private ByteArrayOutputStream copyResource(String resource, ResourceCopier resourceCopier)
  71. throws IOException {
  72. final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  73. InputStream in = null;
  74. try {
  75. in = getClass().getResourceAsStream(resource);
  76. resourceCopier.copy(in, baos);
  77. } finally {
  78. in.close();
  79. }
  80. return baos;
  81. }
  82. private byte[] resourceAsByteArray(String resource) throws IOException {
  83. InputStream in = null;
  84. byte[] expectedBytes = null;
  85. try {
  86. in = getClass().getResourceAsStream(resource);
  87. expectedBytes = IOUtils.toByteArray(in);
  88. } finally {
  89. in.close();
  90. }
  91. return expectedBytes;
  92. }
  93. /**
  94. * Tests the validity of a closing structured field having an FF FF name which
  95. * allows it to match any existing matching starting field
  96. * @throws Exception -
  97. */
  98. @Test
  99. public void testResourceAnyName() throws Exception {
  100. testResource(RESOURCE_ANY_NAME, PSEG_B);
  101. }
  102. /**
  103. * Tests a matching end structured field name
  104. * @throws Exception -
  105. */
  106. @Test
  107. public void testResourceNameMatch() throws Exception {
  108. testResource(RESOURCE_NAME_MATCH, PSEG_B);
  109. }
  110. /**
  111. * Tests to see whether a matching structured field pair with mismatching
  112. * names fails.
  113. * @throws Exception -
  114. */
  115. @Test(expected = Exception.class)
  116. public void testResourceNameMismatch() throws Exception {
  117. testResource(RESOURCE_NAME_MISMATCH, PSEG_B);
  118. }
  119. /**
  120. * Tests a matching structured end field with no name
  121. * @throws Exception -
  122. */
  123. @Test
  124. public void testResourceNoEndName() throws Exception {
  125. testResource(RESOURCE_NO_END_NAME, PSEG_B);
  126. }
  127. private void testResource(String resource, final String pseg) throws Exception {
  128. copyResource(resource, new ResourceCopier() {
  129. public void copy(InputStream in, OutputStream out) throws IOException {
  130. AFPResourceUtil.copyNamedResource(pseg, in, out);
  131. }
  132. });
  133. }
  134. private interface ResourceCopier {
  135. void copy(InputStream in, OutputStream out) throws IOException;
  136. }
  137. }