JFlexXrefTest.java revision 1108
1020N/A/*
1020N/A * CDDL HEADER START
1020N/A *
1020N/A * The contents of this file are subject to the terms of the
1020N/A * Common Development and Distribution License (the "License").
1020N/A * You may not use this file except in compliance with the License.
1020N/A *
1020N/A * See LICENSE.txt included in this distribution for the specific
1020N/A * language governing permissions and limitations under the License.
1020N/A *
1020N/A * When distributing Covered Code, include this CDDL HEADER in each
1020N/A * file and include the License file at LICENSE.txt.
1020N/A * If applicable, add the following below this CDDL HEADER, with the
1020N/A * fields enclosed by brackets "[]" replaced with your own identifying
1020N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1020N/A *
1020N/A * CDDL HEADER END
1020N/A */
1020N/A
1020N/A/*
1050N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
1020N/A */
1020N/A
1020N/Apackage org.opensolaris.opengrok.analysis;
1020N/A
1020N/Aimport java.io.File;
1020N/Aimport java.io.FileInputStream;
1020N/Aimport java.io.InputStreamReader;
1020N/Aimport java.io.Reader;
1020N/Aimport java.io.StringReader;
1020N/Aimport java.io.StringWriter;
1020N/Aimport org.junit.AfterClass;
1020N/Aimport org.junit.BeforeClass;
1020N/Aimport org.junit.Test;
1020N/Aimport org.opensolaris.opengrok.analysis.c.CXref;
1020N/Aimport org.opensolaris.opengrok.analysis.c.CxxXref;
1020N/Aimport org.opensolaris.opengrok.analysis.document.TroffXref;
1020N/Aimport org.opensolaris.opengrok.analysis.fortran.FortranXref;
1020N/Aimport org.opensolaris.opengrok.analysis.java.JavaXref;
1020N/Aimport org.opensolaris.opengrok.analysis.lisp.LispXref;
1076N/Aimport org.opensolaris.opengrok.analysis.perl.PerlXref;
1020N/Aimport org.opensolaris.opengrok.analysis.plain.PlainXref;
1020N/Aimport org.opensolaris.opengrok.analysis.plain.XMLXref;
1020N/Aimport org.opensolaris.opengrok.analysis.sh.ShXref;
1020N/Aimport org.opensolaris.opengrok.analysis.sql.SQLXref;
1020N/Aimport org.opensolaris.opengrok.analysis.tcl.TclXref;
1020N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
1020N/Aimport org.opensolaris.opengrok.util.TestRepository;
1020N/A
1020N/Aimport static org.junit.Assert.*;
1020N/A
1020N/A/**
1020N/A * Unit tests for JFlexXref.
1020N/A */
1020N/Apublic class JFlexXrefTest {
1020N/A
1020N/A private static Ctags ctags;
1020N/A private static TestRepository repository;
1020N/A
1083N/A /**
1083N/A * This is what we expect to find at the beginning of the first line
1083N/A * returned by an xref.
1083N/A */
1083N/A private static final String FIRST_LINE_PREAMBLE =
1083N/A "<a class=\"l\" name=\"1\" href=\"#1\">" +
1083N/A "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;</a>";
1083N/A
1020N/A @BeforeClass
1020N/A public static void setUpClass() throws Exception {
1020N/A ctags = new Ctags();
1020N/A ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
1020N/A repository = new TestRepository();
1020N/A repository.create(JFlexXrefTest.class.getResourceAsStream(
1020N/A "/org/opensolaris/opengrok/index/source.zip"));
1020N/A }
1020N/A
1020N/A @AfterClass
1020N/A public static void tearDownClass() throws Exception {
1020N/A ctags.close();
1020N/A ctags = null;
1023N/A repository.destroy();
1020N/A }
1020N/A
1020N/A /**
1020N/A * Regression test case for bug #15890. Check that we get the expected the
1020N/A * expected line count from input with some special characters that used
1020N/A * to cause trouble.
1020N/A */
1020N/A @Test
1020N/A public void testBug15890LineCount() throws Exception {
1020N/A String fileContents =
1020N/A "line 1\n" +
1020N/A "line 2\n" +
1020N/A "line 3\n" +
1020N/A "line 4 with \u000B char\n" +
1020N/A "line 5 with \u000C char\n" +
1020N/A "line 6 with \u0085 char\n" +
1020N/A "line 7 with \u2028 char\n" +
1020N/A "line 8 with \u2029 char\n" +
1020N/A "line 9\n";
1020N/A
1020N/A bug15890LineCount(new CXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new CxxXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new LispXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new JavaXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new FortranXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new XMLXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new ShXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new TclXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new SQLXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new TroffXref(new StringReader(fileContents)));
1020N/A bug15890LineCount(new PlainXref(new StringReader(fileContents)));
1076N/A bug15890LineCount(new PerlXref(new StringReader(fileContents)));
1020N/A }
1020N/A
1020N/A /**
1020N/A * Helper method that checks the line count for
1020N/A * {@link #testBug15890LineCount()}.
1020N/A *
1020N/A * @param xref an instance of the xref class to test
1020N/A */
1020N/A private void bug15890LineCount(JFlexXref xref) throws Exception {
1020N/A xref.write(new StringWriter());
1020N/A assertEquals(10, xref.getLineNumber());
1020N/A }
1020N/A
1020N/A /**
1020N/A * Regression test case for bug #15890. Check that an anchor is correctly
1020N/A * inserted for definitions that appear after some special characters that
1020N/A * used to cause trouble.
1020N/A */
1020N/A @Test
1020N/A public void testBug15890Anchor() throws Exception {
1020N/A bug15890Anchor(CXref.class, "c/bug15890.c");
1020N/A bug15890Anchor(CxxXref.class, "c/bug15890.c");
1020N/A bug15890Anchor(LispXref.class, "lisp/bug15890.lisp");
1020N/A bug15890Anchor(JavaXref.class, "java/bug15890.java");
1020N/A }
1020N/A
1020N/A /**
1020N/A * Helper method for {@link #testBug15890Anchor()}.
1020N/A *
1020N/A * @param klass the Xref sub-class to test
1020N/A * @param path path to input file with a definition
1020N/A */
1020N/A private void bug15890Anchor(Class<? extends JFlexXref> klass, String path)
1020N/A throws Exception {
1023N/A File file = new File(repository.getSourceRoot() + File.separator + path);
1020N/A Definitions defs = ctags.doCtags(file.getAbsolutePath() + "\n");
1020N/A
1020N/A // Input files contain non-ascii characters and are encoded in UTF-8
1020N/A Reader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
1020N/A
1020N/A JFlexXref xref = klass.getConstructor(Reader.class).newInstance(in);
1020N/A xref.setDefs(defs);
1020N/A
1020N/A StringWriter out = new StringWriter();
1020N/A xref.write(out);
1108N/A //TODO improve below to reflect all possible classes of a definition
1020N/A assertTrue(
1020N/A "No anchor found",
1108N/A out.toString().contains("\" name=\"bug15890\"/><a href="));
1020N/A }
1050N/A
1050N/A /**
1050N/A * Regression test case for bug #14663, which used to break syntax
1050N/A * highlighting in ShXref.
1050N/A */
1050N/A @Test
1050N/A public void testBug14663() throws Exception {
1050N/A // \" should not start a new string literal
1050N/A assertXrefLine(ShXref.class, "echo \\\"", "<b>echo</b> \\\"");
1050N/A // \" should not terminate a string literal
1050N/A assertXrefLine(ShXref.class, "echo \"\\\"\"",
1050N/A "<b>echo</b> <span class=\"s\">\"\\\"\"</span>");
1050N/A // \` should not start a command substitution
1050N/A assertXrefLine(ShXref.class, "echo \\`", "<b>echo</b> \\`");
1050N/A // \` should not start command substitution inside a string
1050N/A assertXrefLine(ShXref.class, "echo \"\\`\"",
1050N/A "<b>echo</b> <span class=\"s\">\"\\`\"</span>");
1050N/A // \` should not terminate command substitution
1050N/A assertXrefLine(ShXref.class, "echo `\\``",
1050N/A "<b>echo</b> <span>`\\``</span>");
1050N/A // $# should not start a comment
1050N/A assertXrefLine(ShXref.class, "$#", "$#");
1050N/A }
1050N/A
1050N/A /**
1050N/A * Helper method that checks that the expected output is produced for a
1050N/A * line with the specified xref class. Fails if the output is not as
1050N/A * expected.
1050N/A *
1050N/A * @param xrefClass xref class to test
1050N/A * @param inputLine the source code line to parse
1050N/A * @param expectedOutput the expected output from the xreffer
1050N/A */
1050N/A private void assertXrefLine(Class<? extends JFlexXref> xrefClass,
1050N/A String inputLine, String expectedOutput) throws Exception {
1050N/A JFlexXref xref = xrefClass.getConstructor(Reader.class).newInstance(
1050N/A new StringReader(inputLine));
1050N/A
1050N/A StringWriter output = new StringWriter();
1050N/A xref.write(output);
1050N/A
1083N/A assertEquals(FIRST_LINE_PREAMBLE + expectedOutput, output.toString());
1083N/A }
1050N/A
1083N/A /**
1083N/A * Regression test case for bug #16883. Some of the state used to survive
1083N/A * across invocations in ShXref, so that a syntax error in one file might
1083N/A * cause broken highlighting in subsequent files. Test that the instance
1083N/A * is properly reset now.
1083N/A */
1083N/A @Test
1083N/A public void bug16883() throws Exception {
1083N/A // Analyze a script with broken syntax (unterminated string literal)
1083N/A ShXref xref = new ShXref(new StringReader("echo \"xyz"));
1083N/A StringWriter out = new StringWriter();
1083N/A xref.write(out);
1083N/A assertEquals(
1083N/A FIRST_LINE_PREAMBLE +
1083N/A "<b>echo</b> <span class=\"s\">\"xyz</span>",
1083N/A out.toString());
1083N/A
1083N/A // Reuse the xref and verify that the broken syntax in the previous
1083N/A // file doesn't cause broken highlighting in the next file
1083N/A out = new StringWriter();
1083N/A String contents = "echo \"hello\"";
1083N/A xref.reInit(contents.toCharArray(), contents.length());
1083N/A xref.write(out);
1083N/A assertEquals(
1083N/A FIRST_LINE_PREAMBLE +
1083N/A "<b>echo</b> <span class=\"s\">\"hello\"</span>",
1083N/A out.toString());
1050N/A }
1020N/A}