Skip to content

Commit

Permalink
Fix small typos and clean up Groovy code
Browse files Browse the repository at this point in the history
to reduce warnings in IDE and align the code base with
good style practices (#343).
  • Loading branch information
ascheman committed Dec 13, 2024
1 parent 70f1efa commit f9d8f54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,44 @@
import java.util.List;

/**
* A single "finding" from any check, i.e.:
* A single "finding" from any check, i.e.:
* A single "finding" from any check, i.e.,
* - a missing image file
* - a missing label/id/bookmark (== broken link)
* - a duplicate label/id/bookmark
*/
@Getter
public class Finding {

@Getter
String whatIsTheProblem; // i.e. which image is missing, which link/anchor is undefined
@Getter
String whatIsTheProblem; // i.e., which image is missing, which link/anchor is undefined
int nrOfOccurrences;// how often does this specific finding occur in the checked-page
// suggestions are ordered: getAt(0) yields the best, getAt(1) the second and so forth
@Setter
@Getter
List<String> suggestions;

public Finding() {
this("");
}

/**
* no finding should exist without giving an explanation ("whatIsTheProblem")
* No finding should exist without giving an explanation ("whatIsTheProblem")
* about what went wrong.
*
* @param whatIsTheProblem An explanation of what went wrong (i.e. name of missing file)
* @param whatIsTheProblem An explanation of what went wrong (i.e., name of the missing file)
*/
public Finding(String whatIsTheProblem) {
this(whatIsTheProblem, 1, new ArrayList<>(3));
}

/**
* finding with explanation and several occurences
* Finding with explanation and several occurrences
*/
public Finding(String whatIsTheProblem, int nrOfOccurrences) {
this(whatIsTheProblem, nrOfOccurrences, new ArrayList<>(3));
}


/**
* most general constructor:
* Most general constructor:
* create Finding with explanation and nrOfOccurrences
*
* @param whatIsTheProblem An explanation of what went wrong (i.e. name of missing file)
Expand All @@ -62,7 +59,7 @@ public Finding(String whatIsTheProblem, int nrOfOccurrences, List<String> sugges
}

/**
* create Finding with explanation and suggestions
* Create Finding with explanation and suggestions
*
* @param whatIsTheProblem explanation what went wrong
* @param suggestions what could have been meant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import org.aim42.htmlsanitycheck.html.HtmlConst
import org.aim42.htmlsanitycheck.html.HtmlPage
import org.aim42.htmlsanitycheck.test.dns.CustomHostNameResolver
import org.wiremock.integrations.testcontainers.WireMockContainer
import spock.lang.Ignore
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
import java.lang.reflect.Field
import java.lang.reflect.Proxy
// see end-of-file for license information

// see end-of-file for license information

class BrokenHttpLinksCheckerSpec extends Specification {

Expand All @@ -40,9 +39,10 @@ class BrokenHttpLinksCheckerSpec extends Specification {
}

/* executed before every single spec */

def setup() {
myConfig = new Configuration()
brokenHttpLinksChecker = new BrokenHttpLinksChecker( myConfig )
brokenHttpLinksChecker = new BrokenHttpLinksChecker(myConfig)

collector = new SingleCheckResults()
}
Expand All @@ -57,24 +57,24 @@ class BrokenHttpLinksCheckerSpec extends Specification {
// Custom method to register the DNS resolver
private void registerCustomDnsResolver() {
try {
Field implField = InetAddress.class.getDeclaredField("impl");
implField.setAccessible(true);
Object currentImpl = implField.get(null);
Field implField = InetAddress.class.getDeclaredField("impl")
implField.setAccessible(true)
Object currentImpl = implField.get(null)

Proxy newImpl = (Proxy) Proxy.newProxyInstance(
currentImpl.getClass().getClassLoader(),
currentImpl.getClass().getInterfaces(),
(proxy, method, args) -> {
if ("lookupAllHostAddr".equals(method.getName()) && args.length == 1 && args[0] instanceof String) {
return customHostNameResolver.resolve((String) args[0]);
if ("lookupAllHostAddr" == method.getName() && args.length == 1 && args[0] instanceof String) {
return customHostNameResolver.resolve((String) args[0])
}
return method.invoke(currentImpl, args);
return method.invoke(currentImpl, args)
}
);
)

implField.set(null, newImpl);
implField.set(null, newImpl)
} catch (Exception e) {
throw new RuntimeException("Failed to register custom DNS resolver", e);
throw new RuntimeException("Failed to register custom DNS resolver", e)
}
}

Expand All @@ -97,6 +97,7 @@ class BrokenHttpLinksCheckerSpec extends Specification {

def "one syntactically correct http URL is ok"() {
given: "an HTML page with a single correct anchor/link"
//noinspection HttpUrlsUsage
String HTML = """$HtmlConst.HTML_HEAD
<a href="http://google.com:$port">google</a>
$HtmlConst.HTML_END """
Expand Down Expand Up @@ -133,16 +134,18 @@ class BrokenHttpLinksCheckerSpec extends Specification {
collector.nrOfProblems() == 0

where:
goodUrl << ["http://junit.org:$port/junit4",
"http://plumelib.org:$port/plume-util",
"http://people.csail.mit.edu:$port/cpacheco"
]
//noinspection HttpUrlsUsage
goodUrl << ["http://junit.org:$port/junit4",
"http://plumelib.org:$port/plume-util",
"http://people.csail.mit.edu:$port/cpacheco"
]
}


def "single bad link is identified as problem"() {

given: "an HTML page with a single (bad) link"
//noinspection HttpUrlsUsage
String badhref = "http://arc42.org:$port/ui98jfuhenu87djch"
String HTML = """$HtmlConst.HTML_HEAD
<a href=${badhref}>nonexisting arc42 link</a>
Expand All @@ -166,6 +169,7 @@ class BrokenHttpLinksCheckerSpec extends Specification {

def "amazon does not deliver 405 statuscode for links that really exist"() {
given: "an HTML page with a single (good) amazon link"
//noinspection HttpUrlsUsage
String goodAmazonLink = "http://www.amazon.com:$port/dp/B01A2QL9SS"
String HTML = """$HtmlConst.HTML_HEAD
<a href=${goodAmazonLink}>Amazon</a>
Expand Down Expand Up @@ -208,6 +212,7 @@ class BrokenHttpLinksCheckerSpec extends Specification {
def 'bad link #badLink is recognized as such'() {

given: "an HTML page with a single (broken) link"
//noinspection HttpUrlsUsage
String goodURL = "http://mock.codes$port/${badLink}"
String HTML = """$HtmlConst.HTML_HEAD
<a href=${goodURL}>${badLink}</a>
Expand All @@ -231,6 +236,7 @@ class BrokenHttpLinksCheckerSpec extends Specification {
def 'redirects are recognized and their new location is contained in warning message'() {

given: "the old arc42 (http!) page "
//noinspection HttpUrlsUsage
String HTML = """$HtmlConst.HTML_HEAD
<a href="http://arc42.de:$port/old"</a>
$HtmlConst.HTML_END """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.aim42.htmlsanitycheck.html;

package org.aim42.htmlsanitycheck.html

class HtmlConst {
public static final String HTML_HEAD = "<!DOCTYPE HTML> <html><head></head><body>";
public static final String HTML_END = "</body></html>";

static final String HTML_HEAD = "<!DOCTYPE HTML> <html><head></head><body>"
static final String HTML_END = "</body></html>"
}


Expand Down

0 comments on commit f9d8f54

Please sign in to comment.