--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/880bac57-571e-456b-8ea5-7039ceb43ab8n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/c01c072a-0026-42d0-8330-4594279cf81dn%40googlegroups.com.
Here’s a full-fledged sample along with required javadocs to help you understand what I am referring to.
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class ExampleFactory {
private final Map<String, String> data = new HashMap<>();
private final String fileName;
/**
* This constructor accepts the file type and the file name from which test data should be read.
*
* @param fileName - The relative location of the file.
* @param fileType - A {@link FileType} that represents the type of the file.
* @throws IOException - In case of problems
*/
@Factory(dataProvider = "files")
public ExampleFactory(String fileName, FileType fileType) throws IOException {
this.fileName = fileName;
data.putAll(fileType.load(fileName));
}
/**
* This static data provider basically lets TestNG create Test class instances. It passes the
* location of the file and its type that a test class instance should use to basically have its
* own data provider run iterations based off of.
*
* @return - A 2D array of objects
*/
@DataProvider(name = "files")
public static Object[][] files() {
return new Object[][]{
{"src/test/resources/props.json", FileType.JSON},
{"src/test/resources/props.properties", FileType.PROPERTIES},
};
}
/**
* A regular test method
*/
@Test(dataProvider = "testData")
public void testMethod(String k, String v) {
System.err.println(fileName + " => (" + k + ", " + v + ")");
}
/**
* A regular data provider which converts a {@link HashMap} into a 2D array wherein every array
* consists of key/value pair.
*/
@DataProvider(name = "testData")
public Object[][] testData() {
Object[][] result = new Object[data.size()][2];
AtomicInteger counter = new AtomicInteger(0);
data.forEach((k, v) -> {
result[counter.get()][0] = k;
result[counter.get()][1] = v;
counter.incrementAndGet();
});
return result;
}
/**
* An enum that represents the supported file types.
*/
enum FileType implements DataLoader {
JSON {
@Override
public Map<String, String> load(String fileName) throws IOException {
JsonParser parser = new JsonParser();
JsonElement parsed = parser.parse(new FileReader(fileName));
Map<String, String> result = new HashMap<>();
parsed.getAsJsonObject().entrySet()
.forEach(each -> result.put(each.getKey(), each.getValue().getAsString()));
return result;
}
},
PROPERTIES {
@Override
public Map<String, String> load(String fileName) throws IOException {
Properties properties = new Properties();
properties.load(new FileReader(fileName));
Map<String, String> result = new HashMap<>();
properties.forEach((key, value) -> result.put(key.toString(), value.toString()));
return result;
}
}
}
/**
* Represents the capabilities to be possed by a data source loader.
*/
interface DataLoader {
/**
* @param fileName - The relative location of the file from the file system from which data is
* to be read from.
* @return - A {@link Map} of the read data.
* @throws IOException - In case of problems.
*/
Map<String, String> load(String fileName) throws IOException;
}
}
To view this discussion on the web visit
https://groups.google.com/d/msgid/testng-users/d2f9b81f-e7be-46d5-be87-78162fb26a60n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/e5eb86be-efc6-4bd5-acc9-b33664699754n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/e5e74f16-f09e-414d-a647-48ef03b0bd41n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/c944b66a-baba-4ec0-9423-e4b34cb32cban%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/3e1a2172-0050-44b5-b355-8696901b6ba5n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/a10ef7c4-f3ec-463d-be23-deaeb2a70dc6n%40googlegroups.com.