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.css;
21  
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  public class TobagoClassUnitTest {
30  
31    @Test
32    public void testNames() throws NoSuchFieldException {
33  
34      final String fieldRegex = "[A-Z_]*[A-Z]";
35      final String nameRegex = "[a-z][a-zA-Z\\-]*[a-z]";
36  
37      for (final TobagoClass value : TobagoClass.values()) {
38        final boolean ignoreByTest = TobagoClass.class.getField(value.name()).isAnnotationPresent(Deprecated.class);
39        if (!ignoreByTest) {
40          final String field = value.toString();
41          final String name = value.getName();
42  
43          Assertions.assertTrue(field.matches(fieldRegex));
44          Assertions.assertTrue(name.matches(nameRegex));
45  
46          final StringBuilder calculatedName = new StringBuilder();
47          calculatedName.append("tobago-");
48          for (int i = 0; i < field.length(); i++) {
49            final char c = field.charAt(i);
50            if (c == '_') {
51              final char nextChar = field.charAt(i + 1);
52              if (nextChar == '_') {
53                calculatedName.append("-");
54              } else {
55                calculatedName.append(nextChar);
56              }
57              i++;
58            } else {
59              calculatedName.append(Character.toLowerCase(c));
60            }
61          }
62  
63          Assertions.assertEquals(calculatedName.toString(), name, field);
64        }
65      }
66    }
67  
68    /**
69     * This test checks whether every item of the {@link TobagoClass} occurs in the _tobago.scss.
70     */
71    @Test
72    public void testCompareTobagoCss() throws IOException, NoSuchFieldException {
73  
74      final TobagoClass[] allValues = TobagoClass.values();
75      final List<TobagoClass> toCheck = new ArrayList<>();
76      for (final TobagoClass value : allValues) {
77        final boolean ignoreByTest = TobagoClass.class.getField(value.name()).isAnnotationPresent(Deprecated.class);
78        if (!ignoreByTest) {
79          toCheck.add(value);
80        }
81      }
82  
83      final List<CssItem> missing =
84          CssClassUtils.compareCss("npm/scss/_tobago.scss",
85              toCheck.toArray(new CssItem[0]));
86  
87      Assertions.assertTrue(missing.isEmpty(), "These classes are missing in _tobago.scss: " + missing);
88    }
89  
90  }