1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package myapp3;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.Locale;
  31 import java.util.logging.Handler;
  32 import java.util.logging.LogRecord;
  33 import java.util.logging.Logger;
  34 import javafx.animation.KeyFrame;
  35 import javafx.animation.Timeline;
  36 import javafx.application.Application;
  37 import javafx.scene.Scene;
  38 import javafx.scene.control.TableColumn;
  39 import javafx.scene.control.TableView;
  40 import javafx.scene.control.cell.PropertyValueFactory;
  41 import javafx.scene.layout.StackPane;
  42 import javafx.stage.Stage;
  43 import javafx.util.Duration;
  44 import myapp3.pkg1.MyData;
  45 
  46 import static myapp3.Constants.*;
  47 
  48 /**
  49  * Modular test application for testing JavaFX beans.
  50  * This is launched by ModuleLauncherTest.
  51  */
  52 public class AppTableViewUnexported extends Application {
  53 
  54     /**
  55      * @param args the command line arguments
  56      */
  57     public static void main(String[] args) {
  58         try {
  59             Application.launch(args);
  60         } catch (Throwable t) {
  61             System.err.println("ERROR: caught unexpected exception: " + t);
  62             t.printStackTrace(System.err);
  63             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  64         }
  65     }
  66 
  67     private Logger logger;
  68     private Handler logHandler;
  69     private final List<Throwable> errs = new ArrayList<>();
  70 
  71     private void initLogger() {
  72         Locale.setDefault(Locale.US);
  73 
  74         // Initialize Logger
  75         logHandler = new Handler() {
  76             @Override
  77             public void publish(LogRecord record) {
  78                 final Throwable t = record.getThrown();
  79                 // detect any Throwable:
  80                 if (t != null) {
  81                     errs.add(t);
  82                 }
  83             }
  84 
  85             @Override
  86             public void flush() {
  87             }
  88 
  89             @Override
  90             public void close() {
  91             }
  92         };
  93 
  94         logger = Logger.getLogger("javafx.scene.control");
  95         logger.addHandler(logHandler);
  96     }
  97 
  98     @Override
  99     public void start(Stage stage) throws Exception {
 100         initLogger();
 101 
 102         try {
 103             StackPane root = new StackPane();
 104             Scene scene = new Scene(root);
 105             TableView<MyData> tableView = new TableView<>();
 106 
 107             // Name column
 108             TableColumn<MyData, String> nameCol = new TableColumn<>();
 109             nameCol.setText("Name");
 110             nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
 111 
 112             // Value column
 113             TableColumn<MyData, Integer> valueCol = new TableColumn<>();
 114             valueCol.setText("Value");
 115             valueCol.setCellValueFactory(new PropertyValueFactory<>("value"));
 116 
 117             tableView.getColumns().addAll(nameCol, valueCol);
 118 
 119             tableView.getItems().add(new MyData("Row A", 1));
 120             tableView.getItems().add(new MyData("Row B", 2));
 121             tableView.getItems().add(new MyData("Row C", 3));
 122 
 123             root.getChildren().add(tableView);
 124 
 125             stage.setScene(scene);
 126             System.err.println("The following two WARNING messages are expected:");
 127             stage.show();
 128 
 129             // Hide the stage after the specified amount of time
 130             KeyFrame kf = new KeyFrame(Duration.millis(SHOWTIME), e -> stage.hide());
 131             Timeline timeline = new Timeline(kf);
 132             timeline.play();
 133         } catch (Error | Exception ex) {
 134             System.err.println("ERROR: caught unexpected exception: " + ex);
 135             ex.printStackTrace(System.err);
 136             System.exit(ERROR_UNEXPECTED_EXCEPTION);
 137         }
 138     }
 139 
 140     private void fail(String message, Throwable t) {
 141         if (message != null) {
 142             System.err.print(message + ": ");
 143         }
 144         if (t != null) {
 145             System.err.println(t);
 146             t.printStackTrace();
 147         } else {
 148             System.err.println();
 149         }
 150         System.exit(ERROR_ASSERTION_FAILURE);
 151     }
 152 
 153     @Override public void stop() {
 154         final int expectedExceptions = 2; // One for each PropertyValueFactory
 155 
 156         if (errs.isEmpty()) {
 157             fail("ERROR: did not get the expected exception", null);
 158         }
 159 
 160         if (expectedExceptions != errs.size()) {
 161             fail("ERROR: expected " + expectedExceptions + " exceptions, got: " + errs.size(), null);
 162         }
 163 
 164         for (Throwable t : errs) {
 165             if (! (t instanceof RuntimeException)) {
 166                 fail("ERROR: unexpected exception: ", t);
 167             }
 168 
 169             RuntimeException ex = (RuntimeException) t;
 170             Throwable cause = ex.getCause();
 171             if (! (cause instanceof IllegalAccessException)) {
 172                 fail("ERROR: unexpected cause: ", ex);
 173             }
 174 
 175             String message = cause.getMessage();
 176             if (message == null) {
 177                 fail("ERROR: detail message of cause is null", ex);
 178             }
 179 
 180             boolean badMessage = false;
 181             if (!message.contains(" cannot access class ")) badMessage = true;
 182             if (!message.contains(" does not open ")) badMessage = true;
 183             if (!message.endsWith(" to javafx.base")) badMessage = true;
 184             if (badMessage) {
 185                 fail("ERROR: detail message not formatted correctly", ex);
 186             }
 187         }
 188 
 189         // We got the expected exception, exit normally
 190         System.exit(ERROR_NONE);
 191     }
 192 
 193 }