KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > SearchLanguageConversionTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @see org.alfresco.util.SearchLanguageConversion
23  *
24  * @author Derek Hulley
25  */

26 public class SearchLanguageConversionTest extends TestCase
27 {
28     /**
29      * A string with a whole lod of badness to stress test with
30      */

31     private static final String JavaDoc BAD_STRING =
32             "\\ | ! \" £ " +
33             "$ % ^ & * ( " +
34             ") _ { } [ ] " +
35             "@ # ~ ' : ; " +
36             ", . < > + ? " +
37             "/ \\\\ \\* \\? \\_";
38     
39     public void testEscapeXPathLike()
40     {
41         String JavaDoc good = SearchLanguageConversion.escapeForXPathLike(BAD_STRING);
42         assertEquals("Escaping for xpath failed",
43                 "\\\\ | ! \" £ " +
44                 "$ \\% ^ & * ( " +
45                 ") \\_ { } [ ] " +
46                 "@ # ~ ' : ; " +
47                 ", . < > + ? " +
48                 "/ \\\\\\\\ \\\\* \\\\? \\\\\\_",
49                 good);
50     }
51     
52     public void testEscapeRegex()
53     {
54         String JavaDoc good = SearchLanguageConversion.escapeForRegex(BAD_STRING);
55         assertEquals("Escaping for regex failed",
56                 "\\\\ \\| ! \" £ " +
57                 "\\$ % \\^ & \\* \\( " +
58                 "\\) _ \\{ \\} [ ] " +
59                 "@ # ~ ' : ; " +
60                 ", \\. < > \\+ \\? " +
61                 "/ \\\\\\\\ \\\\\\* \\\\\\? \\\\_",
62                 good);
63     }
64     
65     public void testEscapeLucene()
66     {
67         String JavaDoc good = SearchLanguageConversion.escapeForLucene(BAD_STRING);
68         assertEquals("Escaping for regex failed",
69                 "\\\\ | \\! \\\" £ " +
70                 "$ % \\^ & \\* \\( " +
71                 "\\) _ \\{ \\} \\[ \\] " +
72                 "@ # \\~ ' \\: ; " +
73                 ", . < > \\+ \\? " +
74                 "/ \\\\\\\\ \\\\\\* \\\\\\? \\\\_",
75                 good);
76     }
77     
78     public void testConvertXPathLikeToRegex()
79     {
80         String JavaDoc good = SearchLanguageConversion.convertXPathLikeToRegex(BAD_STRING);
81         assertEquals("XPath like to regex failed",
82                 "(?s)\\ \\| ! \" £ " +
83                 "\\$ .* \\^ & \\* \\( " +
84                 "\\) . \\{ \\} [ ] " +
85                 "@ # ~ ' : ; " +
86                 ", \\. < > \\+ \\? " +
87                 "/ \\\\ \\* \\? \\_",
88                 good);
89     }
90     
91     public void testConvertXPathLikeToLucene()
92     {
93         String JavaDoc good = SearchLanguageConversion.convertXPathLikeToLucene(BAD_STRING);
94         assertEquals("XPath like to regex failed",
95                 "\\ | \\! \\\" £ " +
96                 "$ * \\^ & \\* \\( " +
97                 "\\) ? \\{ \\} \\[ \\] " +
98                 "@ # \\~ ' \\: ; " +
99                 ", . < > \\+ \\? " +
100                 "/ \\\\ \\* \\? \\_",
101                 good);
102     }
103 }
104
Popular Tags