[seam-commits] Seam SVN: r11299 - in branches/community/Seam_2_2/src/test/ftest: examples/rss/src/org/jboss/seam/example/rss/test/selenium and 3 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Jul 21 03:33:59 EDT 2009


Author: kpiwko at redhat.com
Date: 2009-07-21 03:33:58 -0400 (Tue, 21 Jul 2009)
New Revision: 11299

Added:
   branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/xml/
   branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/xml/SeamXMLRSSTest.java
   branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/
   branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/NodeCondition.java
   branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/SeamXMLTest.java
Modified:
   branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/selenium/SeleniumRSSTest.java
Log:
JBQA-2276 missing JAXP based test sources

Modified: branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/selenium/SeleniumRSSTest.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/selenium/SeleniumRSSTest.java	2009-07-21 06:46:08 UTC (rev 11298)
+++ branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/selenium/SeleniumRSSTest.java	2009-07-21 07:33:58 UTC (rev 11299)
@@ -22,7 +22,7 @@
    /**
     * Place holder - just verifies that example deploys
     */
-   @Test
+   @Test(alwaysRun=false)
    public void homePageLoadTest()
    {
       assertEquals("Unexpected page title.", HOME_PAGE_TITLE, browser.getTitle());

Added: branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/xml/SeamXMLRSSTest.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/xml/SeamXMLRSSTest.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/test/ftest/examples/rss/src/org/jboss/seam/example/rss/test/xml/SeamXMLRSSTest.java	2009-07-21 07:33:58 UTC (rev 11299)
@@ -0,0 +1,67 @@
+package org.jboss.seam.example.rss.test.xml;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertTrue;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPathExpressionException;
+
+import org.jboss.seam.example.common.test.xml.NodeCondition;
+import org.jboss.seam.example.common.test.xml.SeamXMLTest;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+public class SeamXMLRSSTest extends SeamXMLTest
+{
+   public static final String HOME_PAGE = "/rss.seam";
+   public static final String HOME_PAGE_TITLE = "Title Feed";
+   public static final String TITLE_XPATH = "/feed/title";
+
+   public static final String ATOM_NS_URI = "http://www.w3.org/2005/Atom";
+
+   private Document doc;
+
+   @BeforeMethod
+   public void setDocument() throws IOException, SAXException
+   {
+      doc = db.parse(BROWSER_URL + CONTEXT_PATH + HOME_PAGE);     
+   }
+
+   /**
+    * Verifies that example deploys and has title
+    * 
+    * @throws XPathExpressionException If XPath expression cannot be compiled or
+    *            executed
+    */
+   @Test
+   public void testRSSTitle() throws XPathExpressionException
+   {
+      List<Node> list = evaluateXPath(doc.getDocumentElement(), TITLE_XPATH);
+      assertEquals("There is only on title", 1, list.size());
+      assertTrue("Document title equals to \"Title Feed\"", evaluateCondition(list, titleCondition));
+   }
+
+   private final NodeCondition titleCondition = new NodeCondition()
+   {
+
+      public boolean match(Node node)
+      {
+         if (node instanceof Element)
+         {
+            Element element = (Element) node;
+            return HOME_PAGE_TITLE.equals(element.getTextContent());
+         }
+         return false;
+      }
+   };
+}

Added: branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/NodeCondition.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/NodeCondition.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/NodeCondition.java	2009-07-21 07:33:58 UTC (rev 11299)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.example.common.test.xml;
+
+import org.w3c.dom.Node;
+
+/**
+ * This interface allows execution of arbitrary test on node
+ * 
+ * @author Karel Piwko
+ * 
+ */
+public interface NodeCondition
+{
+   /**
+    * Executes conditional test on give node.
+    * 
+    * @param node Node to be tested
+    * @return {@code true} if node matches condition, {@code false} otherwise
+    */
+   public boolean match(Node node);
+}

Added: branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/SeamXMLTest.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/SeamXMLTest.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/xml/SeamXMLTest.java	2009-07-21 07:33:58 UTC (rev 11299)
@@ -0,0 +1,143 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.example.common.test.xml;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Optional;
+import org.testng.annotations.Parameters;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * This class encapsulates XML factories to makes XML testing easier. 
+ * It accepts and requires this properties:
+ * 
+ * <ul>
+ *    <li><b>selenium.browser.url</b> for URL where server is running</li>
+ *    <li><b>example.context.path</b> for context path of example</li> 
+ *    <li><b>xml.namespace.aware</b> for namaspace awareness during parse, default <code>true</code></li>
+ * </ul>
+ * 
+ * @author Karel Piwko
+ * 
+ */
+public abstract class SeamXMLTest
+{
+
+   private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+   private static XPathFactory xpf = XPathFactory.newInstance();
+
+   protected String BROWSER_URL;
+   protected String CONTEXT_PATH;
+   protected boolean NAMESPACE_AWARE;
+   protected DocumentBuilder db;
+   protected XPath xp;
+
+   /**
+    * Initializes context path for given test
+    * 
+    * @param contextPath
+    */
+   @BeforeClass
+   @Parameters( { "selenium.browser.url", "example.context.path", "xml.namespace.aware" })
+   public void setParameters(String browserURL, @Optional("") String contextPath, @Optional("true") String namespaceAware)
+   {
+      BROWSER_URL = browserURL;
+      CONTEXT_PATH = contextPath;
+      NAMESPACE_AWARE = Boolean.parseBoolean(namespaceAware);
+   }
+
+   /**
+    * Initializes DocumentBuilder and XPath generic factories. 
+    * Sets document builder factory to ignore namespaces.
+    * 
+    * @throws ParserConfigurationException If document builder factory couldn't
+    *            be created
+    */
+   @BeforeClass
+   @Parameters( {"xml.namespace.aware"})
+   public void initializeBuilders() throws ParserConfigurationException
+   {      
+      dbf.setNamespaceAware(NAMESPACE_AWARE);
+      db = dbf.newDocumentBuilder();      
+      xp = xpf.newXPath();
+   }
+
+   /**
+    * Evaluates XPath on given part of DOM document
+    * 
+    * @param root Relative root for XPath evaluation
+    * @param xpath XPath expression
+    * @return List of node returned by evaluation
+    * @throws XPathExpressionException If XPath expression is invalid
+    */
+   protected List<Node> evaluateXPath(Node root, String xpath) throws XPathExpressionException
+   {
+      NodeList nl = (NodeList) xp.compile(xpath).evaluate(root, XPathConstants.NODESET);
+      List<Node> list = new ArrayList<Node>(nl.getLength());
+      for (int i = 0, max = nl.getLength(); i < max; i++)
+      {
+         list.add(nl.item(i));
+      }
+      return list;
+   }
+
+   /**
+    * Evaluates XPath on given part of DOM document and tests all returned
+    * results againts condition
+    * 
+    * @param root Relative root for XPath evaluation
+    * @param xpath XPath expression
+    * @param conditions Conditions evaluated on each node
+    * @return List of node returned by evaluation
+    * @throws XPathExpressionException If XPath expression is invalid
+    */
+   protected boolean evaluateXPathCondition(Node root, String xpath, NodeCondition... conditions) throws XPathExpressionException
+   {
+      return evaluateCondition(evaluateXPath(root, xpath), conditions);
+   }
+
+   protected boolean evaluateCondition(List<Node> list, NodeCondition... conditions)
+   {
+      for (Node node : list)
+      {
+         for (NodeCondition condition : conditions)
+         {
+            if (!condition.match(node))
+               return false;
+         }
+      }
+      return true;
+   }
+
+}
\ No newline at end of file



More information about the seam-commits mailing list