1416N/A/*
1416N/A * CDDL HEADER START
1416N/A *
1416N/A * The contents of this file are subject to the terms of the
1416N/A * Common Development and Distribution License (the "License").
1416N/A * You may not use this file except in compliance with the License.
1416N/A *
1416N/A * See LICENSE.txt included in this distribution for the specific
1416N/A * language governing permissions and limitations under the License.
1416N/A *
1416N/A * When distributing Covered Code, include this CDDL HEADER in each
1416N/A * file and include the License file at LICENSE.txt.
1416N/A * If applicable, add the following below this CDDL HEADER, with the
1416N/A * fields enclosed by brackets "[]" replaced with your own identifying
1416N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1416N/A *
1416N/A * CDDL HEADER END
1416N/A */
1416N/A
1416N/A/*
1416N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
1416N/A */
1416N/Apackage org.opensolaris.opengrok.analysis;
1416N/A
1416N/Aimport java.io.IOException;
1416N/Aimport java.io.StringReader;
1416N/Aimport java.lang.reflect.InvocationTargetException;
1416N/Aimport java.lang.reflect.Method;
1416N/Aimport java.util.Iterator;
1416N/Aimport junit.framework.Test;
1416N/Aimport junit.framework.TestCase;
1416N/Aimport junit.framework.TestSuite;
1416N/Aimport org.apache.lucene.analysis.Analyzer;
1416N/Aimport org.apache.lucene.analysis.TokenStream;
1416N/A
1416N/A/**
1416N/A * external tests, need to have test-framework on the path this will do a sanity
1416N/A * test on analyzers/tokenizers if they follow latest lucene asserts
1416N/A *
1416N/A * on compile test cp there needs to be lucene-test-framework, lucene-codecs and
1416N/A * randomizedtesting-runner on test src path there can be then the whole
1416N/A * test-framework/src from lucene
1416N/A *
1416N/A * @author Lubos Kosco
1416N/A */
1416N/Apublic class LuceneCompatibilityTest extends TestCase {
1416N/A
1416N/A //TODO use reflection to init tests in case LUCENE_TEST_CLASS is present,
1416N/A // create the object out of it and call it's methods
1416N/A public LuceneCompatibilityTest() {
1416N/A super();
1416N/A }
1416N/A private static final String LUCENE_TEST_CLASS =
1416N/A "org.apache.lucene.analysis.BaseTokenStreamTestCase";
1416N/A private static final String LUCENE_TEST_METHOD = "assertTokenStreamContents";
1416N/A private static final String LUCENE_DEP =
1416N/A "com.carrotsearch.randomizedtesting.RandomizedTest";
1416N/A
1416N/A /**
1416N/A * Create a suite of tests to run. If the lucene test-framework classes are
1416N/A * not present, skip this test.
1416N/A *
1416N/A * @return tests to run
1416N/A */
1416N/A public static Test suite() {
1416N/A try {
1416N/A Class.forName(LUCENE_DEP);
1416N/A Class.forName(LUCENE_TEST_CLASS);
1416N/A return new TestSuite(LuceneCompatibilityTest.class);
1416N/A } catch (ClassNotFoundException e) {
1416N/A return new TestSuite("LuceneCompatibility - empty (no external lucene test framework on classpath)");
1416N/A }
1416N/A }
1416N/A Analyzer testA;
1416N/A AnalyzerGuru guru;
1416N/A Method testM;
1416N/A Object testC = null;
1416N/A
1416N/A /**
1416N/A * Set up the test environment with repositories and a cache instance.
1416N/A */
1416N/A @Override
1416N/A protected void setUp() throws Exception {
1416N/A guru = new AnalyzerGuru();
1416N/A Class<?> c = Class.forName(LUCENE_TEST_CLASS);
1416N/A //testC = c.newInstance(); //this is static call
1416N/A Class[] argTypes = new Class[]{TokenStream.class, String[].class, int[].class, int[].class, String[].class, int[].class, int[].class, Integer.class, boolean.class};
1416N/A testM = c.getDeclaredMethod(LUCENE_TEST_METHOD, argTypes);
1416N/A }
1416N/A
1416N/A @Override
1416N/A protected void tearDown() throws Exception {
1416N/A }
1416N/A
1416N/A public void testCompatibility() throws Exception, IOException, IllegalAccessException, IllegalArgumentException {
1416N/A for (Iterator it = guru.getAnalyzerFactories().iterator(); it.hasNext();) {
1416N/A FileAnalyzerFactory fa = (FileAnalyzerFactory) it.next();
1416N/A String input = "Hello world";
1416N/A String[] output = new String[]{"Hello", "world"};
1416N/A testA = fa.getAnalyzer();
1416N/A String name = testA.getClass().getName();
1416N/A //below analyzers have no refs
1416N/A
1416N/A // !!!!!!!!!!!!!!!!!!!!
1416N/A // below will fail for some analyzers because of the way how we
1416N/A // deal with data - we don't use the reader, but cache the whole
1416N/A // file instead inside "content" buffer (which is reused for xref)
1416N/A // !!!!!!!!!!!!!!!!!!!!
1416N/A try {
1416N/A if (!name.endsWith("FileAnalyzer") && !name.endsWith("BZip2Analyzer") && !name.endsWith("GZIPAnalyzer")
1416N/A && !name.endsWith("XMLAnalyzer") && !name.endsWith("TroffAnalyzer") && !name.endsWith("ELFAnalyzer")
1416N/A && !name.endsWith("JavaClassAnalyzer") && !name.endsWith("JarAnalyzer") && !name.endsWith("ZipAnalyzer")
1416N/A //TODO below php and fortran analyzers have some problems with dummy input and asserts fail,
1416N/A // analyzers should properly set the tokens in case of wrongly formulated input
1416N/A && !name.endsWith("TarAnalyzer") && !name.endsWith("PhpAnalyzer") && !name.endsWith("FortranAnalyzer")) {
1416N/A
1416N/A System.out.println("Testing refs with " + name);
1416N/A //BaseTokenStreamTestCase.assertTokenStreamContents(testA.tokenStream("refs", new StringReader(input)), output, null, null, null, null, null, input.length());
1416N/A testM.invoke(testC, testA.tokenStream("refs", new StringReader(input)), output, null, null, null, null, null, input.length(), true);
1416N/A }
1416N/A output = new String[]{"hello", "world"};
1416N/A //below analyzers have no full, they just wrap data inside them
1416N/A if (!name.endsWith("FileAnalyzer") && !name.endsWith("BZip2Analyzer") && !name.endsWith("GZIPAnalyzer")) {
1416N/A System.out.println("Testing full with " + name);
1416N/A //BaseTokenStreamTestCase.assertTokenStreamContents(testA.tokenStream("full", new StringReader(input)), output, null, null, null, null, null, input.length());
1416N/A testM.invoke(testC, testA.tokenStream("full", new StringReader(input)), output, null, null, null, null, null, input.length(), true);
1416N/A }
1416N/A } catch (InvocationTargetException x) {
1416N/A Throwable cause = x.getCause();
1416N/A System.err.println(name + " failed: " + cause.getMessage() + " from " + LUCENE_TEST_CLASS + ":" + LUCENE_TEST_METHOD);
1416N/A throw (new Exception(cause));
1416N/A }
1416N/A }
1416N/A }
1416N/A}