bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco/*
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * CDDL HEADER START
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco *
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * The contents of this file are subject to the terms of the
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * Common Development and Distribution License (the "License").
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * You may not use this file except in compliance with the License.
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco *
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * See LICENSE.txt included in this distribution for the specific
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * language governing permissions and limitations under the License.
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco *
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * When distributing Covered Code, include this CDDL HEADER in each
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * file and include the License file at LICENSE.txt.
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * If applicable, add the following below this CDDL HEADER, with the
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * fields enclosed by brackets "[]" replaced with your own identifying
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * information: Portions Copyright [yyyy] [name of copyright owner]
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco *
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * CDDL HEADER END
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco */
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco/*
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco */
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariapackage org.opensolaris.opengrok.analysis.clojure;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.apache.lucene.document.Document;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.apache.lucene.document.Field;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.junit.AfterClass;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.junit.BeforeClass;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.junit.Test;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.opensolaris.opengrok.analysis.Ctags;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.opensolaris.opengrok.analysis.Definitions;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.opensolaris.opengrok.analysis.FileAnalyzer;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.opensolaris.opengrok.analysis.StreamSource;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.opensolaris.opengrok.search.QueryBuilder;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport org.opensolaris.opengrok.util.TestRepository;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport java.io.File;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport java.io.FileInputStream;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport java.io.IOException;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport java.io.InputStream;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport java.io.StringWriter;
f2c2e34eec8ec66a7f830c31be39e568a6ecf686Farid Zakariaimport static org.hamcrest.CoreMatchers.is;
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Koscoimport static org.junit.Assert.assertThat;
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Koscoimport static org.junit.Assert.assertTrue;
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Koscoimport static org.junit.Assert.fail;
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariaimport static org.opensolaris.opengrok.analysis.AnalyzerGuru.string_ft_nstored_nanalyzed_norms;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria/**
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria * @author Farid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria */
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakariapublic class ClojureAnalyzerFactoryTest {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria FileAnalyzer analyzer;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria private final String ctagsProperty = "org.opensolaris.opengrok.analysis.Ctags";
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria private static Ctags ctags;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria private static TestRepository repository;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria public ClojureAnalyzerFactoryTest() {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria ClojureAnalyzerFactory analFact = new ClojureAnalyzerFactory();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria this.analyzer = analFact.getAnalyzer();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria RuntimeEnvironment env = RuntimeEnvironment.getInstance();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria env.setCtags(System.getProperty(ctagsProperty, "ctags"));
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria if (env.validateExuberantCtags()) {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria this.analyzer.setCtags(new Ctags());
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria private static StreamSource getStreamSource(final String fname) {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria return new StreamSource() {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria @Override
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria public InputStream getStream() throws IOException {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria return new FileInputStream(fname);
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria };
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria @BeforeClass
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria public static void setUpClass() throws Exception {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria ctags = new Ctags();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria repository = new TestRepository();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria repository.create(ClojureAnalyzerFactoryTest.class.getResourceAsStream(
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria "/org/opensolaris/opengrok/index/source.zip"));
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria @AfterClass
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria public static void tearDownClass() throws Exception {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria ctags.close();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria ctags = null;
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria /**
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria * Test of writeXref method, of class CAnalyzerFactory.
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria *
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria * @throws java.lang.Exception
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria */
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria @Test
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria public void testScopeAnalyzer() throws Exception {
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria String path = repository.getSourceRoot() + "/clojure/sample.clj";
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria File f = new File(path);
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria if (!(f.canRead() && f.isFile())) {
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria fail("clojure testfile " + f + " not found");
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria Document doc = new Document();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria doc.add(new Field(QueryBuilder.FULLPATH, path,
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria string_ft_nstored_nanalyzed_norms));
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria StringWriter xrefOut = new StringWriter();
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria analyzer.setCtags(ctags);
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria analyzer.analyze(doc, getStreamSource(path), xrefOut);
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria Definitions definitions = Definitions.deserialize(doc.getField(QueryBuilder.TAGS).binaryValue().bytes);
f2c2e34eec8ec66a7f830c31be39e568a6ecf686Farid Zakaria
f2c2e34eec8ec66a7f830c31be39e568a6ecf686Farid Zakaria String[] type = new String[1];
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertTrue(definitions.hasDefinitionAt("opengrok", 4, type));
f2c2e34eec8ec66a7f830c31be39e568a6ecf686Farid Zakaria assertThat(type[0], is("namespace"));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertTrue(definitions.hasDefinitionAt("power-set", 8, type));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertThat(type[0], is("function"));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertTrue(definitions.hasDefinitionAt("power-set-private", 14, type));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertThat(type[0], is("private function"));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertTrue(definitions.hasDefinitionAt("author", 19, type));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertThat(type[0], is("struct"));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertTrue(definitions.hasDefinitionAt("author-first-name", 22, type));
f2c2e34eec8ec66a7f830c31be39e568a6ecf686Farid Zakaria assertThat(type[0], is("definition"));
7a901a0be55a8a3c63aed0013b4a3aa10ace0adbFarid Zakaria assertTrue(definitions.hasDefinitionAt("Farid", 24, type));
f2c2e34eec8ec66a7f830c31be39e568a6ecf686Farid Zakaria assertThat(type[0], is("definition"));
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria }
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria
6f57e337e147fcb6a473df55a97ea94b017bc21cFarid Zakaria}