1 /*
   2  * Copyright (c) 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8024500
  27  * @run testng BiFunctionTest
  28  */
  29 
  30 import java.util.function.BiFunction;
  31 import java.util.function.Function;
  32 import org.testng.annotations.Test;
  33 
  34 import static org.testng.Assert.assertEquals;
  35 import static org.testng.Assert.fail;
  36 
  37 @Test(groups = "unit")
  38 public class BiFunctionTest {
  39     static class Quote {
  40         double unit_price;
  41 
  42         Quote(double price) {
  43             unit_price = price;
  44         }
  45     };
  46 
  47     static class Order {
  48         int quantity;
  49 
  50         Order(int quantity) {
  51             this.quantity = quantity;
  52         }
  53     };
  54 
  55     BiFunction<Quote, Order, Double> estimate = (quote, order) -> {
  56         if (quote.unit_price < 0) {
  57             throw new IllegalArgumentException("quote");
  58         }
  59 
  60         if (order.quantity < 0) {
  61             throw new IllegalArgumentException("order");
  62         }
  63 
  64         return quote.unit_price * order.quantity;
  65     };
  66 
  67     Function<Double, Long> creditcheck = total -> {
  68         if (total > 100.00) {
  69             throw new RuntimeException("overlimit");
  70         }
  71         return total.longValue();
  72     };
  73 
  74     public void testAndThen() {
  75         try {
  76             BiFunction<Quote, Order, Long> checkout = estimate.andThen(null);
  77             fail("Null argument should throw NPE");
  78         } catch (NullPointerException npe) {
  79             // ignore
  80         }
  81 
  82         BiFunction<Quote, Order, Long> checkout = estimate.andThen(creditcheck);
  83         try {
  84             checkout.apply(new Quote(20.0), new Order(-1));
  85             fail("First function delieve exception");
  86         } catch (IllegalArgumentException e) {
  87             assertEquals(e.getMessage(), "order");
  88         }
  89 
  90         try {
  91             checkout.apply(new Quote(20.0), new Order(10));
  92             fail("Second function delieve exception");
  93         } catch (RuntimeException e) {
  94             assertEquals(e.getMessage(), "overlimit");
  95         }
  96 
  97         assertEquals(49, checkout.apply(new Quote(24.99), new Order(2)).longValue());
  98         assertEquals(50, checkout.apply(new Quote(25), new Order(2)).longValue());
  99     }
 100 }