xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/analysis/plain/XMLAnalyzerTest.java (revision 0e4c55544f8ea0a68e8bae37b0e502097e008ec1)
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) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.indexer.analysis.plain;
25 
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.io.StringWriter;
29 
30 import org.junit.jupiter.api.Test;
31 import org.opengrok.indexer.analysis.AbstractAnalyzer;
32 import org.opengrok.indexer.analysis.WriteXrefArgs;
33 
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35 
36 public class XMLAnalyzerTest {
37     @Test
bug2225()38     public void bug2225() throws IOException {
39         String xmlText =
40                 "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n" +
41                 "<foo>\n" +
42                 "  <bar name=\"com.foo.bar.MyClass\"/>\n" +
43                 "  <bar name=\"README.txt\"/>\n" +
44                 "</foo>";
45         StringReader sr = new StringReader(xmlText);
46         StringWriter sw = new StringWriter();
47         XMLAnalyzerFactory fac = new XMLAnalyzerFactory();
48         AbstractAnalyzer analyzer = fac.getAnalyzer();
49         analyzer.writeXref(new WriteXrefArgs(sr, sw));
50         String[] xref = sw.toString().split("\n");
51         // Reference to a Java class should have / instead of . in the path
52         assertTrue(xref[2].contains("path=com/foo/bar/MyClass"));
53         // Ordinary file names should not have .'s replaced
54         assertTrue(xref[3].contains("path=README.txt"));
55     }
56 
57     @Test
bug806()58     public void bug806() throws IOException {
59         String xmlText
60                 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
61                 + "<server>\n"
62                 + "  <mbean code=\"QuartzBean\" \n"
63                 + "      name=\"user:service=QuartzService,name=QuartzService\">\n"
64                 + "    <depends>jboss.jca:service=DataSourceBinding,name=ServerDS</depends>\n"
65                 + "    <attribute name=\"JndiName\">Quartz</attribute>\n"
66                 + "    <attribute name=\"Properties\">\n"
67                 + "      org.quartz.plugin.jobInitializer.fileNames = ../server/default/conf/scheduler/quartz_jobs.xml\n"
68                 + "    </attribute>\n"
69                 + "  </mbean>\n"
70                 + "</server>";
71         StringReader sr = new StringReader(xmlText);
72         StringWriter sw = new StringWriter();
73         XMLAnalyzerFactory fac = new XMLAnalyzerFactory();
74         AbstractAnalyzer analyzer = fac.getAnalyzer();
75         analyzer.writeXref(new WriteXrefArgs(sr, sw));
76         String[] xref = sw.toString().split("\n");
77         // don't remove ../
78         assertTrue(xref[7].contains("org.quartz.plugin.jobInitializer.fileNames</a> = <a href=\"/source/s?path=../\">..</a>/"));
79     }
80 
81     /**
82      * XML special chars inside a string were not escaped if single quotes
83      * were used around the string. Bug #15859.
84      * @throws IOException I/O exception
85      */
86     @Test
xrefWithSpecialCharsInStringLiterals()87     public void xrefWithSpecialCharsInStringLiterals() throws IOException {
88         StringReader input =
89                 new StringReader("<foo xyz='<betweensinglequotes>'> </foo>");
90         StringWriter output = new StringWriter();
91         XMLAnalyzerFactory fac = new XMLAnalyzerFactory();
92         AbstractAnalyzer analyzer = fac.getAnalyzer();
93         analyzer.writeXref(new WriteXrefArgs(input, output));
94         assertTrue(output.toString().contains("&lt;betweensinglequotes&gt;"));
95 
96         input = new StringReader("<foo xyz=\"<betweendoublequotes>\"> </foo>");
97         output = new StringWriter();
98         analyzer.writeXref(new WriteXrefArgs(input, output));
99         assertTrue(output.toString().contains("&lt;betweendoublequotes&gt;"));
100     }
101 }
102