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.source;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.apache.commons.io.FileUtils;
24  
25  /**
26   * Prints out a tiny test case, from a provided HTML file.
27   *
28   * @author Ahmed Ashour
29   */
30  public final class TestCaseCreator {
31  
32      /**
33       * The entry point.
34       *
35       * @param args the arguments
36       * @throws IOException if an error occurs
37       */
38      public static void main(final String[] args) throws IOException {
39          if (args.length == 0) {
40              System.out.println("HTML file location is not provided");
41              return;
42          }
43  
44          final File file = new File(args[0]);
45          if (!file.exists()) {
46              System.out.println("File does not exist " + file.getAbsolutePath());
47          }
48  
49          System.out.println("        /**");
50          System.out.println("         * @throws Exception if an error occurs");
51          System.out.println("         */");
52          System.out.println("        @Test");
53          System.out.println("        @Alerts()");
54          System.out.println("        public void test() throws Exception {");
55  
56          final List<String> lines = FileUtils.readLines(file, ISO_8859_1);
57          for (int i = 0; i < lines.size(); i++) {
58              final String line = lines.get(i);
59              if (i == 0) {
60                  System.out.println("            final String html = \"" + line.replace("\"", "\\\"") + "\\n\"");
61              }
62              else {
63                  System.out.print("                + \"" + line.replace("\"", "\\\"") + "\\n\"");
64                  if (i == lines.size() - 1) {
65                      System.out.print(";");
66                  }
67                  System.out.println();
68              }
69          }
70          System.out.println("            loadPageWithAlerts2(html);");
71          System.out.println("        }");
72      }
73  
74      private TestCaseCreator() {
75      }
76  }