Skip to content

Commit

Permalink
Merge branch 'feature/#42-local-resource-checker-ref-counter' into de…
Browse files Browse the repository at this point in the history
…velop
  • Loading branch information
gernotstarke committed Dec 26, 2014
2 parents 4afff45 + 1883a41 commit a75ad54
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import org.slf4j.LoggerFactory

class MissingLocalResourcesChecker extends Checker {

// members are initialized in implicit constructor
private List<String> localResources
public static final String MLRC_MESSAGE_PREFIX = "local resource"
public static final String MLRC_MESSAGE_MISSING = "missing"
public static final String MLRC_REFCOUNT = ", reference count: "

// List of the local resources referenced in anchor tags
private List<String> localResourcesList

// unique local references - every one is unique
// created from the List of all by toSet() method
private Set<String> localResourcesSet

// we need to know the baseDir of the html file, so we can check
// for local resources either with relative or absolute paths
Expand All @@ -32,11 +40,14 @@ class MissingLocalResourcesChecker extends Checker {
List<String> allHrefs = pageToCheck.getAllHrefStrings()

// now filter out all local resources
localResources = allHrefs.findAll {
localResourcesList = allHrefs.findAll {
URLUtil.isLocalResource( it )
}

logger.info """local resources: $localResources"""
// filter duplicates by reducing to set
localResourcesSet = localResourcesList.toSet()

logger.debug """local resources set: ${localResourcesSet}"""

// make sure we have a non-null baseDir
// (for html pages given as "string", this should be "")
Expand All @@ -45,13 +56,13 @@ class MissingLocalResourcesChecker extends Checker {
}

// perform the actual checks
checkAllLocalResources()
checkAllLocalResources( localResourcesSet )

return checkingResults

}

private void checkAllLocalResources() {
private void checkAllLocalResources( Set<String> localResources ) {
localResources.each { localResource ->
checkSingleLocalResource( localResource )
}
Expand All @@ -68,7 +79,7 @@ class MissingLocalResourcesChecker extends Checker {
private void checkSingleLocalResource( String localResource ) {
// the localResource is either path+filename or filename or directory

//logger.info( "resource to be checked: ", localResource )
logger.debug( "single resource to be checked: + $localResource" )

// bookkeeping:
checkingResults.incNrOfChecks()
Expand All @@ -80,10 +91,20 @@ class MissingLocalResourcesChecker extends Checker {
File localFile = new File( baseDirPath, localResourcePath );

if (!localFile.exists() ) {
String findingText = "local resource \"$localResource\" missing"
checkingResults.newFinding(findingText)
String findingText = """$MLRC_MESSAGE_PREFIX \"${localResource}\" $MLRC_MESSAGE_MISSING"""

// how often is localResource referenced?
int nrOfOccurrences = localResourcesList.count( localResource )

if (nrOfOccurrences > 1)
findingText += MLRC_REFCOUNT + nrOfOccurrences

// add Finding to our current checking results, increment nrOfFindings by nrOfOccurrences
checkingResults.newFinding(findingText, nrOfOccurrences)
}
}


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

package org.aim42.htmlsanitycheck.collect
/**
* collects results for a specific checking field
* (i.e. missing images).
* collects results for a specific type of @see Checker
* (i.e. missing images, broken cross-references).
*
* @author Gernot Starke <gs@gernotstarke.de>
*/
Expand All @@ -17,6 +17,7 @@ class SingleCheckResults implements CheckResults {
public String targetItemName // i.e. local-image-file, id/bookmark

public int nrOfItemsChecked
private int nrOfIssues

public ArrayList<Finding> findings

Expand All @@ -29,25 +30,44 @@ class SingleCheckResults implements CheckResults {
public SingleCheckResults() {

this.nrOfItemsChecked = 0
this.nrOfIssues = 0
this.findings = new ArrayList<Finding>()
}


/**
* add a single finding to the collection,
* based upon whatToCheck.
* @param message: what kind of finding is it?
*/
public void newFinding( String message ) {
addFinding( new Finding( message ))
addFinding( new Finding( message ), 1)
}

/**
* add a single finding to the collection,
* @param message: what kind of finding is it?
* @param nrOfOccurrences: how often does this occur?
*/
public void newFinding( String message, int nrOfOccurrences ) {
addFinding( new Finding( message ), nrOfOccurrences)
}


/**
* add a single finding to the collection of Finding instances
* @param singleFinding
*/
public void addFinding(Finding singleFinding) {
findings.add(singleFinding)
incNrOfIssues()
}

/**
* add single Finding with multiple occurrences
*/
public void addFinding( Finding singleFinding, int nrOfOccurrences ) {
findings.add( singleFinding )
addNrOfIssues( nrOfOccurrences )
}

/**
Expand All @@ -65,6 +85,18 @@ class SingleCheckResults implements CheckResults {
nrOfItemsChecked = nrOfChecks
}

/**
* bookkeeping on the number of issues
*/
public void incNrOfIssues() {
nrOfIssues += 1
}

public void addNrOfIssues( int nrOfIssuesToAdd ) {
nrOfIssues += nrOfIssuesToAdd
}


/**
* @return a description of what is checked
*/
Expand All @@ -83,10 +115,10 @@ class SingleCheckResults implements CheckResults {

/**
*
* @return ( int ) the nr of issues found for this checkingResults.
* @return ( int ) the nr of issues/findings found for this checkingResults.
*/
public int nrOfProblems() {
return findings.size()
return nrOfIssues
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.aim42.htmlsanitycheck.check

import org.aim42.htmlsanitycheck.collect.SingleCheckResults
import org.aim42.htmlsanitycheck.html.HtmlConst
import org.aim42.htmlsanitycheck.html.HtmlPage
import spock.lang.Specification
import spock.lang.Unroll

class MissingLocalResourceRefCountSpec extends Specification {

// missing local resource name
private static final String MIS_LOC_RES = "MissingLocalResource.html"

private static final String badLocalRef = """<a href="${MIS_LOC_RES}">missing</a>"""


private static final String msgPrefix = MissingLocalResourcesChecker.MLRC_MESSAGE_PREFIX
private static final String msgMissing = MissingLocalResourcesChecker.MLRC_MESSAGE_MISSING
private static final String msgRefCount = MissingLocalResourcesChecker.MLRC_REFCOUNT


private File tmpDir
private File tmpHtmlFile
private File tmpLocalResourceFile


/*
* data-driven test to specify behavior of MissingLocalResourceChecker reference counter:
* if a single local resource is referenced multiple times,
* it will only result in a single line of output of the following form:
* "local resource <name> missing missing, reference count: 5"
*
* @param htmlsnippet
* @param nrOfChecks
* @param nrOfFindings
* @param resultingText
*/

@Unroll
def "MLR checker has reference counter"(int nrOfChecks,
int nrOfFindings,
String htmlSnippet,
String result) {

given:

String html = HtmlConst.HTML_HEAD + htmlSnippet + HtmlConst.HTML_END

// create a file
(tmpHtmlFile, tmpDir) = createTempLocalResources("index.html", html)

HtmlPage htmlPage = new HtmlPage( tmpHtmlFile )

when:
def missingLocalResourcesChecker = new MissingLocalResourcesChecker(
pageToCheck: htmlPage,
baseDirPath: tmpDir )
SingleCheckResults collector = missingLocalResourcesChecker.performCheck()


then:

println "testing with $nrOfChecks, $nrOfFindings, $htmlSnippet, expected: $result "

// our temporary files still exists
tmpHtmlFile.exists()

// we get the correct nr of checks and findings
collector.nrOfItemsChecked == nrOfChecks
collector.nrOfProblems() == nrOfFindings

// we get the correct finding-message
collector.findings.first().item == result


where:

nrOfChecks | nrOfFindings | htmlSnippet | result
// one bad local reference:
1 | 1 | badLocalRef | """${msgPrefix} "${MIS_LOC_RES}" ${msgMissing}"""

// two bad local references to the same file -> reference count == 2
1 | 2 | badLocalRef*2 | """${msgPrefix} "${MIS_LOC_RES}" ${msgMissing}${msgRefCount}2"""

// five bad local references to the same file -> reference count == 5
1 | 5 | badLocalRef*5 | """${msgPrefix} "${MIS_LOC_RES}" ${msgMissing}${msgRefCount}5"""
}

/*
* helper to create local resource
*/

def List createTempLocalResources(String htmlFileName, String htmlContent) {
// 1.) create tmp directory tmpDir
File tmpDir = File.createTempDir()

// 2.) create tmp html file "index.html" linking to localResourceFile in directory tmpDir
File indexFile = new File(tmpDir, htmlFileName) << htmlContent

assert indexFile.exists()

return [indexFile, tmpDir]
}
}

/************************************************************************
* This is free software - without ANY guarantee!
*
*
* Copyright Dr. Gernot Starke, arc42.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*********************************************************************** */


Binary file modified documentation/resources/hsc_diagrams.eap
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project_version = 0.5.3
project_version = 0.8.0-SNAPSHOT

version_description = first public version of HtmlSanityCheck
version_description = second and enhanced version of HtmlSanityCheck

project_vendor = Dr. Gernot Starke
project_author_id = gernotstarke
Expand Down

0 comments on commit a75ad54

Please sign in to comment.