Tweak output
This commit is contained in:
parent
f5b06b1370
commit
dd9c244b07
3 changed files with 75 additions and 22 deletions
|
@ -9,6 +9,7 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -17,14 +18,21 @@ public abstract class AbstractDocsTest {
|
|||
private static final Logger log = Logger.getLogger(AbstractDocsTest.class);
|
||||
|
||||
protected static final Config config = new Config();
|
||||
|
||||
|
||||
protected static LinkUtils linkUtils;
|
||||
protected static String body;
|
||||
protected static Boolean verbose = System.getProperties().containsKey("verbose") ? true : false;
|
||||
|
||||
protected DocUtils utils = new DocUtils();
|
||||
protected LinkUtils linkUtils = new LinkUtils(config);
|
||||
|
||||
protected File guideDir;
|
||||
protected static String body;
|
||||
protected String guideUrl;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
linkUtils = new LinkUtils(config, verbose);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() throws IOException {
|
||||
guideDir = config.getGuideDir(getGuideDirName());
|
||||
|
@ -43,13 +51,9 @@ public abstract class AbstractDocsTest {
|
|||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
linkUtils.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
linkUtils.close();
|
||||
body = null;
|
||||
}
|
||||
|
||||
|
@ -57,45 +61,48 @@ public abstract class AbstractDocsTest {
|
|||
|
||||
@Test
|
||||
public void checkVariables() {
|
||||
System.out.println("Checking variables");
|
||||
List<String> missingVariables = utils.findMissingVariables(body, config.getIgnoredVariables());
|
||||
checkFailures("Variables not found", missingVariables);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkIncludes() {
|
||||
System.out.println("Checking includes");
|
||||
List<String> missingIncludes = utils.findMissingIncludes(body);
|
||||
checkFailures("Includes not found", missingIncludes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkImages() {
|
||||
System.out.println("Checking images");
|
||||
List<String> failures = linkUtils.findInvalidImages(body, guideDir, guideUrl);
|
||||
checkFailures("Images not found", failures);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkInternalAnchors() {
|
||||
System.out.println("Checking internal anchors");
|
||||
List<String> invalidInternalAnchors = linkUtils.findInvalidInternalAnchors(body);
|
||||
checkFailures("Internal anchors not found", invalidInternalAnchors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkExternalLinks() throws IOException {
|
||||
System.out.println("Checking external links");
|
||||
List<LinkUtils.InvalidLink> invalidLinks = linkUtils.findInvalidLinks(body, config.getIgnoredLinks(), config.getIgnoredLinkRedirects());
|
||||
if (!invalidLinks.isEmpty()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Invalid links:");
|
||||
List<String> failures = new LinkedList<>();
|
||||
for (LinkUtils.InvalidLink l : invalidLinks) {
|
||||
sb.append("\n");
|
||||
sb.append(" * " + l.getLink() + " (" + l.getError() + ")");
|
||||
failures.add(l.getLink() + " (" + l.getError() + ")");
|
||||
}
|
||||
Assert.fail(sb.toString());
|
||||
throw new Failures("Invalid links", failures);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkFailures(String message, List<String> failures) {
|
||||
if (!failures.isEmpty()) {
|
||||
Assert.fail(message + ":\n * " + String.join("\n * ", failures));
|
||||
throw new Failures(message, failures);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package org.keycloak.documentation.test;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.List;
|
||||
|
||||
public class Failures extends AssertionError {
|
||||
|
||||
private List<String> failures;
|
||||
|
||||
public Failures(String error, List<String> failures) {
|
||||
super(error);
|
||||
this.failures = failures;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStackTrace() {
|
||||
printStackTrace(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStackTrace(PrintStream s) {
|
||||
for (String f : failures) {
|
||||
s.println("* " + f);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStackTrace(PrintWriter s) {
|
||||
for (String f : failures) {
|
||||
s.println("* " + f);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -20,11 +20,13 @@ public class LinkUtils {
|
|||
private HttpUtils http = new HttpUtils();
|
||||
private Config config;
|
||||
private File verifiedLinksCacheFile;
|
||||
private boolean verbose;
|
||||
private Map<String, Long> verifiedLinks;
|
||||
|
||||
public LinkUtils(Config config) {
|
||||
public LinkUtils(Config config, boolean verbose) {
|
||||
this.config = config;
|
||||
this.verifiedLinksCacheFile = config.getVerifiedLinksCache();
|
||||
this.verbose = verbose;
|
||||
this.verifiedLinks = loadCheckedLinksCache();
|
||||
}
|
||||
|
||||
|
@ -77,10 +79,15 @@ public class LinkUtils {
|
|||
if (error == null) {
|
||||
verifiedLinks.put(link, System.currentTimeMillis());
|
||||
|
||||
logger.debug("Checked link: " + link);
|
||||
if (verbose) {
|
||||
System.out.println("[OK] " + link);
|
||||
}
|
||||
} else {
|
||||
logger.debug("Bad link: " + link + " (" + error + ")");
|
||||
invalidLinks.add(new InvalidLink(link, error));
|
||||
|
||||
if (verbose) {
|
||||
System.out.println("[BAD] " + link);
|
||||
}
|
||||
}
|
||||
} else if (link.startsWith("file")) {
|
||||
File f = new File(new URL(link).getFile());
|
||||
|
@ -121,13 +128,17 @@ public class LinkUtils {
|
|||
if (!verifiedLinks.containsKey(image)) {
|
||||
boolean valid = http.isValid(image);
|
||||
if (valid) {
|
||||
logger.debug("Checked image: " + image);
|
||||
|
||||
verifiedLinks.put(image, System.currentTimeMillis());
|
||||
} else {
|
||||
logger.debug("Bad image: " + image);
|
||||
|
||||
if (verbose) {
|
||||
System.out.println("[OK] " + image);
|
||||
}
|
||||
} else {
|
||||
missingImages.add(image);
|
||||
|
||||
if (verbose) {
|
||||
System.out.println("[BAD] " + image);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -196,7 +207,7 @@ public class LinkUtils {
|
|||
p.load(new FileInputStream(verifiedLinksCacheFile));
|
||||
for(Map.Entry<Object, Object> e : p.entrySet()) {
|
||||
long checked = Long.valueOf((String) e.getValue());
|
||||
if (checked + Constants.LINK_CHECK_EXPIRATION <= System.currentTimeMillis()) {
|
||||
if (checked + Constants.LINK_CHECK_EXPIRATION >= System.currentTimeMillis()) {
|
||||
m.put((String) e.getKey(), checked);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue