View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gwes;
9   
10  import junit.framework.Test;
11  import junit.framework.TestSuite;
12  import junit.framework.Assert;
13  import net.kwfgrid.gwes.exception.LoggingException;
14  import org.apache.log4j.Logger;
15  import org.jaxen.XPath;
16  import org.jaxen.JaxenException;
17  import org.jaxen.SimpleNamespaceContext;
18  import org.jaxen.NamespaceContext;
19  import org.jaxen.jdom.JDOMXPath;
20  import org.jdom.Element;
21  import org.jdom.Document;
22  import net.kwfgrid.gworkflowdl.structure.JdomString;
23  
24  import java.io.IOException;
25  
26  /**
27   * <code>
28   * maven -Dtestcase=net.kwfgrid.gwes.XPathEvaluationTest test:single
29   * </code>
30   *
31   * @author Andreas Hoheisel
32   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
33   * @version $Id: XPathEvaluationTest.java 1529 2011-06-22 16:37:19Z hoheisel $
34   */
35  public final class XPathEvaluationTest extends LocalGWESAbstractTestCase {
36  
37      static Logger logger = Logger.getLogger(XPathEvaluationTest.class);
38  
39      /**
40       * Create the test case.
41       *
42       * @param testName name of the test case
43       */
44      public XPathEvaluationTest(String testName) throws LoggingException {
45          super(testName);
46      }
47  
48      /**
49       * @return the suite of tests being tested
50       */
51      public static Test suite() {
52          return new TestSuite(XPathEvaluationTest.class);
53      }
54  
55      /**
56       * *******************************************************************************************
57       * Begin of public test classes.
58       * ********************************************************************************************
59       */
60  
61      public void testXPathEvaluationDocument() throws JaxenException, IOException {
62          SimpleNamespaceContext nsContext = new TransitionOccurrence.DefaultNamespaceContext();
63          Element rootElement = new Element("data",JdomString.wfSpace);
64          Document context = new Document(rootElement);
65          Element v1 = new Element("value1");
66          v1.addContent("1");
67          rootElement.addContent(v1);
68          Element v2 = new Element("value2");
69          v2.addContent("2");
70          rootElement.addContent(v2);
71          logger.info("context:\n"+JdomString.document2string(context));
72  
73          Object ret;
74          String xpath;
75          ret = evaluateXPath(nsContext, context, xpath="*");
76          Assert.assertEquals("XPath return value for '"+xpath+"'","[[Element: <data [Namespace: http://www.gridworkflow.org/gworkflowdl]/>]]",ret.toString());
77          
78          ret = evaluateXPath(nsContext, context, xpath="/gwdl:data");
79          Assert.assertEquals("XPath return value for '"+xpath+"'","[[Element: <data [Namespace: http://www.gridworkflow.org/gworkflowdl]/>]]",ret.toString());
80  
81          ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/value1");
82          Assert.assertEquals("XPath return value for '"+xpath+"'","[[Element: <value1/>]]",ret.toString());
83  
84          ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/value2");
85          Assert.assertEquals("XPath return value for '"+xpath+"'","[[Element: <value2/>]]",ret.toString());
86  
87          ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/value1 = 1");
88          Assert.assertEquals("XPath return value for '"+xpath+"'",Constants.TRUE,ret.toString());
89  
90          ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/value1 = 2");
91          Assert.assertEquals("XPath return value for '"+xpath+"'",Constants.FALSE,ret.toString());
92  
93          ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/value1 + 1 = 2");
94          Assert.assertEquals("XPath return value for '"+xpath+"'",Constants.TRUE,ret.toString());
95  
96          ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/value1/text()");
97          Assert.assertEquals("XPath return value for '"+xpath+"'","[[Text: 1]]",ret.toString());
98  
99          ret = evaluateXPath(nsContext, context, xpath="normalize-space(/gwdl:data/value1)");
100         Assert.assertEquals("XPath return value for '"+xpath+"'","1",ret.toString());
101 
102         ret = evaluateXPath(nsContext, context, xpath="normalize-space(/gwdl:data/value1 + 1)");
103         Assert.assertEquals("XPath return value for '"+xpath+"'","2",ret.toString());
104     }
105 
106 
107     public void testXPathItemList() throws JaxenException, IOException {
108         SimpleNamespaceContext nsContext = new TransitionOccurrence.DefaultNamespaceContext();
109         Element rootElement = new Element("data",JdomString.wfSpace);
110         Document context = new Document(rootElement);
111 
112         Element number = new Element("i");
113         number.setText("7.0");
114         rootElement.addContent(number);
115 
116         Element list = new Element("list");
117         for (int i = 1; i<=10; i++ ){
118             Element item = new Element("item");
119             item.setText(""+i);
120             list.addContent(item);
121         }
122 
123         rootElement.addContent(list);
124         logger.info("context:\n"+JdomString.document2string(context));
125 
126         Object ret;
127         String xpath;
128 
129         ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/i/text()");
130         Assert.assertEquals("XPath return value for '"+xpath+"'","[[Text: 7.0]]",ret.toString());
131 
132         ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/list/item[ 6.0 ]/text()");
133         Assert.assertEquals("XPath return value for '"+xpath+"'","[[Text: 6]]",ret.toString());
134 
135         ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/list/item[ number(/gwdl:data/i) ]/text()");
136         Assert.assertEquals("XPath return value for '"+xpath+"'","[[Text: 7]]",ret.toString());
137 
138 
139     }
140 
141 
142     public void testXPathEvaluationSOAPReturnDocument() throws JaxenException, IOException {
143         SimpleNamespaceContext nsContext = new TransitionOccurrence.DefaultNamespaceContext();
144         Element rootElement = new Element("data",JdomString.wfSpace);
145         Document context = new Document(rootElement);
146         Element v1 = new Element("sortReturn");
147         v1.addNamespaceDeclaration(JdomString.NAMESPACE_SOAPENC);
148         v1.addNamespaceDeclaration(JdomString.NAMESPACE_XSI);
149         v1.setAttribute("type","soapenc:string",JdomString.NAMESPACE_XSI);
150         v1.addContent("0\n8\n15");
151         rootElement.addContent(v1);
152         logger.info("context:\n"+JdomString.document2string(context));
153 
154         Object ret;
155         String xpath;
156         ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/sortReturn");
157         Assert.assertEquals("XPath return value for '"+xpath+"'","[[Element: <sortReturn/>]]",ret.toString());
158 
159         ret = evaluateXPath(nsContext, context, xpath="/gwdl:data/sortReturn/text()");
160         Assert.assertEquals("XPath return value for '"+xpath+"'","[[Text: 0\n" +
161                 "8\n" +
162                 "15]]",ret.toString());
163     }
164     /**
165      * XPath evaluation on JDom element lists seems not to work?
166      */
167 //    public void testXPathEvaluationElementList() throws JaxenException, IOException {
168 //        SimpleNamespaceContext nsContext = new TransitionOccurrence.DefaultNamespaceContext();
169 //        List<Element> context = new ArrayList<Element>();
170 //        Element e1 = new Element("e1");
171 //        e1.addContent("v1");
172 //        context.add(e1);
173 //        Element e2 = new Element("e2");
174 //        e2.addContent("v2");
175 //        context.add(e2);
176 //        logger.info("context:");
177 //        for (Element e : context) {
178 //            logger.info(JdomString.element2string(e));
179 //        }
180 //
181 //        Object ret;
182 //        String xpath;
183 //        ret = evaluateXPath(nsContext, context, xpath="*");
184 //        Assert.assertEquals("XPath return value for '"+xpath+"'","[[Element: <data [Namespace: http://www.gridworkflow.org/gworkflowdl]/>]]",ret.toString());
185 //
186 //    }
187 
188     /**
189      * *******************************************************************************************
190      * End of public test classes.
191      * ********************************************************************************************
192      */
193     
194     private Object evaluateXPath(NamespaceContext nsContext, Object context, String xpathExpression) throws JaxenException {
195         XPath xpath = new JDOMXPath(xpathExpression);
196         xpath.setNamespaceContext(nsContext);
197         Object retObject = xpath.evaluate(context);
198         logger.info("Evaluation of expression '" + xpathExpression + "' returns " + retObject.toString());
199         return retObject;
200     }
201 
202 }