@Test
  public void should_provide_default_file_name_when_not_specified_in_annotation() throws Exception {
    // given
    String expectedFileName = DEFAULT_FILENAME_FOR_TEST_METHOD;
    TestEvent testEvent = createTestEvent("shouldPassWithDataFileNotSpecified");
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    List<String> files =
        new ArrayList<String>(scriptsProvider.getResourceFileNames(testEvent.getTestMethod()));

    // then
    assertThat(files).containsOnly(expectedFileName);
  }
  @Test
  public void should_fetch_data_file_name_from_test_level_annotation() throws Exception {
    // given
    String expectedDataFile = SQL_DATA_SET_ON_METHOD_LEVEL;
    TestEvent testEvent = createTestEvent("shouldPassWithDataButWithoutFormatDefinedOnMethodLevel");
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    List<String> dataFiles =
        new ArrayList<String>(scriptsProvider.getResourceFileNames(testEvent.getTestMethod()));

    // then
    assertThat(dataFiles).containsOnly(expectedDataFile);
  }
  @Test(expected = InvalidResourceLocation.class)
  public void should_throw_exception_for_non_existing_file_infered_from_class_level_annotation()
      throws Exception {
    // given
    TestEvent testEvent =
        new TestEvent(
            new UsingScriptAnnotatedOnClassLevelOnlyNonExistingFile(),
            UsingScriptAnnotatedOnClassLevelOnlyNonExistingFile.class.getMethod("shouldFail"));
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    List<SqlScriptResourceDescriptor> scriptDescriptors =
        new ArrayList<SqlScriptResourceDescriptor>(
            scriptsProvider.getDescriptorsDefinedFor(testEvent.getTestMethod()));

    // then
    // exception should be thrown
  }
  @Test(expected = InvalidResourceLocation.class)
  public void should_throw_exception_for_non_existing_file_defined_on_method_level_annotation()
      throws Exception {
    // given
    TestEvent testEvent =
        new TestEvent(
            new UsingScriptOnTestMethodLevelWithNonExistingFileAndDefaultLocation(),
            UsingScriptOnTestMethodLevelWithNonExistingFileAndDefaultLocation.class.getMethod(
                "shouldFailForNonExistingFile"));
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    List<SqlScriptResourceDescriptor> scriptDescriptors =
        new ArrayList<SqlScriptResourceDescriptor>(
            scriptsProvider.getDescriptorsDefinedFor(testEvent.getTestMethod()));

    // then
    // exception should be thrown
  }
  @Test
  public void should_provide_default_file_name_when_not_specified_in_annotation_on_class_level()
      throws Exception {
    // given
    String expectedFileName =
        "before-" + ApplyScriptBeforeAnnotatedOnClassLevelOnly.class.getName() + ".sql";
    TestEvent testEvent =
        new TestEvent(
            new ApplyScriptBeforeAnnotatedOnClassLevelOnly(),
            ApplyScriptBeforeAnnotatedOnClassLevelOnly.class.getMethod("shouldPass"));
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    List<String> files =
        new ArrayList<String>(scriptsProvider.getResourceFileNames(testEvent.getTestMethod()));

    // then
    assertThat(files).containsOnly(expectedFileName);
  }
  private void doProcess(TestEvent event) {
    Object testCase = event.getTestInstance();
    TestClass testClass = event.getTestClass();
    String testMethodName = event.getTestMethod().getName();

    for (Method m : filterAndOrderMethods(testMethodName, testClass.getMethods(annotationClass))) {
      try {
        m.invoke(testCase, (Object[]) null);
      } catch (Exception e) {
        throw new IllegalStateException(
            "Failed to execute @"
                + annotationClass.getSimpleName()
                + " method ["
                + m
                + "] - cause: "
                + e,
            e);
      }
    }
  }
  @Test
  public void should_fetch_all_scripts_defined_for_test_class() throws Exception {
    // given
    TestEvent testEvent = createTestEvent("shouldPassWithDataButWithoutFormatDefinedOnMethodLevel");
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    Collection<SqlScriptResourceDescriptor> scriptDescriptors =
        scriptsProvider.getDescriptors(testEvent.getTestClass());

    // then
    SqlScriptDescriptorAssert.assertThat(scriptDescriptors)
        .containsOnlyFollowingFiles(
            SQL_DATA_SET_ON_CLASS_LEVEL,
            SQL_DATA_SET_ON_METHOD_LEVEL,
            DEFAULT_FILENAME_FOR_TEST_METHOD,
            "one.sql",
            "two.sql",
            "three.sql");
  }
  @Test
  public void should_find_file_in_default_location_if_not_specified_explicitly() throws Exception {
    // given
    FileSqlScriptResourceDescriptor expectedFile =
        new FileSqlScriptResourceDescriptor(
            defaultConfiguration.getDefaultSqlScriptLocation() + "/tables-in-scripts-folder.sql",
            defaultConfiguration.getCharset());
    TestEvent testEvent =
        new TestEvent(
            new UsingScriptOnTestMethodLevelWithNonExistingFileAndDefaultLocation(),
            UsingScriptOnTestMethodLevelWithNonExistingFileAndDefaultLocation.class.getMethod(
                "shouldPassForFileStoredInDefaultLocation"));
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    List<SqlScriptResourceDescriptor> dataSetDescriptors =
        new ArrayList<SqlScriptResourceDescriptor>(
            scriptsProvider.getDescriptorsDefinedFor(testEvent.getTestMethod()));

    // then
    assertThat(dataSetDescriptors).containsOnly(expectedFile);
  }
  @Test
  public void should_extract_all_scripts() throws Exception {
    // given
    FileSqlScriptResourceDescriptor one =
        new FileSqlScriptResourceDescriptor("one.sql", defaultConfiguration.getCharset());
    FileSqlScriptResourceDescriptor two =
        new FileSqlScriptResourceDescriptor("two.sql", defaultConfiguration.getCharset());
    FileSqlScriptResourceDescriptor three =
        new FileSqlScriptResourceDescriptor("three.sql", defaultConfiguration.getCharset());
    TestEvent testEvent =
        new TestEvent(
            new ApplyScriptBeforeAnnotatedClass(),
            ApplyScriptBeforeAnnotatedClass.class.getMethod("shouldPassWithMultipleFilesDefined"));
    SqlScriptProvider<ApplyScriptBefore> scriptsProvider = createSqlScriptProviderFor(testEvent);

    // when
    List<SqlScriptResourceDescriptor> scriptDescriptors =
        new ArrayList<SqlScriptResourceDescriptor>(
            scriptsProvider.getDescriptorsDefinedFor(testEvent.getTestMethod()));

    // then
    assertThat(scriptDescriptors).containsExactly(one, two, three);
  }
 public BusEvent(TestEvent event) {
   this.testInstance = event.getTestInstance();
   this.testMethod = event.getTestMethod();
 }
 private SqlScriptProvider<ApplyScriptBefore> createSqlScriptProviderFor(TestEvent testEvent) {
   return SqlScriptProvider.createProviderForScriptsToBeAppliedBeforeTest(
       testEvent.getTestClass(), defaultConfiguration);
 }