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.

NoOperationTestCase.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.afp;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.fail;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.UnsupportedEncodingException;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.fop.afp.AFPConstants;
  27. import org.apache.fop.afp.parser.MODCAParser;
  28. import org.apache.fop.afp.parser.UnparsedStructuredField;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.junit.Test;
  31. /**
  32. * Tests generation of afp:no-operation (NOPs).
  33. */
  34. public class NoOperationTestCase extends AbstractAFPTestCase {
  35. /**
  36. * Tests afp:no-operation.
  37. * @throws Exception if an error occurs
  38. */
  39. @Test
  40. public void testNoOperation() throws Exception {
  41. FOUserAgent ua = fopFactory.newFOUserAgent();
  42. File outputFile = renderFile(ua, "nops.fo", "");
  43. InputStream in = new java.io.FileInputStream(outputFile);
  44. try {
  45. MODCAParser parser = new MODCAParser(in);
  46. UnparsedStructuredField field = skipTo(parser, 0xD3A8A8); //Begin Document
  47. //NOP in fo:declarations
  48. field = parser.readNextStructuredField();
  49. assertEquals(0xD3EEEE, field.getSfTypeID());
  50. assertEquals("fo:declarations", getNopText(field));
  51. field = parser.readNextStructuredField();
  52. assertEquals(0xD3A8AD, field.getSfTypeID()); //Begin Named Page Group
  53. //NOPs in fo:page-sequence
  54. field = parser.readNextStructuredField();
  55. assertEquals(0xD3EEEE, field.getSfTypeID());
  56. assertEquals("fo:page-sequence: start", getNopText(field));
  57. field = parser.readNextStructuredField();
  58. assertEquals(0xD3EEEE, field.getSfTypeID());
  59. assertEquals("fo:page-sequence: end", getNopText(field));
  60. field = parser.readNextStructuredField();
  61. assertEquals(0xD3A8AF, field.getSfTypeID()); //Begin Page
  62. field = skipTo(parser, 0xD3A9C9); //End Active Environment Group
  63. field = parser.readNextStructuredField();
  64. assertEquals(0xD3EEEE, field.getSfTypeID());
  65. assertEquals("fo:simple-page-master: first", getNopText(field));
  66. field = skipTo(parser, 0xD3A9C9); //End Active Environment Group
  67. field = parser.readNextStructuredField();
  68. assertEquals(0xD3EEEE, field.getSfTypeID());
  69. assertEquals("fo:simple-page-master: rest", getNopText(field));
  70. } finally {
  71. IOUtils.closeQuietly(in);
  72. }
  73. int counter = 0;
  74. in = new java.io.FileInputStream(outputFile);
  75. try {
  76. MODCAParser parser = new MODCAParser(in);
  77. while (true) {
  78. UnparsedStructuredField field = parser.readNextStructuredField();
  79. if (field == null) {
  80. break;
  81. }
  82. if (field.getSfTypeID() == 0xD3EEEE) {
  83. counter++;
  84. }
  85. }
  86. } finally {
  87. IOUtils.closeQuietly(in);
  88. }
  89. assertEquals(6, counter); //decl, 2 * ps, 3 * page/spm
  90. }
  91. private String getNopText(UnparsedStructuredField field) throws UnsupportedEncodingException {
  92. byte[] data = field.getData();
  93. String text = new String(data, AFPConstants.EBCIDIC_ENCODING);
  94. return text;
  95. }
  96. private UnparsedStructuredField skipTo(MODCAParser parser, int typeID) throws IOException {
  97. UnparsedStructuredField field = null;
  98. do {
  99. field = parser.readNextStructuredField();
  100. if (field.getSfTypeID() == typeID) {
  101. return field;
  102. }
  103. } while (field != null);
  104. fail("Structured field not found: " + Integer.toHexString(typeID));
  105. return null;
  106. }
  107. }