@Test
  public void testGetFilteredExtensions() {
    Sensor sensor1 = new FakeSensor();
    Sensor sensor2 = new FakeSensor();
    Decorator decorator = mock(Decorator.class);

    BatchExtensionDictionnary selector = newSelector(sensor1, sensor2, decorator);
    Collection<Sensor> sensors = selector.select(Sensor.class, null, true, null);

    assertThat(sensors).containsOnly(sensor1, sensor2);
  }
 @Test(expected = IllegalStateException.class)
 public void annotatedMethodsCanNotBePrivate() {
   BatchExtensionDictionnary selector = newSelector();
   BatchExtension wrong =
       new BatchExtension() {
         @DependsUpon
         private Object foo() {
           return "foo";
         }
       };
   selector.evaluateAnnotatedClasses(wrong, DependsUpon.class);
 }
  @Test
  public void checkProject() {
    BatchExtension ok = new CheckProjectOK();
    BatchExtension ko = new CheckProjectKO();

    BatchExtensionDictionnary selector = newSelector(ok, ko);
    List<BatchExtension> extensions =
        Lists.newArrayList(selector.select(BatchExtension.class, new Project("key"), true, null));

    assertThat(extensions).hasSize(1);
    assertThat(extensions.get(0)).isInstanceOf(CheckProjectOK.class);
  }
  @Test
  public void buildStatusCheckersAreExecutedAfterOtherPostJobs() {
    BuildBreaker checker =
        new BuildBreaker() {
          public void executeOn(Project project, SensorContext context) {}
        };

    BatchExtensionDictionnary selector = newSelector(new FakePostJob(), checker, new FakePostJob());
    List extensions = Lists.newArrayList(selector.select(PostJob.class, null, true, null));

    assertThat(extensions).hasSize(3);
    assertThat(extensions.get(2)).isEqualTo(checker);
  }
  @Test
  public void dependsUponInheritedPhase() {
    BatchExtension pre = new PreSensorSubclass();
    BatchExtension analyze = new GeneratesSomething("something");
    BatchExtension post = new PostSensorSubclass();

    BatchExtensionDictionnary selector = newSelector(analyze, post, pre);
    List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(3);
    assertThat(extensions.get(0)).isEqualTo(pre);
    assertThat(extensions.get(1)).isEqualTo(analyze);
    assertThat(extensions.get(2)).isEqualTo(post);
  }
  @Test
  public void sortExtensionsByDependency() {
    BatchExtension a = new MethodDependentOf(null);
    BatchExtension b = new MethodDependentOf(a);
    BatchExtension c = new MethodDependentOf(b);

    BatchExtensionDictionnary selector = newSelector(b, c, a);
    List<BatchExtension> extensions =
        Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(3);
    assertThat(extensions.get(0)).isEqualTo(a);
    assertThat(extensions.get(1)).isEqualTo(b);
    assertThat(extensions.get(2)).isEqualTo(c);
  }
  @Test
  public void shouldSearchInParentContainers() {
    Sensor a = new FakeSensor();
    Sensor b = new FakeSensor();
    Sensor c = new FakeSensor();

    ComponentContainer grandParent = new ComponentContainer();
    grandParent.addSingleton(a);

    ComponentContainer parent = grandParent.createChild();
    parent.addSingleton(b);

    ComponentContainer child = parent.createChild();
    child.addSingleton(c);

    BatchExtensionDictionnary dictionnary =
        new BatchExtensionDictionnary(
            child, mock(DefaultSensorContext.class), mock(SensorOptimizer.class));
    assertThat(dictionnary.select(Sensor.class, null, true, null)).containsOnly(a, b, c);
  }
  @Test
  public void testGetFilteredExtensionWithExtensionMatcher() {
    final Sensor sensor1 = new FakeSensor();
    final Sensor sensor2 = new FakeSensor();

    BatchExtensionDictionnary selector = newSelector(sensor1, sensor2);
    Collection<Sensor> sensors =
        selector.select(
            Sensor.class,
            null,
            true,
            new ExtensionMatcher() {
              @Override
              public boolean accept(Object extension) {
                return extension.equals(sensor1);
              }
            });

    assertThat(sensors).contains(sensor1);
    assertEquals(1, sensors.size());
  }
  @Test
  public void inheritAnnotations() {
    BatchExtension a = new SubClass("foo");
    BatchExtension b = new MethodDependentOf("foo");

    BatchExtensionDictionnary selector = newSelector(b, a);
    List<BatchExtension> extensions =
        Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(2);
    assertThat(extensions.get(0)).isEqualTo(a);
    assertThat(extensions.get(1)).isEqualTo(b);

    // change initial order
    selector = newSelector(a, b);
    extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(2);
    assertThat(extensions.get(0)).isEqualTo(a);
    assertThat(extensions.get(1)).isEqualTo(b);
  }
  @Test
  public void useClassAnnotationsOnInterfaces() {
    BatchExtension a = new InterfaceDependedUpon() {};
    BatchExtension b = new InterfaceDependsUpon() {};

    BatchExtensionDictionnary selector = newSelector(a, b);
    List<BatchExtension> extensions =
        Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(2);
    assertThat(extensions.get(0)).isEqualTo(a);
    assertThat(extensions.get(1)).isEqualTo(b);

    // different initial order
    selector = newSelector(b, a);
    extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(2);
    assertThat(extensions.get(0)).isEqualTo(a);
    assertThat(extensions.get(1)).isEqualTo(b);
  }
  @Test
  public void methodDependsUponArray() {
    BatchExtension a = new GeneratesSomething("foo");
    BatchExtension b = new MethodDependentOf(new String[] {"foo"});

    BatchExtensionDictionnary selector = newSelector(a, b);
    List<BatchExtension> extensions =
        Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(2);
    assertThat(extensions.get(0)).isEqualTo(a);
    assertThat(extensions.get(1)).isEqualTo(b);

    // different initial order
    selector = newSelector(b, a);
    extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));

    assertThat(extensions).hasSize(2);
    assertThat(extensions.get(0)).isEqualTo(a);
    assertThat(extensions.get(1)).isEqualTo(b);
  }