Skip to content

Commit

Permalink
Code style improvements in toc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akurtakov committed Jan 14, 2024
1 parent 68c8c4a commit 0114667
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -53,8 +54,8 @@ public String getHref() {
return href; }
}

public static abstract class Filter {
abstract public boolean isIncluded(String href);
public abstract static class Filter {
public abstract boolean isIncluded(String href);
}

public static class PassThroughFilter extends Filter {
Expand All @@ -73,11 +74,13 @@ public boolean isIncluded(String href) {
* @return An ArrayList of BrokenLink objects in the toc. If no broken links are found, an empty ArrayList
* is returned.
*/
public static ArrayList<BrokenLink> validate(String[] hrefs) throws IOException, SAXException, ParserConfigurationException{
public static List<BrokenLink> validate(String[] hrefs)
throws IOException, SAXException, ParserConfigurationException {
return filteredValidate(hrefs, new PassThroughFilter());
}

public static ArrayList<BrokenLink> filteredValidate (String[] hrefs, Filter filter) throws IOException, SAXException, ParserConfigurationException{
public static List<BrokenLink> filteredValidate(String[] hrefs, Filter filter)
throws IOException, SAXException, ParserConfigurationException {
TocValidator v = new TocValidator();
ArrayList<BrokenLink> result = new ArrayList<>();
for (String href : hrefs)
Expand Down Expand Up @@ -127,27 +130,26 @@ private void processToc(String href, String plugin, ArrayList<BrokenLink> result
*/
private void process(IUAElement element, String plugin, String path, ArrayList<BrokenLink> result, Filter filter) throws SAXException, ParserConfigurationException {
String href;
if (element instanceof ILink) {
href = ((ILink)element).getToc();
if (element instanceof ILink link) {
href = link.getToc();
try {
processToc(href, plugin, result, filter);
} catch (IOException e) {
result.add(new BrokenLink("/" + plugin + "/" + path, href)); //$NON-NLS-1$ //$NON-NLS-2$
}
}
else if (element instanceof IHelpResource) {
if (element instanceof IToc)
href = ((IToc)element).getTopic(null).getHref();
else if (element instanceof IHelpResource helpResource) {
if (element instanceof IToc toc)
href = toc.getTopic(null).getHref();
else
href = ((IHelpResource)element).getHref();
if (href != null && filter.isIncluded(href))
if (!checkLink(href, plugin)) {
href = helpResource.getHref();
if (href != null && filter.isIncluded(href) && !checkLink(href, plugin)) {
result.add(new BrokenLink("/" + plugin + "/" + path, href)); //$NON-NLS-1$ //$NON-NLS-2$
}
}
IUAElement [] children = element.getChildren();
for (int i = 0; i < children.length; i++)
process(children[i], plugin, path, result, filter);
for (IUAElement child : children)
process(child, plugin, path, result, filter);
}

/* Checks validity of a given link from a toc in a given plug-in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*******************************************************************************/
package org.eclipse.ua.tests.doc.internal.linkchecker;

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

import org.eclipse.help.internal.validation.TocValidator;
Expand Down Expand Up @@ -74,56 +73,55 @@ public boolean isIncluded(String href) {

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

@Test
public void testPlatformIsvStatic() throws Exception {
ArrayList<BrokenLink> failures = TocValidator.filteredValidate(PLATFORM_ISV, new NonReferenceNonSampleFilter());
List<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());
List<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());
List<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());
List<BrokenLink> failures = TocValidator.filteredValidate(PDE_USER, new ReferenceFilter());
doAssert(failures);
}

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

@Test
public void testJdtIsvStatic() throws Exception {
ArrayList<BrokenLink> failures = TocValidator.filteredValidate(JDT_ISV, new NonReferenceFilter());
List<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());
List<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);
for (BrokenLink link : failures) {
message.append("Invalid link in \"" + link.getTocID() + "\": " + link.getHref() + "\n");
}
Assert.assertTrue(message.toString(), failures.isEmpty());
Expand Down

0 comments on commit 0114667

Please sign in to comment.