View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.internal;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.io.File;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.util.Set;
28  
29  import org.junit.jupiter.api.Test;
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Node;
32  import org.w3c.dom.NodeList;
33  
34  import com.puppycrawl.tools.checkstyle.internal.utils.XdocUtil;
35  import com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil;
36  
37  public class XdocsMobileWrapperTest {
38  
39      private static final Set<String> NODES_TO_WRAP = Set.of(
40          "pre",
41          "table",
42          "svg",
43          "img"
44      );
45  
46      @Test
47      public void testAllCheckSectionMobileWrapper() throws Exception {
48          for (Path path : XdocUtil.getXdocsFilePaths()) {
49              final File file = path.toFile();
50              final String fileName = file.getName();
51  
52              final String input = Files.readString(path);
53              assertWithMessage(fileName + ": input file cannot be empty")
54                      .that(input)
55                      .isNotEmpty();
56              final Document document = XmlUtil.getRawXml(fileName, input, input);
57              final NodeList sources = document.getElementsByTagName("section");
58  
59              for (int position = 0; position < sources.getLength(); position++) {
60                  final Node section = sources.item(position);
61                  final String sectionName = XmlUtil.getNameAttributeOfNode(section);
62  
63                  if ("Content".equals(sectionName) || "Overview".equals(sectionName)) {
64                      continue;
65                  }
66  
67                  iterateNode(section, fileName, sectionName);
68              }
69          }
70      }
71  
72      private static void iterateNode(Node node, String fileName, String sectionName) {
73          for (Node child : XmlUtil.getChildrenElements(node)) {
74              if (NODES_TO_WRAP.contains(child.getNodeName())) {
75                  final String wrapperMessage = fileName + "/" + sectionName + ": Tag '"
76                          + child.getNodeName() + "' in '" + node.getNodeName()
77                          + "' needs a wrapping <span> or <div> with the class 'wrapper'.";
78                  assertWithMessage(wrapperMessage)
79                          .that(node.getNodeName())
80                          .isAnyOf("div", "span");
81                  assertWithMessage(wrapperMessage)
82                          .that(node.hasAttributes())
83                          .isTrue();
84                  assertWithMessage(wrapperMessage)
85                          .that(node.getAttributes().getNamedItem("class"))
86                          .isNotNull();
87                  assertWithMessage(wrapperMessage)
88                          .that(node.getAttributes().getNamedItem("class")
89                                  .getNodeValue())
90                          .contains("wrapper");
91  
92                  if ("table".equals(child.getNodeName())) {
93                      iterateNode(child, fileName, sectionName);
94                  }
95                  if ("img".equals(child.getNodeName())) {
96                      final String dataImageInlineMessage = fileName + "/" + sectionName + ": img "
97                              + "needs the additional class inline if it should be displayed inline "
98                              + "or block if scrolling in mobile view should be enabled.";
99                      assertWithMessage(dataImageInlineMessage)
100                             .that(node.getAttributes().getNamedItem("class")
101                                     .getNodeValue().matches(".*(block|inline).*"))
102                             .isTrue();
103                 }
104             }
105             else {
106                 iterateNode(child, fileName, sectionName);
107             }
108         }
109     }
110 }