diff --git a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroStrategy.java b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroStrategy.java index 34d5bd26..383a2b73 100644 --- a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroStrategy.java +++ b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroStrategy.java @@ -76,11 +76,9 @@ public double score(String first, String second) { int transpositions = transpositions(m1, m2); // Calculate the distance. - double dist = - (m1.length() / ((double)shorter.length()) + + return (m1.length() / ((double)shorter.length()) + m2.length() / ((double)longer.length()) + (m1.length() - transpositions) / ((double)m1.length())) / 3.0; - return dist; } diff --git a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroWinklerStrategy.java b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroWinklerStrategy.java index 7f3baa7e..a8e3c727 100644 --- a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroWinklerStrategy.java +++ b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroWinklerStrategy.java @@ -30,7 +30,7 @@ * @see About Jaro-Winkler Distance */ public class JaroWinklerStrategy extends JaroStrategy implements SimilarityStrategy { - final double DEFAULT_SCALING_FACTOR = 0.1; // This is the default scaling factor Winkler used. + static final double DEFAULT_SCALING_FACTOR = 0.1; // This is the default scaling factor Winkler used. private double scalingFactor; @@ -71,9 +71,7 @@ public double score(String first, String second) // The Jaro-Winkler distance uses a prefix scale which gives more favorable ratings // to strings that match from the beginning for a set prefix length. - double winkler = jaro + (scalingFactor * cl * (1.0 - jaro)); - - return winkler; + return jaro + (scalingFactor * cl * (1.0 - jaro)); } diff --git a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/StringSimilarityServiceImpl.java b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/StringSimilarityServiceImpl.java index 08d0ee0e..15ef8600 100644 --- a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/StringSimilarityServiceImpl.java +++ b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/StringSimilarityServiceImpl.java @@ -55,7 +55,7 @@ public StringSimilarityServiceImpl(SimilarityStrategy strategy) { */ public List scoreAll(List features, String target) { - ArrayList scores = new ArrayList(); + List scores = new ArrayList<>(); for(String feature: features) { double score = strategy.score(feature, target); @@ -97,10 +97,10 @@ public SimilarityScore findTop(List features, String target) */ public SimilarityScore findTop(List features, String target, Comparator comparator) { - if (features.size() == 0) { + if (features.isEmpty()) { return null; } - List scores= scoreAll(features, target); + List scores = scoreAll(features, target); Collections.sort(scores, comparator); return scores.get(0); } @@ -115,9 +115,9 @@ public SimilarityScore findTop(List features, String target, Comparator< * according to the comparator */ public List findBestN( List features, String target, int n) { - List result = new ArrayList(); + List result = new ArrayList<>(); - if ((features.size() > 0) && (n >= 1)) { + if ((!features.isEmpty()) && (n >= 1)) { List scores = scoreAll(features, target); Collections.sort(scores, new DescendingSimilarityScoreComparator()); diff --git a/htmlSanityCheck-core/src/main/java/org/aim42/net/TrustAllCertificates.java b/htmlSanityCheck-core/src/main/java/org/aim42/net/TrustAllCertificates.java index 24ed8f79..bba13b59 100644 --- a/htmlSanityCheck-core/src/main/java/org/aim42/net/TrustAllCertificates.java +++ b/htmlSanityCheck-core/src/main/java/org/aim42/net/TrustAllCertificates.java @@ -14,10 +14,17 @@ public final class TrustAllCertificates implements X509TrustManager, HostnameVerifier { - public X509Certificate[] getAcceptedIssuers() {return null;} - public void checkClientTrusted(X509Certificate[] certs, String authType) {} - public void checkServerTrusted(X509Certificate[] certs, String authType) {} - public boolean verify(String hostname, SSLSession session) {return true;} + public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];} + public void checkClientTrusted(X509Certificate[] certs, String authType) { //NOSONAR(S4830) + /* As we do only read external content, we do not care much about SSL certs */ + } + public void checkServerTrusted(X509Certificate[] certs, String authType) { //NOSONAR(S4830) + /* As we do only read external content, we do not care much about SSL certs */ + } + public boolean verify(String hostname, SSLSession session) { + /* As we do only read external content, we do not care much about hostname equality */ + return !hostname.isEmpty(); + } /** * Installs a new {@link TrustAllCertificates} as trust manager and hostname verifier. @@ -29,7 +36,8 @@ public static void install() TrustAllCertificates trustAll = new TrustAllCertificates(); // Install the all-trusting trust manager - SSLContext sc = SSLContext.getInstance("SSL"); + // Intentiaonally using "SSL" to make it work with a low security level + SSLContext sc = SSLContext.getInstance("SSL"); //NOSONAR(S4424) sc.init(null, new TrustManager[]{trustAll}, new java.security.SecureRandom()); @@ -38,14 +46,11 @@ public static void install() // Install the all-trusting host verifier HttpsURLConnection.setDefaultHostnameVerifier(trustAll); } - catch (NoSuchAlgorithmException e) - { - throw new RuntimeException("Failed setting up all thrusting certificate manager.", e); - } - catch (KeyManagementException e) + catch (NoSuchAlgorithmException | KeyManagementException e) { throw new RuntimeException("Failed setting up all thrusting certificate manager.", e); } + } } diff --git a/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/AscendingComparatorTest.java b/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/AscendingComparatorTest.java index 1ef396d6..a4a6709e 100644 --- a/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/AscendingComparatorTest.java +++ b/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/AscendingComparatorTest.java @@ -53,8 +53,8 @@ public void testCompareScoreEquality() { SimilarityScore first = new SimilarityScore("First", 0.96); SimilarityScore second = new SimilarityScore("Second", 0.96); AscendingSimilarityScoreComparator c = new AscendingSimilarityScoreComparator(); - assertEquals(c.compare(first, second), 0); - assertEquals(c.compare(second, first), 0); + assertEquals(0, c.compare(first, second)); + assertEquals(0, c.compare(second, first)); } } diff --git a/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/DescendingComparatorTest.java b/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/DescendingComparatorTest.java index 250fb7ac..c62d4db2 100644 --- a/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/DescendingComparatorTest.java +++ b/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/DescendingComparatorTest.java @@ -53,8 +53,8 @@ public void testCompareScoreEquality() { SimilarityScore first = new SimilarityScore("First", 0.96); SimilarityScore second = new SimilarityScore("Second", 0.96); DescendingSimilarityScoreComparator c = new DescendingSimilarityScoreComparator(); - assertEquals(c.compare(first, second), 0); - assertEquals(c.compare(second, first), 0); + assertEquals(0, c.compare(first, second)); + assertEquals(0, c.compare(second, first)); } } diff --git a/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/JaroWinklerStrategyTest.java b/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/JaroWinklerStrategyTest.java index 2be1c26f..5b8262ba 100644 --- a/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/JaroWinklerStrategyTest.java +++ b/htmlSanityCheck-core/src/test/java/net/ricecode/similarity/JaroWinklerStrategyTest.java @@ -24,44 +24,41 @@ package net.ricecode.similarity; import org.junit.Test; - +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import java.util.Arrays; +import java.util.Collection; import static org.junit.Assert.assertEquals; +@RunWith(Parameterized.class) public class JaroWinklerStrategyTest { - @Test - public void testOneTranspostion() { - SimilarityStrategy s = new JaroWinklerStrategy(); - String first = "Martha"; - String second = "Marhta"; - double expected = 0.961; - double delta = 0.001; - double actual = s.score(first, second); - assertEquals(expected, actual, delta); + private String first; + private String second; + private double expected; + private double delta; + + public JaroWinklerStrategyTest(String first, String second, double expected, double delta) { + this.first = first; + this.second = second; + this.expected = expected; + this.delta = delta; } - @Test - public void testSoundAlike() { - SimilarityStrategy s = new JaroWinklerStrategy(); - String first = "Dwayne"; - String second = "Duane"; - double expected = 0.840; - double delta = 0.001; - double actual = s.score(first, second); - assertEquals(expected, actual, delta); - + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{ + {"Martha", "Marhta", 0.961, 0.001}, + {"Dwayne", "Duane", 0.840, 0.001}, + {"Dixon", "Dicksonx", 0.813, 0.001} + }); } - + @Test - public void testMisspelledSoundAlike() { + public void testWithMultipleCases() { SimilarityStrategy s = new JaroWinklerStrategy(); - String first = "Dixon"; - String second = "Dicksonx"; - double expected = 0.813; - double delta = 0.001; double actual = s.score(first, second); assertEquals(expected, actual, delta); - } @Test