View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.css;
16  
17  import static org.junit.jupiter.api.Assertions.fail;
18  
19  import java.io.IOException;
20  import java.nio.file.Files;
21  import java.nio.file.Paths;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  import java.util.stream.Collectors;
27  import java.util.stream.Stream;
28  
29  import org.htmlunit.css.StyleAttributes.Definition;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests for {@link StyleAttributes}.
34   *
35   * @author Ahmed Ashour
36   * @author Ronald Brill
37   */
38  public class StyleAttributesTest {
39  
40      /**
41       * Test of alphabetical order.
42       */
43      @Test
44      public void lexicographicOrder() {
45          String lastName = null;
46          for (final Definition definition : StyleAttributes.Definition.values()) {
47              final String name = definition.name();
48              if (lastName != null && name.compareToIgnoreCase(lastName) < 1) {
49                  fail("StyleAttributes.Definition: '"
50                      + name + "' should be before '" + lastName + "'");
51              }
52              lastName = name;
53          }
54      }
55  
56      /**
57       * Test the uniqueness of the property names.
58       */
59      @Test
60      public void unique() {
61          final List<String> nameList = new ArrayList<>();
62          for (final Definition definition : StyleAttributes.Definition.values()) {
63              final String propertyName = definition.getPropertyName();
64              if (nameList.contains(propertyName)) {
65                  fail("StyleAttributes.Definition: the property name '"
66                      + propertyName + "' is defined more than once");
67              }
68              nameList.add(propertyName);
69          }
70      }
71  
72      /**
73       * Test the naming convention.
74       */
75      @Test
76      public void name() {
77          for (final Definition definition : StyleAttributes.Definition.values()) {
78              final String propertyName = definition.getPropertyName();
79              if (propertyName.indexOf('-') == -1) {
80                  if (definition.name().endsWith("_") && Character.isLowerCase(definition.name().charAt(0))) {
81                      fail("StyleAttributes.Definition: '" + definition.name() + "' must not end with underscore");
82                  }
83              }
84              else {
85                  if (!definition.name().endsWith("_")) {
86                      fail("StyleAttributes.Definition: '" + definition.name() + "' must end with underscore");
87                  }
88              }
89          }
90      }
91  
92      /**
93       * Test the name in the JavaDoc matches the definition name.
94       * @throws IOException if an error occurs
95       */
96      @Test
97      public void javaDoc() throws IOException {
98          try (Stream<String> stream = Files.lines(Paths.get("src/main/java/"
99                  + getClass().getPackage().getName().replace('.', '/') + "/StyleAttributes.java"))) {
100             final List<String> lines = stream.collect(Collectors.toList());
101             final Pattern pattern = Pattern.compile("\\s+[A-Z_]+\\(\"(.*?)\",");
102             for (int i = 1; i < lines.size(); i++) {
103                 final Matcher matcher = pattern.matcher(lines.get(i));
104                 if (matcher.find() && !lines.get(i - 1).contains("{@code " + matcher.group(1) + "}")) {
105                     fail("StyleAttributes: not matching JavaDoc in line " + i);
106                 }
107             }
108         }
109     }
110 
111 }