Ignore:
Timestamp:
2016-05-15T21:14:06+02:00 (8 years ago)
Author:
Don-vip
Message:

findbugs - fix/suppress most of warnings reported in unit tests + enable low confidence warnings for core

Location:
trunk/test/unit/org/openstreetmap/josm/data/validation
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java

    r10219 r10222  
    4848import org.junit.Test;
    4949
     50import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     51
    5052/**
    5153 * Integration tests for the DomainValidator.
     
    8284        download(htmlFile, "http://www.iana.org/domains/root/db", timestamp);
    8385
    84         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(txtFile), StandardCharsets.UTF_8));
    85         String line;
    86         final String header;
    87         line = br.readLine(); // header
    88         if (line != null && line.startsWith("# Version ")) {
    89             header = line.substring(2);
    90         } else {
    91             br.close();
    92             throw new IOException("File does not have expected Version header");
    93         }
    94         final boolean generateUnicodeTlds = false; // Change this to generate Unicode TLDs as well
    95 
    96         // Parse html page to get entries
    97         Map<String, String[]> htmlInfo = getHtmlInfo(htmlFile);
    98         Map<String, String> missingTLD = new TreeMap<>(); // stores entry and comments as String[]
    99         Map<String, String> missingCC = new TreeMap<>();
    100         while ((line = br.readLine()) != null) {
    101             if (!line.startsWith("#")) {
    102                 final String unicodeTld; // only different from asciiTld if that was punycode
    103                 final String asciiTld = line.toLowerCase(Locale.ENGLISH);
    104                 if (line.startsWith("XN--")) {
    105                     unicodeTld = IDN.toUnicode(line);
    106                 } else {
    107                     unicodeTld = asciiTld;
    108                 }
    109                 if (!dv.isValidTld(asciiTld)) {
    110                     String[] info = htmlInfo.get(asciiTld);
    111                     if (info != null) {
    112                         String type = info[0];
    113                         String comment = info[1];
    114                         if ("country-code".equals(type)) { // Which list to use?
    115                             missingCC.put(asciiTld, unicodeTld + " " + comment);
    116                             if (generateUnicodeTlds) {
    117                                 missingCC.put(unicodeTld, asciiTld + " " + comment);
     86        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(txtFile), StandardCharsets.UTF_8))) {
     87            String line;
     88            final String header;
     89            line = br.readLine(); // header
     90            if (line != null && line.startsWith("# Version ")) {
     91                header = line.substring(2);
     92            } else {
     93                throw new IOException("File does not have expected Version header");
     94            }
     95            final boolean generateUnicodeTlds = false; // Change this to generate Unicode TLDs as well
     96
     97            // Parse html page to get entries
     98            Map<String, String[]> htmlInfo = getHtmlInfo(htmlFile);
     99            Map<String, String> missingTLD = new TreeMap<>(); // stores entry and comments as String[]
     100            Map<String, String> missingCC = new TreeMap<>();
     101            while ((line = br.readLine()) != null) {
     102                if (!line.startsWith("#")) {
     103                    final String unicodeTld; // only different from asciiTld if that was punycode
     104                    final String asciiTld = line.toLowerCase(Locale.ENGLISH);
     105                    if (line.startsWith("XN--")) {
     106                        unicodeTld = IDN.toUnicode(line);
     107                    } else {
     108                        unicodeTld = asciiTld;
     109                    }
     110                    if (!dv.isValidTld(asciiTld)) {
     111                        String[] info = htmlInfo.get(asciiTld);
     112                        if (info != null) {
     113                            String type = info[0];
     114                            String comment = info[1];
     115                            if ("country-code".equals(type)) { // Which list to use?
     116                                missingCC.put(asciiTld, unicodeTld + " " + comment);
     117                                if (generateUnicodeTlds) {
     118                                    missingCC.put(unicodeTld, asciiTld + " " + comment);
     119                                }
     120                            } else {
     121                                missingTLD.put(asciiTld, unicodeTld + " " + comment);
     122                                if (generateUnicodeTlds) {
     123                                    missingTLD.put(unicodeTld, asciiTld + " " + comment);
     124                                }
    118125                            }
    119126                        } else {
    120                             missingTLD.put(asciiTld, unicodeTld + " " + comment);
    121                             if (generateUnicodeTlds) {
    122                                 missingTLD.put(unicodeTld, asciiTld + " " + comment);
    123                             }
    124                         }
     127                            System.err.println("Expected to find HTML info for "+ asciiTld);
     128                        }
     129                    }
     130                    ianaTlds.add(asciiTld);
     131                    // Don't merge these conditions; generateUnicodeTlds is final so needs to be separate to avoid a warning
     132                    if (generateUnicodeTlds) {
     133                        if (!unicodeTld.equals(asciiTld)) {
     134                            ianaTlds.add(unicodeTld);
     135                        }
     136                    }
     137                }
     138            }
     139            // List html entries not in TLD text list
     140            for (String key : (new TreeMap<>(htmlInfo)).keySet()) {
     141                if (!ianaTlds.contains(key)) {
     142                    if (isNotInRootZone(key)) {
     143                        System.out.println("INFO: HTML entry not yet in root zone: "+key);
    125144                    } else {
    126                         System.err.println("Expected to find HTML info for "+ asciiTld);
    127                     }
    128                 }
    129                 ianaTlds.add(asciiTld);
    130                 // Don't merge these conditions; generateUnicodeTlds is final so needs to be separate to avoid a warning
    131                 if (generateUnicodeTlds) {
    132                     if (!unicodeTld.equals(asciiTld)) {
    133                         ianaTlds.add(unicodeTld);
    134                     }
    135                 }
    136             }
    137         }
    138         br.close();
    139         // List html entries not in TLD text list
    140         for (String key : (new TreeMap<>(htmlInfo)).keySet()) {
    141             if (!ianaTlds.contains(key)) {
    142                 if (isNotInRootZone(key)) {
    143                     System.out.println("INFO: HTML entry not yet in root zone: "+key);
    144                 } else {
    145                     System.err.println("WARN: Expected to find text entry for html: "+key);
    146                 }
    147             }
    148         }
    149         if (!missingTLD.isEmpty()) {
    150             printMap(header, missingTLD, "TLD");
    151             fail("missing TLD");
    152         }
    153         if (!missingCC.isEmpty()) {
    154             printMap(header, missingCC, "CC");
    155             fail("missing CC");
     145                        System.err.println("WARN: Expected to find text entry for html: "+key);
     146                    }
     147                }
     148            }
     149            if (!missingTLD.isEmpty()) {
     150                printMap(header, missingTLD, "TLD");
     151                fail("missing TLD");
     152            }
     153            if (!missingCC.isEmpty()) {
     154                printMap(header, missingCC, "CC");
     155                fail("missing CC");
     156            }
    156157        }
    157158        // Check if internal tables contain any additional entries
     
    175176    }
    176177
     178    @SuppressFBWarnings(value = "PERFORMANCE")
    177179    private static Map<String, String[]> getHtmlInfo(final File f) throws IOException {
    178180        final Map<String, String[]> info = new HashMap<>();
     
    186188        final Pattern comment = Pattern.compile("\\s+<td>([^<]+)</td>");
    187189
    188         final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8));
    189         String line;
    190         while ((line = br.readLine()) != null) {
    191             Matcher m = domain.matcher(line);
    192             if (m.lookingAt()) {
    193                 String dom = m.group(1);
    194                 String typ = "??";
    195                 String com = "??";
    196                 line = br.readLine();
    197                 while (line != null && line.matches("^\\s*$")) { // extra blank lines introduced
     190        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8))) {
     191            String line;
     192            while ((line = br.readLine()) != null) {
     193                Matcher m = domain.matcher(line);
     194                if (m.lookingAt()) {
     195                    String dom = m.group(1);
     196                    String typ = "??";
     197                    String com = "??";
    198198                    line = br.readLine();
    199                 }
    200                 Matcher t = type.matcher(line);
    201                 if (t.lookingAt()) {
    202                     typ = t.group(1);
    203                     line = br.readLine();
    204                     if (line != null && line.matches("\\s+<!--.*")) {
    205                         while (line != null && !line.matches(".*-->.*")) {
     199                    while (line != null && line.matches("^\\s*$")) { // extra blank lines introduced
     200                        line = br.readLine();
     201                    }
     202                    Matcher t = type.matcher(line);
     203                    if (t.lookingAt()) {
     204                        typ = t.group(1);
     205                        line = br.readLine();
     206                        if (line != null && line.matches("\\s+<!--.*")) {
     207                            while (line != null && !line.matches(".*-->.*")) {
     208                                line = br.readLine();
     209                            }
    206210                            line = br.readLine();
    207211                        }
    208                         line = br.readLine();
    209                     }
    210                     // Should have comment; is it wrapped?
    211                     while (line != null && !line.matches(".*</td>.*")) {
    212                         line += " " +br.readLine();
    213                     }
    214                     Matcher n = comment.matcher(line);
    215                     if (n.lookingAt()) {
    216                         com = n.group(1);
    217                     }
    218                     // Don't save unused entries
    219                     if (com.contains("Not assigned") || com.contains("Retired") || typ.equals("test")) {
    220 //                        System.out.println("Ignored: " + typ + " " + dom + " " +com);
     212                        // Should have comment; is it wrapped?
     213                        while (line != null && !line.matches(".*</td>.*")) {
     214                            line += " " +br.readLine();
     215                        }
     216                        Matcher n = comment.matcher(line);
     217                        if (n.lookingAt()) {
     218                            com = n.group(1);
     219                        }
     220                        // Don't save unused entries
     221                        if (com.contains("Not assigned") || com.contains("Retired") || typ.equals("test")) {
     222    //                        System.out.println("Ignored: " + typ + " " + dom + " " +com);
     223                        } else {
     224                            info.put(dom.toLowerCase(Locale.ENGLISH), new String[]{typ, com});
     225    //                        System.out.println("Storing: " + typ + " " + dom + " " +com);
     226                        }
    221227                    } else {
    222                         info.put(dom.toLowerCase(Locale.ENGLISH), new String[]{typ, com});
    223 //                        System.out.println("Storing: " + typ + " " + dom + " " +com);
    224                     }
    225                 } else {
    226                     System.err.println("Unexpected type: " + line);
    227                 }
    228             }
    229         }
    230         br.close();
     228                        System.err.println("Unexpected type: " + line);
     229                    }
     230                }
     231            }
     232        }
    231233        return info;
    232234    }
     
    262264            System.out.println("Downloading " + tldurl);
    263265            byte[] buff = new byte[1024];
    264             InputStream is = hc.getInputStream();
    265 
    266             FileOutputStream fos = new FileOutputStream(f);
    267             int len;
    268             while ((len = is.read(buff)) != -1) {
    269                 fos.write(buff, 0, len);
    270             }
    271             fos.close();
    272             is.close();
     266            try (InputStream is = hc.getInputStream();
     267                 FileOutputStream fos = new FileOutputStream(f)) {
     268                int len;
     269                while ((len = is.read(buff)) != -1) {
     270                    fos.write(buff, 0, len);
     271                }
     272            }
    273273            System.out.println("Done");
    274274        }
  • trunk/test/unit/org/openstreetmap/josm/data/validation/routines/EmailValidatorTest.java

    r10202 r10222  
    3737     * rules from the xml file.
    3838     */
    39     protected static String FORM_KEY = "emailForm";
     39    protected static final String FORM_KEY = "emailForm";
    4040
    4141    /**
    4242     * The key used to retrieve the validator action.
    4343     */
    44     protected static String ACTION = "email";
     44    protected static final String ACTION = "email";
    4545
    4646    private EmailValidator validator;
  • trunk/test/unit/org/openstreetmap/josm/data/validation/routines/UrlValidatorTest.java

    r10133 r10222  
    3131public class UrlValidatorTest {
    3232
    33    private final boolean printStatus = false;
    34    private final boolean printIndex = false; //print index that indicates current scheme,host,port,path, query test were using.
     33   private static final boolean printStatus = false;
     34   private static final boolean printIndex = false; //print index that indicates current scheme,host,port,path, query test were using.
    3535
    3636   /**
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MultipolygonTestTest.java

    r9490 r10222  
    6565        MULTIPOLYGON_TEST.startTest(null);
    6666
    67         List<Node> nodes = new ArrayList<>();
    68         nodes.add(new Node(new LatLon(0, 1)));
    69         nodes.add(new Node(new LatLon(0, 2)));
    70 
    7167        // Erroneous tag
    7268        Way w = createUnclosedWay("amenity=parking");
Note: See TracChangeset for help on using the changeset viewer.