NodeStateAnalyzerFactory { public Analyzer createInstance(NodeState state) { if (state.hasProperty(LuceneIndexConstants.ANL_CLASS)){ return createAnalyzerViaReflection(state); } return composeAnalyzer(state); } NodeStateAnalyzerFactory(Version defaultVersion);   NodeStateAnalyzerFactory(ResourceLoader defaultLoader, Version defaultVersion); Analyzer createInstance(NodeState state);  }
@Test public void analyzerViaReflection() throws Exception{ NodeBuilder nb = EMPTY_NODE.builder(); nb.setProperty(ANL_CLASS, TestAnalyzer.class.getName()); TestAnalyzer analyzer = (TestAnalyzer) factory.createInstance(nb.getNodeState()); assertNotNull(analyzer); assertEquals(LuceneIndexConstants.VERSION, analyzer.matchVersion); nb.setProperty(LuceneIndexConstants.ANL_LUCENE_MATCH_VERSION, Version.LUCENE_31.toString()); analyzer = (TestAnalyzer) factory.createInstance(nb.getNodeState()); assertEquals("Version field not picked from config",Version.LUCENE_31, analyzer.matchVersion); byte[] stopWords = newCharArraySet("foo", "bar"); createFileNode(nb, LuceneIndexConstants.ANL_STOPWORDS, stopWords); analyzer = (TestAnalyzer) factory.createInstance(nb.getNodeState()); assertTrue("Configured stopword set not used",analyzer.getStopwordSet().contains("foo")); }
@Test public void analyzerByComposition_Tokenizer() throws Exception{ NodeBuilder nb = EMPTY_NODE.builder(); nb.child(ANL_TOKENIZER).setProperty(ANL_NAME, "whitespace"); TokenizerChain analyzer = (TokenizerChain) factory.createInstance(nb.getNodeState()); assertEquals(WhitespaceTokenizerFactory.class.getName(), analyzer.getTokenizer().getClassArg()); nb.child(ANL_TOKENIZER) .setProperty(ANL_NAME, "pathhierarchy") .setProperty("delimiter", "#"); analyzer = (TokenizerChain) factory.createInstance(nb.getNodeState()); assertEquals(PathHierarchyTokenizerFactory.class.getName(), analyzer.getTokenizer().getClassArg()); assertEquals('#', getValue(analyzer.getTokenizer(), "delimiter")); }
@Test public void analyzerByComposition_TokenFilter() throws Exception{ NodeBuilder nb = EMPTY_NODE.builder(); nb.child(ANL_TOKENIZER).setProperty(ANL_NAME, "whitespace"); NodeBuilder filters = nb.child(ANL_FILTERS); filters.setProperty(OAK_CHILD_ORDER, ImmutableList.of("stop", "LowerCase"),NAMES); filters.child("LowerCase").setProperty(ANL_NAME, "LowerCase"); filters.child("LowerCase").setProperty(JCR_PRIMARYTYPE, "nt:unstructured"); filters.child("stop").setProperty(ANL_LUCENE_MATCH_VERSION, Version.LUCENE_31.toString()); TokenizerChain analyzer = (TokenizerChain) factory.createInstance(nb.getNodeState()); assertEquals(2, analyzer.getFilters().length); assertEquals(StopFilterFactory.class.getName(), analyzer.getFilters()[0].getClassArg()); assertEquals(LowerCaseFilterFactory.class.getName(), analyzer.getFilters()[1].getClassArg()); assertTrue(analyzer.getFilters()[0].isExplicitLuceneMatchVersion()); }
@Test public void analyzerByComposition_CharFilter() throws Exception{ NodeBuilder nb = EMPTY_NODE.builder(); nb.child(ANL_TOKENIZER).setProperty(ANL_NAME, "whitespace"); NodeBuilder filters = nb.child(ANL_CHAR_FILTERS); filters.setProperty(OAK_CHILD_ORDER, ImmutableList.of("htmlStrip", "mapping"),NAMES); filters.child("mapping").setProperty(ANL_NAME, "mapping"); filters.child("htmlStrip"); TokenizerChain analyzer = (TokenizerChain) factory.createInstance(nb.getNodeState()); assertEquals(2, analyzer.getCharFilters().length); assertEquals(HTMLStripCharFilterFactory.class.getName(), analyzer.getCharFilters()[0].getClassArg()); assertEquals(MappingCharFilterFactory.class.getName(), analyzer.getCharFilters()[1].getClassArg()); }
@Test public void analyzerByComposition_FileResource() throws Exception{ NodeBuilder nb = EMPTY_NODE.builder(); nb.child(ANL_TOKENIZER).setProperty(ANL_NAME, "whitespace"); NodeBuilder filters = nb.child(ANL_FILTERS); NodeBuilder stop = filters.child("stop"); stop.setProperty("words", "set1.txt, set2.txt"); createFileNode(stop, "set1.txt", newCharArraySet("foo", "bar")); createFileNode(stop, "set2.txt", newCharArraySet("foo1", "bar1")); TokenizerChain analyzer = (TokenizerChain) factory.createInstance(nb.getNodeState()); assertEquals(1, analyzer.getFilters().length); assertEquals(StopFilterFactory.class.getName(), analyzer.getFilters()[0].getClassArg()); StopFilterFactory sff = (StopFilterFactory) analyzer.getFilters()[0]; assertTrue(sff.getStopWords().contains("foo")); assertTrue(sff.getStopWords().contains("foo1")); }