Exemplo n.º 1
0
 /**
  * Load the Data for the given class or method. This method will try to find {@link DataLoader} on
  * either the class level or the method level. In case the annotation is found, this method will
  * load the data using the specified loader class and then save it in the DataContext for further
  * use by the system. We also create another copy of the input test data that we store in the
  * {@link DataDrivenTestRunner#writableData} field. This is done in order to facilitate the
  * writing of the data that might be returned by the test method.
  *
  * @param testClass the class object, if any.
  * @param method current executing method, if any.
  * @param currentTestClass the currently executing test class. this is used to append in front of
  *     the method name to get unique method names as there could be methods in different classes
  *     with the same name and thus we want to avoid conflicts.
  * @param writableData The writable data that is used internally for reporting purposes
  */
 public static void loadData(
     Class<?> testClass,
     FrameworkMethod method,
     TestClass currentTestClass,
     Map<String, List<Map<String, Object>>> writableData) {
   if (testClass == null && method == null) {
     Assert.fail(
         "The framework should provide either the testClass parameter or the method parameter in order to load the test data.");
   }
   // We give priority to Class Loading and then to method loading
   DataLoader testData = null;
   if (testClass != null) {
     testData = testClass.getAnnotation(DataLoader.class);
   } else {
     testData = method.getAnnotation(DataLoader.class);
   }
   if (testData != null) {
     TestInfo testInfo = DataLoaderUtil.determineLoader(testData, currentTestClass);
     Loader dataLoader = testInfo.getDataLoader();
     if (testInfo.getDataLoader() == null) {
       Assert.fail(
           "The framework currently does not support the specified Loader type. "
               + "You can provide the custom Loader by choosing LoaderType.CUSTOM in TestData "
               + "annotation and providing your custom loader using DataLoader annotation.");
     } else {
       if (testInfo.getFilePaths() == null || testInfo.getFilePaths().length == 0) {
         // implies that there exists a CUSTOM loader that loads the data using Java classes
         Map<String, List<Map<String, Object>>> data = dataLoader.loadData(new EmptyResource());
         // We also maintain the copy of the actual data for our write functionality.
         writableData.putAll(data);
         DataContext.setData(DataConverter.appendClassName(data, currentTestClass.getJavaClass()));
         DataContext.setConvertedData(
             DataConverter.convert(data, currentTestClass.getJavaClass()));
       } else {
         ResourceLoader resourceLoader =
             new ResourceLoaderStrategy(currentTestClass.getJavaClass());
         for (String filePath : testInfo.getFilePaths()) {
           Resource resource = resourceLoader.getResource(filePath);
           try {
             if (resource.exists()) {
               Map<String, List<Map<String, Object>>> data = dataLoader.loadData(resource);
               // We also maintain the copy of the actual data for our write functionality.
               writableData.putAll(data);
               DataContext.setData(
                   DataConverter.appendClassName(data, currentTestClass.getJavaClass()));
               DataContext.setConvertedData(
                   DataConverter.convert(data, currentTestClass.getJavaClass()));
             } else {
               LOG.warn(
                   "Resource {} does not exists in the specified path. If it is a classpath resource, use 'classpath:' "
                       + "before the path name, else check the path.",
                   resource);
             }
           } catch (Exception e) {
             LOG.error(
                 "Exception occured while trying to load the data for resource {}", resource, e);
             throw new RuntimeException(e);
           }
         }
       }
     }
   }
 }