4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony/*
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * CDDL HEADER START
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony *
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * The contents of this file are subject to the terms of the
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * Common Development and Distribution License (the "License").
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * You may not use this file except in compliance with the License.
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony *
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * See LICENSE.txt included in this distribution for the specific
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * language governing permissions and limitations under the License.
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony *
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * When distributing Covered Code, include this CDDL HEADER in each
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * file and include the License file at LICENSE.txt.
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * If applicable, add the following below this CDDL HEADER, with the
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * fields enclosed by brackets "[]" replaced with your own identifying
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * information: Portions Copyright [yyyy] [name of copyright owner]
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony *
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * CDDL HEADER END
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony */
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony /*
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony */
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonypackage org.opensolaris.opengrok.analysis.pascal;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport java.io.File;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport java.io.FileInputStream;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport java.io.IOException;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport java.io.InputStream;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport java.io.StringWriter;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.apache.lucene.document.Document;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.apache.lucene.document.Field;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport static org.hamcrest.CoreMatchers.is;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.junit.AfterClass;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport static org.junit.Assert.assertNotNull;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport static org.junit.Assert.assertThat;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport static org.junit.Assert.assertTrue;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport static org.junit.Assert.fail;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.junit.BeforeClass;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.junit.Test;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport static org.opensolaris.opengrok.analysis.AnalyzerGuru.string_ft_nstored_nanalyzed_norms;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.opensolaris.opengrok.analysis.Ctags;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.opensolaris.opengrok.analysis.Definitions;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.opensolaris.opengrok.analysis.FileAnalyzer;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.opensolaris.opengrok.analysis.StreamSource;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.opensolaris.opengrok.search.QueryBuilder;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonyimport org.opensolaris.opengrok.util.TestRepository;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony/**
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony *
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * @author alexanthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony */
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthonypublic class PascalAnalyzerFactoryTest {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony FileAnalyzer analyzer;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony private final String ctagsProperty = "org.opensolaris.opengrok.analysis.Ctags";
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony private static Ctags ctags;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony private static TestRepository repository;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony public PascalAnalyzerFactoryTest() {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony PascalAnalyzerFactory analyzerFactory = new PascalAnalyzerFactory();
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony this.analyzer = analyzerFactory.getAnalyzer();
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony RuntimeEnvironment env = RuntimeEnvironment.getInstance();
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony env.setCtags(System.getProperty(ctagsProperty, "ctags"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony if (env.validateExuberantCtags()) {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony this.analyzer.setCtags(new Ctags());
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony private static StreamSource getStreamSource(final String fname) {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony return new StreamSource() {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony @Override
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony public InputStream getStream() throws IOException {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony return new FileInputStream(fname);
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony };
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony @BeforeClass
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony public static void setUpClass() throws Exception {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony ctags = new Ctags();
cdf0e447de808476dc7ae748effd07627a24f29eLubos Kosco ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony repository = new TestRepository();
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony repository.create(PascalAnalyzerFactoryTest.class.getResourceAsStream(
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony "/org/opensolaris/opengrok/index/source.zip"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony @AfterClass
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony public static void tearDownClass() throws Exception {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony ctags.close();
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony ctags = null;
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony /**
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * Test of writeXref method, of class PascalAnalyzerFactory.
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony *
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony * @throws java.lang.Exception
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony */
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony @Test
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony public void testAnalyzer() throws Exception {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony String path = repository.getSourceRoot() + "/pascal/Sample.pas";
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony File f = new File(path);
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony if (!(f.canRead() && f.isFile())) {
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony fail("pascal testfile " + f + " not found");
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony Document doc = new Document();
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony doc.add(new Field(QueryBuilder.FULLPATH, path,
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony string_ft_nstored_nanalyzed_norms));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony StringWriter xrefOut = new StringWriter();
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony analyzer.setCtags(ctags);
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony analyzer.setScopesEnabled(true);
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony analyzer.analyze(doc, getStreamSource(path), xrefOut);
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony Definitions definitions = Definitions.deserialize(doc.getField(QueryBuilder.TAGS).binaryValue().bytes);
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertNotNull(definitions);
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony String[] type = new String[1];
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("Sample", 22, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("unit"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("TSample", 28, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("Class"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("Id", 40, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("property"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("Description", 41, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("property"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("TSample.GetId", 48, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("function"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("TSample.SetId", 53, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("procedure"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("TSample.GetClassName", 58, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("function"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertTrue(definitions.hasDefinitionAt("TSample.GetUser", 63, type));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony assertThat(type[0], is("function"));
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony }
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony
4eeac49e1b7fcd20b9586bbdc636a67f6feed3bfAlex Anthony}