KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > SimpleSearchTagTest


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import junit.framework.TestCase;
16
17 import org.apache.commons.lang.RandomStringUtils;
18 import org.apache.commons.lang.math.RandomUtils;
19 import org.apache.jackrabbit.core.query.xpath.TokenMgrError;
20 import org.apache.jackrabbit.core.query.xpath.XPathQueryBuilder;
21 import org.apache.jackrabbit.name.NamespaceResolver;
22 import org.apache.log4j.Logger;
23
24
25 /**
26  * @author Fabrizio Giustina
27  * @version $Revision: $ ($Author: $)
28  */

29 public class SimpleSearchTagTest extends TestCase {
30
31     /**
32      * Logger.
33      */

34     private static Logger log = Logger.getLogger(SimpleSearchTagTest.class);
35
36     /**
37      * Test for GenerateXPathQuery(). Uses Jackrabbit internal XPathQueryBuilder in order to validate the query.
38      */

39     public void testGenerateXPathQuery() {
40
41         SimpleSearchTag tag = new SimpleSearchTag();
42         tag.setQuery("AND test query AND path OR and OR join AND AND test AND OR");
43
44         String JavaDoc xpath = tag.generateXPathQuery();
45         log.debug(xpath);
46
47         try {
48             NamespaceResolver resolver = null; // session.getNamespaceResolver()
49
XPathQueryBuilder.createQuery(xpath, resolver);
50         }
51         catch (TokenMgrError e) {
52             fail("Invalid query: [" + xpath + "] " + e.getMessage());
53         }
54         catch (Throwable JavaDoc e) {
55             // ok, we are setting a namespace resolver since we are running "off line", so it's normal to see exceptions
56
}
57     }
58
59     /**
60      * Test for GenerateXPathQuery(). Uses Jackrabbit internal XPathQueryBuilder in order to validate the query.
61      */

62     public void testGenerateXPathQuerySmokeTest() {
63
64         SimpleSearchTag tag = new SimpleSearchTag();
65
66         for (int j = 0; j < 100; j++) {
67
68             StringBuffer JavaDoc inputstring = new StringBuffer JavaDoc(100);
69             for (int u = 0; u < 10; u++) {
70                 inputstring.append(RandomStringUtils.random(RandomUtils.nextInt(10)));
71                 inputstring.append(" ");
72                 inputstring.append(RandomStringUtils.randomAlphanumeric(RandomUtils.nextInt(10)));
73                 inputstring.append(" ");
74             }
75             tag.setQuery(inputstring.toString());
76             String JavaDoc xpath = tag.generateXPathQuery();
77             log.debug(xpath);
78
79             try {
80                 NamespaceResolver resolver = null; // session.getNamespaceResolver()
81
XPathQueryBuilder.createQuery(xpath, resolver);
82             }
83             catch (TokenMgrError e) {
84                 fail("Invalid query: ["
85                     + xpath
86                     + "] "
87                     + e.getMessage()
88                     + ". Input query was: ["
89                     + inputstring.toString()
90                     + "]");
91             }
92             catch (Throwable JavaDoc e) {
93                 // ok, we are setting a namespace resolver since we are running "off line", so it's normal to see
94
// exceptions
95
}
96         }
97     }
98
99 }
100
Popular Tags