Skip to content

Commit 27d0b26

Browse files
authored
Merge pull request #1935 from idodeclare/feature/json_tests
Feature/json tests
2 parents df247ee + df106d9 commit 27d0b26

File tree

12 files changed

+421
-25
lines changed

12 files changed

+421
-25
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
158158
<exclude>*.java</exclude>
159159
</excludes>
160160
</testResource>
161+
<testResource>
162+
<targetPath>org/opensolaris/opengrok/analysis/json/</targetPath>
163+
<directory>../test/org/opensolaris/opengrok/analysis/json/</directory>
164+
<excludes>
165+
<exclude>*.java</exclude>
166+
</excludes>
167+
</testResource>
161168
<testResource>
162169
<targetPath>org/opensolaris/opengrok/analysis/perl/</targetPath>
163170
<directory>../test/org/opensolaris/opengrok/analysis/perl/</directory>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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) 2017, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
/*
26+
* TODO add unicode support
27+
*/
28+
Identifier = [a-zA-Z_$] [a-zA-Z0-9_$]*
29+
30+
Number = (0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]+|[0-9]+)(([eE][+-]?[0-9]+)?[ufdlUFDL]*)?

src/org/opensolaris/opengrok/analysis/json/JsonSymbolTokenizer.lex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,30 @@ super(in);
4444
%include CommonTokenizer.lexh
4545
%char
4646

47-
/* TODO : add unicode support */
48-
Identifier = [a-zA-Z_$] [a-zA-Z0-9_$]*
49-
5047
%state STRING
5148

49+
%include Json.lexh
5250
%%
5351

5452
//TODO improve per http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
5553

5654
<YYINITIAL> {
57-
{Identifier} {String id = yytext();
55+
{Identifier} {
56+
String id = yytext();
5857
if(!Consts.kwd.contains(id)){
5958
setAttribs(id, yychar, yychar + yylength());
60-
return yystate(); }
61-
}
59+
return yystate();
60+
}
61+
}
62+
63+
{Number} {}
64+
6265
\" { yybegin(STRING); }
6366
}
6467

6568
<STRING> {
69+
\\[\"\\] {}
6670
\" { yybegin(YYINITIAL); }
67-
\\\\ | \\\" {}
6871
}
6972

7073
<YYINITIAL, STRING> {

src/org/opensolaris/opengrok/analysis/json/JsonXref.lex

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
*/
2828

2929
package org.opensolaris.opengrok.analysis.json;
30-
import org.opensolaris.opengrok.analysis.JFlexXref;
30+
import org.opensolaris.opengrok.analysis.JFlexXrefSimple;
3131
import java.io.IOException;
3232
import java.io.Writer;
3333
import java.io.Reader;
34+
import org.opensolaris.opengrok.web.HtmlConsts;
3435
import org.opensolaris.opengrok.web.Util;
3536

3637
%%
3738
%public
3839
%class JsonXref
39-
%extends JFlexXref
40+
%extends JFlexXrefSimple
4041
%unicode
41-
%ignorecase
4242
%int
4343
%include CommonXref.lexh
4444
%{
@@ -49,18 +49,17 @@ import org.opensolaris.opengrok.web.Util;
4949
protected void setLineNumber(int x) { yyline = x; }
5050
%}
5151

52-
//TODO add unicode support
53-
Identifier = [a-zA-Z_$] [a-zA-Z0-9_$]+
54-
55-
File = [a-zA-Z]{FNameChar}* "." ("js"|"properties"|"props"|"xml"|"conf"|"txt"|"htm"|"html"|"ini"|"diff"|"patch")
56-
57-
Number = (0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]+|[0-9]+)(([eE][+-]?[0-9]+)?[ufdlUFDL]*)?
52+
File = [a-zA-Z]{FNameChar}* "." ([Jj][Ss] |
53+
[Pp][Rr][Oo][Pp][Ee][Rr][Tt][Ii][Ee][Ss] | [Pp][Rr][Oo][Pp][Ss] |
54+
[Xx][Mm][Ll] | [Cc][Oo][Nn][Ff] | [Tt][Xx][Tt] | [Hh][Tt][Mm] |
55+
[Hh][Tt][Mm][Ll]? | [Ii][Nn][Ii] | [Dd][Ii][Ff][Ff] | [Pp][Aa][Tt][Cc][Hh])
5856

5957
%state STRING
6058

6159
%include Common.lexh
6260
%include CommonURI.lexh
6361
%include CommonPath.lexh
62+
%include Json.lexh
6463
%%
6564
<YYINITIAL>{
6665

@@ -89,22 +88,30 @@ Number = (0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]+|[0-9]+)(([eE][+-]?[0-9]+)?[ufdlUFDL]*
8988
{ out.write(Util.breadcrumbPath(urlPrefix+"defs=",yytext(),'.'));}
9089
*/
9190

92-
{Number} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
91+
{Number} {
92+
disjointSpan(HtmlConsts.NUMBER_CLASS);
93+
out.write(yytext());
94+
disjointSpan(null);
95+
}
9396

94-
\" { yybegin(STRING);out.write("<span class=\"s\">\"");}
97+
\" {
98+
pushSpan(STRING, HtmlConsts.STRING_CLASS);
99+
out.write(htmlize(yytext()));
100+
}
95101
}
96102

97103
<STRING> {
98-
\" {WhiteSpace} \" { out.write(yytext());}
99-
\" { yybegin(YYINITIAL); out.write("\"</span>"); }
100-
\\\\ { out.write("\\\\"); }
101-
\\\" { out.write("\\\""); }
104+
\\[\"\\] |
105+
\" {WhiteSpace} \" { out.write(htmlize(yytext())); }
106+
\" {
107+
out.write(htmlize(yytext()));
108+
yypop();
109+
}
102110
}
103111

104112
<YYINITIAL, STRING> {
105-
"&" {out.write( "&amp;");}
106-
"<" {out.write( "&lt;");}
107-
">" {out.write( "&gt;");}
113+
[&<>\'\"] { out.write(htmlize(yytext())); }
114+
108115
{WhspChar}*{EOL} { startNewLine(); }
109116
{WhiteSpace} { out.write(yytext()); }
110117
[!-~] { out.write(yycharat(0)); }
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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) 2010, 2015, 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.json;
26+
27+
import java.io.BufferedReader;
28+
import java.io.InputStream;
29+
import java.io.InputStreamReader;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
import static org.junit.Assert.assertNotNull;
33+
import org.junit.Test;
34+
import static org.opensolaris.opengrok.util.CustomAssertions.assertSymbolStream;
35+
36+
/**
37+
* Tests the {@link JsonSymbolTokenizer} class.
38+
*/
39+
public class JsonSymbolTokenizerTest {
40+
41+
/**
42+
* Test sample.json v. samplesymbols.txt
43+
* @throws java.lang.Exception thrown on error
44+
*/
45+
@Test
46+
public void testJsonSymbolStream() throws Exception {
47+
InputStream jres = getClass().getClassLoader().getResourceAsStream(
48+
"org/opensolaris/opengrok/analysis/json/sample.json");
49+
assertNotNull("despite sample.json as resource,", jres);
50+
InputStream symres = getClass().getClassLoader().getResourceAsStream(
51+
"org/opensolaris/opengrok/analysis/json/samplesymbols.txt");
52+
assertNotNull("despite samplesymbols.txt as resource,", symres);
53+
54+
List<String> expectedSymbols = new ArrayList<>();
55+
try (BufferedReader wdsr = new BufferedReader(new InputStreamReader(
56+
symres, "UTF-8"))) {
57+
String line;
58+
while ((line = wdsr.readLine()) != null) {
59+
int hasho = line.indexOf('#');
60+
if (hasho != -1) line = line.substring(0, hasho);
61+
expectedSymbols.add(line.trim());
62+
}
63+
}
64+
65+
assertSymbolStream(JsonSymbolTokenizer.class, jres, expectedSymbols);
66+
}
67+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.json;
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 JsonXref} class.
47+
*/
48+
public class JsonXrefTest {
49+
50+
@Test
51+
public void sampleTest() throws IOException {
52+
writeAndCompare("org/opensolaris/opengrok/analysis/json/sample.json",
53+
"org/opensolaris/opengrok/analysis/json/sample_xref.html",
54+
getTagsDefinitions());
55+
}
56+
57+
@Test
58+
public void shouldCloseTruncatedStringSpan() throws IOException {
59+
writeAndCompare("org/opensolaris/opengrok/analysis/json/truncated.json",
60+
"org/opensolaris/opengrok/analysis/json/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+
writeJsonXref(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("Json xref", expected, gotten);
92+
}
93+
94+
private void writeJsonXref(PrintStream oss, InputStream iss,
95+
Definitions defs)
96+
throws IOException {
97+
98+
oss.print(getHtmlBegin());
99+
100+
Writer sw = new StringWriter();
101+
JsonAnalyzerFactory fac = new JsonAnalyzerFactory();
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/json/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

Comments
 (0)