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.junit.annotation;
16  
17  import static org.htmlunit.junit.SetExpectedAlertsBeforeTestExecutionCallback.isDefined;
18  
19  import java.lang.reflect.Method;
20  import java.util.Arrays;
21  
22  import org.htmlunit.BrowserVersion;
23  import org.htmlunit.junit.SetExpectedAlertsBeforeTestExecutionCallback;
24  import org.junit.jupiter.api.Assertions;
25  
26  /**
27   * Utility functions for working with our annotations.
28   *
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   */
32  public final class AnnotationUtils {
33  
34      /**
35       * Disallow instantiation of this class.
36       */
37      private AnnotationUtils() {
38          // Empty.
39      }
40  
41      /**
42       * Helper to validate defined annotations for various errors.
43       *
44       * @param method the method to validate
45       */
46      public static void assertAlerts(final Method method) {
47          final Alerts alerts = method.getAnnotation(Alerts.class);
48          if (alerts != null) {
49              if (!isDefined(alerts.value())) {
50                  Assertions.assertFalse(
51                          isDefined(alerts.DEFAULT())
52                          && isDefined(alerts.CHROME())
53                          && isDefined(alerts.FF())
54                          && isDefined(alerts.FF_ESR())
55                          && isDefined(alerts.EDGE()),
56                          "Obsolete DEFAULT because all browser expectations defined individually");
57  
58                  assertNotEquals("@Alerts", method, BrowserVersion.CHROME, alerts.CHROME(), alerts.DEFAULT());
59                  assertNotEquals("@Alerts", method, BrowserVersion.FIREFOX, alerts.FF(), alerts.DEFAULT());
60                  assertNotEquals("@Alerts", method, BrowserVersion.FIREFOX_ESR, alerts.FF_ESR(), alerts.DEFAULT());
61                  assertNotEquals("@Alerts", method, BrowserVersion.EDGE, alerts.EDGE(), alerts.DEFAULT());
62              }
63  
64              final HtmlUnitNYI nyiAlerts = method.getAnnotation(HtmlUnitNYI.class);
65              if (nyiAlerts != null) {
66                  if (isDefined(nyiAlerts.value())) {
67                      if (isDefined(alerts.CHROME())) {
68                          assertNotEquals("@HtmlUnitNYI",
69                                  method, null, alerts.CHROME(), nyiAlerts.value());
70                      }
71                      else if (isDefined(alerts.DEFAULT())) {
72                          assertNotEquals("@HtmlUnitNYI",
73                                  method, null, alerts.DEFAULT(), nyiAlerts.value());
74                      }
75                      else if (isDefined(alerts.value())) {
76                          assertNotEquals("@HtmlUnitNYI",
77                                  method, null, alerts.value(), nyiAlerts.value());
78                      }
79                  }
80  
81                  if (isDefined(nyiAlerts.CHROME())) {
82                      if (isDefined(alerts.CHROME())) {
83                          assertNotEquals("@HtmlUnitNYI",
84                                  method, BrowserVersion.CHROME, alerts.CHROME(), nyiAlerts.CHROME());
85                      }
86                      else if (isDefined(alerts.DEFAULT())) {
87                          assertNotEquals("@HtmlUnitNYI",
88                                  method, BrowserVersion.CHROME, alerts.DEFAULT(), nyiAlerts.CHROME());
89                      }
90                      else if (isDefined(alerts.value())) {
91                          assertNotEquals("@HtmlUnitNYI",
92                                  method, BrowserVersion.CHROME, alerts.value(), nyiAlerts.CHROME());
93                      }
94                  }
95  
96                  if (isDefined(nyiAlerts.FF_ESR())) {
97                      if (isDefined(alerts.FF_ESR())) {
98                          assertNotEquals("@HtmlUnitNYI",
99                                  method, BrowserVersion.FIREFOX_ESR, alerts.FF_ESR(), nyiAlerts.FF_ESR());
100                     }
101                     else if (isDefined(alerts.DEFAULT())) {
102                         assertNotEquals("@HtmlUnitNYI",
103                                 method, BrowserVersion.FIREFOX_ESR, alerts.DEFAULT(), nyiAlerts.FF_ESR());
104                     }
105                     else if (isDefined(alerts.value())) {
106                         assertNotEquals("@HtmlUnitNYI",
107                                 method, BrowserVersion.FIREFOX_ESR, alerts.value(), nyiAlerts.FF_ESR());
108                     }
109                 }
110 
111                 if (isDefined(nyiAlerts.FF())) {
112                     if (isDefined(alerts.FF())) {
113                         assertNotEquals("@HtmlUnitNYI",
114                                 method, BrowserVersion.FIREFOX, alerts.FF(), nyiAlerts.FF());
115                     }
116                     else if (isDefined(alerts.DEFAULT())) {
117                         assertNotEquals("@HtmlUnitNYI",
118                                 method, BrowserVersion.FIREFOX, alerts.DEFAULT(), nyiAlerts.FF());
119                     }
120                     else if (isDefined(alerts.value())) {
121                         assertNotEquals("@HtmlUnitNYI",
122                                 method, BrowserVersion.FIREFOX, alerts.value(), nyiAlerts.FF());
123                     }
124                 }
125 
126                 if (isDefined(nyiAlerts.EDGE())) {
127                     if (isDefined(alerts.EDGE())) {
128                         assertNotEquals("@HtmlUnitNYI",
129                                 method, BrowserVersion.EDGE, alerts.EDGE(), nyiAlerts.EDGE());
130                     }
131                     else if (isDefined(alerts.DEFAULT())) {
132                         assertNotEquals("@HtmlUnitNYI",
133                                 method, BrowserVersion.EDGE, alerts.DEFAULT(), nyiAlerts.EDGE());
134                     }
135                     else if (isDefined(alerts.value())) {
136                         assertNotEquals("@HtmlUnitNYI",
137                                 method, BrowserVersion.EDGE, alerts.value(), nyiAlerts.EDGE());
138                     }
139                 }
140             }
141         }
142     }
143 
144     private static void assertNotEquals(final String annotation, final Method method, final BrowserVersion browser,
145             final String[] value1, final String[] value2) {
146         if (value1.length != 0 && !SetExpectedAlertsBeforeTestExecutionCallback.EMPTY_DEFAULT.equals(value1[0])
147                 && value1.length == value2.length
148                 && Arrays.asList(value1).toString().equals(Arrays.asList(value2).toString())) {
149             final String nickname = browser == null ? "DEFAULT" : browser.getNickname();
150             throw new AssertionError("Redundant " + annotation + " for " + nickname + " in "
151                     + method.getDeclaringClass().getSimpleName() + '.' + method.getName() + "()");
152         }
153     }
154 }