[testng] 14/355: Fix handling of relative paths in suite files.

Eugene Zhukov eugene-guest at moszumanska.debian.org
Tue Aug 18 10:19:42 UTC 2015


This is an automated email from the git hooks/post-receive script.

eugene-guest pushed a commit to annotated tag OpenBSD
in repository testng.

commit a306ac24a343df4742e639d14167ecb610c9315e
Author: Nalin Makar <nullin at nullin.com>
Date:   Sun Jun 2 17:24:30 2013 -0700

    Fix handling of relative paths in suite files.
    
    Currently there was a bug in how second level of relative paths were being handled during the initial parsing of suite files. All the relative paths were being resolved against the location of the main (initial) suite file and not the actual suite file being parsed/processed.
    
     Added a test for this as well. Everything compiles and all tests passed.
---
 CHANGES.txt                                        |  1 +
 pom-test.xml                                       |  2 +-
 src/main/java/org/testng/TestNG.java               | 42 +++++++++++-----------
 src/main/java/org/testng/xml/Parser.java           | 27 ++++++--------
 .../java/test/CheckSuitesInitializationTest.java   | 42 ++++++++++++++++++++++
 .../checksuitesinitialization/child-suite1.xml     |  7 ++++
 .../checksuitesinitialization/child-suite2.xml     |  7 ++++
 .../children/child-suite-3.xml                     |  8 +++++
 .../children/child-suite-4.xml                     |  7 ++++
 .../children/morechildren/child-suite-5.xml        |  7 ++++
 .../checksuitesinitialization/parent-suite.xml     |  7 ++++
 11 files changed, 119 insertions(+), 38 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 9276731..3f18f1d 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,6 +1,7 @@
 Current
 
 Fixed: GITHUB-376: Some results can be lost (Konstantin Savin)
+Fixed: Handle relative paths of Suite XML files properly (Nalin Makar)
 
 6.8.5:
 2013/05/13
diff --git a/pom-test.xml b/pom-test.xml
index 8eeee89..0af9d07 100644
--- a/pom-test.xml
+++ b/pom-test.xml
@@ -67,7 +67,7 @@
     <dependency>
       <groupId>org.testng</groupId>
       <artifactId>testng</artifactId>
-      <version>6.8.2-SNAPSHOT</version>
+      <version>6.8.6-SNAPSHOT</version>
       <scope>test</scope>
     </dependency>
    </dependencies>
diff --git a/src/main/java/org/testng/TestNG.java b/src/main/java/org/testng/TestNG.java
index 0e77bb1..105b578 100644
--- a/src/main/java/org/testng/TestNG.java
+++ b/src/main/java/org/testng/TestNG.java
@@ -275,26 +275,26 @@ public class TestNG {
     if (m_suites.size() > 0) {
     	//to parse the suite files (<suite-file>), if any
     	for (XmlSuite s: m_suites) {
-	      List<String> suiteFiles = s.getSuiteFiles();
-  			for (int i = 0; i < suiteFiles.size(); i++) {
-  				try {
-  					Collection<XmlSuite> childSuites = getParser(suiteFiles.get(i)).parse();
-  					for (XmlSuite cSuite : childSuites){
-  						cSuite.setParentSuite(s);
-  						s.getChildSuites().add(cSuite);
-  					}
-  				}
-  				catch(FileNotFoundException e) {
-  					e.printStackTrace(System.out);
-  				}
-  				catch (ParserConfigurationException e) {
-  					e.printStackTrace(System.out);
-  				} catch (SAXException e) {
-  					e.printStackTrace(System.out);
-  				} catch (IOException e) {
-  					e.printStackTrace(System.out);
-  				}
-  			}
+
+        for (String suiteFile : s.getSuiteFiles()) {
+            try {
+                Collection<XmlSuite> childSuites = getParser(suiteFile).parse();
+                for (XmlSuite cSuite : childSuites){
+                    cSuite.setParentSuite(s);
+                    s.getChildSuites().add(cSuite);
+                }
+            }
+            catch(FileNotFoundException e) {
+                e.printStackTrace(System.out);
+            }
+            catch (ParserConfigurationException e) {
+                e.printStackTrace(System.out);
+            } catch (SAXException e) {
+                e.printStackTrace(System.out);
+            } catch (IOException e) {
+                e.printStackTrace(System.out);
+            }
+        }
 
     	}
       return;
@@ -364,7 +364,7 @@ public class TestNG {
     File jarFile = new File(m_jarPath);
 
     try {
- 
+
       Utils.log("TestNG", 2, "Trying to open jar file:" + jarFile);
 
       JarFile jf = new JarFile(jarFile);
diff --git a/src/main/java/org/testng/xml/Parser.java b/src/main/java/org/testng/xml/Parser.java
index 5bcc04b..914fe15 100755
--- a/src/main/java/org/testng/xml/Parser.java
+++ b/src/main/java/org/testng/xml/Parser.java
@@ -38,7 +38,7 @@ public class Parser {
       new SuiteXmlParser();
   private static final IFileParser<XmlSuite> YAML_PARSER = new YamlParser();
   private static final IFileParser<XmlSuite> DEFAULT_FILE_PARSER = XML_PARSER;
-  
+
   /** The file name of the xml suite being parsed. This may be null if the Parser
    * has not been initialized with a file name. TODO CQ This member is never used. */
   private String m_fileName;
@@ -51,9 +51,8 @@ public class Parser {
   /**
    * Constructs a <code>Parser</code> to use the inputStream as the source of
    * the xml test suite to parse.
-   * @param filename the filename corresponding to the inputStream or null if
+   * @param fileName the filename corresponding to the inputStream or null if
    * unknown.
-   * @param inputStream the xml test suite input stream.
    */
   public Parser(String fileName) {
     init(fileName, null, null);
@@ -135,7 +134,7 @@ public class Parser {
    * if the default testng.xml file is not found.
    */
   public Collection<XmlSuite> parse()
-    throws ParserConfigurationException, SAXException, IOException 
+    throws ParserConfigurationException, SAXException, IOException
   {
     // Each suite found is put in this list, using their canonical
     // path to make sure we don't add a same file twice
@@ -143,19 +142,14 @@ public class Parser {
     List<String> processedSuites = Lists.newArrayList();
     XmlSuite resultSuite = null;
 
-    File parentFile = null;
-    String mainFilePath = null;
-
-    if (m_fileName != null) {
-        File mainFile = new File(m_fileName);
-        mainFilePath = mainFile.getCanonicalPath();
-        parentFile = mainFile.getParentFile();
-    }
-
     List<String> toBeParsed = Lists.newArrayList();
     List<String> toBeAdded = Lists.newArrayList();
     List<String> toBeRemoved = Lists.newArrayList();
-    toBeParsed.add(mainFilePath);
+
+    if (m_fileName != null) {
+      File mainFile = new File(m_fileName);
+      toBeParsed.add(mainFile.getCanonicalPath());
+    }
 
     /*
      * Keeps a track of parent XmlSuite for each child suite
@@ -164,13 +158,14 @@ public class Parser {
     while (toBeParsed.size() > 0) {
 
       for (String currentFile : toBeParsed) {
+        File currFile = new File(currentFile);
+        File parentFile = currFile.getParentFile();
         InputStream inputStream = m_inputStream != null
             ? m_inputStream
             : new FileInputStream(currentFile);
 
         IFileParser<XmlSuite> fileParser = getParser(currentFile);
-        XmlSuite result = fileParser.parse(currentFile, inputStream, m_loadClasses);
-        XmlSuite currentXmlSuite = result;
+        XmlSuite currentXmlSuite = fileParser.parse(currentFile, inputStream, m_loadClasses);
         processedSuites.add(currentFile);
         toBeRemoved.add(currentFile);
 
diff --git a/src/test/java/test/CheckSuitesInitializationTest.java b/src/test/java/test/CheckSuitesInitializationTest.java
new file mode 100644
index 0000000..beddee8
--- /dev/null
+++ b/src/test/java/test/CheckSuitesInitializationTest.java
@@ -0,0 +1,42 @@
+package test;
+
+import java.util.Arrays;
+
+import org.testng.Assert;
+import org.testng.TestListenerAdapter;
+import org.testng.TestNG;
+import org.testng.annotations.Test;
+
+/**
+ * Check for a bug in how relative paths in suite files were being handled.
+ *
+ * All paths were being resolved using the initial suite's location and not
+ * that of the current suite being parsed/processed.
+ *
+ * This test checks that TestNG can handle cases where we have the following set of
+ * files (all linked using relative paths):
+ *
+ * - parent-suite -> [child-suite-1, children/child-suite-3]
+ * - children/child-suite-3 -> [../child-suite-2, child-suite-4, morechildren/child-suite-5]
+ *
+ * Check the <code>checksuitesinitialization</code> folder under test resources
+ *
+ * @author Nalin Makar
+ */
+public class CheckSuitesInitializationTest extends SimpleBaseTest {
+
+  /**
+   * Child suites and tests within different suites have same names
+   */
+  @Test
+  public void check() {
+    TestListenerAdapter tla = new TestListenerAdapter();
+    TestNG tng = create();
+    String testngXmlPath = getPathToResource("checksuitesinitialization/parent-suite.xml");
+    tng.setTestSuites(Arrays.asList(testngXmlPath));
+    tng.addListener(tla);
+    tng.run();
+    Assert.assertEquals(tla.getPassedTests().size(), 4);
+  }
+
+}
diff --git a/src/test/resources/checksuitesinitialization/child-suite1.xml b/src/test/resources/checksuitesinitialization/child-suite1.xml
new file mode 100644
index 0000000..0ad2aa6
--- /dev/null
+++ b/src/test/resources/checksuitesinitialization/child-suite1.xml
@@ -0,0 +1,7 @@
+<suite name="Child Suite 1">
+  <test name="Test">
+    <classes>
+      <class name="test.parameters.SampleTest"/>
+    </classes>
+  </test>
+</suite>
diff --git a/src/test/resources/checksuitesinitialization/child-suite2.xml b/src/test/resources/checksuitesinitialization/child-suite2.xml
new file mode 100644
index 0000000..460378b
--- /dev/null
+++ b/src/test/resources/checksuitesinitialization/child-suite2.xml
@@ -0,0 +1,7 @@
+<suite name="Child Suite 2">
+  <test name="Test">
+    <classes>
+      <class name="test.parameters.SampleTest"/>
+    </classes>
+  </test>
+</suite>
diff --git a/src/test/resources/checksuitesinitialization/children/child-suite-3.xml b/src/test/resources/checksuitesinitialization/children/child-suite-3.xml
new file mode 100644
index 0000000..91a4d83
--- /dev/null
+++ b/src/test/resources/checksuitesinitialization/children/child-suite-3.xml
@@ -0,0 +1,8 @@
+<suite name="Child Suite 3">
+  <parameter name="foo" value="bar"/>
+  <suite-files>
+    <suite-file path="../child-suite2.xml" />
+    <suite-file path="child-suite-4.xml" />
+    <suite-file path="morechildren/child-suite-5.xml" />
+  </suite-files>
+</suite>
\ No newline at end of file
diff --git a/src/test/resources/checksuitesinitialization/children/child-suite-4.xml b/src/test/resources/checksuitesinitialization/children/child-suite-4.xml
new file mode 100644
index 0000000..ac511c1
--- /dev/null
+++ b/src/test/resources/checksuitesinitialization/children/child-suite-4.xml
@@ -0,0 +1,7 @@
+<suite name="Child Suite 4">
+  <test name="Test">
+    <classes>
+      <class name="test.parameters.SampleTest"/>
+    </classes>
+  </test>
+</suite>
\ No newline at end of file
diff --git a/src/test/resources/checksuitesinitialization/children/morechildren/child-suite-5.xml b/src/test/resources/checksuitesinitialization/children/morechildren/child-suite-5.xml
new file mode 100644
index 0000000..04ab130
--- /dev/null
+++ b/src/test/resources/checksuitesinitialization/children/morechildren/child-suite-5.xml
@@ -0,0 +1,7 @@
+<suite name="Child Suite 5">
+  <test name="Test">
+    <classes>
+      <class name="test.parameters.SampleTest"/>
+    </classes>
+  </test>
+</suite>
\ No newline at end of file
diff --git a/src/test/resources/checksuitesinitialization/parent-suite.xml b/src/test/resources/checksuitesinitialization/parent-suite.xml
new file mode 100644
index 0000000..7239e8c
--- /dev/null
+++ b/src/test/resources/checksuitesinitialization/parent-suite.xml
@@ -0,0 +1,7 @@
+<suite name="Parent Suite">
+  <parameter name="foo" value="bar"/>
+  <suite-files>
+    <suite-file path="./child-suite1.xml" />
+    <suite-file path="children/child-suite-3.xml" />
+  </suite-files>
+</suite>
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git



More information about the pkg-java-commits mailing list