ClasspathAnalyzerBuilder extends AnalyzerBuilder { @Override public Analyzer analyzer() { try { Class<?> analyzerClass = Class.forName(className); Constructor<?> constructor = analyzerClass.getConstructor(); return (Analyzer) constructor.newInstance(); } catch (Exception e) { throw new IndexException(e, "Not found analyzer '{}'", className); } } @JsonCreator  ClasspathAnalyzerBuilder(@JsonProperty("class") String className); @Override Analyzer analyzer();  }
@Test public void testBuild() { String className = "org.apache.lucene.analysis.en.EnglishAnalyzer"; ClasspathAnalyzerBuilder builder = new ClasspathAnalyzerBuilder(className); Analyzer analyzer = builder.analyzer(); assertEquals("Expected EnglishAnalyzer class", EnglishAnalyzer.class, analyzer.getClass()); }
@Test(expected = IndexException.class) public void testBuildWithWrongClassName() { new ClasspathAnalyzerBuilder("abc").analyzer(); }
@Test public void testParseJSON() throws IOException { String json = "{type:\"classpath\", class:\"org.apache.lucene.analysis.en.EnglishAnalyzer\"}"; AnalyzerBuilder builder = JsonSerializer.fromString(json, AnalyzerBuilder.class); Analyzer analyzer = builder.analyzer(); assertEquals("Expected EnglishAnalyzer class", EnglishAnalyzer.class, analyzer.getClass()); }