How to use Interface IWeaveXml class of org.testng.xml package

Best Testng code snippet using org.testng.xml.Interface IWeaveXml

Run Testng automation tests on LambdaTest cloud grid

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

copy
1package org.testng.xml;
2
3import org.testng.TestNGException;
4import org.testng.internal.ClassHelper;
5import org.testng.internal.RuntimeBehavior;
6import org.testng.internal.reflect.ReflectionRecipes;
7
8/** A Utility class that helps represent a {@link XmlSuite} and {@link XmlTest} as String. */
9final class XmlWeaver {
10  private static IWeaveXml instance = null;
11  private static final boolean testMode = RuntimeBehavior.isTestMode();
12
13  private XmlWeaver() {}
14
15  private static IWeaveXml getInstance() {
16    if (testMode) {
17      // Donot resort to caching when running Unit tests for TestNG, because we have to check
18      // both implementations. If we cache the instance, then its not possible to do that.
19      return attemptDefaultImplementationInstantiation();
20    }
21    return instantiateIfRequired();
22  }
23
24  private static IWeaveXml instantiateIfRequired() {
25    if (instance != null) {
26      return instance;
27    }
28    instance = attemptDefaultImplementationInstantiation();
29    if (instance != null) {
30      return instance;
31    }
32    Class<?> clazz = ClassHelper.forName(getClassName());
33    boolean isValid = ReflectionRecipes.isOrImplementsInterface(IWeaveXml.class, clazz);
34    if (!isValid) {
35      String msg =
36          "In order for "
37              + getClassName()
38              + " to be used by TestNG for generating suite/test xmls, "
39              + getClassName()
40              + " needs to implement "
41              + IWeaveXml.class.getName();
42      throw new TestNGException(msg);
43    }
44    instance = (IWeaveXml) ClassHelper.newInstance(clazz);
45    return instance;
46  }
47
48  private static String getClassName() {
49    return RuntimeBehavior.getDefaultXmlGenerationImpl();
50  }
51
52  /**
53   * Helps represent the contents of {@link XmlSuite} as a String.
54   *
55   * @param suite - The {@link XmlSuite} that needs to be transformed to a String.
56   * @return - The String representation
57   */
58  static String asXml(XmlSuite suite) {
59    return getInstance().asXml(suite);
60  }
61
62  /**
63   * Helps represent the contents of {@link XmlTest} as a String.
64   *
65   * @param xmlTest - The {@link XmlTest} that needs to be transformed to a String.
66   * @param indent - The indentation.
67   * @return - The String representation
68   */
69  static String asXml(XmlTest xmlTest, String indent) {
70    return getInstance().asXml(xmlTest, indent);
71  }
72
73  private static IWeaveXml attemptDefaultImplementationInstantiation() {
74    String clazz = getClassName();
75    if (clazz.equals(DefaultXmlWeaver.class.getName())) {
76      return new DefaultXmlWeaver();
77    }
78    if (clazz.equals(CommentDisabledXmlWeaver.class.getName())) {
79      return new CommentDisabledXmlWeaver();
80    }
81    if (testMode) {
82      return null;
83    }
84    Class<?> clazzName = ClassHelper.forName(clazz);
85    if (ReflectionRecipes.isOrExtends(IWeaveXml.class, clazzName)) {
86      return ClassHelper.newInstance((Class<IWeaveXml>) clazzName);
87    }
88    throw new IllegalArgumentException(clazz + " does not implement " + IWeaveXml.class.getName());
89  }
90}
91
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on Interface-IWeaveXml

    No relevant questions found for this class 😞

Most used methods in Interface-IWeaveXml

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)