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;
16  
17  import org.htmlunit.BrowserVersion;
18  import org.htmlunit.WebDriverTestCase;
19  import org.junit.runners.model.FrameworkMethod;
20  import org.junit.runners.model.Statement;
21  
22  /**
23   * The Browser Statement.
24   *
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  class BrowserStatement extends Statement {
29  
30      private Statement next_;
31      private final boolean notYetImplemented_;
32      private final FrameworkMethod method_;
33      private final boolean realBrowser_;
34      private final BrowserVersion browserVersion_;
35      private final int tries_;
36  
37      BrowserStatement(final Statement next, final FrameworkMethod method, final boolean realBrowser,
38              final boolean notYetImplemented, final int tries, final BrowserVersion browserVersion) {
39          next_ = next;
40          method_ = method;
41          realBrowser_ = realBrowser;
42          notYetImplemented_ = notYetImplemented;
43          tries_ = tries;
44          browserVersion_ = browserVersion;
45      }
46  
47      @Override
48      public void evaluate() throws Throwable {
49          for (int i = 0; i < tries_; i++) {
50              try {
51                  evaluateSolo();
52                  break;
53              }
54              catch (final Throwable ex) {
55                  if (Boolean.parseBoolean(System.getProperty(WebDriverTestCase.AUTOFIX_))) {
56                      TestCaseCorrector.correct(method_, realBrowser_, browserVersion_, notYetImplemented_, ex);
57                  }
58                  if (notYetImplemented_) {
59                      throw ex;
60                  }
61                  if (BrowserVersionClassRunner.MAVEN) {
62                      System.out.println("Failed test "
63                              + method_.getDeclaringClass().getName() + '.' + method_.getName()
64                              + (tries_ != 1 ? " #" + (i + 1) : ""));
65                  }
66                  if (i == tries_ - 1) {
67                      throw ex;
68                  }
69              }
70          }
71      }
72  
73      public void evaluateSolo() throws Throwable {
74          Exception toBeThrown = null;
75          try {
76              next_.evaluate();
77              if (notYetImplemented_) {
78                  final String errorMessage;
79                  if (browserVersion_.getNickname() == null) {
80                      errorMessage = method_.getName() + " is marked as not implemented but already works";
81                  }
82                  else {
83                      errorMessage = method_.getName() + " is marked as not implemented with "
84                          + browserVersion_.getNickname() + " but already works";
85                  }
86                  toBeThrown = new Exception(errorMessage);
87              }
88          }
89          catch (final Throwable e) {
90              if (!notYetImplemented_) {
91                  throw e;
92              }
93          }
94          if (toBeThrown != null) {
95              throw toBeThrown;
96          }
97      }
98  }