1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Alexander Kurtakov - Bug 460858
14  *******************************************************************************/
15 package org.eclipse.ua.tests.doc.internal.linkchecker;
16 
17 import java.util.ArrayList;
18 import java.util.List;
19 
20 import org.eclipse.help.internal.validation.TocValidator;
21 import org.eclipse.help.internal.validation.TocValidator.BrokenLink;
22 import org.junit.Assert;
23 import org.junit.Test;
24 
25 /**
26  * Contains tests for the documentation bundles that
27  * are included with the Eclipse SDK. This tests that the
28  * table of contents can be parsed and that the file
29  * corresponding to each href actually exists.
30  * It does not check for broken links within the files or
31  * references to missing images, css files etc.
32  *
33  * Note that some API documents are generated as part of the
34  * Eclipse build process. Tests for these documents contain
35  * "Generated" in their name and are not expected to pass
36  * if that project is checked out in your workspace.
37  */
38 
39 public class TocLinkChecker {
40 
41 	private final class ReferenceFilter extends TocValidator.Filter {
42 		@Override
isIncluded(String href)43 		public boolean isIncluded(String href) {
44 			return href.startsWith("reference");
45 		}
46 	}
47 
48 	private final class NonReferenceFilter extends TocValidator.Filter {
49 		@Override
isIncluded(String href)50 		public boolean isIncluded(String href) {
51 			return !href.startsWith("reference");
52 		}
53 	}
54 
55 	private final class NonReferenceNonSampleFilter extends TocValidator.Filter {
56 		@Override
isIncluded(String href)57 		public boolean isIncluded(String href) {
58 			return !href.startsWith("reference") && !href.startsWith("samples");
59 		}
60 	}
61 
62 	private final class ReferenceOrSampleFilter extends TocValidator.Filter {
63 		@Override
isIncluded(String href)64 		public boolean isIncluded(String href) {
65 			return href.startsWith("reference") || href.startsWith("samples");
66 		}
67 	}
68 
69 	private static final String[] PLATFORM_USER = {"/org.eclipse.platform.doc.user/toc.xml"};
70 	private static final String[] PLATFORM_ISV = {"/org.eclipse.platform.doc.isv/toc.xml"};
71 	private static final String[] PDE_USER = {"/org.eclipse.pde.doc.user/toc.xml"};
72 	private static final String[] JDT_USER = {"/org.eclipse.jdt.doc.user/toc.xml"};
73 	private static final String[] JDT_ISV = {"/org.eclipse.jdt.doc.isv/toc.xml"};
74 
75 	@Test
testPlatformUser()76 	public void testPlatformUser() throws Exception {
77 		ArrayList<BrokenLink> failures = TocValidator.validate(PLATFORM_USER);
78 		doAssert(failures);
79 	}
80 
81 	@Test
testPlatformIsvStatic()82 	public void testPlatformIsvStatic() throws Exception {
83 		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PLATFORM_ISV, new NonReferenceNonSampleFilter());
84 		doAssert(failures);
85 	}
86 
87 	@Test
testPlatformIsvGenerated()88 	public void testPlatformIsvGenerated() throws Exception {
89 		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PLATFORM_ISV, new ReferenceOrSampleFilter());
90 		doAssert(failures);
91 	}
92 
93 	@Test
testPdeUserStatic()94 	public void testPdeUserStatic() throws Exception {
95 		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PDE_USER, new NonReferenceFilter());
96 		doAssert(failures);
97 	}
98 
99 	@Test
testPdeUserGenerated()100 	public void testPdeUserGenerated() throws Exception {
101 		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PDE_USER, new ReferenceFilter());
102 		doAssert(failures);
103 	}
104 
105 	@Test
testJdtUser()106 	public void testJdtUser() throws Exception {
107 		ArrayList<BrokenLink> failures = TocValidator.validate(JDT_USER);
108 		doAssert(failures);
109 	}
110 
111 	@Test
testJdtIsvStatic()112 	public void testJdtIsvStatic() throws Exception {
113 		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(JDT_ISV, new NonReferenceFilter());
114 		doAssert(failures);
115 	}
116 
117 	@Test
testJdtIsvGenerated()118 	public void testJdtIsvGenerated() throws Exception {
119 		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(JDT_ISV, new ReferenceFilter());
120 		doAssert(failures);
121 	}
122 
doAssert(List<BrokenLink> failures)123 	private void doAssert(List<BrokenLink> failures) {
124 		StringBuilder message = new StringBuilder();
125 		for (int i = 0; i < failures.size(); i++) {
126 			BrokenLink link = failures.get(i);
127 			message.append("Invalid link in \"" + link.getTocID() + "\": " + link.getHref() + "\n");
128 		}
129 		Assert.assertTrue(message.toString(), failures.isEmpty());
130 	}
131 }
132