Changeset 19689 in josm for trunk/test/unit
- Timestamp:
- 2026-11-12T18:02:08+01:00 (11 hours ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java
r19527 r19689 32 32 import java.net.HttpURLConnection; 33 33 import java.net.IDN; 34 import java.net.UR L;34 import java.net.URI; 35 35 import java.nio.charset.StandardCharsets; 36 36 import java.nio.file.Files; … … 43 43 import java.util.Set; 44 44 import java.util.TreeMap; 45 import java.util.regex.Matcher; 46 import java.util.regex.Pattern; 45 46 import jakarta.json.Json; 47 import jakarta.json.JsonArray; 48 import jakarta.json.JsonObject; 49 import jakarta.json.JsonReader; 50 import jakarta.json.JsonValue; 47 51 48 52 import org.junit.jupiter.api.Test; … … 50 54 import org.openstreetmap.josm.testutils.annotations.IntegrationTest; 51 55 import org.openstreetmap.josm.tools.Logging; 52 53 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;54 56 55 57 /** … … 83 85 File txtFile = new File(System.getProperty("java.io.tmpdir"), "tlds-alpha-by-domain.txt"); 84 86 long timestamp; 87 String tlds = "https://data.iana.org/TLD/tlds-alpha-by-domain.txt"; 85 88 try { 86 timestamp = download(txtFile, "http://data.iana.org/TLD/tlds-alpha-by-domain.txt", 0L);89 timestamp = download(txtFile, tlds, 0L); 87 90 } catch (ConnectException e) { 88 91 Logging.error(e); 89 92 // Try again one more time in case of random network issue 90 timestamp = download(txtFile, "http://data.iana.org/TLD/tlds-alpha-by-domain.txt", 0L);91 } 92 final File htmlFile = new File(System.getProperty("java.io.tmpdir"), "tlds-alpha-by-domain.html");93 // N.B. sometimes the html filemay be updated a day or so after the txt file94 // if the txt file contains entries not found in the htmlfile, try again in a day or two95 download( htmlFile, "http://www.iana.org/domains/root/db", timestamp);93 timestamp = download(txtFile, tlds, 0L); 94 } 95 final File jsonFile = new File(System.getProperty("java.io.tmpdir"), "tlds-alpha-by-domain.json"); 96 // N.B. sometimes the JSON data may be updated a day or so after the txt file 97 // if the txt file contains entries not found in the JSON file, try again in a day or two 98 download(jsonFile, "https://data.iana.org/TLD/v1/tlds.json", timestamp); 96 99 97 100 try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(txtFile.toPath()), StandardCharsets.UTF_8))) { … … 106 109 final boolean generateUnicodeTlds = false; // Change this to generate Unicode TLDs as well 107 110 108 // Parse html pageto get entries109 Map<String, String[]> htmlInfo = getHtmlInfo(htmlFile);111 // Parse JOSN to get entries 112 Map<String, String[]> jsonInfo = getJSONInfo(jsonFile); 110 113 Map<String, String> missingTLD = new TreeMap<>(); // stores entry and comments as String[] 111 114 Map<String, String> missingCC = new TreeMap<>(); … … 121 124 unicodeTld = asciiTld; 122 125 } 123 String[] info = htmlInfo.get(asciiTld);126 String[] info = jsonInfo.get(asciiTld); 124 127 if (info != null) { 125 128 String type = info[0]; 126 String comment = info[1] .replaceAll(""", "\"").replaceAll("'", "'").replaceAll("&", "&");129 String comment = info[1]; 127 130 if ("country-code".equals(type)) { // Which list to use? 128 131 if (!dv.isValidInfrastructureTld(asciiTld)) { … … 150 153 } 151 154 } 152 } else {153 Logging.error("Expected to find HTML info for "+ asciiTld);154 155 } 156 } else { 157 Logging.error("Expected to find JSON information for "+ asciiTld); 155 158 } 156 159 ianaTlds.add(asciiTld); … … 163 166 } 164 167 } 165 // List htmlentries not in TLD text list166 for (String key : (new TreeMap<>( htmlInfo)).keySet()) {168 // List JSON entries not in TLD text list 169 for (String key : (new TreeMap<>(jsonInfo)).keySet()) { 167 170 if (!ianaTlds.contains(key)) { 168 if (isNotInRootZone(key)) { 169 Logging.info("HTML entry not yet in root zone: "+key); 170 } else { 171 Logging.warn("Expected to find text entry for html: "+key); 172 } 171 Logging.warn("Expected to find text entry for JSON: "+key); 173 172 } 174 173 } … … 177 176 if (s != null) /* text too long, shorten a bit */ 178 177 allTLD.replace("melbourne", s.replace("represented by its ", "")); 179 printMap(header, allTLD, "allTLD"); 180 printMap(header, allCC, "allCC"); 178 printMap(header, allTLD, "allTLD", "in"); 179 printMap(header, allCC, "allCC", "in"); 181 180 if (!missingTLD.isEmpty()) { 182 printMap(header, missingTLD, "TLD"); 181 printMap(header, missingTLD, "TLD", "missing from"); 183 182 fail("missing TLD"); 184 183 } 185 184 if (!missingCC.isEmpty()) { 186 printMap(header, missingCC, "CC"); 185 printMap(header, missingCC, "CC", "missing from"); 187 186 fail("missing CC"); 188 187 } … … 195 194 } 196 195 197 private static void printMap(final String header, Map<String, String> map, String string) { 198 Logging.warn("Entries missing from"+ string +" List\n");196 private static void printMap(final String header, Map<String, String> map, String string, String mode) { 197 Logging.warn("Entries " + mode + " "+ string +" List\n"); 199 198 if (header != null) { 200 199 Logging.warn(" // Taken from " + header); … … 206 205 } 207 206 208 @SuppressFBWarnings(value = "PERFORMANCE") 209 private static Map<String, String[]> getHtmlInfo(final File f) throws IOException { 207 private static Map<String, String[]> getJSONInfo(final File f) throws IOException { 210 208 final Map<String, String[]> info = new HashMap<>(); 211 209 212 final Pattern domain = Pattern.compile(".*<a href=\"/domains/root/db/([^.]+)\\.html"); 213 final Pattern type = Pattern.compile("\\s+<td>([^<]+)</td>"); 214 final Pattern comment = Pattern.compile("\\s+<td>([^<]+)</td>"); 215 216 try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(f.toPath()), StandardCharsets.UTF_8))) { 217 String line; 218 while ((line = br.readLine()) != null) { 219 Matcher m = domain.matcher(line); 220 if (m.lookingAt()) { 221 String dom = m.group(1); 222 String typ = "??"; 210 try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(f.toPath()), StandardCharsets.UTF_8)); 211 JsonReader reader = Json.createReader(br)) { 212 213 JsonObject root = reader.readObject(); 214 JsonArray domains = root.getJsonArray("domains"); 215 216 if (domains != null) { 217 for (JsonValue val : domains) { 218 JsonObject domainNode = val.asJsonObject(); 219 220 String dom = domainNode.getString("name", ""); 221 String typ = domainNode.getString("type", "??"); 222 String status = domainNode.getString("status", ""); 223 223 224 String com = "??"; 224 line = br.readLine(); 225 while (line != null && line.matches("^\\s*$")) { // extra blank lines introduced 226 line = br.readLine(); 227 } 228 Matcher t = type.matcher(line); 229 if (t.lookingAt()) { 230 typ = t.group(1); 231 line = br.readLine(); 232 if (line != null && line.matches("\\s+<!--.*")) { 233 while (line != null && !line.matches(".*-->.*")) { 234 line = br.readLine(); 235 } 236 line = br.readLine(); 225 if (domainNode.containsKey("manager") && !domainNode.isNull("manager")) { 226 JsonObject managerNode = domainNode.getJsonObject("manager"); 227 if (managerNode != null && managerNode.containsKey("organization") && !managerNode.isNull("organization")) { 228 com = managerNode.getString("organization", "??"); 237 229 } 238 // Should have comment; is it wrapped? 239 while (line != null && !line.matches(".*</td>.*")) { 240 line += " " +br.readLine(); 241 } 242 Matcher n = comment.matcher(line); 243 if (n.lookingAt()) { 244 com = n.group(1); 245 } 246 // Don't save unused entries 247 if (!com.contains("Not assigned") && !com.contains("Retired") && !typ.equals("test")) { 248 info.put(dom.toLowerCase(Locale.ENGLISH), new String[]{typ, com}); 249 } 250 } else { 251 Logging.error("Unexpected type: " + line); 230 } 231 232 boolean isRetired = "retired".equalsIgnoreCase(status); 233 boolean isTest = "test".equalsIgnoreCase(typ); 234 boolean isNotAssigned = com.contains("Not assigned") || com.equals("??"); 235 236 if (!isRetired && !isTest && !isNotAssigned) { 237 info.put(dom.toLowerCase(Locale.ENGLISH), new String[]{typ, com}); 252 238 } 253 239 } … … 260 246 * Download a file if it is more recent than our cached copy. 261 247 * Unfortunately the server does not seem to honour If-Modified-Since for the 262 * Htmlpage, so we check if it is newer than the txt file and skip download if so248 * page, so we check if it is newer than the txt file and skip download if so 263 249 */ 264 250 private static long download(File f, String tldurl, long timestamp) throws IOException { … … 275 261 modTime = 0; 276 262 } 277 HttpURLConnection hc = (HttpURLConnection) new URL(tldurl).openConnection();263 HttpURLConnection hc = (HttpURLConnection) URI.create(tldurl).toURL().openConnection(); 278 264 if (modTime > 0) { 279 265 SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); //Sun, 06 Nov 1994 08:49:37 GMT … … 288 274 byte[] buff = new byte[1024]; 289 275 try (InputStream is = hc.getInputStream(); 290 FileOutputStream fos = new FileOutputStream(f)) {291 int len; 292 while ((len = is.read(buff)) != -1) { 293 fos.write(buff, 0, len); 276 FileOutputStream fos = new FileOutputStream(f)) { 277 int len; 278 while ((len = is.read(buff)) != -1) { 279 fos.write(buff, 0, len); 294 280 } 295 281 } … … 297 283 } 298 284 return f.lastModified(); 299 }300 301 /**302 * Check whether the domain is in the root zone currently.303 * Reads the URL http://www.iana.org/domains/root/db/*domain*.html304 * (using a local disk cache)305 * and checks for the string "This domain is not present in the root zone at this time."306 * @param domain the domain to check307 * @return true if the string is found308 */309 private static boolean isNotInRootZone(String domain) {310 String tldurl = "http://www.iana.org/domains/root/db/" + domain + ".html";311 BufferedReader in = null;312 try {313 File rootCheck = new File(System.getProperty("java.io.tmpdir"), "tld_" + domain + ".html");314 download(rootCheck, tldurl, 0L);315 in = new BufferedReader(new InputStreamReader(Files.newInputStream(rootCheck.toPath()), StandardCharsets.UTF_8));316 String inputLine;317 while ((inputLine = in.readLine()) != null) {318 if (inputLine.contains("This domain is not present in the root zone at this time.")) {319 return true;320 }321 }322 in.close();323 } catch (IOException e) {324 e.printStackTrace();325 } finally {326 closeQuietly(in);327 }328 return false;329 285 } 330 286
Note:
See TracChangeset
for help on using the changeset viewer.
