@Test void testResolvePackagesNoRoot() { Entity middle = mock(Entity.class); when(middle.getString(EMX_PACKAGE_NAME)).thenReturn("parent_middle"); when(middle.getString(EMX_PACKAGE_PARENT)).thenReturn("parent"); Entity child = mock(Entity.class); when(child.getString(EMX_PACKAGE_NAME)).thenReturn("parent_middle_child"); when(child.getString(EMX_PACKAGE_PARENT)).thenReturn("parent_middle"); List packageRepo = Arrays.asList(middle, child); assertThrows( MissingRootPackageException.class, () -> PackageResolver.resolvePackages(packageRepo).containsAll(packageRepo)); }
public static List<Entity> resolvePackages(Iterable<Entity> packageRepo) { List<Entity> resolved = new LinkedList<>(); if ((packageRepo == null) || Iterables.isEmpty(packageRepo)) return resolved; List<Entity> unresolved = new ArrayList<>(); Map<String, Entity> resolvedByName = new HashMap<>(); for (Entity pack : packageRepo) { String name = pack.getString(EMX_PACKAGE_NAME); String parentName = pack.getString(EMX_PACKAGE_PARENT); if (Strings.isNullOrEmpty(parentName)) { resolved.add(pack); resolvedByName.put(name, pack); } else { unresolved.add(pack); } } if (resolved.isEmpty()) { throw new MissingRootPackageException(); } List<Entity> ready = new ArrayList<>(); while (!unresolved.isEmpty()) { for (Entity pack : unresolved) { Entity parent = resolvedByName.get(pack.getString(EMX_PACKAGE_PARENT)); if (parent != null) { String name = pack.getString(EMX_PACKAGE_NAME); ready.add(pack); resolvedByName.put(name, pack); } } if (ready.isEmpty()) { throw new PackageResolveException(); } resolved.addAll(ready); unresolved.removeAll(ready); ready.clear(); } return resolved; }
PackageResolver { public static List<Entity> resolvePackages(Iterable<Entity> packageRepo) { List<Entity> resolved = new LinkedList<>(); if ((packageRepo == null) || Iterables.isEmpty(packageRepo)) return resolved; List<Entity> unresolved = new ArrayList<>(); Map<String, Entity> resolvedByName = new HashMap<>(); for (Entity pack : packageRepo) { String name = pack.getString(EMX_PACKAGE_NAME); String parentName = pack.getString(EMX_PACKAGE_PARENT); if (Strings.isNullOrEmpty(parentName)) { resolved.add(pack); resolvedByName.put(name, pack); } else { unresolved.add(pack); } } if (resolved.isEmpty()) { throw new MissingRootPackageException(); } List<Entity> ready = new ArrayList<>(); while (!unresolved.isEmpty()) { for (Entity pack : unresolved) { Entity parent = resolvedByName.get(pack.getString(EMX_PACKAGE_PARENT)); if (parent != null) { String name = pack.getString(EMX_PACKAGE_NAME); ready.add(pack); resolvedByName.put(name, pack); } } if (ready.isEmpty()) { throw new PackageResolveException(); } resolved.addAll(ready); unresolved.removeAll(ready); ready.clear(); } return resolved; } }
PackageResolver { public static List<Entity> resolvePackages(Iterable<Entity> packageRepo) { List<Entity> resolved = new LinkedList<>(); if ((packageRepo == null) || Iterables.isEmpty(packageRepo)) return resolved; List<Entity> unresolved = new ArrayList<>(); Map<String, Entity> resolvedByName = new HashMap<>(); for (Entity pack : packageRepo) { String name = pack.getString(EMX_PACKAGE_NAME); String parentName = pack.getString(EMX_PACKAGE_PARENT); if (Strings.isNullOrEmpty(parentName)) { resolved.add(pack); resolvedByName.put(name, pack); } else { unresolved.add(pack); } } if (resolved.isEmpty()) { throw new MissingRootPackageException(); } List<Entity> ready = new ArrayList<>(); while (!unresolved.isEmpty()) { for (Entity pack : unresolved) { Entity parent = resolvedByName.get(pack.getString(EMX_PACKAGE_PARENT)); if (parent != null) { String name = pack.getString(EMX_PACKAGE_NAME); ready.add(pack); resolvedByName.put(name, pack); } } if (ready.isEmpty()) { throw new PackageResolveException(); } resolved.addAll(ready); unresolved.removeAll(ready); ready.clear(); } return resolved; } private  PackageResolver(); }
PackageResolver { public static List<Entity> resolvePackages(Iterable<Entity> packageRepo) { List<Entity> resolved = new LinkedList<>(); if ((packageRepo == null) || Iterables.isEmpty(packageRepo)) return resolved; List<Entity> unresolved = new ArrayList<>(); Map<String, Entity> resolvedByName = new HashMap<>(); for (Entity pack : packageRepo) { String name = pack.getString(EMX_PACKAGE_NAME); String parentName = pack.getString(EMX_PACKAGE_PARENT); if (Strings.isNullOrEmpty(parentName)) { resolved.add(pack); resolvedByName.put(name, pack); } else { unresolved.add(pack); } } if (resolved.isEmpty()) { throw new MissingRootPackageException(); } List<Entity> ready = new ArrayList<>(); while (!unresolved.isEmpty()) { for (Entity pack : unresolved) { Entity parent = resolvedByName.get(pack.getString(EMX_PACKAGE_PARENT)); if (parent != null) { String name = pack.getString(EMX_PACKAGE_NAME); ready.add(pack); resolvedByName.put(name, pack); } } if (ready.isEmpty()) { throw new PackageResolveException(); } resolved.addAll(ready); unresolved.removeAll(ready); ready.clear(); } return resolved; } private  PackageResolver(); static List<Entity> resolvePackages(Iterable<Entity> packageRepo); }
PackageResolver { public static List<Entity> resolvePackages(Iterable<Entity> packageRepo) { List<Entity> resolved = new LinkedList<>(); if ((packageRepo == null) || Iterables.isEmpty(packageRepo)) return resolved; List<Entity> unresolved = new ArrayList<>(); Map<String, Entity> resolvedByName = new HashMap<>(); for (Entity pack : packageRepo) { String name = pack.getString(EMX_PACKAGE_NAME); String parentName = pack.getString(EMX_PACKAGE_PARENT); if (Strings.isNullOrEmpty(parentName)) { resolved.add(pack); resolvedByName.put(name, pack); } else { unresolved.add(pack); } } if (resolved.isEmpty()) { throw new MissingRootPackageException(); } List<Entity> ready = new ArrayList<>(); while (!unresolved.isEmpty()) { for (Entity pack : unresolved) { Entity parent = resolvedByName.get(pack.getString(EMX_PACKAGE_PARENT)); if (parent != null) { String name = pack.getString(EMX_PACKAGE_NAME); ready.add(pack); resolvedByName.put(name, pack); } } if (ready.isEmpty()) { throw new PackageResolveException(); } resolved.addAll(ready); unresolved.removeAll(ready); ready.clear(); } return resolved; } private  PackageResolver(); static List<Entity> resolvePackages(Iterable<Entity> packageRepo);  }