• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package test.java.util.function.BiFunction;
31 
32 import java.util.function.BiFunction;
33 import java.util.function.Function;
34 import org.testng.annotations.Test;
35 
36 import static org.testng.Assert.assertEquals;
37 import static org.testng.Assert.fail;
38 
39 @Test(groups = "unit")
40 public class BiFunctionTest {
41     static class Quote {
42         double unit_price;
43 
Quote(double price)44         Quote(double price) {
45             unit_price = price;
46         }
47     };
48 
49     static class Order {
50         int quantity;
51 
Order(int quantity)52         Order(int quantity) {
53             this.quantity = quantity;
54         }
55     };
56 
57     BiFunction<Quote, Order, Double> estimate = (quote, order) -> {
58         if (quote.unit_price < 0) {
59             throw new IllegalArgumentException("quote");
60         }
61 
62         if (order.quantity < 0) {
63             throw new IllegalArgumentException("order");
64         }
65 
66         return quote.unit_price * order.quantity;
67     };
68 
69     Function<Double, Long> creditcheck = total -> {
70         if (total > 100.00) {
71             throw new RuntimeException("overlimit");
72         }
73         return total.longValue();
74     };
75 
testAndThen()76     public void testAndThen() {
77         try {
78             BiFunction<Quote, Order, Long> checkout = estimate.andThen(null);
79             fail("Null argument should throw NPE");
80         } catch (NullPointerException npe) {
81             // ignore
82         }
83 
84         BiFunction<Quote, Order, Long> checkout = estimate.andThen(creditcheck);
85         try {
86             checkout.apply(new Quote(20.0), new Order(-1));
87             fail("First function delivers exception");
88         } catch (IllegalArgumentException e) {
89             assertEquals(e.getMessage(), "order");
90         }
91 
92         try {
93             checkout.apply(new Quote(20.0), new Order(10));
94             fail("Second function delivers exception");
95         } catch (RuntimeException e) {
96             assertEquals(e.getMessage(), "overlimit");
97         }
98 
99         assertEquals(49, checkout.apply(new Quote(24.99), new Order(2)).longValue());
100         assertEquals(50, checkout.apply(new Quote(25), new Order(2)).longValue());
101     }
102 }
103