1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.site;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.io.File;
25 import java.lang.reflect.Field;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.io.TempDir;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.NodeList;
41
42
43
44
45
46
47
48
49
50
51
52 public class SearchIndexGeneratorTest {
53
54
55
56
57
58 private static final String SITE_XML_PATH = "src/site/site.xml";
59
60
61
62
63
64
65
66 private static final Pattern CHECKS_CATEGORY_HREF =
67 Pattern.compile("^checks/([a-z]+)/index\\.html$");
68
69
70 private static final String CATEGORY_FIELD_NAME = "CHECKS_CATEGORY_DISPLAY_NAMES";
71
72 private static String minimalXdoc(String sectionName) {
73 return "<?xml version=\"1.0\"?>\n"
74 + "<document>\n"
75 + " <body>\n"
76 + " <section name=\"" + sectionName + "\">\n"
77 + " <subsection name=\"Description\">\n"
78 + " <p>Description of " + sectionName + ".</p>\n"
79 + " </subsection>\n"
80 + " </section>\n"
81 + " </body>\n"
82 + "</document>\n";
83 }
84
85 @Test
86 public void checksCategoryDisplayNamesMatchSidebarNavigation() throws Exception {
87 final Map<String, String> navCategoryNames = readNavCategoryNames();
88 final Map<String, String> generatorCategoryNames = readGeneratorCategoryNames();
89
90 assertWithMessage("No 'checks/<category>/index.html' entries found in " + SITE_XML_PATH
91 + " - has the nav structure or href pattern changed?")
92 .that(navCategoryNames.isEmpty())
93 .isFalse();
94
95 assertWithMessage("SearchIndexGenerator." + CATEGORY_FIELD_NAME + " must match the check"
96 + " category labels shown in the sidebar navigation ("
97 + SITE_XML_PATH + "). Update whichever one is out of date.")
98 .that(generatorCategoryNames)
99 .isEqualTo(navCategoryNames);
100 }
101
102
103
104
105
106
107
108
109
110
111 @SuppressWarnings("unchecked")
112 private static Map<String, String> readGeneratorCategoryNames()
113 throws ReflectiveOperationException {
114 final Field field = SearchIndexGenerator.class.getDeclaredField(CATEGORY_FIELD_NAME);
115 field.setAccessible(true);
116 return (Map<String, String>) field.get(null);
117 }
118
119
120
121
122
123
124
125
126
127 private static Map<String, String> readNavCategoryNames() throws Exception {
128 final Map<String, String> result = new LinkedHashMap<>();
129 final File siteXml = new File(SITE_XML_PATH);
130
131 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
132 final DocumentBuilder builder = factory.newDocumentBuilder();
133 final Document doc = builder.parse(siteXml);
134
135 final NodeList items = doc.getElementsByTagName("item");
136 for (int index = 0; index < items.getLength(); index++) {
137 final Element item = (Element) items.item(index);
138 final String href = item.getAttribute("href");
139 final Matcher matcher = CHECKS_CATEGORY_HREF.matcher(href);
140 if (matcher.matches()) {
141 final String dirName = matcher.group(1);
142 final String displayName = item.getAttribute("name");
143 result.put(dirName, displayName);
144 }
145 }
146 return result;
147 }
148
149
150
151
152
153
154
155
156
157
158 @Test
159 public void testReleaseNotesAreExcludedFromSearchIndex(@TempDir Path tempDir) throws Exception {
160 final Path xdocsDir = tempDir.resolve("xdocs");
161 Files.createDirectories(xdocsDir);
162
163 Files.writeString(xdocsDir.resolve("release-notes.xml"),
164 minimalXdoc("Release 10.21.0"));
165 Files.writeString(xdocsDir.resolve("release-notes-old-8-0-8-34.xml"),
166 minimalXdoc("Release 8.0 to 8.34"));
167
168 final Path outputFile = tempDir.resolve("search-index.json");
169
170 SearchIndexGenerator.main(
171 xdocsDir.toString(),
172 outputFile.toString()
173 );
174
175 final String output = Files.readString(outputFile);
176
177 assertWithMessage(
178 "Release note pages must not produce entries in the search index")
179 .that(output)
180 .doesNotContain("release-notes");
181 }
182
183 }