Skip to content

Commit 6a64198

Browse files
committed
Add ErlangXrefTest with one non-passing for dangling span
1 parent 91d0e02 commit 6a64198

File tree

7 files changed

+313
-0
lines changed

7 files changed

+313
-0
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
102102
<exclude>*.java</exclude>
103103
</excludes>
104104
</testResource>
105+
<testResource>
106+
<targetPath>org/opensolaris/opengrok/analysis/erlang/</targetPath>
107+
<directory>../test/org/opensolaris/opengrok/analysis/erlang/</directory>
108+
<excludes>
109+
<exclude>*.java</exclude>
110+
</excludes>
111+
</testResource>
105112
<testResource>
106113
<targetPath>org/opensolaris/opengrok/analysis/fortran/</targetPath>
107114
<directory>../test/org/opensolaris/opengrok/analysis/fortran/</directory>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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.erlang;
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 ErlangXref} class.
47+
*/
48+
public class ErlangXrefTest {
49+
50+
@Test
51+
public void sampleTest() throws IOException {
52+
writeAndCompare("org/opensolaris/opengrok/analysis/erlang/sample.erl",
53+
"org/opensolaris/opengrok/analysis/erlang/sample_xref.html",
54+
getTagsDefinitions());
55+
}
56+
57+
@Test
58+
public void shouldCloseTruncatedStringSpan() throws IOException {
59+
writeAndCompare("org/opensolaris/opengrok/analysis/erlang/truncated.erl",
60+
"org/opensolaris/opengrok/analysis/erlang/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+
writeErlangXref(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("Erlang xref", expected, gotten);
92+
}
93+
94+
private void writeErlangXref(PrintStream oss, InputStream iss,
95+
Definitions defs)
96+
throws IOException {
97+
98+
oss.print(getHtmlBegin());
99+
100+
Writer sw = new StringWriter();
101+
ErlangAnalyzerFactory fac = new ErlangAnalyzerFactory();
102+
FileAnalyzer analyzer = fac.getAnalyzer();
103+
WriteXrefArgs wargs = new WriteXrefArgs(
104+
new InputStreamReader(iss, "UTF-8"), sw);
105+
wargs.setDefs(defs);
106+
analyzer.writeXref(wargs);
107+
oss.print(sw.toString());
108+
109+
oss.print(getHtmlEnd());
110+
}
111+
112+
private void copyStream(InputStream iss, OutputStream oss)
113+
throws IOException {
114+
115+
byte buffer[] = new byte[8192];
116+
int read;
117+
do {
118+
read = iss.read(buffer, 0, buffer.length);
119+
if (read > 0) {
120+
oss.write(buffer, 0, read);
121+
}
122+
} while (read >= 0);
123+
}
124+
125+
private Definitions getTagsDefinitions() throws IOException {
126+
InputStream res = getClass().getClassLoader().getResourceAsStream(
127+
"org/opensolaris/opengrok/analysis/erlang/sampletags");
128+
assertNotNull("though sampletags should stream,", res);
129+
130+
BufferedReader in = new BufferedReader(new InputStreamReader(
131+
res, "UTF-8"));
132+
133+
CtagsReader rdr = new CtagsReader();
134+
String line;
135+
while ((line = in.readLine()) != null) {
136+
rdr.readLine(line);
137+
}
138+
return rdr.getDefinitions();
139+
}
140+
141+
private static String getHtmlBegin() {
142+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
143+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" +
144+
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
145+
"<html xmlns=\"http://www.w3.org/1999/xhtml\"" +
146+
" xml:lang=\"en\" lang=\"en\"\n" +
147+
" class=\"xref\">\n" +
148+
"<head>\n" +
149+
"<title>sampleFile - OpenGrok cross reference" +
150+
" for /sampleFile</title></head><body>\n";
151+
}
152+
153+
private static String getHtmlEnd() {
154+
return "</body>\n" +
155+
"</html>\n";
156+
}
157+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
%% Copyright (c) 2011-2017, Loïc Hoguin <[email protected]>
2+
%%
3+
%% Permission to use, copy, modify, and/or distribute this software for any
4+
%% purpose with or without fee is hereby granted, provided that the above
5+
%% copyright notice and this permission notice appear in all copies.
6+
%%
7+
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
-module(ranch_acceptor).
16+
17+
-export([start_link/3]).
18+
-export([loop/3]).
19+
20+
-spec start_link(inet:socket(), module(), pid())
21+
-> {ok, pid()}.
22+
start_link(LSocket, Transport, ConnsSup) ->
23+
Pid = spawn_link(?MODULE, loop, [LSocket, Transport, ConnsSup]),
24+
{ok, Pid}.
25+
26+
-spec loop(inet:socket(), module(), pid()) -> no_return().
27+
loop(LSocket, Transport, ConnsSup) ->
28+
_ = case Transport:accept(LSocket, infinity) of
29+
{ok, CSocket} ->
30+
case Transport:controlling_process(CSocket, ConnsSup) of
31+
ok ->
32+
%% This call will not return until process has been started
33+
%% AND we are below the maximum number of connections.
34+
ranch_conns_sup:start_protocol(ConnsSup, CSocket);
35+
{error, _} ->
36+
Transport:close(CSocket)
37+
end;
38+
%% Reduce the accept rate if we run out of file descriptors.
39+
%% We can't accept anymore anyway, so we might as well wait
40+
%% a little for the situation to resolve itself.
41+
{error, emfile} ->
42+
error_logger:warning_msg("Ranch acceptor reducing accept rate: out of file descriptors~n"),
43+
receive after 100 -> ok end;
44+
%% We want to crash if the listening socket got closed.
45+
{error, Reason} when Reason =/= closed ->
46+
ok
47+
end,
48+
flush(),
49+
?MODULE:loop(LSocket, Transport, ConnsSup).
50+
51+
flush() ->
52+
receive Msg ->
53+
error_logger:error_msg(
54+
"Ranch acceptor received unexpected message: ~p~n",
55+
[Msg]),
56+
flush()
57+
after 0 ->
58+
ok
59+
end.

0 commit comments

Comments
 (0)