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.TreeItem;
  39 import javafx.scene.control.TreeTableColumn;
  40 import javafx.scene.control.TreeTableView;
  41 import javafx.scene.control.cell.TreeItemPropertyValueFactory;
  42 import javafx.scene.layout.StackPane;
  43 import javafx.stage.Stage;
  44 import javafx.util.Duration;
  45 import myapp3.pkg3.MyData;
  46 
  47 import static myapp3.Constants.*;
  48 
  49 // This logic is copied from AppTreeTableViewUnexported.
  50 
  51 /**
  52  * Modular test application for testing JavaFX beans.
  53  * This is launched by ModuleLauncherTest.
  54  */
  55 public class AppTreeTableViewQualExported extends Application {
  56 
  57     /**
  58      * @param args the command line arguments
  59      */
  60     public static void main(String[] args) {
  61         try {
  62             Application.launch(args);
  63         } catch (Throwable t) {
  64             t.printStackTrace(System.err);
  65             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  66         }
  67     }
  68 
  69     private Logger logger;
  70     private Handler logHandler;
  71     private final List<Throwable> errs = new ArrayList<>();
  72 
  73     private void initLogger() {
  74         Locale.setDefault(Locale.US);
  75 
  76         // Initialize Logger
  77         logHandler = new Handler() {
  78             @Override
  79             public void publish(LogRecord record) {
  80                 final Throwable t = record.getThrown();
  81                 // detect any Throwable:
  82                 if (t != null) {
  83                     errs.add(t);
  84                 }
  85             }
  86 
  87             @Override
  88             public void flush() {
  89             }
  90 
  91             @Override
  92             public void close() {
  93             }
  94         };
  95 
  96         logger = Logger.getLogger("javafx.scene.control");
  97         logger.addHandler(logHandler);
  98     }
  99 
 100     @Override
 101     public void start(Stage stage) throws Exception {
 102         initLogger();
 103 
 104         try {
 105             StackPane root = new StackPane();
 106             Scene scene = new Scene(root);
 107             TreeTableView<MyData> treeTableView = new TreeTableView<>();
 108 
 109             // Name column
 110             TreeTableColumn<MyData, String> nameCol = new TreeTableColumn<>();
 111             nameCol.setText("Name");
 112             nameCol.setPrefWidth(150);
 113             nameCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
 114 
 115             // Value column
 116             TreeTableColumn<MyData, Integer> valueCol = new TreeTableColumn<>();
 117             valueCol.setText("Value");
 118             valueCol.setPrefWidth(100);
 119             valueCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("value"));
 120 
 121             treeTableView.getColumns().addAll(nameCol, valueCol);
 122 
 123             TreeItem<MyData> treeRoot = new TreeItem<>(new MyData("Row A", 1));
 124             treeRoot.setExpanded(true);
 125             treeTableView.setRoot(treeRoot);
 126 
 127             TreeItem<MyData> item1 = new TreeItem<>(new MyData("Row B", 2));
 128             TreeItem<MyData> item2 = new TreeItem<>(new MyData("Row C", 3));
 129             treeRoot.getChildren().addAll(item1, item2);
 130 
 131             root.getChildren().add(treeTableView);
 132 
 133             stage.setScene(scene);
 134             System.err.println("The following two WARNING messages are expected:");
 135             stage.show();
 136 
 137             // Hide the stage after the specified amount of time
 138             KeyFrame kf = new KeyFrame(Duration.millis(SHOWTIME), e -> stage.hide());
 139             Timeline timeline = new Timeline(kf);
 140             timeline.play();
 141         } catch (Error | Exception ex) {
 142             ex.printStackTrace(System.err);
 143             System.exit(ERROR_UNEXPECTED_EXCEPTION);
 144         }
 145     }
 146 
 147     private void fail(String message, Throwable t) {
 148         if (message != null) {
 149             System.err.print(message + ": ");
 150         }
 151         if (t != null) {
 152             System.err.println(t);
 153             t.printStackTrace();
 154         } else {
 155             System.err.println();
 156         }
 157         System.exit(ERROR_ASSERTION_FAILURE);
 158     }
 159 
 160     @Override public void stop() {
 161         final int expectedExceptions = 2; // One for each PropertyValueFactory
 162 
 163         if (errs.isEmpty()) {
 164             fail("ERROR: did not get the expected exception", null);
 165         }
 166 
 167         if (expectedExceptions != errs.size()) {
 168             fail("ERROR: expected " + expectedExceptions + " exceptions, got: " + errs.size(), null);
 169         }
 170 
 171         for (Throwable t : errs) {
 172             if (! (t instanceof RuntimeException)) {
 173                 fail("ERROR: unexpeted exception: ", t);
 174             }
 175 
 176             RuntimeException ex = (RuntimeException) t;
 177             Throwable cause = ex.getCause();
 178             if (! (cause instanceof IllegalAccessException)) {
 179                 fail("ERROR: unexpeted cause: ", ex);
 180             }
 181 
 182             String message = cause.getMessage();
 183             if (message == null) {
 184                 fail("ERROR: detail message of cause is null", ex);
 185             }
 186 
 187             boolean badMessage = false;
 188             if (!message.contains(" cannot access class ")) badMessage = true;
 189             if (!message.contains(" does not open ")) badMessage = true;
 190             if (!message.endsWith(" to javafx.base")) badMessage = true;
 191             if (badMessage) {
 192                 fail("ERROR: detail message not formatted correctly", ex);
 193             }
 194         }
 195 
 196         // We got the expected exception, exit normally
 197         System.exit(ERROR_NONE);
 198     }
 199 
 200 }