Question

I am reading from ini files and passing them via data providers to test cases.

(The data provider reads these and returns an Ini.Section[][] array. If there are several sections, testng runs the test that many times.)

Let's imagine there is a section like this:

[sectionx]
key1=111
key2=222
key3=aaa,bbb,ccc

What I want, in the end, is to read this data and execute the test case three times, each time with a different value of key3, the other keys being the same.

One way would be to copy&paste the section as many times as needed... which is clearly not an ideal solution.

The way to go about it would seem to create further copies of the section, then change the key values to aaa, bbb and ccc. The data provider would return the new array and testng would do the rest.

However, I cannot seem to be able to create a new instance of the section object. Ini.Section is actually an interface; the implementing class org.ini4j.BasicProfileSection is not visible. It does not appear to be possible to create a copy of the object, or to inherit the class. I can only manipulate existing objects of this type, but not create new ones. Is there any way around it?

Was it helpful?

Solution

It seems that it is not possible to create copies of sections or the ini files. I ended up using this workaround:

First create an 'empty' ini file, that will serve as a sort of a placeholder. It will look like this:

    [env]
    test1=1
    test2=2
    test3=3

    [1]
    [2]
    [3]

...with a sufficiently large number of sections, equal or greater to the number of sections in the other ini files.

Second, read the data in the data provider. When there is a key that contains several values, create a new Ini object for each value. The new Ini object must be created from a new file object. (You can read the placeholder file over and over, creating any number of Ini files.)

Finally, you have to copy the content of the actual ini file into the placeholder file.

The following code code works for me:

    public static Ini copyIniFile(Ini originalFile){
        Set<Entry<String, Section>> entries = originalFile.entrySet();
        Ini emptyFile;
        try {
            FileInputStream file = new FileInputStream(new File(EMPTY_DATA_FILE_NAME));
            emptyFile = new Ini(file);
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        for(Entry<String, Section> entry : entries){
            String key = (String) entry.getKey();
            Section section = (Section) entry.getValue();
            copySection(key, section, emptyFile);
        }

        return emptyFile;
    }

    public static Ini.Section copySection(String key, Ini.Section origin, Ini destinationFile){
        Ini.Section newSection = destinationFile.get(key);
        if(newSection==null) throw new IllegalArgumentException();
        for(Entry<String, String> entry : origin.entrySet()){
            newSection.put(entry.getKey().toString(), entry.getValue().toString());
        }
        return newSection;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top