View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2012-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.doc.maven.site;
18  
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.forgerock.doc.maven.AbstractDocbkxMojo;
21  import org.twdata.maven.mojoexecutor.MojoExecutor;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  
26  /**
27   * Test links in pre-processed copy of the sources.
28   *
29   * <p>
30   *
31   * Errors are written by default to {@code ${project.build.directory}/docbkx/linktester.err}.
32   * The test does not fail on error.
33   */
34  public class LinkTest {
35  
36      /**
37       * The Mojo that holds configuration and related methods.
38       */
39      private AbstractDocbkxMojo m;
40  
41      /**
42       * The Executor to run the linktester plugin.
43       */
44      private final Executor executor;
45  
46      /**
47       * Constructor setting the Mojo that holds the configuration.
48       *
49       * @param mojo The Mojo that holds the configuration.
50       */
51      public LinkTest(final AbstractDocbkxMojo mojo) {
52          m = mojo;
53          this.executor = new Executor();
54      }
55  
56      /**
57       * Test links in pre-processed copy of the sources.
58       *
59       * @throws MojoExecutionException Failed to complete link tests.
60       */
61      public void execute() throws MojoExecutionException {
62          executor.test();
63      }
64  
65      /**
66       * Enclose methods to run plugins.
67       */
68      class Executor extends MojoExecutor {
69  
70          /**
71           * Run link tester.
72           *
73           * @throws MojoExecutionException Failed to run link tester.
74           */
75          public void test() throws MojoExecutionException {
76  
77              if (m.runLinkTester().equalsIgnoreCase("false")) {
78                  return;
79              }
80  
81              final String log = m.path(new File(m.getDocbkxOutputDirectory(), "linktester.err"));
82  
83              // The list of URL patterns to skip can be extended by the configuration.
84              MojoExecutor.Element skipUrlPatterns = getSkipUrlPatterns();
85  
86              executeMojo(
87                      plugin(
88                              groupId("org.forgerock.maven.plugins"),
89                              artifactId("linktester-maven-plugin"),
90                              version(m.getLinkTesterVersion())),
91                      goal("check"),
92                      configuration(
93                              element(name("docSources"),
94                                      element(name("docSource"),
95                                              element(name("directory"),
96                                                      m.getBuildDirectory().getPath()),
97                                              element(name("includes"),
98                                                      element(name("include"),
99                                                              "**/" + 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 }