|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. |
| 22 | + * Portions Copyright (c) 2017, Chris Fraire <[email protected]>. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.opensolaris.opengrok.analysis.tcl; |
| 26 | + |
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.InputStream; |
| 31 | +import java.io.InputStreamReader; |
| 32 | +import java.io.OutputStream; |
| 33 | +import java.io.PrintStream; |
| 34 | +import java.io.StringWriter; |
| 35 | +import java.io.Writer; |
| 36 | + |
| 37 | +import org.opensolaris.opengrok.analysis.CtagsReader; |
| 38 | +import org.opensolaris.opengrok.analysis.Definitions; |
| 39 | +import org.opensolaris.opengrok.analysis.FileAnalyzer; |
| 40 | +import org.opensolaris.opengrok.analysis.WriteXrefArgs; |
| 41 | +import org.junit.Test; |
| 42 | +import static org.junit.Assert.assertNotNull; |
| 43 | +import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual; |
| 44 | + |
| 45 | +/** |
| 46 | + * Tests the {@link TclXref} class. |
| 47 | + */ |
| 48 | +public class TclXrefTest { |
| 49 | + |
| 50 | + @Test |
| 51 | + public void sampleTest() throws IOException { |
| 52 | + writeAndCompare("org/opensolaris/opengrok/analysis/tcl/sample.tcl", |
| 53 | + "org/opensolaris/opengrok/analysis/tcl/sample_xref.html", |
| 54 | + getTagsDefinitions()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void shouldCloseTruncatedStringSpan() throws IOException { |
| 59 | + writeAndCompare("org/opensolaris/opengrok/analysis/tcl/truncated.tcl", |
| 60 | + "org/opensolaris/opengrok/analysis/tcl/truncated_xref.html", |
| 61 | + null); |
| 62 | + } |
| 63 | + |
| 64 | + private void writeAndCompare(String sourceResource, String resultResource, |
| 65 | + Definitions defs) |
| 66 | + throws IOException { |
| 67 | + |
| 68 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 69 | + ByteArrayOutputStream baosExp = new ByteArrayOutputStream(); |
| 70 | + |
| 71 | + InputStream res = getClass().getClassLoader().getResourceAsStream( |
| 72 | + sourceResource); |
| 73 | + assertNotNull(sourceResource + " should get-as-stream", res); |
| 74 | + writeTclXref(new PrintStream(baos), res, defs); |
| 75 | + res.close(); |
| 76 | + |
| 77 | + InputStream exp = getClass().getClassLoader().getResourceAsStream( |
| 78 | + resultResource); |
| 79 | + assertNotNull(resultResource + " should get-as-stream", exp); |
| 80 | + copyStream(exp, baosExp); |
| 81 | + exp.close(); |
| 82 | + baosExp.close(); |
| 83 | + baos.close(); |
| 84 | + |
| 85 | + String ostr = new String(baos.toByteArray(), "UTF-8"); |
| 86 | + String gotten[] = ostr.split("\n"); |
| 87 | + |
| 88 | + String estr = new String(baosExp.toByteArray(), "UTF-8"); |
| 89 | + String expected[] = estr.split("\n"); |
| 90 | + |
| 91 | + assertLinesEqual("Tcl xref", expected, gotten); |
| 92 | + } |
| 93 | + |
| 94 | + private void writeTclXref(PrintStream oss, InputStream iss, |
| 95 | + Definitions defs) |
| 96 | + throws IOException { |
| 97 | + |
| 98 | + oss.print(getHtmlBegin()); |
| 99 | + |
| 100 | + Writer sw = new StringWriter(); |
| 101 | + TclAnalyzerFactory fac = new TclAnalyzerFactory(); |
| 102 | + FileAnalyzer analyzer = fac.getAnalyzer(); |
| 103 | + analyzer.setScopesEnabled(true); |
| 104 | + analyzer.setFoldingEnabled(true); |
| 105 | + WriteXrefArgs wargs = new WriteXrefArgs( |
| 106 | + new InputStreamReader(iss, "UTF-8"), sw); |
| 107 | + wargs.setDefs(defs); |
| 108 | + analyzer.writeXref(wargs); |
| 109 | + oss.print(sw.toString()); |
| 110 | + |
| 111 | + oss.print(getHtmlEnd()); |
| 112 | + } |
| 113 | + |
| 114 | + private void copyStream(InputStream iss, OutputStream oss) |
| 115 | + throws IOException { |
| 116 | + |
| 117 | + byte buffer[] = new byte[8192]; |
| 118 | + int read; |
| 119 | + do { |
| 120 | + read = iss.read(buffer, 0, buffer.length); |
| 121 | + if (read > 0) { |
| 122 | + oss.write(buffer, 0, read); |
| 123 | + } |
| 124 | + } while (read >= 0); |
| 125 | + } |
| 126 | + |
| 127 | + private Definitions getTagsDefinitions() throws IOException { |
| 128 | + InputStream res = getClass().getClassLoader().getResourceAsStream( |
| 129 | + "org/opensolaris/opengrok/analysis/tcl/sampletags"); |
| 130 | + assertNotNull("though sampletags should stream,", res); |
| 131 | + |
| 132 | + BufferedReader in = new BufferedReader(new InputStreamReader( |
| 133 | + res, "UTF-8")); |
| 134 | + |
| 135 | + CtagsReader rdr = new CtagsReader(); |
| 136 | + String line; |
| 137 | + while ((line = in.readLine()) != null) { |
| 138 | + rdr.readLine(line); |
| 139 | + } |
| 140 | + return rdr.getDefinitions(); |
| 141 | + } |
| 142 | + |
| 143 | + private static String getHtmlBegin() { |
| 144 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 145 | + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" + |
| 146 | + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" + |
| 147 | + "<html xmlns=\"http://www.w3.org/1999/xhtml\"" + |
| 148 | + " xml:lang=\"en\" lang=\"en\"\n" + |
| 149 | + " class=\"xref\">\n" + |
| 150 | + "<head>\n" + |
| 151 | + "<title>sampleFile - OpenGrok cross reference" + |
| 152 | + " for /sampleFile</title></head><body>\n"; |
| 153 | + } |
| 154 | + |
| 155 | + private static String getHtmlEnd() { |
| 156 | + return "</body>\n" + |
| 157 | + "</html>\n"; |
| 158 | + } |
| 159 | +} |
0 commit comments