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 myapp4;
  27 
  28 import java.lang.reflect.UndeclaredThrowableException;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import java.util.Locale;
  32 import java.util.logging.Handler;
  33 import java.util.logging.LogRecord;
  34 import java.util.logging.Logger;
  35 import javafx.beans.binding.Bindings;
  36 import javafx.beans.binding.DoubleBinding;
  37 import javafx.beans.binding.ObjectBinding;
  38 import myapp4.pkg1.MyProps;
  39 
  40 import static myapp4.Constants.*;
  41 
  42 /**
  43  * Modular test application for testing JavaFX beans.
  44  * This is launched by ModuleLauncherTest.
  45  */
  46 public class AppBindingsUnexported {
  47 
  48     /**
  49      * @param args the command line arguments
  50      */
  51     public static void main(String[] args) {
  52         try {
  53             new AppBindingsUnexported().doTest();
  54             System.exit(ERROR_NONE);
  55         } catch (Throwable t) {
  56             t.printStackTrace(System.err);
  57             System.exit(ERROR_ASSERTION_FAILURE);
  58         }
  59     }
  60 
  61     private void checkException(RuntimeException ex) {
  62         Throwable cause = ex.getCause();
  63         if (! (cause instanceof IllegalAccessException)) {
  64             System.err.println("ERROR: unexpected cause: " + cause);
  65             throw ex;
  66         }
  67 
  68         String message = cause.getMessage();
  69         if (message == null) {
  70             System.err.println("ERROR: detail message of cause is null");
  71             throw ex;
  72         }
  73 
  74         boolean badMessage = false;
  75         if (!message.contains(" cannot access class ")) badMessage = true;
  76         if (!message.contains(" does not open ")) badMessage = true;
  77         if (!message.endsWith(" to javafx.base")) badMessage = true;
  78         if (badMessage) {
  79             System.err.println("ERROR: detail message not formatted correctly: " + message);
  80             throw ex;
  81         }
  82     }
  83 
  84     private final double EPSILON = 1.0e-4;
  85 
  86     private void assertEquals(double expected, double observed) {
  87         if (Math.abs(expected - observed) > EPSILON) {
  88             throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">");
  89         }
  90     }
  91 
  92     private void assertEquals(String expected, String observed) {
  93         if (!expected.equals(observed)) {
  94             throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">");
  95         }
  96     }
  97 
  98     private void assertSame(Object expected, Object observed) {
  99         if (expected != observed) {
 100             throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">");
 101         }
 102     }
 103 
 104     private Logger logger;
 105     private Handler logHandler;
 106     private final List<Throwable> errs = new ArrayList<>();
 107 
 108     private void initLogger() {
 109         Locale.setDefault(Locale.US);
 110 
 111         // Initialize Logger
 112         logHandler = new Handler() {
 113             @Override
 114             public void publish(LogRecord record) {
 115                 final Throwable t = record.getThrown();
 116                 // detect any Throwable:
 117                 if (t != null) {
 118                     errs.add(t);
 119                 }
 120             }
 121 
 122             @Override
 123             public void flush() {
 124             }
 125 
 126             @Override
 127             public void close() {
 128             }
 129         };
 130 
 131         logger = Logger.getLogger("javafx.beans");
 132         logger.addHandler(logHandler);
 133     }
 134 
 135     public void doTest() throws Exception {
 136         initLogger();
 137 
 138         MyProps root = new MyProps();
 139         MyProps a = new MyProps();
 140         MyProps b = new MyProps();
 141 
 142         root.setNext(a);
 143         a.setNext(b);
 144         a.setFoo(1.2);
 145         b.setFoo(2.3);
 146 
 147         try {
 148             Bindings.selectDouble(root, "next", "foo");
 149             throw new AssertionError("ERROR: did not get the expected exception");
 150         } catch (UndeclaredThrowableException ex) {
 151             checkException(ex);
 152         }
 153 
 154         try {
 155             Bindings.select(root, "next", "next");
 156             throw new AssertionError("ERROR: did not get the expected exception");
 157         } catch (UndeclaredThrowableException ex) {
 158             checkException(ex);
 159         }
 160 
 161         RootProps root2 = new RootProps();
 162         MyProps c = new MyProps();
 163         MyProps d = new MyProps();
 164 
 165         root2.setNext(c);
 166         c.setNext(d);
 167         c.setFoo(1.2);
 168         d.setFoo(2.3);
 169 
 170         // In this case, the binding will succeed; calling get() will return 0;
 171         // the first time it is called it will log a warning.
 172         DoubleBinding binding3 = Bindings.selectDouble(root2, "next", "foo");
 173         assertEquals(0, binding3.get()); // This will log a warning
 174         c.setFoo(3.4);
 175         assertEquals(0, binding3.get()); // No warning here
 176 
 177         // In this case, the binding will succeed; calling get() will return null;
 178         // the first time it is called it will log a warning.
 179         ObjectBinding<MyProps> binding4 = Bindings.select(root2, "next", "next");
 180         assertSame(null, binding4.get()); // This will log a warning
 181         assertSame(null, binding4.get()); // No warning here
 182 
 183         // Assert that we got 2 warnings
 184         final int expectedExceptions = 2; // First call to get for each binding
 185 
 186         if (errs.isEmpty()) {
 187             throw new AssertionError("ERROR: did not get the expected exception");
 188         }
 189 
 190         assertEquals(expectedExceptions, errs.size());
 191 
 192         for (Throwable t : errs) {
 193             if (!(t instanceof RuntimeException)) {
 194                 throw new AssertionError("ERROR: unexpected exception: ", t);
 195             }
 196             checkException((RuntimeException) t);
 197         }
 198     }
 199 
 200 }