Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b00667f44283432c2ba53ece0acfb4fd4aa78ab1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
 * Copyright (c) 2007, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Alexander Kurtakov - Bug 460858
 *******************************************************************************/
package org.eclipse.ua.tests.doc.internal.linkchecker;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.help.internal.validation.TocValidator;
import org.eclipse.help.internal.validation.TocValidator.BrokenLink;
import org.junit.Assert;
import org.junit.Test;

/**
 * Contains tests for the documentation bundles that
 * are included with the Eclipse SDK. This tests that the
 * table of contents can be parsed and that the file
 * corresponding to each href actually exists.
 * It does not check for broken links within the files or
 * references to missing images, css files etc.
 *
 * Note that some API documents are generated as part of the
 * Eclipse build process. Tests for these documents contain
 * "Generated" in their name and are not expected to pass
 * if that project is checked out in your workspace.
 */

public class TocLinkChecker {

	private final class ReferenceFilter extends TocValidator.Filter {
		@Override
		public boolean isIncluded(String href) {
			return href.startsWith("reference");
		}
	}

	private final class NonReferenceFilter extends TocValidator.Filter {
		@Override
		public boolean isIncluded(String href) {
			return !href.startsWith("reference");
		}
	}

	private final class NonReferenceNonSampleFilter extends TocValidator.Filter {
		@Override
		public boolean isIncluded(String href) {
			return !href.startsWith("reference") && !href.startsWith("samples");
		}
	}

	private final class ReferenceOrSampleFilter extends TocValidator.Filter {
		@Override
		public boolean isIncluded(String href) {
			return href.startsWith("reference") || href.startsWith("samples");
		}
	}

	private static final String[] PLATFORM_USER = {"/org.eclipse.platform.doc.user/toc.xml"};
	private static final String[] PLATFORM_ISV = {"/org.eclipse.platform.doc.isv/toc.xml"};
	private static final String[] PDE_USER = {"/org.eclipse.pde.doc.user/toc.xml"};
	private static final String[] JDT_USER = {"/org.eclipse.jdt.doc.user/toc.xml"};
	private static final String[] JDT_ISV = {"/org.eclipse.jdt.doc.isv/toc.xml"};

	@Test
	public void testPlatformUser() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.validate(PLATFORM_USER);
		doAssert(failures);
	}

	@Test
	public void testPlatformIsvStatic() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PLATFORM_ISV, new NonReferenceNonSampleFilter());
		doAssert(failures);
	}

	@Test
	public void testPlatformIsvGenerated() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PLATFORM_ISV, new ReferenceOrSampleFilter());
		doAssert(failures);
	}

	@Test
	public void testPdeUserStatic() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PDE_USER, new NonReferenceFilter());
		doAssert(failures);
	}

	@Test
	public void testPdeUserGenerated() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PDE_USER, new ReferenceFilter());
		doAssert(failures);
	}

	@Test
	public void testJdtUser() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.validate(JDT_USER);
		doAssert(failures);
	}

	@Test
	public void testJdtIsvStatic() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(JDT_ISV, new NonReferenceFilter());
		doAssert(failures);
	}

	@Test
	public void testJdtIsvGenerated() throws Exception {
		ArrayList<BrokenLink> failures = TocValidator.filteredValidate(JDT_ISV, new ReferenceFilter());
		doAssert(failures);
	}

	private void doAssert(List<BrokenLink> failures) {
		StringBuilder message = new StringBuilder();
		for (int i = 0; i < failures.size(); i++) {
			BrokenLink link = failures.get(i);
			message.append("Invalid link in \"" + link.getTocID() + "\": " + link.getHref() + "\n");
		}
		Assert.assertTrue(message.toString(), failures.isEmpty());
	}
}

Back to the top