|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + * info@exist-db.org |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.util; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | +import org.w3c.dom.Document; |
| 26 | + |
| 27 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.file.FileVisitResult; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.nio.file.SimpleFileVisitor; |
| 33 | +import java.nio.file.attribute.BasicFileAttributes; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Set; |
| 37 | + |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | + |
| 40 | +/** |
| 41 | + * Visibility check for test/sample fixture copies of the canonical config templates (the ones |
| 42 | + * named e.g. {@code conf.xml} scattered across module test resources, each a hand-trimmed, |
| 43 | + * per-module subset of the real {@code exist-distribution/.../conf.xml} -- never literal copies, |
| 44 | + * so they can't be mechanically regenerated from canonical without destroying intentional |
| 45 | + * per-module customization). |
| 46 | + * <p> |
| 47 | + * None of these ~39 fixtures carry {@link SchemaVersion#ATTRIBUTE}, so none of them are checked |
| 48 | + * for drift the way {@link SchemaVersionSyncTest} checks {@link SchemaVersion} itself. This is |
| 49 | + * the cheaper "visibility before automation" interim step: list which fixtures are missing the |
| 50 | + * attribute, so the gap is visible in CI rather than silent. Actually adding {@code schemaVersion} |
| 51 | + * to all of them (via Maven resource filtering, so it can't drift once added) is a separate, |
| 52 | + * larger follow-up -- this test does not edit any fixture. |
| 53 | + */ |
| 54 | +public class SchemaVersionFixtureAuditTest { |
| 55 | + |
| 56 | + private static final Set<String> FIXTURE_FILE_NAMES = Set.of("conf.xml", "controller-config.xml", "collection.xconf.init"); |
| 57 | + |
| 58 | + /** The canonical instances themselves are not fixtures -- excluded from the scan. */ |
| 59 | + private static final Set<String> CANONICAL_PATHS = Set.of( |
| 60 | + "exist-distribution/src/main/config/conf.xml", |
| 61 | + "exist-distribution/src/main/config/collection.xconf.init", |
| 62 | + "exist-jetty-config/src/main/resources/webapp/WEB-INF/controller-config.xml"); |
| 63 | + |
| 64 | + @Test |
| 65 | + public void reportFixturesMissingSchemaVersion() throws Exception { |
| 66 | + final Path repoRoot = resolveRepoRoot(); |
| 67 | + |
| 68 | + final List<Path> fixtures = findFixtures(repoRoot); |
| 69 | + assertTrue("expected to find test/sample fixture copies of conf.xml/controller-config.xml/" |
| 70 | + + "collection.xconf.init under " + repoRoot + " (found none -- is repo root resolution broken?)", |
| 71 | + !fixtures.isEmpty()); |
| 72 | + |
| 73 | + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 74 | + factory.setNamespaceAware(true); |
| 75 | + |
| 76 | + final List<String> missing = new ArrayList<>(); |
| 77 | + for (final Path fixture : fixtures) { |
| 78 | + final Document doc = factory.newDocumentBuilder().parse(fixture.toFile()); |
| 79 | + final String declared = doc.getDocumentElement().getAttribute(SchemaVersion.ATTRIBUTE); |
| 80 | + if (declared == null || declared.isEmpty()) { |
| 81 | + missing.add(repoRoot.relativize(fixture).toString()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Not a hard failure (yet) -- every one of these is currently missing schemaVersion, by |
| 86 | + // design (see class javadoc); this is the visibility step, not the enforcement step. The |
| 87 | + // assertion just keeps the count itself from silently drifting (e.g. if a fixture |
| 88 | + // unexpectedly starts carrying schemaVersion, or a new copy appears uninspected). |
| 89 | + assertTrue("Found " + missing.size() + " fixture(s) without " + SchemaVersion.ATTRIBUTE |
| 90 | + + " (expected, see class javadoc -- this is a visibility check, not enforcement): " |
| 91 | + + missing, |
| 92 | + missing.size() == fixtures.size()); |
| 93 | + } |
| 94 | + |
| 95 | + private static List<Path> findFixtures(final Path repoRoot) throws IOException { |
| 96 | + final List<Path> fixtures = new ArrayList<>(); |
| 97 | + Files.walkFileTree(repoRoot, new SimpleFileVisitor<>() { |
| 98 | + @Override |
| 99 | + public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) { |
| 100 | + final String name = dir.getFileName() != null ? dir.getFileName().toString() : ""; |
| 101 | + if (name.equals("target") || name.equals(".git") || name.equals(".moderne") || name.equals("node_modules")) { |
| 102 | + return FileVisitResult.SKIP_SUBTREE; |
| 103 | + } |
| 104 | + return FileVisitResult.CONTINUE; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) { |
| 109 | + if (FIXTURE_FILE_NAMES.contains(file.getFileName().toString())) { |
| 110 | + final String relative = repoRoot.relativize(file).toString().replace('\\', '/'); |
| 111 | + if (!CANONICAL_PATHS.contains(relative)) { |
| 112 | + fixtures.add(file); |
| 113 | + } |
| 114 | + } |
| 115 | + return FileVisitResult.CONTINUE; |
| 116 | + } |
| 117 | + }); |
| 118 | + return fixtures; |
| 119 | + } |
| 120 | + |
| 121 | + private static Path resolveRepoRoot() { |
| 122 | + final Path base = Path.of(System.getProperty("user.dir")); |
| 123 | + Path p = base.resolve("schema"); |
| 124 | + if (Files.isDirectory(p)) { |
| 125 | + return base; |
| 126 | + } |
| 127 | + return base.getParent(); |
| 128 | + } |
| 129 | +} |
0 commit comments