SentryUpgradeOrder { public static Map<String, List<String>> readUpgradeGraph(Reader reader) throws IOException { Map<String, List<String>> upgradeSchemaVersions = new HashMap<>(); try (BufferedReader bfReader = new BufferedReader(reader)) { String schemaVersion; while ((schemaVersion = bfReader.readLine()) != null) { String[] versions = schemaVersion.toLowerCase().split("-to-"); if (versions.length != 2) { LOGGER.warn("Ignoring unknown Sentry schema upgrade path: " + schemaVersion); continue; } String source = versions[0].trim(); String target = versions[1].trim(); if (source.isEmpty() || source.startsWith("#") || source.equals(target)) { continue; } List<String> toVersions = upgradeSchemaVersions.getOrDefault(source, new LinkedList<>()); toVersions.add(target); upgradeSchemaVersions.put(source, toVersions); } } return upgradeSchemaVersions; }  static Map<String, List<String>> readUpgradeGraph(Reader reader); static List<String> getUpgradePath(Map<String, List<String>> upgrades, String from, String to);  }
@Test public void testReadUpgradeOrderGraphInNormalOrder() throws IOException { List<String> normalOrder = Arrays.asList( "1.7.0-to-1.8.0", "1.8.0-to-2.0.0", "2.0.0-to-2.1.0" ); Map<String, List<String>> upgradeGraph = SentryUpgradeOrder.readUpgradeGraph(new StringReader(StringUtils.join(normalOrder, "\n"))); assertEquals(3, upgradeGraph.size()); assertEquals(Arrays.asList("1.8.0"), upgradeGraph.get("1.7.0")); assertEquals(Arrays.asList("2.0.0"), upgradeGraph.get("1.8.0")); assertEquals(Arrays.asList("2.1.0"), upgradeGraph.get("2.0.0")); }
@Test public void testReadUpgradeOrderGraphInReverseOrder() throws IOException { List<String> simpleOrder = Arrays.asList( "2.0.0-to-2.1.0", "1.8.0-to-2.0.0", "1.7.0-to-1.8.0" ); Map<String, List<String>> upgradeGraph = SentryUpgradeOrder.readUpgradeGraph(new StringReader(StringUtils.join(simpleOrder, "\n"))); assertEquals(3, upgradeGraph.size()); assertEquals(Arrays.asList("1.8.0"), upgradeGraph.get("1.7.0")); assertEquals(Arrays.asList("2.0.0"), upgradeGraph.get("1.8.0")); assertEquals(Arrays.asList("2.1.0"), upgradeGraph.get("2.0.0")); }
@Test public void testReadUpgradeOrderGraphWithSameSourceVersions() throws IOException { List<String> normalOrder = Arrays.asList( "1.7.0-to-1.8.0", "1.7.0-to-2.0.0", "1.8.0-to-2.0.0", "2.0.0-to-2.1.0", "2.0.0-to-2.2.0" ); Map<String, List<String>> upgradeGraph = SentryUpgradeOrder.readUpgradeGraph(new StringReader(StringUtils.join(normalOrder, "\n"))); assertEquals(3, upgradeGraph.size()); assertEquals(Arrays.asList("1.8.0", "2.0.0"), upgradeGraph.get("1.7.0")); assertEquals(Arrays.asList("2.0.0"), upgradeGraph.get("1.8.0")); assertEquals(Arrays.asList("2.1.0", "2.2.0"), upgradeGraph.get("2.0.0")); }
@Test public void testReadUpgradeOrderGraphWithIgnoredVersions() throws IOException { List<String> simpleOrder = Arrays.asList( "# comment", "# 1.8.0-to-1.9.0", "2.0.0-to-2.0.0", "1.8.0-to-", "-to-1.8.0", "1.8.0-1.7.0", "1.8.0-to-1.7.0-to-2.0.0", "1.7.0-to-1.8.0" ); Map<String, List<String>> upgradeGraph = SentryUpgradeOrder.readUpgradeGraph(new StringReader(StringUtils.join(simpleOrder, "\n"))); assertEquals(1, upgradeGraph.size()); assertEquals(Arrays.asList("1.8.0"), upgradeGraph.get("1.7.0")); }