View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit.html;
21  
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  import java.io.IOException;
26  import java.lang.reflect.Field;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.List;
34  
35  public class HtmlElementsUnitTest {
36  
37    @Test
38    public void testNames() throws IllegalAccessException {
39      for (final Field field : HtmlElements.class.getFields()) {
40  
41        final HtmlElements element = (HtmlElements) field.get(null);
42        final String value = element.getValue();
43        Assertions.assertEquals(
44            value,
45            element.name().toLowerCase().replaceAll("_", "-"),
46            "Check to lower: '" + element + "'");
47        Assertions.assertEquals(
48            value.toUpperCase().replaceAll("-", "_"),
49            element.name(),
50            "Check to upper: '" + element + "'");
51      }
52    }
53  
54    @Test
55    public void testVoid() throws IllegalAccessException {
56  
57      // list from spec.
58      final List<String> voids = Arrays.asList(
59          "area", "base", "br", "col", "command", "embed",
60          "hr", "img", "input", "keygen", "link", "meta",
61          "param", "source", "track", "wbr");
62  
63      for (final Field field : HtmlElements.class.getFields()) {
64        final HtmlElements element = (HtmlElements) field.get(null);
65  
66        Assertions.assertEquals(voids.contains(element.getValue()), element.isVoid(), "Check void: '" + element + "'");
67      }
68  
69    }
70  
71    /**
72     * This test checks whether every tobago custom element {@link HtmlElements} occurs in the _tobago.scss.
73     */
74    @Test
75    public void testCompareTobagoCustomElement() throws IOException, IllegalAccessException {
76  
77      Path scssPath = Paths.get("npm/scss/_tobago.scss");
78      final String fileContent = new String(Files.readAllBytes(scssPath), StandardCharsets.UTF_8);
79  
80      final List<HtmlElements> missing = new ArrayList<>();
81      for (final Field field : HtmlElements.class.getFields()) {
82        final HtmlElements element = (HtmlElements) field.get(null);
83        final String tagName = element.getValue();
84  
85        if (tagName.startsWith("tobago-") && !containsTagName(fileContent, tagName)) {
86          missing.add(element);
87        }
88      }
89  
90      Assertions.assertTrue(missing.isEmpty(), "These custom elements are missing in _tobago.scss: " + missing);
91    }
92  
93    private boolean containsTagName(final String content, final String tagName) {
94      return content.contains(tagName + " ")
95          || content.contains(tagName + "{")
96          || content.contains(tagName + ",")
97          || content.contains(tagName + ":")
98          || content.contains(tagName + ".")
99          || content.contains(tagName + ">");
100   }
101 }