/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.components.yfilter;
import junit.framework.TestCase;
import edu.berkeley.cs.db.yfilter.filter.EXfilterBasic;
import edu.berkeley.cs.db.yfilterplus.queryparser.Query;
import edu.berkeley.cs.db.yfilterplus.queryparser.QueryParser;
import edu.berkeley.cs.db.yfilterplus.queryparser.XPQueryParser;
import edu.berkeley.cs.db.yfilterplus.queryparser.xpathparser.XPathParser;
import edu.berkeley.cs.db.yfilterplus.queryparser.xpathparser.PathQuery;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.StringReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* @version $Revision: 350 $
*/
public class YFilterTest extends TestCase {
protected EXfilterBasic filter = new EXfilterBasic();
protected String[] queries = { "/foo[@bar = 'abc']", "/foo[name = 'James']", "/foo[@dummy = 'def']", "/foo[name = 'Rob']"};
public void testYFilter() throws Exception {
Integer[] queryArray = new Integer[queries.length];
for (int i = 0; i < queries.length; i++) {
String query = queries[i];
queryArray[i] = addQuery(query);
}
filter.allocateStreams();
// now lets parse the XML Document
parseXml("<foo bar='abc'><name>James</name></foo>");
// now lets assert the results
ArrayList matchedQueries = filter.getMatchedQueries();
assertEquals("Should match 2 queries: " + matchedQueries, 2, matchedQueries.size());
assertEquals("First matching query", queryArray[0], matchedQueries.get(0));
assertEquals("Second matching query", queryArray[1], matchedQueries.get(1));
}
protected void setUp() throws Exception {
super.setUp();
}
protected Integer addQuery(String queryText) throws Exception {
Query query = parseQuery(queryText);
return new Integer(filter.addQuery(query));
}
protected Query parseQuery(String queryText) throws Exception {
XPathParser p = new XPathParser(queryText);
p.setDebug(true);
java_cup.runtime.Symbol s = p.parse();
if (s == null) {
throw new Exception("Failed to parse: " + queryText);
}
PathQuery parsedQuery = (PathQuery)s.value;
Query query = XPQueryParser.compile(parsedQuery);
return query;
}
protected void parseXml(String xmlText) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
SAXParser parser = parserFactory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(filter);
xmlReader.setDTDHandler(filter);
xmlReader.parse(new InputSource(new StringReader(xmlText)));
}
}