### Question:
NamePredicate implements Predicate { @Override public boolean apply(Node n) { String name = ""; if (n instanceof Simulation) { name = ((Simulation) n).getName(); } else if (n instanceof SimulationPeriod) { name = ((SimulationPeriod) n).getName(); } else if (n instanceof Plan) { name = ((Plan) n).getName(); } for (Pattern pattern : patterns) { if (pattern.matcher(name).find()) { return true; } } return false; } NamePredicate(List<Pattern> patterns); @Override boolean apply(Node n);  }### Answer:
@Test public void anchored_name_pattern_matches_exact_name() { Simulation n = mock(Simulation.class); NamePredicate predicate = new NamePredicate(asList(Pattern.compile("^a name$"))); when(n.getName()).thenReturn("a name"); assertTrue(predicate.apply(n)); }
@Test public void anchored_name_pattern_does_not_match_part_of_name() { Simulation n = mock(Simulation.class); NamePredicate predicate = new NamePredicate(asList(Pattern.compile("^a name$"))); when(n.getName()).thenReturn("a name with suffix"); assertFalse(predicate.apply(n)); }
@Test public void non_anchored_name_pattern_matches_part_of_name() { Simulation n = mock(Simulation.class); NamePredicate predicate = new NamePredicate(asList(Pattern.compile("a name"))); when(n.getName()).thenReturn("a name with suffix"); assertTrue(predicate.apply(n)); }
@Test public void wildcard_name_pattern_matches_part_of_name() { Simulation n = mock(Simulation.class); NamePredicate predicate = new NamePredicate(asList(Pattern.compile("a .* name"))); when(n.getName()).thenReturn("a pickleEvent name"); assertTrue(predicate.apply(n)); }