View Javadoc

    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
    18  package org.apache.any23.extractor.html;
    19
    20  import org.junit.Assert;
    21  import org.junit.Test;
    22
    23  import java.io.ByteArrayInputStream;
    24  import java.io.IOException;
    25  import java.io.InputStream;
    26  import java.nio.charset.StandardCharsets;
    27
    28  /**
    29   * Test case for {@link SpanCloserInputStream}.
    30   *
    31   * @author Michele Mostarda (mostarda@fbk.eu)
    32   */
    33  public class SpanCloserInputStreamTest {
    34
    35      @Test
    36      public void testSpanPositiveReplacement() throws IOException {
    37          processInput("pre<span attr1=\"value\" attr2/>post", "pre<span attr1=\"value\" attr2></span>post");
    38      }
    39
    40      @Test
    41      public void testSpanNegativeReplacement() throws IOException {
    42          processInput("pre<span attr1=\"value\" attr2>mid</span>post", "pre<span attr1=\"value\" attr2>mid</span>post");
    43      }
    44
    45      @Test
    46      public void testSubsequentSpanReplacement() throws IOException {
    47          processInput("<span/><span/><span a=\"v\"/><span/>",
    48                  "<span></span><span></span><span a=\"v\"></span><span></span>");
    49          processInput("<span name=\"span1\"/><span name=\"span2\"/>",
    50                  "<span name=\"span1\"></span><span name=\"span2\"></span>");
    51      }
    52
    53      @Test
    54      public void testNestedSpanReplacement() throws IOException {
    55          processInput("<span name=\"outer\"><span name=\"inner\"/></span>",
    56                  "<span name=\"outer\"><span name=\"inner\"></span></span>");
    57          processInput("<span name=\"outer1\"><span name=\"outer2\"><span name=\"inner\"/></span></span>",
    58                  "<span name=\"outer1\"><span name=\"outer2\"><span name=\"inner\"></span></span></span>");
    59      }
    60
    61      @Test
    62      public void testMixedReplacement() throws IOException {
    63          processInput(
    64                  "<span name=\"outer1\">" + "<span name=\"outer2\">" + "<span name=\"inner1\"/>"
    65                          + "<span name=\"inner2\"></span>" + "<span a=\"inner3\"/>" + "</span>" + "</span>",
    66                  "<span name=\"outer1\">" + "<span name=\"outer2\">" + "<span name=\"inner1\"></span>"
    67                          + "<span name=\"inner2\"></span>" + "<span a=\"inner3\"></span>" + "</span>" + "</span>");
    68      }
    69
    70      @Test
    71      public void testRealSpanReplacement() throws IOException {
    72          processInput(
    73                  "<span about=\"#me\" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">\n"
    74                          + "  <span rel=\"foaf:homepage\" resource=\"http://richard.cyganiak.de/\" />\n"
    75                          + "  <span property=\"foaf:nick\" content=\"cygri\" />\n" + "</span>",
    76                  "<span about=\"#me\" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">\n"
    77                          + "  <span rel=\"foaf:homepage\" resource=\"http://richard.cyganiak.de/\" ></span>\n"
    78                          + "  <span property=\"foaf:nick\" content=\"cygri\" ></span>\n" + "</span>");
    79      }
    80
    81      @Test
    82      public void testCloserTransparency() throws IOException {
    83          final String TEST_FILE = "/html/encoding-test.html";
    84          final InputStream original = this.getClass().getResourceAsStream(TEST_FILE);
    85          final InputStream wrapped = new SpanCloserInputStream(this.getClass().getResourceAsStream(TEST_FILE));
    86          int intc;
    87          try {
    88              while ((intc = original.read()) != -1) {
    89                  Assert.assertEquals(intc, wrapped.read());
    90              }
    91          } finally {
    92              original.close();
    93              wrapped.close();
    94          }
    95      }
    96
    97      private void processInput(String inStr, String expected) throws IOException {
    98          final ByteArrayInputStream in = new ByteArrayInputStream(inStr.getBytes(StandardCharsets.UTF_8));
    99          @SuppressWarnings("resource")
    100         SpanCloserInputStream scis = new SpanCloserInputStream(in);
    101         int c;
    102         final StringBuilder sb = new StringBuilder();
    103         while ((c = scis.read()) != -1) {
    104             sb.append((char) c);
    105         }
    106         final String out = sb.toString();
    107         Assert.assertEquals("Unexpected replacement.", expected, out);
    108     }
    109 }