From 131f1d11caa1ec2b6f59aa365b5c627ba3ef4bc0 Mon Sep 17 00:00:00 2001 From: Gerd Aschemann Date: Thu, 28 Mar 2024 10:21:27 +0100 Subject: [PATCH] #320 Fix Sonar Issues --- .../net/ricecode/similarity/JaroStrategy.java | 8 +- .../similarity/JaroWinklerStrategy.java | 6 +- .../StringSimilarityServiceImpl.java | 3 +- .../org/aim42/net/TrustAllCertificates.java | 81 +++++++++++-------- .../similarity/AscendingComparatorTest.java | 4 +- .../similarity/DescendingComparatorTest.java | 4 +- .../similarity/JaroWinklerStrategyTest.java | 51 ++++++------ 7 files changed, 82 insertions(+), 75 deletions(-) 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 a71a73f8..057c70df 100644 --- a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroStrategy.java +++ b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/JaroStrategy.java @@ -74,11 +74,9 @@ public double score(String first, String second) { int transpositions = transpositions(m1, m2); // Calculate the distance. - double dist = - (m1.length() / ((double) shorter.length()) + - m2.length() / ((double) longer.length()) + - (m1.length() - transpositions) / ((double) m1.length())) / 3.0; - return dist; + return (m1.length() / ((double)shorter.length()) + + m2.length() / ((double)longer.length()) + + (m1.length() - transpositions) / ((double)m1.length())) / 3.0; } 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 7b34136a..fbe1c3a9 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 final double scalingFactor; @@ -68,9 +68,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 4464863f..19bafcb4 100644 --- a/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/StringSimilarityServiceImpl.java +++ b/htmlSanityCheck-core/src/main/java/net/ricecode/similarity/StringSimilarityServiceImpl.java @@ -66,7 +66,6 @@ public List scoreAll(List features, String target) { return scores; } - /** * Calculates the similarity score of a single feature. * @@ -124,7 +123,7 @@ public List findBestN(List features, String target, int List scores = scoreAll(features, target); scores.sort(new DescendingSimilarityScoreComparator()); - // fails if n> scores.size(): result = scores.subList(0, n); + // fails if n> scores.size(): result = scores.subList(0, n); //NOSONAR(S125) result = scores.subList(0, Math.min(scores.size(), n)); } 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 9b89b8fe..af878613 100644 --- a/htmlSanityCheck-core/src/main/java/org/aim42/net/TrustAllCertificates.java +++ b/htmlSanityCheck-core/src/main/java/org/aim42/net/TrustAllCertificates.java @@ -7,7 +7,20 @@ import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; -public final class TrustAllCertificates implements X509TrustManager, HostnameVerifier { +public final class TrustAllCertificates implements X509TrustManager, HostnameVerifier +{ + 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. */ @@ -16,7 +29,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()); @@ -24,39 +38,40 @@ 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) { - throw new RuntimeException("Failed setting up all thrusting certificate manager.", e); } - } - - public X509Certificate[] getAcceptedIssuers() { - return null; - } - - public void checkClientTrusted(X509Certificate[] certs, String authType) { - } - - public void checkServerTrusted(X509Certificate[] certs, String authType) { - } + catch (NoSuchAlgorithmException | KeyManagementException e) + { + throw new RuntimeException("Failed setting up all thrusting certificate manager.", e); //NOSONAR(S112) + } - public boolean verify(String hostname, SSLSession session) { - return true; } } -/*============================================================= - 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. - =============================================================*/ +/* + * Copyright Gernot Starke and aim42 contributors. + * + * 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 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ 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