Пример #1
0
  /**
   * Loads the data from a file
   *
   * @param fs file system
   * @param fpath data file path
   * @throws IOException if any problem is encountered
   */
  public static Data loadData(Dataset dataset, FileSystem fs, Path fpath) throws IOException {
    FSDataInputStream input = fs.open(fpath);
    Scanner scanner = new Scanner(input);

    List<Instance> instances = Lists.newArrayList();

    DataConverter converter = new DataConverter(dataset);

    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      if (line.isEmpty()) {
        log.warn("{}: empty string", instances.size());
        continue;
      }

      Instance instance = converter.convert(instances.size(), line);
      if (instance == null) {
        // missing values found
        log.warn("{}: missing values", instances.size());
        continue;
      }

      instances.add(instance);
    }

    scanner.close();

    return new Data(dataset, instances);
  }
Пример #2
0
  /** Loads the data from a String array */
  public static Data loadData(Dataset dataset, String[] data) {
    List<Instance> instances = new ArrayList<>();

    DataConverter converter = new DataConverter(dataset);

    for (String line : data) {
      if (!line.isEmpty()) {
        Instance instance = converter.convert(line);
        if (instance != null) {
          instances.add(instance);
        } else {
          // missing values found
          log.warn("{}: missing values", instances.size());
        }
      } else {
        log.warn("{}: empty string", instances.size());
      }
    }

    return new Data(dataset, instances);
  }
Пример #3
0
  /** Loads the data from a String array */
  public static Data loadData(Dataset dataset, String[] data) {
    List<Instance> instances = Lists.newArrayList();

    DataConverter converter = new DataConverter(dataset);

    for (String line : data) {
      if (line.isEmpty()) {
        log.warn("{}: empty string", instances.size());
        continue;
      }

      Instance instance = converter.convert(instances.size(), line);
      if (instance == null) {
        // missing values found
        log.warn("{}: missing values", instances.size());
        continue;
      }

      instances.add(instance);
    }

    return new Data(dataset, instances);
  }
Пример #4
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);
           }
         }
       }
     }
   }
 }