Skip to content

Commit 802b34a

Browse files
authored
Merge pull request #2674 from idodeclare/feature/some_tests_sharing
Extract xref test code--customizable for odd duck, Troff
2 parents 36cb0d3 + a8758e2 commit 802b34a

35 files changed

+534
-3027
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, 2019, Chris Fraire <cfraire@me.com>.
23+
*/
24+
25+
package org.opengrok.indexer.analysis;
26+
27+
import org.opengrok.indexer.util.StreamUtils;
28+
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.IOException;
31+
import java.io.PrintStream;
32+
import java.io.Reader;
33+
import java.io.StringWriter;
34+
import java.io.Writer;
35+
import java.nio.charset.StandardCharsets;
36+
37+
import static org.junit.Assert.assertEquals;
38+
import static org.opengrok.indexer.util.CustomAssertions.assertLinesEqual;
39+
import static org.opengrok.indexer.util.StreamUtils.readerFromResource;
40+
41+
/**
42+
* Represents an abstract base class for language-specific xref test classes.
43+
*/
44+
public abstract class XrefTestBase {
45+
46+
/**
47+
* Tests the XREF result of a specified factory and arguments.
48+
* @param factory a defined instance
49+
* @param sourceResource a defined resource name for source code
50+
* @param resultResource a defined resource name for expected XREF result
51+
* @param defs an optional instance
52+
* @param expectedLOC the number of expected LOC
53+
* @throws IOException thrown if an I/O error occurs
54+
*/
55+
protected void writeAndCompare(
56+
AnalyzerFactory factory, String sourceResource,
57+
String resultResource, Definitions defs, int expectedLOC)
58+
throws IOException {
59+
60+
try (Reader sourceRes = readerFromResource(sourceResource);
61+
Reader resultRes = readerFromResource(resultResource)) {
62+
writeAndCompare(factory, sourceRes, resultRes, defs, expectedLOC);
63+
}
64+
}
65+
66+
/**
67+
* Tests the XREF result of a specified factory and arguments.
68+
* @param factory a defined instance
69+
* @param source a defined instance for source code
70+
* @param result a defined instance for expected XREF result
71+
* @param defs an optional instance
72+
* @param expectedLOC the number of expected LOC
73+
* @throws IOException thrown if an I/O error occurs
74+
*/
75+
protected void writeAndCompare(
76+
AnalyzerFactory factory, Reader source, Reader result,
77+
Definitions defs, int expectedLOC) throws IOException {
78+
79+
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
80+
81+
int actLOC = writeXref(new PrintStream(outBytes), factory, source, defs);
82+
outBytes.close();
83+
String outStr = new String(outBytes.toByteArray(), StandardCharsets.UTF_8);
84+
String[] gotten = outStr.split("\n");
85+
86+
String expStr = StreamUtils.readToEnd(result);
87+
String[] expected = expStr.split("\n");
88+
89+
String messagePrefix = factory.getClass().getName();
90+
assertLinesEqual(messagePrefix + " xref", expected, gotten);
91+
assertEquals(messagePrefix + " LOC", expectedLOC, actLOC);
92+
}
93+
94+
private int writeXref(
95+
PrintStream oss, AnalyzerFactory factory, Reader in,
96+
Definitions defs) throws IOException {
97+
98+
oss.print(getHtmlBegin());
99+
100+
Writer out = new StringWriter();
101+
AbstractAnalyzer analyzer = factory.getAnalyzer();
102+
analyzer.setScopesEnabled(true);
103+
analyzer.setFoldingEnabled(true);
104+
105+
WriteXrefArgs writeArgs = new WriteXrefArgs(in, out);
106+
writeArgs.setDefs(defs);
107+
108+
Xrefer xref = analyzer.writeXref(writeArgs);
109+
oss.print(out.toString());
110+
111+
oss.print(getHtmlEnd());
112+
return xref.getLOC();
113+
}
114+
115+
/**
116+
* Subclasses can override if the XREF is non-standard.
117+
* @return default HTML document header
118+
*/
119+
protected String getHtmlBegin() {
120+
return "<!DOCTYPE html>\n" +
121+
"<html lang=\"en\">\n" +
122+
"<head>\n" +
123+
"<meta charset=\"UTF-8\">\n" +
124+
"<title>sampleFile - OpenGrok cross reference" +
125+
" for /sampleFile</title></head><body>\n";
126+
}
127+
128+
/**
129+
* Subclasses can override if the XREF is non-standard.
130+
* @return default HTML document footer
131+
*/
132+
protected String getHtmlEnd() {
133+
return "</body>\n" +
134+
"</html>\n";
135+
}
136+
}

opengrok-indexer/src/test/java/org/opengrok/indexer/analysis/ada/AdaXrefTest.java

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,84 +19,24 @@
1919

2020
/*
2121
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
22+
* Portions Copyright (c) 2017, 2019, Chris Fraire <cfraire@me.com>.
2323
*/
2424

2525
package org.opengrok.indexer.analysis.ada;
2626

27-
import java.io.ByteArrayOutputStream;
28-
import java.io.IOException;
29-
import java.io.InputStream;
30-
import java.io.InputStreamReader;
31-
import java.io.PrintStream;
32-
import java.io.StringWriter;
33-
import java.io.Writer;
34-
3527
import org.junit.Test;
36-
import static org.junit.Assert.assertEquals;
37-
import static org.junit.Assert.assertNotNull;
38-
39-
import org.opengrok.indexer.analysis.AbstractAnalyzer;
40-
import org.opengrok.indexer.analysis.WriteXrefArgs;
41-
import org.opengrok.indexer.analysis.Xrefer;
42-
import static org.opengrok.indexer.util.CustomAssertions.assertLinesEqual;
43-
import static org.opengrok.indexer.util.StreamUtils.copyStream;
28+
import org.opengrok.indexer.analysis.XrefTestBase;
29+
import java.io.IOException;
4430

4531
/**
4632
* Tests the {@link AdaXref} class.
4733
*/
48-
public class AdaXrefTest {
34+
public class AdaXrefTest extends XrefTestBase {
4935

5036
@Test
5137
public void sampleTest() throws IOException {
52-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
53-
54-
InputStream res = getClass().getClassLoader().getResourceAsStream(
55-
"analysis/ada/sample.adb");
56-
assertNotNull("though sample.adb should stream,", res);
57-
int actLOC = writeAdaXref(res, new PrintStream(baos));
58-
res.close();
59-
60-
InputStream exp = getClass().getClassLoader().getResourceAsStream(
61-
"analysis/ada/ada_xrefres.html");
62-
assertNotNull("ada_xrefres.html should stream,", exp);
63-
byte[] expbytes = copyStream(exp);
64-
exp.close();
65-
baos.close();
66-
67-
String ostr = new String(baos.toByteArray(), "UTF-8");
68-
String estr = new String(expbytes, "UTF-8");
69-
assertLinesEqual("Ada xref", estr, ostr);
70-
assertEquals("Ada LOC", 19, actLOC);
71-
}
72-
73-
private int writeAdaXref(InputStream iss, PrintStream oss)
74-
throws IOException {
75-
oss.print(getHtmlBegin());
76-
77-
Writer sw = new StringWriter();
78-
AdaAnalyzerFactory fac = new AdaAnalyzerFactory();
79-
AbstractAnalyzer analyzer = fac.getAnalyzer();
80-
WriteXrefArgs wargs = new WriteXrefArgs(
81-
new InputStreamReader(iss, "UTF-8"), sw);
82-
Xrefer xref = analyzer.writeXref(wargs);
83-
84-
oss.print(sw.toString());
85-
oss.print(getHtmlEnd());
86-
return xref.getLOC();
87-
}
88-
89-
private String getHtmlBegin() {
90-
return "<!DOCTYPE html>\n" +
91-
"<html lang=\"en\">\n" +
92-
"<head>\n" +
93-
"<meta charset=\"UTF-8\">\n" +
94-
"<title>sampleFile - OpenGrok cross reference" +
95-
" for /sampleFile</title></head><body>\n";
96-
}
97-
98-
private String getHtmlEnd() {
99-
return "</body>\n" +
100-
"</html>\n";
38+
writeAndCompare(new AdaAnalyzerFactory(),
39+
"analysis/ada/sample.adb",
40+
"analysis/ada/ada_xrefres.html", null, 19);
10141
}
10242
}

opengrok-indexer/src/test/java/org/opengrok/indexer/analysis/c/CXrefTest.java

Lines changed: 9 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -19,107 +19,27 @@
1919

2020
/*
2121
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017-2018, Chris Fraire <cfraire@me.com>.
22+
* Portions Copyright (c) 2017-2019, Chris Fraire <cfraire@me.com>.
2323
*/
2424

2525
package org.opengrok.indexer.analysis.c;
2626

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.PrintStream;
33-
import java.io.StringWriter;
34-
import java.io.Writer;
35-
3627
import org.junit.Test;
37-
import static org.junit.Assert.assertEquals;
38-
import static org.junit.Assert.assertNotNull;
28+
import org.opengrok.indexer.analysis.XrefTestBase;
29+
import java.io.IOException;
3930

40-
import org.opengrok.indexer.analysis.AbstractAnalyzer;
41-
import org.opengrok.indexer.analysis.CtagsReader;
42-
import org.opengrok.indexer.analysis.Definitions;
43-
import org.opengrok.indexer.analysis.WriteXrefArgs;
44-
import org.opengrok.indexer.analysis.Xrefer;
45-
import static org.opengrok.indexer.util.CustomAssertions.assertLinesEqual;
46-
import static org.opengrok.indexer.util.StreamUtils.copyStream;
31+
import static org.opengrok.indexer.util.StreamUtils.readTagsFromResource;
4732

4833
/**
4934
* Tests the {@link CXref} class.
5035
*/
51-
public class CXrefTest {
36+
public class CXrefTest extends XrefTestBase {
5237

5338
@Test
5439
public void sampleTest() throws IOException {
55-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
56-
57-
InputStream res = getClass().getClassLoader().getResourceAsStream(
58-
"analysis/c/sample.c");
59-
assertNotNull("though sample.c should stream,", res);
60-
int actLOC = writeCXref(res, new PrintStream(baos));
61-
res.close();
62-
63-
InputStream exp = getClass().getClassLoader().getResourceAsStream(
64-
"analysis/c/c_xrefres.html");
65-
assertNotNull("c_xrefres.html should stream,", exp);
66-
byte[] expbytes = copyStream(exp);
67-
exp.close();
68-
baos.close();
69-
70-
String ostr = new String(baos.toByteArray(), "UTF-8");
71-
String estr = new String(expbytes, "UTF-8");
72-
assertLinesEqual("C xref", estr, ostr);
73-
assertEquals("C LOC", 69, actLOC);
74-
}
75-
76-
private int writeCXref(InputStream iss, PrintStream oss)
77-
throws IOException {
78-
79-
oss.print(getHtmlBegin());
80-
81-
Writer sw = new StringWriter();
82-
CAnalyzerFactory fac = new CAnalyzerFactory();
83-
AbstractAnalyzer analyzer = fac.getAnalyzer();
84-
WriteXrefArgs wargs = new WriteXrefArgs(
85-
new InputStreamReader(iss, "UTF-8"), sw);
86-
wargs.setDefs(getTagsDefinitions());
87-
analyzer.setScopesEnabled(true);
88-
analyzer.setFoldingEnabled(true);
89-
Xrefer xref = analyzer.writeXref(wargs);
90-
91-
oss.print(sw.toString());
92-
oss.print(getHtmlEnd());
93-
return xref.getLOC();
94-
}
95-
96-
private Definitions getTagsDefinitions() throws IOException {
97-
InputStream res = getClass().getClassLoader().getResourceAsStream(
98-
"analysis/c/sampletags_c");
99-
assertNotNull("though sampletags_c should stream,", res);
100-
101-
BufferedReader in = new BufferedReader(new InputStreamReader(
102-
res, "UTF-8"));
103-
104-
CtagsReader rdr = new CtagsReader();
105-
String line;
106-
while ((line = in.readLine()) != null) {
107-
rdr.readLine(line);
108-
}
109-
return rdr.getDefinitions();
110-
}
111-
112-
private static String getHtmlBegin() {
113-
return "<!DOCTYPE html>\n" +
114-
"<html lang=\"en\">\n" +
115-
"<head>\n" +
116-
"<meta charset=\"UTF-8\">\n" +
117-
"<title>sampleFile - OpenGrok cross reference" +
118-
" for /sampleFile</title></head><body>\n";
119-
}
120-
121-
private static String getHtmlEnd() {
122-
return "</body>\n" +
123-
"</html>\n";
40+
writeAndCompare(new CAnalyzerFactory(),
41+
"analysis/c/sample.c",
42+
"analysis/c/c_xrefres.html",
43+
readTagsFromResource("analysis/c/sampletags_c"), 69);
12444
}
12545
}

0 commit comments

Comments
 (0)