Search code examples
javacsvjunit4bufferedreader

Returning two arguments from a csv list using BufferedReader and splitting on comma


Forgive the beginners question but I'm a tester first and foremost and am struggling to comprehend how to work through this. I'm simply trying to populate some test data by reading a partial website url and its corresponding syndicator login ID by using a buffered input stream reader and returning two values from each line, split by a comma.

Here's my csv:

 website1.uk, website1syndicator
 website2.uk, website2syndicator
 website3.uk, website3syndicator

Here's my class to read the csv and populate List with one String element:

public class AbstractTestAllSites extends PageBase {

private static Logger log = LoggerFactory.getLogger(AbstractTestAllSites.class);

private static List<String> allWebsiteNames;

static {
    try (InputStream websiteListInputStream = AbstractTestAllSites.class.getResourceAsStream("/websites/my_sites.csv")) {
        readAllWebsiteNamesFrom(websiteListInputStream);
    } catch (IOException e) {
        log.error("Failed to read websitelist!", e);
    }
}

private static void readAllWebsiteNamesFrom(InputStream input) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
    List<String> websites = new ArrayList<String>();
    String listLine;
    while ((listLine = reader.readLine()) != null) {
        listLine = listLine.trim();
        if (!(listLine.startsWith("#") || isBlank(listLine))) {
            websites.add(listLine);
        }
    }
    allWebsiteNames = unmodifiableList(websites);
}

@Parameterized.Parameters
public static final List<String> data() {
    return allWebsiteNames;
}

}

I can then pass the website endpoints into my test like so:

private static final String url = "http://mydomain.";
private String website;
private String syndicator;
public static WebDriver driver;

public TestAllSitesTest(String website, String syndicator){
    this.website = website;
    this.syndicator = syndicator;
}

@Before
public void getNextWebsite(){
    driver.get(url + this.website);
}
//run my tests here...

...and iterate over them until done. But how can I pass two params in so I can access the syndicator variable - probably need a HashMap or similar and then split on the comma but struggling a bit.


Solution

  • In case you want to know how to split each line of your csv file in order to create an object of class TestAllSitesTest (which has a constructor taking a website and a syndicator), you can do it as follows (at the desired position in your code, this is just a main method showing an example):

    public static void main(String[] args) {
        // create two ArrayLists, first one containing lines, second containing desired objects
        List<String> websites = new ArrayList<String>();
        List<TestAllSitesTest> testAllSitesTests = new ArrayList<TestAllSitesTest>();
    
        // add csv lines to the first ArrayList
        websites.add("website1.uk, website1syndicator");
        websites.add("website2.uk, website2syndicator");
        websites.add("website3.uk, website3syndicator");
    
        // iterate the list containing the csv lines
        websites.forEach((String website) -> {
            // split one line into the desired two parts, eliminating comma and space
            String[] splitWebsite = website.split(", ");
            // create a new object passing the parts of the split line as constructor parameters
            TestAllSitesTest test = new TestAllSitesTest(splitWebsite[0], splitWebsite[1]);
            testAllSitesTests.add(test);
        });
    
        // print the resulting objects
        testAllSitesTests.forEach((TestAllSitesTest t) -> {
            System.out.println("Website: " + t.getWebsite()
                    + ", Syndicator: " + t.getSyndicator());
        });
    }
    

    I hope this helps…