Class CsvDatabaseReader

java.lang.Object
com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReader

public class CsvDatabaseReader extends Object
A class to read a CSV file and populate a Spring Data CrudRepository using the information in the file.

This is useful for testing in-memory repositories using the same test cases as for real repository implementations, and also for populating in-memory repositories for testing services, without having to use a real database.

The file is assumed to have a header containing the names of the database columns to populate, followed by the data rows. An example:

     id;name
     1;foo
     2;bar
 

Author:
RealLifeDeveloper
  • Constructor Summary

    Constructors
    Constructor
    Description
    CsvDatabaseReader(char csvSeparatorCharacter, int csvSkipLines)
    Creates a new CsvDatabaseReader with the given configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T, E, ID extends Serializable>
    void
    read(String resourceName, org.springframework.data.repository.CrudRepository<T,ID> repository, Class<T> repositoryEntityType, @Nullable Class<E> entityType, String tableName)
    Reads a CSV file from the named resource, populating the given repository with entities of the given type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CsvDatabaseReader

      public CsvDatabaseReader(char csvSeparatorCharacter, int csvSkipLines)
      Creates a new CsvDatabaseReader with the given configuration.
      Parameters:
      csvSeparatorCharacter - the separator character to use when reading the file, normally ',' or ';'
      csvSkipLines - the number of lines to skip at the beginning of the file
  • Method Details

    • read

      public <T, E, ID extends Serializable> void read(String resourceName, org.springframework.data.repository.CrudRepository<T,ID> repository, Class<T> repositoryEntityType, @Nullable Class<E> entityType, String tableName) throws IOException, com.opencsv.exceptions.CsvException
      Reads a CSV file from the named resource, populating the given repository with entities of the given type.
      Type Parameters:
      T - the type of entities in the repository
      E - the type of entity being read
      ID - the type of the primary key of the entities in the repository
      Parameters:
      resourceName - the classpath resource containing a CSV file
      repository - the repository to populate with the entities from the CSV file
      repositoryEntityType - the class object representing <T>, i.e., the class of entities in the repository
      entityType - the class object representing <E>, i.e., the class of entity being read
      tableName - the name of the database table to use; may be either the table associated with the entity, or a join table
      Throws:
      IOException - if reading the file failed
      com.opencsv.exceptions.CsvException - if parsing the file failed