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.Assert.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.Test;
31  
32  /**
33   * Tests for {@link StyleAttributes}.
34   *
35   * @author Ahmed Ashour
36   */
37  public class StyleAttributesTest {
38  
39      /**
40       * Test of alphabetical order.
41       */
42      @Test
43      public void lexicographicOrder() {
44          String lastName = null;
45          for (final Definition definition : StyleAttributes.Definition.values()) {
46              final String name = definition.name();
47              if (lastName != null && name.compareToIgnoreCase(lastName) < 1) {
48                  fail("StyleAttributes.Definition: '"
49                      + name + "' should be before '" + lastName + "'");
50              }
51              lastName = name;
52          }
53      }
54  
55      /**
56       * Test the uniqueness of the property names.
57       */
58      @Test
59      public void unique() {
60          final List<String> nameList = new ArrayList<>();
61          for (final Definition definition : StyleAttributes.Definition.values()) {
62              final String propertyName = definition.getPropertyName();
63              if (nameList.contains(propertyName)) {
64                  fail("StyleAttributes.Definition: the property name '"
65                      + propertyName + "' is defined more than once");
66              }
67              nameList.add(propertyName);
68          }
69      }
70  
71      /**
72       * Test the naming convention.
73       */
74      @Test
75      public void name() {
76          for (final Definition definition : StyleAttributes.Definition.values()) {
77              final String propertyName = definition.getPropertyName();
78              if (propertyName.indexOf('-') == -1) {
79                  if (definition.name().endsWith("_") && Character.isLowerCase(definition.name().charAt(0))) {
80                      fail("StyleAttributes.Definition: '" + definition.name() + "' must not end with underscore");
81                  }
82              }
83              else {
84                  if (!definition.name().endsWith("_")) {
85                      fail("StyleAttributes.Definition: '" + definition.name() + "' must end with underscore");
86                  }
87              }
88          }
89      }
90  
91      /**
92       * Test the name in the JavaDoc matches the definition name.
93       * @throws IOException if an error occurs
94       */
95      @Test
96      public void javaDoc() throws IOException {
97          try (Stream<String> stream = Files.lines(Paths.get("src/main/java/"
98                  + getClass().getPackage().getName().replace('.', '/') + "/StyleAttributes.java"))) {
99              final List<String> lines = stream.collect(Collectors.toList());
100             final Pattern pattern = Pattern.compile("\\s+[A-Z_]+\\(\"(.*?)\",");
101             for (int i = 1; i < lines.size(); i++) {
102                 final Matcher matcher = pattern.matcher(lines.get(i));
103                 if (matcher.find() && !lines.get(i - 1).contains("{@code " + matcher.group(1) + "}")) {
104                     fail("StyleAttributes: not matching JavaDoc in line " + i);
105                 }
106             }
107         }
108     }
109 
110 }