001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2012-2015 ForgeRock AS.
015 */
016
017package org.forgerock.doc.maven.site;
018
019import org.apache.maven.plugin.MojoExecutionException;
020import org.forgerock.doc.maven.AbstractDocbkxMojo;
021import org.twdata.maven.mojoexecutor.MojoExecutor;
022
023import java.io.File;
024import java.util.ArrayList;
025
026/**
027 * Test links in pre-processed copy of the sources.
028 *
029 * <p>
030 *
031 * Errors are written by default to {@code ${project.build.directory}/docbkx/linktester.err}.
032 * The test does not fail on error.
033 */
034public class LinkTest {
035
036    /**
037     * The Mojo that holds configuration and related methods.
038     */
039    private AbstractDocbkxMojo m;
040
041    /**
042     * The Executor to run the linktester plugin.
043     */
044    private final Executor executor;
045
046    /**
047     * Constructor setting the Mojo that holds the configuration.
048     *
049     * @param mojo The Mojo that holds the configuration.
050     */
051    public LinkTest(final AbstractDocbkxMojo mojo) {
052        m = mojo;
053        this.executor = new Executor();
054    }
055
056    /**
057     * Test links in pre-processed copy of the sources.
058     *
059     * @throws MojoExecutionException Failed to complete link tests.
060     */
061    public void execute() throws MojoExecutionException {
062        executor.test();
063    }
064
065    /**
066     * Enclose methods to run plugins.
067     */
068    class Executor extends MojoExecutor {
069
070        /**
071         * Run link tester.
072         *
073         * @throws MojoExecutionException Failed to run link tester.
074         */
075        public void test() throws MojoExecutionException {
076
077            if (m.runLinkTester().equalsIgnoreCase("false")) {
078                return;
079            }
080
081            final String log = m.path(new File(m.getDocbkxOutputDirectory(), "linktester.err"));
082
083            // The list of URL patterns to skip can be extended by the configuration.
084            MojoExecutor.Element skipUrlPatterns = getSkipUrlPatterns();
085
086            executeMojo(
087                    plugin(
088                            groupId("org.forgerock.maven.plugins"),
089                            artifactId("linktester-maven-plugin"),
090                            version(m.getLinkTesterVersion())),
091                    goal("check"),
092                    configuration(
093                            element(name("docSources"),
094                                    element(name("docSource"),
095                                            element(name("directory"),
096                                                    m.getBuildDirectory().getPath()),
097                                            element(name("includes"),
098                                                    element(name("include"),
099                                                            "**/" + m.getDocumentSrcName())))),
100                            element(name("validating"), "true"),
101                            element(name("skipUrls"), m.skipLinkCheck()),
102                            element(name("xIncludeAware"), "true"),
103                            element(name("failOnError"), "false"),
104                            element(name("outputFile"), log),
105                            skipUrlPatterns),
106                    executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
107        }
108
109        /**
110         * Return the URL patterns to skip, which can be extended by configuration.
111         *
112         * @return      The URL patterns to skip.
113         */
114        private MojoExecutor.Element getSkipUrlPatterns() {
115
116            // The full list includes both default patterns and also configured patterns.
117            final ArrayList<Element> patterns = new ArrayList<Element>();
118
119            // Default patterns
120            patterns.add(element(name("skipUrlPattern"), // ForgeRock JIRA
121                    "^https://bugster.forgerock.org/jira/browse/.+$"));
122            patterns.add(element(name("skipUrlPattern"), // RFCs
123                    "^http://tools.ietf.org/html/rfc[0-9]+$"));
124            patterns.add(element(name("skipUrlPattern"), // Internet-Drafts
125                    "^http://tools.ietf.org/html/draft-.+$"));
126            patterns.add(element(name("skipUrlPattern"), // example (see RFC 2606)
127                    "^https?://[^/]*example.*$"));
128            patterns.add(element(name("skipUrlPattern"), // localhost
129                    "^https?://localhost.*$"));
130            patterns.add(element(name("skipUrlPattern"), // relative URLs to arbitrary resources
131                    "^\\.\\./\\Q" + m.getRelativeResourcesDirectoryPath() + "\\E.*$"));
132
133            // Configured patterns
134            if (m.getSkipUrlPatterns() != null) {
135                for (String pattern : m.getSkipUrlPatterns()) {
136                    patterns.add(element("skipUrlPattern", pattern));
137                }
138            }
139            return element(name("skipUrlPatterns"), patterns.toArray(new Element[patterns.size()]));
140        }
141    }
142}