KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > analysis > PathTokenFilterTest


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.repo.search.impl.lucene.analysis;
18
19 import java.io.IOException JavaDoc;
20 import java.io.StringReader JavaDoc;
21
22 import org.apache.lucene.analysis.Token;
23 import org.apache.lucene.analysis.TokenStream;
24
25 import junit.framework.TestCase;
26
27 public class PathTokenFilterTest extends TestCase
28 {
29
30     public PathTokenFilterTest()
31     {
32         super();
33     }
34
35     public PathTokenFilterTest(String JavaDoc arg0)
36     {
37         super(arg0);
38     }
39
40     
41     public void testFullPath() throws IOException JavaDoc
42     {
43         tokenise("{uri1}one", new String JavaDoc[]{"uri1", "one"});
44         tokenise("/{uri1}one", new String JavaDoc[]{"uri1", "one"});
45         tokenise("{uri1}one/{uri2}two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
46         tokenise("/{uri1}one/{uri2}two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
47         tokenise("{uri1}one/{uri2}two/{uri3}three", new String JavaDoc[]{"uri1", "one", "uri2", "two", "uri3", "three"});
48         tokenise("/{uri1}one/{uri2}two/{uri3}three", new String JavaDoc[]{"uri1", "one", "uri2", "two", "uri3", "three"});
49         try
50         {
51            tokenise("{uri1}one;{uri2}two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
52         }
53         catch(IllegalStateException JavaDoc ise)
54         {
55             
56         }
57        
58     }
59     
60     
61     public void testPrefixPath() throws IOException JavaDoc
62     {
63         tokenise("uri1:one", new String JavaDoc[]{"uri1", "one"});
64         tokenise("/uri1:one", new String JavaDoc[]{"uri1", "one"});
65         tokenise("uri1:one/uri2:two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
66         tokenise("/uri1:one/uri2:two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
67         tokenise("uri1:one/uri2:two/uri3:three", new String JavaDoc[]{"uri1", "one", "uri2", "two", "uri3", "three"});
68         tokenise("/uri1:one/uri2:two/uri3:three", new String JavaDoc[]{"uri1", "one", "uri2", "two", "uri3", "three"});
69         try
70         {
71            tokenise("{uri1}one;{uri2}two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
72         }
73         catch(IllegalStateException JavaDoc ise)
74         {
75             
76         }
77        
78     }
79     
80     
81     public void testMixedPath() throws IOException JavaDoc
82     {
83      
84         tokenise("{uri1}one/uri2:two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
85         tokenise("/{uri1}one/uri2:two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
86         tokenise("uri1:one/{uri2}two/uri3:three", new String JavaDoc[]{"uri1", "one", "uri2", "two", "uri3", "three"});
87         tokenise("/uri1:one/{uri2}two/uri3:three", new String JavaDoc[]{"uri1", "one", "uri2", "two", "uri3", "three"});
88         try
89         {
90            tokenise("{uri1}one;{uri2}two/", new String JavaDoc[]{"uri1", "one", "uri2", "two"});
91         }
92         catch(IllegalStateException JavaDoc ise)
93         {
94             
95         }
96        
97     }
98     
99     
100     private void tokenise(String JavaDoc path, String JavaDoc[] tokens) throws IOException JavaDoc
101     {
102         StringReader JavaDoc reader = new StringReader JavaDoc(path);
103         TokenStream ts = new PathTokenFilter(reader, PathTokenFilter.PATH_SEPARATOR,
104                 PathTokenFilter.SEPARATOR_TOKEN_TEXT, PathTokenFilter.NO_NS_TOKEN_TEXT,
105                 PathTokenFilter.NAMESPACE_START_DELIMITER, PathTokenFilter.NAMESPACE_END_DELIMITER, true);
106        Token t;
107        int i = 0;
108        while( (t = ts.next()) != null)
109        {
110            if(t.type().equals(PathTokenFilter.TOKEN_TYPE_PATH_ELEMENT_NAMESPACE))
111            {
112                assert(i % 2 == 0);
113                assertEquals(t.termText(), tokens[i++]);
114            }
115            else if(t.type().equals(PathTokenFilter.TOKEN_TYPE_PATH_ELEMENT_NAMESPACE_PREFIX))
116            {
117                assert(i % 2 == 0);
118                assertEquals(t.termText(), tokens[i++]);
119            }
120            else if(t.type().equals(PathTokenFilter.TOKEN_TYPE_PATH_ELEMENT_NAME))
121            {
122                assert(i % 2 == 1);
123                assertEquals(t.termText(), tokens[i++]);
124            }
125        }
126        if(i != tokens.length)
127        {
128            fail("Invalid number of tokens, found "+i+" and expected "+tokens.length);
129        }
130     }
131 }
132
Popular Tags