0

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?

4

1 回答 1

0

似乎无法创建节或 ini 文件的副本。我最终使用了这个解决方法:

首先创建一个“空”ini 文件,它将用作一种占位符。它看起来像这样:

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

    [1]
    [2]
    [3]

...具有足够多的节数,等于或大于其他 ini 文件中的节数。

其次,读取数据提供者中的数据。当一个键包含多个值时,Ini为每个值创建一个新对象。新Ini对象必须从新文件对象创建。(您可以一遍又一遍地读取占位符文件,创建任意数量的Ini文件。)

最后,您必须将实际 ini 文件的内容复制到占位符文件中。

以下代码代码适用于我:

    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;
    }
于 2014-01-20T15:08:02.810 回答