Skip to main content
summaryrefslogtreecommitdiffstats
blob: cac8384311eee378662db2fce0f23d4c481f8e8b (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
/*******************************************************************************
 * Copyright (c) 2007, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
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 junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * 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 extends TestCase {
	
	private final class ReferenceFilter extends TocValidator.Filter {
		public boolean isIncluded(String href) {
			return href.startsWith("reference");
		}
	}
	
	private final class NonReferenceFilter extends TocValidator.Filter {
		public boolean isIncluded(String href) {
			return !href.startsWith("reference");
		}
	}
	
	private final class NonReferenceNonSampleFilter extends TocValidator.Filter {
		public boolean isIncluded(String href) {
			return !href.startsWith("reference") && !href.startsWith("samples");
		}
	}
	
	private final class ReferenceOrSampleFilter extends TocValidator.Filter {
		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"};
	
	public static Test suite() {
		return new TestSuite(TocLinkChecker.class);
	}
	
	public void testPlatformUser() throws Exception {
		ArrayList failures = TocValidator.validate(PLATFORM_USER);
		doAssert(failures);
	}

	public void testPlatformIsvStatic() throws Exception {
		ArrayList failures = TocValidator.filteredValidate(PLATFORM_ISV, new NonReferenceNonSampleFilter());
		doAssert(failures);
	}
	
	public void testPlatformIsvGenerated() throws Exception {
		ArrayList failures = TocValidator.filteredValidate(PLATFORM_ISV, new ReferenceOrSampleFilter());
		doAssert(failures);
	}

	public void testPdeUserStatic() throws Exception {
		ArrayList failures = TocValidator.filteredValidate(PDE_USER, new NonReferenceFilter());
		doAssert(failures);
	}
	
	public void testPdeUserGenerated() throws Exception {
		ArrayList failures = TocValidator.filteredValidate(PDE_USER, new ReferenceFilter());
		doAssert(failures);
	}
	
	public void testJdtUser() throws Exception {
		ArrayList failures = TocValidator.validate(JDT_USER);
		doAssert(failures);
	}
	
	public void testJdtIsvStatic() throws Exception {
		ArrayList failures = TocValidator.filteredValidate(JDT_ISV, new NonReferenceFilter());
		doAssert(failures);
	}
	
	public void testJdtIsvGenerated() throws Exception {
		ArrayList failures = TocValidator.filteredValidate(JDT_ISV, new ReferenceFilter());
		doAssert(failures);
	}
	
	private void doAssert(List failures) {
		StringBuffer message = new StringBuffer();
		for (int i = 0; i < failures.size(); i++) {
			BrokenLink link = (BrokenLink)failures.get(i);
			message.append("Invalid link in \"" + link.getTocID() + "\": " + link.getHref());
		}
		Assert.assertTrue(message.toString(), failures.isEmpty());
	}
}

Back to the top