InstrumentModule { @Provides static ImmutableSet<InstrumentedMethod> provideInstrumentedMethods( CaliperOptions options, BenchmarkClassModel benchmarkClass, ImmutableSet<Instrument> instruments) throws InvalidBenchmarkException { ImmutableSet.Builder<InstrumentedMethod> builder = ImmutableSet.builder(); ImmutableSet<String> benchmarkMethodNames = options.benchmarkMethodNames(); Set<String> unusedBenchmarkNames = new HashSet<String>(benchmarkMethodNames); for (Instrument instrument : instruments) { for (MethodModel method : findAllBenchmarkMethods(benchmarkClass, instrument)) { if (benchmarkMethodNames.isEmpty() || benchmarkMethodNames.contains(method.name())) { builder.add(instrument.createInstrumentedMethod(method)); unusedBenchmarkNames.remove(method.name()); } } } if (!unusedBenchmarkNames.isEmpty()) { throw new InvalidBenchmarkException( "Invalid benchmark method(s) specified in options: %s", unusedBenchmarkNames); } return builder.build(); } private  InstrumentModule();   }
@Test public void provideInstrumentedMethods_noNames() throws Exception { when(options.benchmarkMethodNames()).thenReturn(ImmutableSet.<String>of()); assertEquals( new ImmutableSet.Builder<InstrumentedMethod>() .add(instrumentA.createInstrumentedMethod(methodA)) .add(instrumentA.createInstrumentedMethod(methodB)) .add(instrumentA.createInstrumentedMethod(methodC)) .add(instrumentB.createInstrumentedMethod(methodA)) .add(instrumentB.createInstrumentedMethod(methodB)) .add(instrumentB.createInstrumentedMethod(methodC)) .build(), InstrumentModule.provideInstrumentedMethods( options, TEST_BENCHMARK_MODEL, ImmutableSet.of(instrumentA, instrumentB))); }
@SuppressWarnings("unchecked") @Test public void provideInstrumentedMethods_withNames() throws Exception { when(options.benchmarkMethodNames()) .thenReturn(ImmutableSet.of("b"), ImmutableSet.of("a", "c")); assertEquals( new ImmutableSet.Builder<InstrumentedMethod>() .add(instrumentA.createInstrumentedMethod(methodB)) .add(instrumentB.createInstrumentedMethod(methodB)) .build(), InstrumentModule.provideInstrumentedMethods( options, TEST_BENCHMARK_MODEL, ImmutableSet.of(instrumentA, instrumentB))); assertEquals( new ImmutableSet.Builder<InstrumentedMethod>() .add(instrumentA.createInstrumentedMethod(methodA)) .add(instrumentA.createInstrumentedMethod(methodC)) .add(instrumentB.createInstrumentedMethod(methodA)) .add(instrumentB.createInstrumentedMethod(methodC)) .build(), InstrumentModule.provideInstrumentedMethods( options, TEST_BENCHMARK_MODEL, ImmutableSet.of(instrumentA, instrumentB))); }
@Test public void provideInstrumentedMethods_withInvalidName() { when(options.benchmarkMethodNames()).thenReturn(ImmutableSet.of("a", "c", "bad")); try { InstrumentModule.provideInstrumentedMethods( options, TEST_BENCHMARK_MODEL, ImmutableSet.of(instrumentA, instrumentB)); fail("should have thrown for invalid benchmark method name"); } catch (Exception expected) { assertThat(expected.getMessage()).contains("[bad]"); } }