org.wildfly.test.distribution.validation.AbstractValidationUnitTest Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.test.distribution.validation;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.jboss.metadata.parser.util.XMLResourceResolver;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* Date: 23.06.2011
*
* @author James R. Perkins
*/
public class AbstractValidationUnitTest {
private static final String SCHEMAS_LOCATION = "docs/schema";
private static final String JBOSS_DIST_PROP_NAME = "jboss.dist";
private static final String FUTURE_SCHEMA_PROP_NAME = "jboss.test.xml.validation.future.schemas";
private static final Set EXCLUDED_SCHEMA_FILES = new HashSet<>();
private static final Set FUTURE_SCHEMA_FILES = new HashSet<>();
private static final Map JBOSS_SCHEMAS_MAP = new HashMap<>();
private static final Map CURRENT_JBOSS_SCHEMAS_MAP = new HashMap<>();
private static final Source[] SCHEMA_SOURCES;
private static final Map NAMESPACE_MAP = new HashMap<>();
private static final Map OUTDATED_NAMESPACES = new HashMap<>();
private static final File JBOSS_DIST_DIR;
static {
// exclude JBoss Jakarta Enterprise Beans specific files which redefine the javaee namespace
// triggering the https://issues.apache.org/jira/browse/XERCESJ-1130 bug
EXCLUDED_SCHEMA_FILES.add("jboss-ejb3-2_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb3-2_1.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb3-spec-2_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb3-spec-2_1.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-cache_1_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-container-interceptors_1_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-delivery-active_1_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-delivery-active_1_1.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-delivery-active_1_2.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-clustering_1_1.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-iiop_1_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-iiop_1_1.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-iiop_1_2.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-pool_1_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-resource-adapter-binding_1_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-security_1_0.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-security_1_1.xsd");
EXCLUDED_SCHEMA_FILES.add("jboss-ejb-security-role_1_0.xsd");
String coreVersion = System.getProperty("version.org.wildfly.core");
if (coreVersion != null) {
// We are testing a different version of core than was used in creating our standard configs
// See if we are configured to specially handle newer schema versions in that core
String excluded = System.getProperty(FUTURE_SCHEMA_PROP_NAME);
if (excluded != null) {
excluded = excluded.trim();
String[] pairs = excluded.split(",");
for (String pair : pairs) {
if (pair.length() > 0) {
// The format is /
String[] tuple = pair.split("/");
// We only care about the pair if the bit matches the
// value of the version.org.wildfly.core system property. This
// way if someone sets -Djboss.test.xml.validation.future.schemas
// in a CI test setup and then forgets to update the setup when
// the relevant core version gets released and integrated, the
// setting will no longer be effective and the no longer "future"
// xsd will get tested normally.
if (tuple.length == 2 && coreVersion.equals(tuple[0])) {
FUTURE_SCHEMA_FILES.add(tuple[1]);
}
}
}
}
}
NAMESPACE_MAP.put("http://java.sun.com/xml/ns/javaee/javaee_6.xsd", "schema/javaee_6.xsd");
NAMESPACE_MAP.put("http://www.w3.org/2001/xml.xsd", "schema/xml.xsd");
NAMESPACE_MAP.put("http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd", "schema/ejb-jar_3_1.xsd");
NAMESPACE_MAP.put("http://www.jboss.org/j2ee/schema/jboss-common_7_0.xsd", "jboss-common_7_0.xsd");
String asDir = System.getProperty(JBOSS_DIST_PROP_NAME);
if (null == asDir) {
JBOSS_DIST_DIR = null;
} else {
JBOSS_DIST_DIR = new File(asDir);
if (!JBOSS_DIST_DIR.exists())
throw new IllegalStateException("Directory set in '" + JBOSS_DIST_PROP_NAME + "' does not exist: " + JBOSS_DIST_DIR.getAbsolutePath());
final File schemaDir = new File(JBOSS_DIST_DIR, SCHEMAS_LOCATION);
final File[] xsds = schemaDir.listFiles(new SchemaFilter(EXCLUDED_SCHEMA_FILES.toArray(new String[0])));
for (File xsd : xsds) {
JBOSS_SCHEMAS_MAP.put(xsd.getName(), xsd);
}
Map mostRecentVersions = new HashMap<>();
Map mostRecentNames = new HashMap<>();
Pattern pattern = Pattern.compile("(.*?)_(\\d+)_(\\d+).xsd");
for(Map.Entry entry : JBOSS_SCHEMAS_MAP.entrySet()) {
if (FUTURE_SCHEMA_FILES.contains(entry.getKey())) {
// not "current"; it's future.
continue;
}
final Matcher match = pattern.matcher(entry.getKey());
if(!match.matches()) {
continue;
}
String name = match.group(1);
String major = match.group(2);
String minor = match.group(3);
BigDecimal version = new BigDecimal(major + "." + minor);
BigDecimal current = mostRecentVersions.get(name);
if(current == null || version.compareTo(current) > 0) {
mostRecentVersions.put(name, version);
mostRecentNames.put(name, entry.getKey());
}
}
for (Map.Entry entry : JBOSS_SCHEMAS_MAP.entrySet()) {
if (FUTURE_SCHEMA_FILES.contains(entry.getKey())) {
// not "current" or "outdated"; it's future.
continue;
}
final Matcher match = pattern.matcher(entry.getKey());
if (!match.matches()) {
continue;
}
String name = match.group(1);
if (!mostRecentNames.get(name).equals(entry.getKey())) {
OUTDATED_NAMESPACES.put(entry.getKey(), mostRecentNames.get(name));
} else {
CURRENT_JBOSS_SCHEMAS_MAP.put(entry.getKey(), entry.getValue());
}
}
}
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy