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