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.javascript.configuration;
16  
17  import java.util.ArrayList;
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.List;
21  
22  import org.htmlunit.javascript.HtmlUnitScriptable;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  /**
27   * Tests for {@link WorkerJavaScriptConfiguration}.
28   *
29   * @author Ronald Brill
30   */
31  public class WorkerJavaScriptConfigurationTest {
32  
33  
34      /**
35       * Test of order.
36       */
37      @Test
38      public void treeOrder() {
39          final List<String> defined = new ArrayList<>(WorkerJavaScriptConfiguration.CLASSES_.length);
40  
41          final HashMap<Integer, List<String>> levels = new HashMap<>();
42          for (final Class<?> c : WorkerJavaScriptConfiguration.CLASSES_) {
43              defined.add(c.getSimpleName());
44  
45              int level = 1;
46              Class<?> parent = c.getSuperclass();
47              while (parent != HtmlUnitScriptable.class) {
48                  level++;
49                  parent = parent.getSuperclass();
50              }
51  
52              List<String> clsAtLevel = levels.get(level);
53              if (clsAtLevel == null) {
54                  clsAtLevel = new ArrayList<>();
55                  levels.put(level, clsAtLevel);
56              }
57              clsAtLevel.add(c.getSimpleName());
58          }
59  
60          final List<String> all = new ArrayList<>(WorkerJavaScriptConfiguration.CLASSES_.length);
61          for (int level = 1; level <= levels.size(); level++) {
62              final List<String> clsAtLevel = levels.get(level);
63              Collections.sort(clsAtLevel);
64              all.addAll(clsAtLevel);
65  
66              // dump
67              /*
68              final String indent = "       ";
69              System.out.println(indent + " // level " + level);
70  
71              System.out.print(indent);
72              int chars = indent.length();
73              for (final String cls : clsAtLevel) {
74                  final String toPrint = " " + cls + ".class,";
75                  chars += toPrint.length();
76                  if (chars > 120) {
77                      System.out.println();
78                      System.out.print(indent);
79                      chars = indent.length() + toPrint.length();
80                  }
81                  System.out.print(toPrint);
82              }
83              System.out.println();
84              */
85          }
86          Assert.assertEquals(all, defined);
87      }
88  }