• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 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 8009736 8010279
27  * @run testng BasicTest
28  */
29 
30 package test.java.util.function.BinaryOperator;
31 
32 import java.util.Comparator;
33 import java.util.function.BinaryOperator;
34 import java.util.function.Function;
35 import java.util.function.ToIntFunction;
36 import org.testng.annotations.Test;
37 
38 
39 import static java.util.function.BinaryOperator.minBy;
40 import static java.util.function.BinaryOperator.maxBy;
41 import static org.testng.Assert.assertSame;
42 import static org.testng.Assert.fail;
43 
44 /**
45  * Unit tests for helper methods in Comparators
46  */
47 @Test(groups = "unit")
48 public class BasicTest {
49 
50     private static class People {
51         final String firstName;
52         final String lastName;
53         final int age;
54 
People(String first, String last, int age)55         People(String first, String last, int age) {
56             firstName = first;
57             lastName = last;
58             this.age = age;
59         }
60 
getFirstName()61         String getFirstName() { return firstName; }
getLastName()62         String getLastName() { return lastName; }
getAge()63         int getAge() { return age; }
64     }
65 
66     private final People people[] = {
67         new People("John", "Doe", 34),
68         new People("Mary", "Doe", 30),
69     };
70 
testMaxBy()71     public void testMaxBy() {
72         Comparator<People> cmp = Comparator.comparing(People::getFirstName);
73         // lesser
74         assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]);
75         // euqal
76         cmp = Comparator.comparing(People::getLastName);
77         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
78         // greater
79         cmp = Comparator.comparingInt(People::getAge);
80         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
81     }
82 
testMinBy()83     public void testMinBy() {
84         Comparator<People> cmp = Comparator.comparing(People::getFirstName);
85         // lesser
86         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
87         // euqal
88         cmp = Comparator.comparing(People::getLastName);
89         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
90         // greater
91         cmp = Comparator.comparingInt(People::getAge);
92         assertSame(minBy(cmp).apply(people[0], people[1]), people[1]);
93     }
94 
testNulls()95     public void testNulls() {
96         try {
97             BinaryOperator<String> op = minBy(null);
98             fail("minBy(null) should throw NPE");
99         } catch (NullPointerException npe) {}
100 
101         try {
102             BinaryOperator<String> op = maxBy(null);
103             fail("maxBy(null) should throw NPE");
104         } catch (NullPointerException npe) {}
105     }
106 }
107