1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */1718package org.apache.any23.validator;
1920import org.apache.any23.validator.rule.MetaNameMisuseFix;
21import org.apache.any23.validator.rule.MetaNameMisuseRule;
22import org.apache.xerces.dom.DocumentImpl;
23import org.junit.After;
24import org.junit.Assert;
25import org.junit.Before;
26import org.junit.Test;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
3132import java.io.ByteArrayOutputStream;
33import java.nio.charset.StandardCharsets;
3435/**36 * Test case for {@link XMLValidationReportSerializer}.37 *38 * @author Michele Mostarda (mostarda@fbk.eu)39 */40publicclassXMLValidationReportSerializerTest {
4142privatestaticfinal Logger logger = LoggerFactory.getLogger(XMLValidationReportSerializerTest.class);
4344private XMLValidationReportSerializer serializer;
4546 @Before
47publicvoid setUp() {
48 serializer = new XMLValidationReportSerializer();
49 }
5051 @After
52publicvoid tearDown() {
53 serializer = null;
54 }
5556 @Test
57publicvoid testSerializeEmptyReport() throws SerializationException {
58 ValidationReport emptyReport = EmptyValidationReport.getInstance();
59 ByteArrayOutputStream baos = new ByteArrayOutputStream();
60 serializer.serialize(emptyReport, baos);
6162 Assert.assertTrue(baos.size() > 0);
63 }
6465 @Test
66publicvoid testSerialize() throws SerializationException, IllegalAccessException, InstantiationException {
67 ValidationReportBuilder validationReportBuilder = new DefaultValidationReportBuilder();
6869 Document document = new DocumentImpl();
70 Element element = document.createElement("html");
71 validationReportBuilder.reportIssue(ValidationReport.IssueLevel.INFO, "Test message", element);
7273 validationReportBuilder.traceRuleActivation(new MetaNameMisuseRule());
7475 validationReportBuilder.reportRuleError(new MetaNameMisuseRule(), new RuntimeException("Fake exc message"),
76"Fake message");
7778 validationReportBuilder.reportFixError(new MetaNameMisuseFix(), new RuntimeException("Fake exc message"),
79"Fake message");
8081 ValidationReport vr = validationReportBuilder.getReport();
82 ByteArrayOutputStream baos = new ByteArrayOutputStream();
83 serializer.serialize(vr, baos);
84 logger.debug(baos.toString(StandardCharsets.UTF_8));
8586final 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 }
9899 }