Source Code Cross Referenced for XQueryMediatorTest.java in  » ESB » synapse » org » apache » synapse » mediators » xquery » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » synapse » org.apache.synapse.mediators.xquery 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one
003:         *  or more contributor license agreements.  See the NOTICE file
004:         *  distributed with this work for additional information
005:         *  regarding copyright ownership.  The ASF licenses this file
006:         *  to you under the Apache License, Version 2.0 (the
007:         *  "License"); you may not use this file except in compliance
008:         *  with the License.  You may obtain a copy of the License at
009:         *
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         *  Unless required by applicable law or agreed to in writing,
013:         *  software distributed under the License is distributed on an
014:         *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         *  KIND, either express or implied.  See the License for the
016:         *  specific language governing permissions and limitations
017:         *  under the License.
018:         */
019:        package org.apache.synapse.mediators.xquery;
020:
021:        import junit.framework.TestCase;
022:        import org.apache.synapse.MessageContext;
023:        import org.apache.synapse.mediators.TestUtils;
024:        import org.apache.axiom.om.OMElement;
025:        import org.apache.axiom.om.impl.builder.StAXOMBuilder;
026:
027:        import javax.xml.namespace.QName;
028:        import javax.xml.stream.XMLStreamReader;
029:        import javax.xml.stream.XMLInputFactory;
030:        import javax.xml.stream.XMLStreamException;
031:        import java.util.List;
032:        import java.util.ArrayList;
033:        import java.io.StringReader;
034:
035:        import net.sf.saxon.javax.xml.xquery.XQItemType;
036:
037:        /**
038:         *
039:         */
040:
041:        public class XQueryMediatorTest extends TestCase {
042:
043:            public final static String sampleXml = "<bookstore><book category=\"COOKING\"> <title lang=\"en\">Everyday Italian</title>\n"
044:                    + "  <author>Giada De Laurentiis</author>\n"
045:                    + "  <year>2005</year>\n"
046:                    + "  <price>30.00</price>\n"
047:                    + "\n"
048:                    + "</book>\n"
049:                    + "\n"
050:                    + "<book category=\"CHILDREN\">\n"
051:                    + "  <title lang=\"en\">Harry Potter</title>\n"
052:                    + "  <author>J K. Rowling</author>\n"
053:                    + "  <year>2005</year>\n"
054:                    + "  <price>29.99</price>\n"
055:                    + "</book>\n"
056:                    + "\n"
057:                    + "<book category=\"WEB\">\n"
058:                    + "  <title lang=\"en\">XQuery Kick Start</title>\n"
059:                    + "  <author>James McGovern</author>\n"
060:                    + "  <author>Per Bothner</author>\n"
061:                    + "  <author>Kurt Cagle</author>\n"
062:                    + "  <author>James Linn</author>\n"
063:                    + "  <author>Vaidyanathan Nagarajan</author>\n"
064:                    + "\n"
065:                    + "  <year>2003</year>\n"
066:                    + "  <price>49.99</price>\n"
067:                    + "</book>\n"
068:                    + "\n"
069:                    + "<book category=\"WEB\">\n"
070:                    + "  <title lang=\"en\">Learning XML</title>\n"
071:                    + "  <author>Erik T. Ray</author>\n"
072:                    + "  <year>2003</year>\n"
073:                    + "\n"
074:                    + "  <price>39.95</price>\n"
075:                    + "</book>\n"
076:                    + "\n"
077:                    + "</bookstore>";
078:
079:            public final static String sampleXml2 = "<m0:CheckPriceRequest xmlns:m0=\"http://www.apache-synapse.org/test\">\n"
080:                    + "    <m0:Code>IBM</m0:Code>\n"
081:                    + "</m0:CheckPriceRequest>";
082:            public final static String sampleXml3 = "<m0:return xmlns:m0=\"http://services.samples/xsd\">\n"
083:                    + "\t<m0:symbol>IBM</m0:symbol>\n"
084:                    + "\t<m0:last>122222</m0:last>\n" + "</m0:return>";
085:            public final static String externalXMl = "<commission>\n"
086:                    + "    <vendor symbol=\"IBM\">44444</vendor>\n"
087:                    + "    <vendor symbol=\"MSFT\">55555</vendor>\n"
088:                    + "    <vendor symbol=\"SUN\">66666</vendor>\n"
089:                    + "</commission>";
090:
091:            public void testQueryWithAll() throws Exception {
092:                MessageContext mc = TestUtils.getTestContext("<foo/>", null);
093:                XQueryMediator mediator = new XQueryMediator();
094:                mediator
095:                        .setQuerySource("declare variable $intVar as xs:int external;"
096:                                + "declare variable $boolVar as xs:boolean external;"
097:                                + "declare variable $byteVar as xs:byte external;"
098:                                + "declare variable $longVar as xs:long external;"
099:                                + "declare variable $doubleVar as xs:double external;"
100:                                + "declare variable $shortVar as xs:short external;"
101:                                + "declare variable $floatVar as xs:float external;"
102:                                + "declare variable $stringVar as xs:string external;"
103:                                + "declare variable $integerVar as xs:integer external;"
104:                                + "document { "
105:                                + "<a xmlns='http://a/uri' z:in='out' xmlns:z='http://z/uri'>"
106:                                + "<b>{$intVar+2}<e>{$boolVar}</e>"
107:                                + "<all>"
108:                                + "{$byteVar},"
109:                                + "{$shortVar},"
110:                                + "{$doubleVar},"
111:                                + "{$longVar},"
112:                                + "{$floatVar},"
113:                                + "{$stringVar},"
114:                                + "{$integerVar+xs:integer('5')},"
115:                                + "</all></b></a> }");
116:                List list = new ArrayList();
117:                MediatorVariable intVariable = new MediatorBaseVariable(
118:                        new QName("intVar"));
119:                intVariable.setType(XQItemType.XQBASETYPE_INT);
120:                intVariable.setValue(new Integer(8));
121:                list.add(intVariable);
122:                MediatorVariable boolVariable = new MediatorBaseVariable(
123:                        new QName("boolVar"));
124:                boolVariable.setType(XQItemType.XQBASETYPE_BOOLEAN);
125:                boolVariable.setValue(Boolean.TRUE);
126:                list.add(boolVariable);
127:                MediatorVariable doubleVariable = new MediatorBaseVariable(
128:                        new QName("doubleVar"));
129:                doubleVariable.setType(XQItemType.XQBASETYPE_DOUBLE);
130:                doubleVariable.setValue(new Double(23.33));
131:                list.add(doubleVariable);
132:                MediatorVariable floatVariable = new MediatorBaseVariable(
133:                        new QName("floatVar"));
134:                floatVariable.setType(XQItemType.XQBASETYPE_FLOAT);
135:                floatVariable.setValue(new Float(23.33));
136:                list.add(floatVariable);
137:                MediatorVariable shortVariable = new MediatorBaseVariable(
138:                        new QName("shortVar"));
139:                shortVariable.setType(XQItemType.XQBASETYPE_SHORT);
140:                shortVariable.setValue(new Short((short) 327));
141:                list.add(shortVariable);
142:                MediatorVariable byteVariable = new MediatorBaseVariable(
143:                        new QName("byteVar"));
144:                byteVariable.setType(XQItemType.XQBASETYPE_BYTE);
145:                byteVariable.setValue(new Byte((byte) 3));
146:                list.add(byteVariable);
147:                MediatorVariable longVariable = new MediatorBaseVariable(
148:                        new QName("longVar"));
149:                longVariable.setType(XQItemType.XQBASETYPE_LONG);
150:                longVariable.setValue(new Long(334));
151:                list.add(longVariable);
152:                MediatorVariable stringValue = new MediatorBaseVariable(
153:                        new QName("stringVar"));
154:                stringValue.setType(XQItemType.XQBASETYPE_STRING);
155:                stringValue.setValue("synapse");
156:                list.add(stringValue);
157:                MediatorVariable integerValue = new MediatorBaseVariable(
158:                        new QName("integerVar"));
159:                integerValue.setType(XQItemType.XQBASETYPE_INTEGER);
160:                integerValue.setValue(new Integer(5));
161:                list.add(integerValue);
162:                mediator.addAllVariables(list);
163:                assertTrue(mediator.mediate(mc));
164:                assertEquals("10", mc.getEnvelope().getBody().getFirstElement()
165:                        .getFirstElement().getText());
166:                assertEquals("true", mc.getEnvelope().getBody()
167:                        .getFirstElement().getFirstElement().getFirstElement()
168:                        .getText());
169:            }
170:
171:            public void testQueryWithPayload() throws Exception {
172:                MessageContext mc = TestUtils.getTestContext(sampleXml, null);
173:                XQueryMediator mediator = new XQueryMediator();
174:                List list = new ArrayList();
175:                MediatorVariable variable = new MediatorCustomVariable(
176:                        new QName("payload"));
177:                variable.setType(XQItemType.XQITEMKIND_DOCUMENT);
178:                list.add(variable);
179:                mediator.addAllVariables(list);
180:                mediator
181:                        .setQuerySource("declare variable $payload as document-node() external;"
182:                                + "$payload//bookstore/book/title");
183:                assertTrue(mediator.mediate(mc));
184:                assertEquals("Everyday Italian", mc.getEnvelope().getBody()
185:                        .getFirstElement().getText());
186:            }
187:
188:            public void testQueryWithPayloadTwo() throws Exception {
189:                MessageContext mc = TestUtils.getTestContext(sampleXml2, null);
190:                XQueryMediator mediator = new XQueryMediator();
191:                List list = new ArrayList();
192:                MediatorVariable variable = new MediatorCustomVariable(
193:                        new QName("payload"));
194:                variable.setType(XQItemType.XQITEMKIND_DOCUMENT);
195:                list.add(variable);
196:                mediator.addAllVariables(list);
197:                mediator
198:                        .setQuerySource("declare namespace m0=\"http://www.apache-synapse.org/test\"; "
199:                                + "declare variable $payload as document-node() external;"
200:                                + "<m:getQuote xmlns:m=\"http://services.samples/xsd\">\n"
201:                                + "<m:request>"
202:                                + "   <m:symbol>{$payload//m0:CheckPriceRequest/m0:Code/child::text()}"
203:                                + "   </m:symbol><"
204:                                + "/m:request>\n"
205:                                + "</m:getQuote> ");
206:                assertTrue(mediator.mediate(mc));
207:
208:                assertEquals("IBM", mc.getEnvelope().getBody()
209:                        .getFirstElement().getFirstElement().getFirstElement()
210:                        .getText());
211:            }
212:
213:            public void testQueryWithPayloadThree() throws Exception {
214:                MessageContext mc = TestUtils.getTestContext(sampleXml3, null);
215:                XQueryMediator mediator = new XQueryMediator();
216:                List list = new ArrayList();
217:                MediatorVariable variable = new MediatorCustomVariable(
218:                        new QName("payload"));
219:                variable.setType(XQItemType.XQITEMKIND_DOCUMENT);
220:                list.add(variable);
221:                mediator.addAllVariables(list);
222:                mediator
223:                        .setQuerySource("declare namespace m0=\"http://services.samples/xsd\";"
224:                                + " declare variable $payload as document-node() external;\n"
225:                                + "<m:CheckPriceResponse xmlns:m=\"http://www.apache-synapse.org/test\">\n"
226:                                + "\t<m:Code>{$payload//m0:return/m0:symbol/child::text()}</m:Code>\n"
227:                                + "\t<m:Price>{$payload//m0:return/m0:last/child::text()}</m:Price>\n"
228:                                + "</m:CheckPriceResponse>");
229:                assertTrue(mediator.mediate(mc));
230:            }
231:
232:            public void testQueryWithPayloadFour() throws Exception {
233:                MessageContext mc = TestUtils.getTestContext(sampleXml3, null);
234:                XQueryMediator mediator = new XQueryMediator();
235:                List list = new ArrayList();
236:                MediatorVariable variable = new MediatorCustomVariable(
237:                        new QName("payload"));
238:                variable.setType(XQItemType.XQITEMKIND_DOCUMENT);
239:                list.add(variable);
240:                MediatorCustomVariable variableForXml = new MediatorCustomVariable(
241:                        new QName("commission"));
242:                variableForXml.setType(XQItemType.XQITEMKIND_DOCUMENT);
243:                variableForXml.setRegKey("file:key");
244:                variableForXml.setValue(createOMElement(externalXMl));
245:                list.add(variableForXml);
246:                mediator.addAllVariables(list);
247:                mediator
248:                        .setQuerySource(" declare namespace m0=\"http://services.samples/xsd\";\n"
249:                                + " declare variable $payload as document-node() external;\n"
250:                                + " declare variable $commission as document-node() external;\n"
251:                                + " <m0:return xmlns:m0=\"http://services.samples/xsd\">\n"
252:                                + "  \t<m0:symbol>{$payload//m0:return/m0:symbol/child::text()}"
253:                                + "   </m0:symbol>\n"
254:                                + "  \t<m0:last>{$payload//m0:return/m0:last/child::text()+ $commission//commission/vendor[@symbol=$payload//m0:return/m0:symbol/child::text()]}</m0:last>\n"
255:                                + " </m0:return>");
256:                assertTrue(mediator.mediate(mc));
257:            }
258:
259:            public void testQueryReturnInt() throws Exception {
260:                MessageContext mc = TestUtils.getTestContext("<foo/>", null);
261:                XQueryMediator mediator = new XQueryMediator();
262:                mediator.setQuerySource("for $n in 1 to 10 return $n*$n");
263:                assertTrue(mediator.mediate(mc));
264:                assertEquals("1", mc.getEnvelope().getBody().getFirstElement()
265:                        .getText());
266:            }
267:
268:            public void testQueryReturnBoolean() throws Exception {
269:                MessageContext mc = TestUtils.getTestContext("<foo/>", null);
270:                XQueryMediator mediator = new XQueryMediator();
271:                mediator
272:                        .setQuerySource("declare variable $boolVar as xs:boolean external; $boolVar");
273:                List list = new ArrayList();
274:                MediatorVariable boolVariable = new MediatorBaseVariable(
275:                        new QName("boolVar"));
276:                boolVariable.setType(XQItemType.XQBASETYPE_BOOLEAN);
277:                boolVariable.setValue(Boolean.TRUE);
278:                list.add(boolVariable);
279:                mediator.addAllVariables(list);
280:                assertTrue(mediator.mediate(mc));
281:                assertEquals("true", mc.getEnvelope().getBody()
282:                        .getFirstElement().getText());
283:            }
284:
285:            protected OMElement createOMElement(String xml) {
286:                try {
287:
288:                    XMLStreamReader reader = XMLInputFactory.newInstance()
289:                            .createXMLStreamReader(new StringReader(xml));
290:                    StAXOMBuilder builder = new StAXOMBuilder(reader);
291:                    OMElement omElement = builder.getDocumentElement();
292:                    return omElement;
293:
294:                } catch (XMLStreamException e) {
295:                    throw new RuntimeException(e);
296:                }
297:            }
298:
299:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.