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.validator;
    19
    20  import org.apache.any23.validator.rule.MetaNameMisuseFix;
    21  import org.apache.any23.validator.rule.MetaNameMisuseRule;
    22  import org.apache.xerces.dom.DocumentImpl;
    23  import org.junit.After;
    24  import org.junit.Assert;
    25  import org.junit.Before;
    26  import org.junit.Test;
    27  import org.slf4j.Logger;
    28  import org.slf4j.LoggerFactory;
    29  import org.w3c.dom.Document;
    30  import org.w3c.dom.Element;
    31
    32  import java.io.ByteArrayOutputStream;
    33  import java.nio.charset.StandardCharsets;
    34
    35  /**
    36   * Test case for {@link XMLValidationReportSerializer}.
    37   *
    38   * @author Michele Mostarda (mostarda@fbk.eu)
    39   */
    40  public class XMLValidationReportSerializerTest {
    41
    42      private static final Logger logger = LoggerFactory.getLogger(XMLValidationReportSerializerTest.class);
    43
    44      private XMLValidationReportSerializer serializer;
    45
    46      @Before
    47      public void setUp() {
    48          serializer = new XMLValidationReportSerializer();
    49      }
    50
    51      @After
    52      public void tearDown() {
    53          serializer = null;
    54      }
    55
    56      @Test
    57      public void testSerializeEmptyReport() throws SerializationException {
    58          ValidationReport emptyReport = EmptyValidationReport.getInstance();
    59          ByteArrayOutputStream baos = new ByteArrayOutputStream();
    60          serializer.serialize(emptyReport, baos);
    61
    62          Assert.assertTrue(baos.size() > 0);
    63      }
    64
    65      @Test
    66      public void testSerialize() throws SerializationException, IllegalAccessException, InstantiationException {
    67          ValidationReportBuilder validationReportBuilder = new DefaultValidationReportBuilder();
    68
    69          Document document = new DocumentImpl();
    70          Element element = document.createElement("html");
    71          validationReportBuilder.reportIssue(ValidationReport.IssueLevel.INFO, "Test message", element);
    72
    73          validationReportBuilder.traceRuleActivation(new MetaNameMisuseRule());
    74
    75          validationReportBuilder.reportRuleError(new MetaNameMisuseRule(), new RuntimeException("Fake exc message"),
    76                  "Fake message");
    77
    78          validationReportBuilder.reportFixError(new MetaNameMisuseFix(), new RuntimeException("Fake exc message"),
    79                  "Fake message");
    80
    81          ValidationReport vr = validationReportBuilder.getReport();
    82          ByteArrayOutputStream baos = new ByteArrayOutputStream();
    83          serializer.serialize(vr, baos);
    84          logger.debug(baos.toString(StandardCharsets.UTF_8));
    85
    86          final String bufferContent = baos.toString(StandardCharsets.UTF_8);
    87          Assert.assertTrue(bufferContent.contains("<validationReport>"));
    88          Assert.assertTrue(bufferContent.contains("</validationReport>"));
    89          Assert.assertTrue(bufferContent.contains("<issue>"));
    90          Assert.assertTrue(bufferContent.contains("</issue>"));
    91          Assert.assertTrue(bufferContent.contains("<ruleActivation>"));
    92          Assert.assertTrue(bufferContent.contains("</ruleActivation>"));
    93          Assert.assertTrue(bufferContent.contains("<ruleError>"));
    94          Assert.assertTrue(bufferContent.contains("</ruleError>"));
    95          Assert.assertTrue(bufferContent.contains("<fixError>"));
    96          Assert.assertTrue(bufferContent.contains("</fixError>"));
    97      }
    98
    99  }