JFlexXrefTest.java revision 1020
340N/A/*
340N/A * CDDL HEADER START
340N/A *
340N/A * The contents of this file are subject to the terms of the
340N/A * Common Development and Distribution License (the "License").
340N/A * You may not use this file except in compliance with the License.
340N/A *
340N/A * See LICENSE.txt included in this distribution for the specific
340N/A * language governing permissions and limitations under the License.
340N/A *
340N/A * When distributing Covered Code, include this CDDL HEADER in each
340N/A * file and include the License file at LICENSE.txt.
340N/A * If applicable, add the following below this CDDL HEADER, with the
340N/A * fields enclosed by brackets "[]" replaced with your own identifying
340N/A * information: Portions Copyright [yyyy] [name of copyright owner]
340N/A *
340N/A * CDDL HEADER END
340N/A */
340N/A
340N/A/*
340N/A * Copyright 2010 Sun Micosystems. All rights reserved.
340N/A * Use is subject to license terms.
340N/A */
340N/A
340N/Apackage org.opensolaris.opengrok.analysis;
340N/A
464N/Aimport java.io.File;
340N/Aimport java.io.FileInputStream;
340N/Aimport java.io.InputStreamReader;
340N/Aimport java.io.Reader;
340N/Aimport java.io.StringReader;
340N/Aimport java.io.StringWriter;
340N/Aimport org.junit.AfterClass;
340N/Aimport org.junit.BeforeClass;
340N/Aimport org.junit.Test;
340N/Aimport org.opensolaris.opengrok.analysis.c.CXref;
340N/Aimport org.opensolaris.opengrok.analysis.c.CxxXref;
340N/Aimport org.opensolaris.opengrok.analysis.document.TroffXref;
340N/Aimport org.opensolaris.opengrok.analysis.fortran.FortranXref;
340N/Aimport org.opensolaris.opengrok.analysis.java.JavaXref;
340N/Aimport org.opensolaris.opengrok.analysis.lisp.LispXref;
340N/Aimport org.opensolaris.opengrok.analysis.plain.PlainXref;
340N/Aimport org.opensolaris.opengrok.analysis.plain.XMLXref;
340N/Aimport org.opensolaris.opengrok.analysis.sh.ShXref;
340N/Aimport org.opensolaris.opengrok.analysis.sql.SQLXref;
340N/Aimport org.opensolaris.opengrok.analysis.tcl.TclXref;
340N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
340N/Aimport org.opensolaris.opengrok.util.TestRepository;
340N/A
340N/Aimport static org.junit.Assert.*;
340N/A
340N/A/**
340N/A * Unit tests for JFlexXref.
340N/A */
340N/Apublic class JFlexXrefTest {
340N/A
340N/A private static Ctags ctags;
340N/A private static TestRepository repository;
370N/A
340N/A @BeforeClass
340N/A public static void setUpClass() throws Exception {
340N/A ctags = new Ctags();
340N/A ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
340N/A repository = new TestRepository();
340N/A repository.create(JFlexXrefTest.class.getResourceAsStream(
340N/A "/org/opensolaris/opengrok/index/source.zip"));
340N/A }
368N/A
368N/A @AfterClass
340N/A public static void tearDownClass() throws Exception {
456N/A ctags.close();
340N/A ctags = null;
340N/A }
340N/A
340N/A /**
340N/A * Regression test case for bug #15890. Check that we get the expected the
340N/A * expected line count from input with some special characters that used
340N/A * to cause trouble.
340N/A */
340N/A @Test
340N/A public void testBug15890LineCount() throws Exception {
340N/A String fileContents =
340N/A "line 1\n" +
340N/A "line 2\n" +
340N/A "line 3\n" +
340N/A "line 4 with \u000B char\n" +
340N/A "line 5 with \u000C char\n" +
340N/A "line 6 with \u0085 char\n" +
340N/A "line 7 with \u2028 char\n" +
340N/A "line 8 with \u2029 char\n" +
340N/A "line 9\n";
340N/A
340N/A bug15890LineCount(new CXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new CxxXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new LispXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new JavaXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new FortranXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new XMLXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new ShXref(new StringReader(fileContents)));
456N/A bug15890LineCount(new TclXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new SQLXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new TroffXref(new StringReader(fileContents)));
340N/A bug15890LineCount(new PlainXref(new StringReader(fileContents)));
456N/A }
340N/A
340N/A /**
340N/A * Helper method that checks the line count for
456N/A * {@link #testBug15890LineCount()}.
340N/A *
340N/A * @param xref an instance of the xref class to test
340N/A */
340N/A private void bug15890LineCount(JFlexXref xref) throws Exception {
340N/A xref.write(new StringWriter());
340N/A assertEquals(10, xref.getLineNumber());
340N/A }
340N/A
340N/A /**
340N/A * Regression test case for bug #15890. Check that an anchor is correctly
340N/A * inserted for definitions that appear after some special characters that
340N/A * used to cause trouble.
340N/A */
340N/A @Test
340N/A public void testBug15890Anchor() throws Exception {
340N/A bug15890Anchor(CXref.class, "c/bug15890.c");
340N/A bug15890Anchor(CxxXref.class, "c/bug15890.c");
340N/A bug15890Anchor(LispXref.class, "lisp/bug15890.lisp");
340N/A bug15890Anchor(JavaXref.class, "java/bug15890.java");
340N/A }
340N/A
340N/A /**
340N/A * Helper method for {@link #testBug15890Anchor()}.
464N/A *
340N/A * @param klass the Xref sub-class to test
340N/A * @param path path to input file with a definition
340N/A */
340N/A private void bug15890Anchor(Class<? extends JFlexXref> klass, String path)
340N/A throws Exception {
340N/A File file = new File(repository.getSourceRoot() + "/" + path);
340N/A Definitions defs = ctags.doCtags(file.getAbsolutePath() + "\n");
340N/A
460N/A // Input files contain non-ascii characters and are encoded in UTF-8
460N/A Reader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
460N/A
340N/A JFlexXref xref = klass.getConstructor(Reader.class).newInstance(in);
340N/A xref.setDefs(defs);
340N/A
340N/A StringWriter out = new StringWriter();
340N/A xref.write(out);
340N/A assertTrue(
340N/A "No anchor found",
340N/A out.toString().contains("<a class=\"d\" name=\"bug15890\"/>"));
340N/A }
340N/A}
340N/A