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.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.layout.Measure;
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.Test;
26  
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  
32  /**
33   * Test if every in Java declared CSS class has really an entry in a CSS file.
34   */
35  public class BootstrapClassUnitTest {
36  
37    @Test
38    public void testNames() throws NoSuchFieldException {
39  
40      final String fieldRegex = "[A-Z][A-Z0-9_]*[A-Z0-9]";
41      final String nameRegex = "[a-z][a-z0-9\\-]*[a-z0-9]";
42  
43      for (final BootstrapClass value : BootstrapClass.values()) {
44        final boolean ignoreByTest = BootstrapClass.class.getField(value.name()).isAnnotationPresent(Deprecated.class);
45        if (!ignoreByTest) {
46          final String field = value.toString();
47          final String name = value.getName();
48  
49          Assertions.assertTrue(field.matches(fieldRegex),
50              "testing: '" + field + "' matches regexp for consts like FOO_BAR");
51          Assertions.assertTrue(name.matches(nameRegex), "testing: '" + name + "' matches regexp for CSS like foo-bar");
52  
53          final StringBuilder calculatedName = new StringBuilder();
54          for (int i = 0; i < field.length(); i++) {
55            final char c = field.charAt(i);
56            if (c == '_') {
57              calculatedName.append("-");
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, if every item of the {@link BootstrapClass} occurs in the tobago.css.
70     */
71    @Test
72    public void testCompareBootstrapCss() throws IOException, NoSuchFieldException {
73  
74      final BootstrapClass[] allValues = BootstrapClass.values();
75      final List<BootstrapClass> toCheck = new ArrayList<>();
76      for (final BootstrapClass value : allValues) {
77        final boolean ignoreByTest = BootstrapClass.class.getField(value.name()).isAnnotationPresent(Deprecated.class);
78        if (!ignoreByTest) {
79          toCheck.add(value);
80        }
81      }
82  
83      final List<CssItem> missing = CssClassUtils.compareCss(
84          "npm/dist/css/tobago.css",
85          toCheck.toArray(new BootstrapClass[0]));
86  
87      Assertions.assertTrue(missing.isEmpty(), "These classes are missing in tobago.css: " + missing);
88    }
89  
90    @Test
91    public void testValueOfMeasureAttributes() {
92      Assertions.assertEquals(BootstrapClass.COL_1,
93          BootstrapClass.valueOf(new Measure("1", Measure.Unit.SEG), Attributes.extraSmall));
94      Assertions.assertEquals(BootstrapClass.COL_12,
95          BootstrapClass.valueOf(new Measure("12", Measure.Unit.SEG), Attributes.extraSmall));
96      Assertions.assertEquals(null,
97          BootstrapClass.valueOf((Measure) null, Attributes.extraSmall));
98      Assertions.assertEquals(BootstrapClass.COL_MD_5,
99          BootstrapClass.valueOf(new Measure("5", Measure.Unit.SEG), Attributes.medium));
100     Assertions.assertEquals(BootstrapClass.COL_LG,
101         BootstrapClass.valueOf(Measure.valueOf("*"), Attributes.large));
102     Assertions.assertEquals(BootstrapClass.COL_XL_AUTO,
103         BootstrapClass.valueOf(Measure.valueOf("auto"), Attributes.extraLarge));
104   }
105 }