Fix testsuite for external docs

This commit is contained in:
Stian Thorgersen 2017-09-12 15:17:41 +02:00
parent a3c1b7317d
commit a5e8c09f3e
4 changed files with 20 additions and 8 deletions

View file

@ -23,18 +23,18 @@ public abstract class AbstractDocsTest {
protected File guideDir; protected File guideDir;
protected static String body; protected static String body;
protected String guideUrl;
@Before @Before
public void before() throws IOException { public void before() throws IOException {
guideDir = config.getGuideDir(getGuideDirName()); guideDir = config.getGuideDir(getGuideDirName());
guideUrl = config.getGuideBaseUrl() + "/" + config.getGuideUrlFragment(getGuideDirName()) + "/";
if (body == null) { if (body == null) {
if (config.isLoadFromFiles()) { if (config.isLoadFromFiles()) {
File htmlFile = config.getGuideHtmlFile(getGuideDirName()); File htmlFile = config.getGuideHtmlFile(getGuideDirName());
body = utils.readBody(htmlFile); body = utils.readBody(htmlFile);
} else { } else {
String guideUrl = config.getGuideBaseUrl() + "/" + config.getGuideUrlFragment(getGuideDirName());
log.info("Loading guide from '" + guideUrl); log.info("Loading guide from '" + guideUrl);
body = utils.readBody(new URL(guideUrl)); body = utils.readBody(new URL(guideUrl));
} }
@ -69,7 +69,7 @@ public abstract class AbstractDocsTest {
@Test @Test
public void checkImages() { public void checkImages() {
List<String> failures = linkUtils.findInvalidImages(body, guideDir); List<String> failures = linkUtils.findInvalidImages(body, guideDir, guideUrl);
checkFailures("Images not found", failures); checkFailures("Images not found", failures);
} }

View file

@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit;
public class Constants { public class Constants {
public static final int HTTP_RETRY = 3; public static final int HTTP_RETRY = 3;
public static final int HTTP_CONNECTION_TIMEOUT = 5000; public static final int HTTP_CONNECTION_TIMEOUT = 30000;
public static final int HTTP_READ_TIMEOUT = 10000; public static final int HTTP_READ_TIMEOUT = 300000;
public static final long LINK_CHECK_EXPIRATION = TimeUnit.DAYS.toMillis(1); public static final long LINK_CHECK_EXPIRATION = TimeUnit.DAYS.toMillis(1);
} }

View file

@ -27,10 +27,16 @@ public class DocUtils {
public String readBody(URL url) throws IOException { public String readBody(URL url) throws IOException {
HttpURLConnection connection = null; HttpURLConnection connection = null;
try { try {
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(Constants.HTTP_CONNECTION_TIMEOUT); connection.setConnectTimeout(Constants.HTTP_CONNECTION_TIMEOUT);
connection.setReadTimeout(Constants.HTTP_READ_TIMEOUT); connection.setReadTimeout(Constants.HTTP_READ_TIMEOUT);
if (connection.getResponseCode() != 200) {
throw new IOException("Invalid status code returned " + connection.getResponseCode());
}
StringWriter w = new StringWriter(); StringWriter w = new StringWriter();
IOUtils.copy(connection.getInputStream(), w, "utf-8"); IOUtils.copy(connection.getInputStream(), w, "utf-8");
String s = w.toString(); String s = w.toString();
@ -43,7 +49,9 @@ public class DocUtils {
} }
Matcher m = p.matcher(s); Matcher m = p.matcher(s);
m.find(); if (!m.find()) {
throw new RuntimeException("Couldn't find body");
}
return m.group(1); return m.group(1);
} finally { } finally {
@ -53,7 +61,7 @@ public class DocUtils {
public List<String> findMissingVariables(String body, List<String> ignoredVariables) { public List<String> findMissingVariables(String body, List<String> ignoredVariables) {
List<String> missingVariables = new LinkedList<>(); List<String> missingVariables = new LinkedList<>();
Pattern p = Pattern.compile("[^$/=\\s]\\{([^ }]*)}"); Pattern p = Pattern.compile("[^$/=\\s]\\{([^ }\"]*)}");
Matcher m = p.matcher(body); Matcher m = p.matcher(body);
while (m.find()) { while (m.find()) {
String key = m.group(1); String key = m.group(1);

View file

@ -102,7 +102,7 @@ public class LinkUtils {
return invalidLinks; return invalidLinks;
} }
public List<String> findInvalidImages(String body, File guideDir) { public List<String> findInvalidImages(String body, File guideDir, String guideUrl) {
List<String> missingImages = new LinkedList<>(); List<String> missingImages = new LinkedList<>();
Pattern p = Pattern.compile("<img src=\"([^ \"]*)[^>]*\""); Pattern p = Pattern.compile("<img src=\"([^ \"]*)[^>]*\"");
Matcher m = p.matcher(body); Matcher m = p.matcher(body);
@ -114,6 +114,10 @@ public class LinkUtils {
missingImages.add(image); missingImages.add(image);
} }
} else { } else {
if (image.startsWith("./")) {
image = guideUrl + image;
}
if (!verifiedLinks.containsKey(image)) { if (!verifiedLinks.containsKey(image)) {
boolean valid = http.isValid(image); boolean valid = http.isValid(image);
if (valid) { if (valid) {