### Question:
DependenciesScheduler { List<Collection<T>> schedule(Collection<DependsOn<T>> dependencies) { log.info("Scheduling: {}", dependencies); Map<T,Collection<T>> prerequisites = Maps.newHashMapWithExpectedSize(dependencies.size()); for (DependsOn<T> dependency : dependencies) { T first = dependency.getHappensFirst(); T next = dependency.getHappensNext(); if (!prerequisites.containsKey(first)) { prerequisites.put(first, Lists.<T>newArrayList()); } if (next != null) { Collection<T> required = prerequisites.get(next); if (required == null) { required = Lists.newArrayList(); prerequisites.put(next, required); } required.add(first); } } return schedule(prerequisites); }    }### Answer:
@Test public void testEmpty() { DependenciesScheduler<String> scheduler = new DependenciesScheduler<String>(); Collection<Collection<String>> schedule = scheduler.schedule(Collections.<DependsOn<String>>emptySet()); assertTrue(schedule.isEmpty()); }
@Test public void testOne() { DependenciesScheduler<String> scheduler = new DependenciesScheduler<String>(); List<Collection<String>> schedule = scheduler.schedule(Collections.singleton(new DependsOn<String>("foo"))); assertEquals(1, schedule.size()); assertEquals(Sets.newHashSet(Collections.singleton("foo")), Sets.newHashSet(schedule.get(0))); }
@Test public void testSequence() { DependenciesScheduler<String> scheduler = new DependenciesScheduler<String>(); List<Collection<String>> schedule = scheduler.schedule(Collections.singleton(new DependsOn<String>("foo", "bar"))); assertEquals(2, schedule.size()); assertEquals(Sets.newHashSet(Collections.singleton("bar")), Sets.newHashSet(schedule.get(0))); assertEquals(Sets.newHashSet(Collections.singleton("foo")), Sets.newHashSet(schedule.get(1))); }
@Test public void testParallel() { Collection<DependsOn<String>> dependencies = Lists.newArrayListWithCapacity(2); dependencies.add(new DependsOn<String>("foo")); dependencies.add(new DependsOn<String>("bar")); DependenciesScheduler<String> scheduler = new DependenciesScheduler<String>(); List<Collection<String>> schedule = scheduler.schedule(dependencies); assertEquals(1, schedule.size()); assertEquals(Sets.newHashSet(Arrays.asList("foo", "bar")), Sets.newHashSet(schedule.get(0))); }
@Test public void testComplex() { Collection<DependsOn<String>> dependencies = Lists.newArrayListWithCapacity(4); dependencies.add(new DependsOn<String>("foo", "bar")); dependencies.add(new DependsOn<String>("foo", "buzz")); dependencies.add(new DependsOn<String>("bing", "foo")); dependencies.add(new DependsOn<String>("bong", "foo")); DependenciesScheduler<String> scheduler = new DependenciesScheduler<String>(); List<Collection<String>> schedule = scheduler.schedule(dependencies); assertEquals(3, schedule.size()); assertEquals(Sets.newHashSet(Arrays.asList("bar", "buzz")), Sets.newHashSet(schedule.get(0))); assertEquals(Sets.newHashSet(Collections.singleton("foo")), Sets.newHashSet(schedule.get(1))); assertEquals(Sets.newHashSet(Arrays.asList("bing", "bong")), Sets.newHashSet(schedule.get(2))); }