1 17 package org.alfresco.repo.search.impl.lucene.analysis; 18 19 import java.io.IOException ; 20 import java.io.StringReader ; 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 arg0) 36 { 37 super(arg0); 38 } 39 40 41 public void testFullPath() throws IOException 42 { 43 tokenise("{uri1}one", new String []{"uri1", "one"}); 44 tokenise("/{uri1}one", new String []{"uri1", "one"}); 45 tokenise("{uri1}one/{uri2}two/", new String []{"uri1", "one", "uri2", "two"}); 46 tokenise("/{uri1}one/{uri2}two/", new String []{"uri1", "one", "uri2", "two"}); 47 tokenise("{uri1}one/{uri2}two/{uri3}three", new String []{"uri1", "one", "uri2", "two", "uri3", "three"}); 48 tokenise("/{uri1}one/{uri2}two/{uri3}three", new String []{"uri1", "one", "uri2", "two", "uri3", "three"}); 49 try 50 { 51 tokenise("{uri1}one;{uri2}two/", new String []{"uri1", "one", "uri2", "two"}); 52 } 53 catch(IllegalStateException ise) 54 { 55 56 } 57 58 } 59 60 61 public void testPrefixPath() throws IOException 62 { 63 tokenise("uri1:one", new String []{"uri1", "one"}); 64 tokenise("/uri1:one", new String []{"uri1", "one"}); 65 tokenise("uri1:one/uri2:two/", new String []{"uri1", "one", "uri2", "two"}); 66 tokenise("/uri1:one/uri2:two/", new String []{"uri1", "one", "uri2", "two"}); 67 tokenise("uri1:one/uri2:two/uri3:three", new String []{"uri1", "one", "uri2", "two", "uri3", "three"}); 68 tokenise("/uri1:one/uri2:two/uri3:three", new String []{"uri1", "one", "uri2", "two", "uri3", "three"}); 69 try 70 { 71 tokenise("{uri1}one;{uri2}two/", new String []{"uri1", "one", "uri2", "two"}); 72 } 73 catch(IllegalStateException ise) 74 { 75 76 } 77 78 } 79 80 81 public void testMixedPath() throws IOException 82 { 83 84 tokenise("{uri1}one/uri2:two/", new String []{"uri1", "one", "uri2", "two"}); 85 tokenise("/{uri1}one/uri2:two/", new String []{"uri1", "one", "uri2", "two"}); 86 tokenise("uri1:one/{uri2}two/uri3:three", new String []{"uri1", "one", "uri2", "two", "uri3", "three"}); 87 tokenise("/uri1:one/{uri2}two/uri3:three", new String []{"uri1", "one", "uri2", "two", "uri3", "three"}); 88 try 89 { 90 tokenise("{uri1}one;{uri2}two/", new String []{"uri1", "one", "uri2", "two"}); 91 } 92 catch(IllegalStateException ise) 93 { 94 95 } 96 97 } 98 99 100 private void tokenise(String path, String [] tokens) throws IOException 101 { 102 StringReader reader = new StringReader (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 |