xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/web/XrefSourceTransformerTest.java (revision 52d10766ed1db3b0fd2c59a0da7292a32f244b50)
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) 2018, Chris Fraire <cfraire@me.com>.
22  */
23 package org.opengrok.indexer.web;
24 
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.io.StringWriter;
28 
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.opengrok.indexer.analysis.AbstractAnalyzer;
32 
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34 
35 /**
36  * Represents a container for tests of {@link XrefSourceTransformer}.
37  */
38 public class XrefSourceTransformerTest {
39 
40     private static final String XREF_FRAG_DFLT = "<a class=\"hl\" name=\"1\" " +
41             "href=\"#1\">1</a><span class=\"c\"># See " +
42             "<a href=\"/source/s?path=LICENSE.txt\">LICENSE.txt</a> included " +
43             "in this distribution for the specific</span>\n";
44 
45     private static final String XREF_FRAG_SVC = "<a class=\"hl\" name=\"1\" " +
46             "href=\"#1\">1</a><span class=\"c\"># See " +
47             "<a href=\"/svc/s?path=LICENSE.txt\">LICENSE.txt</a> included " +
48             "in this distribution for the specific</span>\n";
49 
50     private static final String XREF_FRAG_ROOT = "<a class=\"hl\" name=\"1\" " +
51             "href=\"#1\">1</a><span class=\"c\"># See " +
52             "<a href=\"/s?path=LICENSE.txt\">LICENSE.txt</a> included " +
53             "in this distribution for the specific</span>\n";
54 
55     XrefSourceTransformer xform;
56     StringWriter out;
57 
58     @BeforeEach
setUp()59     public void setUp() {
60         StringReader rdr = new StringReader(XREF_FRAG_DFLT);
61         // Test the normal path of dummy-first then actual data.
62         xform = new XrefSourceTransformer(AbstractAnalyzer.DUMMY_READER);
63         xform.yyreset(rdr);
64 
65         out = new StringWriter();
66         xform.setWriter(out);
67     }
68 
69     @Test
testDefaultContext1()70     public void testDefaultContext1() throws IOException {
71         xform.setContextPath(null);
72         while (xform.yylex()) {
73         }
74         String res = out.toString();
75         assertEquals(XREF_FRAG_DFLT, res, "context=null");
76     }
77 
78     @Test
testDefaultContext2()79     public void testDefaultContext2() throws IOException {
80         xform.setContextPath("source");
81         while (xform.yylex()) {
82         }
83         String res = out.toString();
84         assertEquals(XREF_FRAG_DFLT, res, "context=source");
85     }
86 
87     @Test
testDefaultContext3()88     public void testDefaultContext3() throws IOException {
89         xform.setContextPath("/source");
90         while (xform.yylex()) {
91         }
92         String res = out.toString();
93         assertEquals(XREF_FRAG_DFLT, res, "context=/source");
94     }
95 
96     @Test
testDefaultContext4()97     public void testDefaultContext4() throws IOException {
98         xform.setContextPath("/source/");
99         while (xform.yylex()) {
100         }
101         String res = out.toString();
102         assertEquals(XREF_FRAG_DFLT, res, "context=/source/");
103     }
104 
105     @Test
testSvcContext()106     public void testSvcContext() throws IOException {
107         xform.setContextPath("svc");
108         while (xform.yylex()) {
109         }
110         String res = out.toString();
111         assertEquals(XREF_FRAG_SVC, res, "context=svc");
112     }
113 
114     @Test
testRootContext()115     public void testRootContext() throws IOException {
116         xform.setContextPath("/");
117         while (xform.yylex()) {
118         }
119         String res = out.toString();
120         assertEquals(XREF_FRAG_ROOT, res, "context=/");
121     }
122 }
123