2 * Copyright (c) 1995-2014, Index Datassss
4 * See the file LICENSE for details.
6 package org.z3950.zing.cql;
8 import java.io.BufferedInputStream;
9 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.Reader;
15 import java.io.StringReader;
16 import java.net.URISyntaxException;
18 import java.net.URLDecoder;
19 import java.util.Enumeration;
20 import java.util.HashSet;
22 import java.util.jar.JarEntry;
23 import java.util.jar.JarFile;
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import static org.junit.Assert.*;
31 import static java.lang.System.out;
32 import java.util.Properties;
38 public class CQLParserTest {
39 public CQLParserTest() {
43 public static void setUpClass() {
47 public static void tearDownClass() {
55 public void tearDown() {
59 * Test of main method, of class CQLParser.
62 public void testRegressionQueries() throws IOException {
63 System.out.println("Testing the parser using pre-canned regression queries...");
64 //we might be running the test from within the jar
65 //list all resource dirs, then traverse them
66 String[] dirs = getResourceListing(this.getClass(), "regression");
67 for (String dir : dirs) {
68 String files[] = getResourceListing(this.getClass(), "regression/" + dir);
69 for (String file : files) {
70 if (!file.endsWith(".cql")) continue;
71 out.println("Parsing "+dir+"/"+file);
72 InputStream is = this.getClass().getResourceAsStream("/regression/"+dir+"/"+file);
73 BufferedReader reader = null, reader2 = null;
75 reader = new BufferedReader(new InputStreamReader(is));
76 String input = reader.readLine();
77 out.println("Query: "+input);
78 CQLParser parser = new CQLParser();
79 CQLNode parsed = parser.parse(input);
80 String xcql = parsed.toXCQL();
81 out.println("Parsed:");
83 //read the expected xcql output
84 String prefix = file.substring(0, file.length()-4);
85 reader2 = new BufferedReader(new InputStreamReader(
86 this.getClass().getResourceAsStream("/regression/"+dir+"/"+prefix+".xcql")));
87 StringBuilder sb = new StringBuilder();
89 while ((line = reader2.readLine()) != null) {
90 sb.append(line).append("\n");
92 String expected = sb.toString();
93 out.println("Expected: ");
94 out.println(expected);
95 assertEquals("Assertion failure for "+dir+"/"+file, expected, xcql);
96 } catch (CQLParseException pe) {
97 fail("Parsing failed with: "+pe.toString());
99 if (reader != null) reader.close();
100 if (reader2 != null) reader2.close();
107 * Test the integrity of the parser as follows:
108 * - Generate a random tree with CQLGenerator
110 * - Canonicalise it by running through the parser
111 * - Compare the before-and-after versions.
112 * Since the CQLGenerator output is in canonical form anyway, the
113 * before-and-after versions should be identical. This process exercises
114 * the comprehensiveness and bullet-proofing of the parser, as well as
115 * the accuracy of the rendering.
116 * @throws IOException
117 * @throws MissingParameterException
120 public void testRandomQueries() throws IOException, MissingParameterException {
121 out.println("Testing the parser using 100 randomly generated queries...");
122 Properties params = new Properties();
123 InputStream is = getClass().getResourceAsStream("/generate.properties");
125 fail("Cannot locate generate.properties");
128 CQLGenerator generator = new CQLGenerator(params);
129 for (int i=0; i<100; i++) {
130 CQLNode random = generator.generate();
131 String expected = random.toCQL();
132 out.println("Generated query: "+expected);
133 CQLParser parser = new CQLParser();
135 CQLNode parsed = parser.parse(expected);
136 String result = parsed.toCQL();
137 assertEquals(expected, result);
138 } catch (CQLParseException pe) {
139 fail("Generated query failed to parse: "+pe.getMessage());
144 //helper methods follow
145 //TODO move to masterkey-common
147 @SuppressWarnings("rawtypes")
148 public static String[] getResourceListing(Class clazz, String path) throws
150 URL dirURL = clazz.getClassLoader().getResource(path);
151 if (dirURL != null && dirURL.getProtocol().equals("file")) {
152 /* A file path: easy enough */
154 return new File(dirURL.toURI()).list();
155 } catch (URISyntaxException use) {
156 throw new UnsupportedOperationException(use);
160 if (dirURL == null) {
162 * In case of a jar file, we can't actually find a directory.
163 * Have to assume the same jar as clazz.
165 String me = clazz.getName().replace(".", "/") + ".class";
166 dirURL = clazz.getClassLoader().getResource(me);
169 if (dirURL.getProtocol().equals("jar")) {
171 String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf(
172 "!")); //strip out only the JAR file
173 JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
174 Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
175 Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
176 while (entries.hasMoreElements()) {
177 String name = entries.nextElement().getName();
178 if (name.startsWith(path)) { //filter according to the path
179 String entry = name.substring(path.length());
180 int checkSubdir = entry.indexOf("/");
181 if (checkSubdir >= 0) {
182 // if it is a subdirectory, we just return the directory name
183 entry = entry.substring(0, checkSubdir);
188 return result.toArray(new String[result.size()]);
191 throw new UnsupportedOperationException("Cannot list files for URL "