1 package org.xmlBlaster.test.mime;
 2 
 3 import java.io.File;
 4 
 5 import org.custommonkey.xmlunit.XMLTestCase;
 6 import org.xmlBlaster.authentication.SessionInfo;
 7 import org.xmlBlaster.engine.ServerScope;
 8 import org.xmlBlaster.engine.mime.Query;
 9 import org.xmlBlaster.engine.mime.xpath.XPathFilter;
10 import org.xmlBlaster.util.FileLocator;
11 import org.xmlBlaster.util.Global;
12 import org.xmlBlaster.util.MsgUnit;
13 import org.xmlBlaster.util.Timestamp;
14 import org.xmlBlaster.util.plugin.PluginInfo;
15 
16 /**
17  * Test the MIME XPath plugin <tt>org.xmlBlaster.engine.mime.xpath.XPathFilter</tt>. 
18  * @author Marcel Ruff
19  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/mime.plugin.access.xpath.html">The mime.plugin.access.xpath requirement</a>
20  */
21 public class XPathTransformerTest extends XMLTestCase {
22 
23    public void testXpathQos() throws Exception {
24       ServerScope scope = new ServerScope();
25       Global glob = scope;
26       XPathFilter filter = new XPathFilter();
27       filter.initialize(scope);
28       String content = "SomethingFancy";
29       String queryStr = "/qos";
30       String qos = "<qos/>";
31       
32       MsgUnit msgUnit = new MsgUnit("<key oid='Hello'/>", content, qos);
33       msgUnit.getQosData().setRcvTimestamp(new Timestamp());
34       SessionInfo sessionInfo = null;
35 
36       PluginInfo info = new PluginInfo(glob, null, "XPathFilter", "1.0");
37       info.getParameters().put(XPathFilter.MATCH_AGAINST_QOS, ""+true);
38       filter.init(glob, info);
39       
40       {
41          Query query = new Query(glob, queryStr);
42          boolean ret = filter.match(sessionInfo, msgUnit, query);
43          System.out.println("Match: " + ret + "\nResult: " + msgUnit.getQos());
44          assertTrue(queryStr + " should match", ret);
45       }
46 
47       {
48          queryStr = "/a";
49          Query query = new Query(glob, queryStr);
50          boolean ret = filter.match(sessionInfo, msgUnit, query);
51          System.out.println("Match: " + ret + "\nResult: " + msgUnit.getQos());
52          assertFalse(queryStr + " shouldn't match", ret);
53       }
54    }
55 
56    public void testXsltTransformation() throws Exception {
57       ServerScope scope = new ServerScope();
58       Global glob = scope;
59       XPathFilter filter = new XPathFilter();
60       filter.initialize(scope);
61       String content = "<a><b/></a>";
62       String xslFile = "test.xsl";
63       String queryStr = "/a";
64       String qos = "<qos/>";
65       
66       FileLocator.writeFile(xslFile,
67          "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
68          "  <xsl:template match ='/'>" +
69          "    <c/>" +
70          "  </xsl:template>" +
71          "</xsl:stylesheet>");
72       
73       try {
74          MsgUnit msgUnit = new MsgUnit("<key oid='Hello'/>", content, qos);
75          msgUnit.getQosData().setRcvTimestamp(new Timestamp());
76          SessionInfo sessionInfo = null;
77    
78          PluginInfo info = new PluginInfo(glob, null, "XPathFilter", "1.0");
79          info.getParameters().put(XPathFilter.XSL_CONTENT_TRANSFORMER_FILE_NAME, xslFile);
80          filter.init(glob, info);
81          
82          Query query = new Query(glob, queryStr);
83          boolean ret = filter.match(sessionInfo, msgUnit, query);
84          System.out.println("Match: " + ret + "\nResult: " + msgUnit.getContentStr());
85          assertTrue(ret);
86          assertXMLEqual("<c/>", msgUnit.getContentStr());
87       }
88       finally {
89          File f = new File(xslFile);
90          f.delete();
91       }
92    }
93 }


syntax highlighted by Code2HTML, v. 0.9.1