How to use SetMultiMap class of org.testng.collections package

Best Testng code snippet using org.testng.collections.SetMultiMap

Source:JUnitReportReporter.java Github

copy

Full Screen

...23import org.testng.Reporter;24import org.testng.collections.ListMultiMap;25import org.testng.collections.Lists;26import org.testng.collections.Maps;27import org.testng.collections.SetMultiMap;28import org.testng.collections.Sets;29import org.testng.internal.Utils;30import org.testng.xml.XmlSuite;31public class JUnitReportReporter implements IReporter {32 @Override33 public void generateReport(34 List<XmlSuite> xmlSuites, List<ISuite> suites, String defaultOutputDirectory) {35 Map<Class<?>, Set<ITestResult>> results = Maps.newHashMap();36 ListMultiMap<Object, ITestResult> befores = Maps.newListMultiMap();37 ListMultiMap<Object, ITestResult> afters = Maps.newListMultiMap();38 SetMultiMap<Class<?>, ITestNGMethod> mapping = new SetMultiMap<>(false);39 for (ISuite suite : suites) {40 Map<String, ISuiteResult> suiteResults = suite.getResults();41 addMapping(mapping, suite.getExcludedMethods());42 for (ISuiteResult sr : suiteResults.values()) {43 ITestContext tc = sr.getTestContext();44 addResults(tc.getPassedTests().getAllResults(), results);45 addResults(tc.getFailedTests().getAllResults(), results);46 addResults(tc.getSkippedTests().getAllResults(), results);47 addResults(tc.getFailedConfigurations().getAllResults(), results);48 for (ITestResult tr : tc.getPassedConfigurations().getAllResults()) {49 if (tr.getMethod().isBeforeMethodConfiguration()) {50 befores.put(tr.getInstance(), tr);51 }52 if (tr.getMethod().isAfterMethodConfiguration()) {53 afters.put(tr.getInstance(), tr);54 }55 }56 }57 }58 for (Map.Entry<Class<?>, Set<ITestResult>> entry : results.entrySet()) {59 Class<?> cls = entry.getKey();60 Properties p1 = new Properties();61 p1.setProperty(XMLConstants.ATTR_NAME, cls.getName());62 p1.setProperty(XMLConstants.ATTR_TIMESTAMP, JUnitXMLReporter.formattedTime());63 List<TestTag> testCases = Lists.newArrayList();64 int failures = 0;65 int errors = 0;66 int skipped = 0;67 int testCount = 0;68 float totalTime = 0;69 Collection<ITestResult> iTestResults = sort(entry.getValue());70 for (ITestResult tr : iTestResults) {71 long time = tr.getEndMillis() - tr.getStartMillis();72 time += getNextConfiguration(befores, tr);73 time += getNextConfiguration(afters, tr);74 Throwable t = tr.getThrowable();75 switch (tr.getStatus()) {76 case ITestResult.SKIP:77 case ITestResult.SUCCESS_PERCENTAGE_FAILURE:78 skipped++;79 break;80 case ITestResult.FAILURE:81 if (t instanceof AssertionError) {82 failures++;83 } else {84 errors++;85 }86 break;87 }88 totalTime += time;89 testCount++;90 TestTag testTag = createTestTagFor(tr, cls);91 testTag.properties.setProperty(XMLConstants.ATTR_TIME, "" + formatTime(time));92 testCases.add(testTag);93 }94 int ignored = getDisabledTestCount(mapping.get(entry.getKey()));95 for (ITestNGMethod eachMethod : mapping.get(entry.getKey())) {96 testCases.add(createIgnoredTestTagFor(eachMethod));97 }98 p1.setProperty(XMLConstants.ATTR_FAILURES, Integer.toString(failures));99 p1.setProperty(XMLConstants.ATTR_ERRORS, Integer.toString(errors));100 p1.setProperty(XMLConstants.SKIPPED, Integer.toString(skipped + ignored));101 p1.setProperty(XMLConstants.ATTR_NAME, cls.getName());102 p1.setProperty(XMLConstants.ATTR_TESTS, Integer.toString(testCount + ignored));103 p1.setProperty(XMLConstants.ATTR_TIME, "" + formatTime(totalTime));104 try {105 p1.setProperty(XMLConstants.ATTR_HOSTNAME, InetAddress.getLocalHost().getHostName());106 } catch (UnknownHostException e) {107 // ignore108 }109 //110 // Now that we have all the information we need, generate the file111 //112 XMLStringBuffer xsb = new XMLStringBuffer();113 xsb.addComment("Generated by " + getClass().getName());114 xsb.push(XMLConstants.TESTSUITE, p1);115 for (TestTag testTag : testCases) {116 if (putElement(xsb, XMLConstants.TESTCASE, testTag.properties, testTag.childTag != null)) {117 Properties p = new Properties();118 safeSetProperty(p, XMLConstants.ATTR_MESSAGE, testTag.message);119 safeSetProperty(p, XMLConstants.ATTR_TYPE, testTag.type);120 if (putElement(xsb, testTag.childTag, p, testTag.stackTrace != null)) {121 xsb.addCDATA(testTag.stackTrace);122 xsb.pop(testTag.childTag);123 }124 xsb.pop(XMLConstants.TESTCASE);125 }126 if (putElement(xsb, XMLConstants.SYSTEM_OUT, new Properties(), testTag.sysOut != null)) {127 xsb.addCDATA(testTag.sysOut);128 xsb.pop(XMLConstants.SYSTEM_OUT);129 }130 }131 xsb.pop(XMLConstants.TESTSUITE);132 String outputDirectory = defaultOutputDirectory + File.separator + "junitreports";133 Utils.writeUtf8File(outputDirectory, getFileName(cls), xsb.toXML());134 }135 }136 private static Collection<ITestResult> sort(Set<ITestResult> results) {137 List<ITestResult> sortedResults = new ArrayList<>(results);138 sortedResults.sort(Comparator.comparingInt(o -> o.getMethod().getPriority()));139 return Collections.unmodifiableList(sortedResults);140 }141 private static int getDisabledTestCount(Set<ITestNGMethod> methods) {142 int count = 0;143 for (ITestNGMethod method : methods) {144 if (!method.getEnabled()) {145 count = count + 1;146 }147 }148 return count;149 }150 private TestTag createIgnoredTestTagFor(ITestNGMethod method) {151 TestTag testTag = new TestTag();152 Properties p2 = new Properties();153 p2.setProperty(XMLConstants.ATTR_CLASSNAME, method.getRealClass().getName());154 p2.setProperty(XMLConstants.ATTR_NAME, method.getMethodName());155 testTag.childTag = XMLConstants.SKIPPED;156 testTag.properties = p2;157 return testTag;158 }159 private TestTag createTestTagFor(ITestResult tr, Class<?> cls) {160 TestTag testTag = new TestTag();161 Properties p2 = new Properties();162 p2.setProperty(XMLConstants.ATTR_CLASSNAME, cls.getName());163 p2.setProperty(XMLConstants.ATTR_NAME, getTestName(tr));164 int status = tr.getStatus();165 if (status == ITestResult.SKIP || status == ITestResult.SUCCESS_PERCENTAGE_FAILURE) {166 testTag.childTag = XMLConstants.SKIPPED;167 } else if (status == ITestResult.FAILURE) {168 handleFailure(testTag, tr.getThrowable());169 }170 List<String> output = Reporter.getOutput(tr);171 if (!output.isEmpty()) {172 testTag.sysOut = String.join("\n", output);173 }174 testTag.properties = p2;175 return testTag;176 }177 private static void handleFailure(TestTag testTag, Throwable t) {178 testTag.childTag = t instanceof AssertionError ? XMLConstants.FAILURE : XMLConstants.ERROR;179 if (t != null) {180 StringWriter sw = new StringWriter();181 PrintWriter pw = new PrintWriter(sw);182 t.printStackTrace(pw);183 testTag.message = t.getMessage();184 testTag.type = t.getClass().getName();185 testTag.stackTrace = sw.toString();186 }187 }188 /** Put a XML start or empty tag to the XMLStringBuffer depending on hasChildElements parameter */189 private boolean putElement(190 XMLStringBuffer xsb, String tagName, Properties attributes, boolean hasChildElements) {191 if (hasChildElements) {192 xsb.push(tagName, attributes);193 } else {194 xsb.addEmptyElement(tagName, attributes);195 }196 return hasChildElements;197 }198 /** Set property if value is non-null */199 private void safeSetProperty(Properties p, String key, String value) {200 if (value != null) {201 p.setProperty(key, value);202 }203 }204 /**205 * Add the time of the configuration method to this test method.206 *207 * <p>The only problem with this method is that the timing of a test method might not be added to208 * the time of the same configuration method that ran before it but since they should all be209 * equivalent, this should never be an issue.210 */211 private long getNextConfiguration(212 ListMultiMap<Object, ITestResult> configurations, ITestResult tr) {213 long result = 0;214 List<ITestResult> confResults = configurations.get(tr.getInstance());215 Map<ITestNGMethod, ITestResult> seen = Maps.newHashMap();216 for (ITestResult r : confResults) {217 if (!seen.containsKey(r.getMethod())) {218 result += r.getEndMillis() - r.getStartMillis();219 seen.put(r.getMethod(), r);220 }221 }222 confResults.removeAll(seen.values());223 return result;224 }225 protected String getFileName(Class cls) {226 return "TEST-" + cls.getName() + ".xml";227 }228 protected String getTestName(ITestResult tr) {229 return tr.getMethod().getMethodName();230 }231 private String formatTime(float time) {232 DecimalFormatSymbols symbols = new DecimalFormatSymbols();233 // JUnitReports wants points here, regardless of the locale234 symbols.setDecimalSeparator('.');235 DecimalFormat format = new DecimalFormat("#.###", symbols);236 format.setMinimumFractionDigits(3);237 return format.format(time / 1000.0f);238 }239 private static class TestTag {240 Properties properties;241 String message;242 String type;243 String stackTrace;244 String childTag;245 String sysOut;246 }247 private void addResults(Set<ITestResult> allResults, Map<Class<?>, Set<ITestResult>> out) {248 for (ITestResult tr : allResults) {249 Class<?> cls = tr.getMethod().getTestClass().getRealClass();250 Set<ITestResult> l = out.computeIfAbsent(cls, k -> Sets.newHashSet());251 l.add(tr);252 }253 }254 private void addMapping(255 SetMultiMap<Class<?>, ITestNGMethod> mapping, Collection<ITestNGMethod> methods) {256 for (ITestNGMethod method : methods) {257 if (!method.getEnabled()) {258 mapping.put(method.getRealClass(), method);259 }260 }261 }262}...

Full Screen

Full Screen

Source:Model.java Github

copy

Full Screen

...6import org.testng.ITestResult;7import org.testng.collections.ListMultiMap;8import org.testng.collections.Lists;9import org.testng.collections.Maps;10import org.testng.collections.SetMultiMap;11import org.testng.internal.Utils;12import java.util.Arrays;13import java.util.Collections;14import java.util.List;15import java.util.Map;16import java.util.Set;17public class Model {18 private ListMultiMap<ISuite, ITestResult> m_model = Maps.newListMultiMap();19 private List<ISuite> m_suites = null;20 private Map<String, String> m_testTags = Maps.newHashMap();21 private Map<ITestResult, String> m_testResultMap = Maps.newHashMap();22 private Map<ISuite, ResultsByClass> m_failedResultsByClass = Maps.newHashMap();23 private Map<ISuite, ResultsByClass> m_skippedResultsByClass = Maps.newHashMap();24 private Map<ISuite, ResultsByClass> m_passedResultsByClass = Maps.newHashMap();25 private List<ITestResult> m_allFailedResults = Lists.newArrayList();26 // Each suite is mapped to failed.png, skipped.png or nothing (which means passed.png)27 private Map<String, String> m_statusBySuiteName = Maps.newHashMap();28 private SetMultiMap<String, String> m_groupsBySuiteName = Maps.newSetMultiMap();29 private SetMultiMap<String, String> m_methodsByGroup = Maps.newSetMultiMap();30 public Model(List<ISuite> suites) {31 m_suites = suites;32 init();33 }34 public List<ISuite> getSuites() {35 return m_suites;36 }37 private void init() {38 int testCounter = 0;39 for (ISuite suite : m_suites) {40 List<ITestResult> passed = Lists.newArrayList();41 List<ITestResult> failed = Lists.newArrayList();42 List<ITestResult> skipped = Lists.newArrayList();43 for (ISuiteResult sr : suite.getResults().values()) {...

Full Screen

Full Screen

Source:IgnoredMethodsPanel.java Github

copy

Full Screen

1package org.testng.reporters.jq;2import org.testng.ISuite;3import org.testng.ITestNGMethod;4import org.testng.collections.Maps;5import org.testng.collections.SetMultiMap;6import org.testng.reporters.XMLStringBuffer;7public class IgnoredMethodsPanel extends BaseMultiSuitePanel {8 public IgnoredMethodsPanel(Model model) {9 super(model);10 }11 @Override12 public String getPrefix() {13 return "ignored-methods-";14 }15 @Override16 public String getHeader(ISuite suite) {17 return pluralize(suite.getExcludedMethods().size(), "ignored method");18 }19 @Override20 public String getContent(ISuite suite, XMLStringBuffer main) {21 XMLStringBuffer xsb = new XMLStringBuffer(main.getCurrentIndent());22 SetMultiMap<Class<?>, ITestNGMethod> map = Maps.newSetMultiMap();23 for (ITestNGMethod method : suite.getExcludedMethods()) {24 map.put(method.getTestClass().getRealClass(), method);25 }26 for (Class<?> c : map.getKeys()) {27 xsb.push(D, C, "ignored-class-div");28 xsb.addRequired(S, c.getName(), C, "ignored-class-name");29 xsb.push(D, C, "ignored-methods-div");30 for (ITestNGMethod m : map.get(c)) {31 xsb.addRequired(S, m.getMethodName(), C, "ignored-method-name");32 xsb.addEmptyElement("br");33 }34 xsb.pop(D);35 xsb.pop(D);36 }...

Full Screen

Full Screen

Source:Maps.java Github

copy

Full Screen

...24 }25 public static <K, V> ListMultiMap<K, V> newListMultiMap() {26 return new ListMultiMap<K, V>();27 }28 public static <K, V> SetMultiMap<K, V> newSetMultiMap() {29 return new SetMultiMap<K, V>();30 }31}...

Full Screen

Full Screen

Source:SetMultiMap.java Github

copy

Full Screen

1package org.testng.collections;2import java.util.Set;3/** A container to hold sets indexed by a key. */4public class SetMultiMap<K, V> extends MultiMap<K, V, Set<V>> {5 public SetMultiMap(boolean isSorted) {6 super(isSorted);7 }8 @Override9 protected Set<V> createValue() {10 return Sets.newHashSet();11 }12}...

Full Screen

Full Screen

SetMultiMap

Using AI Code Generation

copy

Full Screen

1public class SetMultiMap<K, V> {2 private final Map<K, Set<V>> map;3 public SetMultiMap() {4 map = new HashMap<K, Set<V>>();5 }6 public void put(K key, V value) {7 Set<V> set = map.get(key);8 if (set == null) {9 set = new HashSet<V>();10 map.put(key, set);11 }12 set.add(value);13 }14 public Set<V> get(K key) {15 return map.get(key);16 }17 public boolean containsKey(K key) {18 return map.containsKey(key);19 }20 public boolean containsValue(K key, V value) {21 Set<V> set = map.get(key);22 return set != null && set.contains(value);23 }24 public void remove(K key, V value) {25 Set<V> set = map.get(key);26 if (set != null) {27 set.remove(value);28 }29 }30 public void remove(K key) {31 map.remove(key);32 }33 public Set<K> keySet() {34 return map.keySet();35 }36 public Set<V> values() {37 Set<V> result = new HashSet<V>();38 for (Set<V> set : map.values()) {39 result.addAll(set);40 }41 return result;42 }43 public void clear() {44 map.clear();45 }46}47public class SetMultiMap<K, V> {48 private final Map<K, Set<V>> map;49 public SetMultiMap() {50 map = new HashMap<K, Set<V>>();51 }52 public void put(K key, V value) {53 Set<V> set = map.get(key);54 if (set == null) {55 set = new HashSet<V>();56 map.put(key, set);57 }58 set.add(value);59 }60 public Set<V> get(K key) {61 return map.get(key);62 }63 public boolean containsKey(K key) {64 return map.containsKey(key);65 }66 public boolean containsValue(K key, V value) {67 Set<V> set = map.get(key);68 return set != null && set.contains(value);69 }70 public void remove(K key, V value) {71 Set<V> set = map.get(key);72 if (set != null) {73 set.remove(value);74 }

Full Screen

Full Screen

SetMultiMap

Using AI Code Generation

copy

Full Screen

1SetMultiMap<String, String> map = new SetMultiMap<String, String>();2map.put("a", "b");3map.put("a", "c");4map.put("d", "e");5map.put("d", "f");6map.put("d", "g");7map.put("h", "i");8map.put("h", "j");9map.put("h", "k");10map.put("h", "l");11map.put("h", "m");12map.put("h", "n");13map.put("h", "o");14map.put("h", "p");15map.put("h", "q");16map.put("h", "r");17map.put("h", "s");18map.put("h", "t");19map.put("h", "u");20map.put("h", "v");21map.put("h", "w");22map.put("h", "x");23map.put("h", "y");24map.put("h", "z");25map.put("h", "aa");26map.put("h", "bb");27map.put("h", "cc");28map.put("h", "dd");29map.put("h", "ee");30map.put("h", "ff");31map.put("h", "gg");32map.put("h", "hh");33map.put("h", "ii");34map.put("h", "jj");35map.put("h", "kk");36map.put("h", "ll");37map.put("h", "mm");38map.put("h", "nn");39map.put("h", "oo");40map.put("h", "pp");41map.put("h", "qq");42map.put("h", "rr");43map.put("h", "ss");44map.put("h", "tt");45map.put("h", "uu");46map.put("h", "vv");47map.put("h", "ww");48map.put("h", "xx");49map.put("h", "yy");50map.put("h", "zz");51map.put("h", "aaa");52map.put("h", "bbb");53map.put("h", "ccc");54map.put("h", "ddd");55map.put("h", "eee");56map.put("h", "fff");57map.put("h", "ggg");58map.put("h", "hhh");59map.put("h", "iii");60map.put("h", "jjj");61map.put("h", "kk

Full Screen

Full Screen

SetMultiMap

Using AI Code Generation

copy

Full Screen

1import org.testng.collections.SetMultiMap;2import java.util.Set;3import java.util.Collection;4import java.util.Iterator;5public class SetMultiMapTest {6 public static void main(String[] args) {7 SetMultiMap map = new SetMultiMap();8 map.put("key1", "value1");9 map.put("key1", "value2");10 map.put("key2", "value3");11 map.put("key3", "value4");12 map.put("key3", "value5");13 map.put("key3", "value6");14 System.out.println("Map: " + map);15 Set keySet = map.keySet();16 System.out.println("Key Set: " + keySet);17 Collection values = map.values();18 System.out.println("Values: " + values);19 Iterator iter = map.keySet().iterator();20 while (iter.hasNext()) {21 Object key = iter.next();22 System.out.println("Key: " + key + " Values: " + map.get(key));23 }24 }25}26Map: {key1=[value1, value2], key2=[value3], key3=[value4, value5, value6]}

Full Screen

Full Screen
copy
1int[] myIntArray = new int[3];2int[] myIntArray = {1, 2, 3};3int[] myIntArray = new int[]{1, 2, 3};45// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html67int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 998int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 1009int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.10int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort 11
Full Screen
copy
1int[] num = new int[5];2
Full Screen
copy
1Type[] variableName = new Type[capacity];23Type[] variableName = {comma-delimited values};4567Type variableName[] = new Type[capacity]; 89Type variableName[] = {comma-delimited values};10
Full Screen

TestNG tutorial

TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.

Chapters

  1. JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
  2. Installing TestNG in Eclipse: Start installing the TestNG Plugin and learn how to set up TestNG in Eclipse to begin constructing a framework for your test project.
  3. Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
  4. Automation using TestNG: Dive into how to install TestNG in this Selenium TestNG tutorial, the fundamentals of developing an automation script for Selenium automation testing.
  5. Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
  6. Creating TestNG XML File: Here is a step-by-step tutorial on creating a TestNG XML file to learn why and how it is created and discover how to run the TestNG XML file being executed in parallel.
  7. Automation with Selenium, Cucumber & TestNG: Explore for an in-depth tutorial on automation using Selenium, Cucumber, and TestNG, as TestNG offers simpler settings and more features.
  8. JUnit Selenium Tests using TestNG: Start running your regular and parallel tests by looking at how to run test cases in Selenium using JUnit and TestNG without having to rewrite the tests.
  9. Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
  10. Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
  11. Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
  12. DataProviders in TestNG: Deep dive into learning more about TestNG's DataProvider and how to effectively use it in our test scripts for Selenium test automation.
  13. Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
  14. TestNG Listeners in Selenium WebDriver: Understand the various TestNG listeners to utilize them effectively for your next plan when working with TestNG and Selenium automation.
  15. TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
  16. TestNG Reporter Log in Selenium: Find out how to use the TestNG Reporter Log and learn how to eliminate the need for external software with TestNG Reporter Class to boost productivity.
  17. TestNG Reports in Jenkins: Discover how to generate TestNG reports in Jenkins if you want to know how to create, install, and share TestNG reports in Jenkins.

Certification

You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.

YouTube

Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in SetMultiMap

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful