|
| 1 | +package com.baeldung.doublecolon; |
| 2 | + |
| 3 | +import com.baeldung.doublecolon.function.TriFunction; |
| 4 | +import org.junit.After; |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.util.*; |
| 10 | +import java.util.function.BiFunction; |
| 11 | + |
| 12 | +import static com.baeldung.doublecolon.ComputerUtils.*; |
| 13 | + |
| 14 | +public class TestComputerUtils { |
| 15 | + |
| 16 | + @Before |
| 17 | + public void setup() { |
| 18 | + } |
| 19 | + |
| 20 | + @After |
| 21 | + public void tearDown() { |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testConstructorReference() { |
| 26 | + |
| 27 | + Computer c1 = new Computer(2015, "white"); |
| 28 | + Computer c2 = new Computer(2009, "black"); |
| 29 | + Computer c3 = new Computer(2014, "black"); |
| 30 | + |
| 31 | + BiFunction<Integer, String, Computer> c4Function = Computer::new; |
| 32 | + Computer c4 = c4Function.apply(2013, "white"); |
| 33 | + BiFunction<Integer, String, Computer> c5Function = Computer::new; |
| 34 | + Computer c5 = c5Function.apply(2010, "black"); |
| 35 | + BiFunction<Integer, String, Computer> c6Function = Computer::new; |
| 36 | + Computer c6 = c6Function.apply(2008, "black"); |
| 37 | + |
| 38 | + List<Computer> inventory = Arrays.asList(c1, c2, c3, c4, c5, c6); |
| 39 | + |
| 40 | + List<Computer> blackComputer = filter(inventory, blackPredicate); |
| 41 | + Assert.assertEquals("The black Computers are: ", blackComputer.size(), 4); |
| 42 | + |
| 43 | + List<Computer> after2010Computer = filter(inventory, after2010Predicate); |
| 44 | + Assert.assertEquals("The Computer bought after 2010 are: ", after2010Computer.size(), 3); |
| 45 | + |
| 46 | + List<Computer> before2011Computer = filter(inventory, c -> c.getAge() < 2011); |
| 47 | + Assert.assertEquals("The Computer bought before 2011 are: ", before2011Computer.size(), 3); |
| 48 | + |
| 49 | + inventory.sort(Comparator.comparing(Computer::getAge)); |
| 50 | + |
| 51 | + Assert.assertEquals("Oldest Computer in inventory", c6, inventory.get(0)); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testStaticMethodReference() { |
| 57 | + |
| 58 | + Computer c1 = new Computer(2015, "white", 35); |
| 59 | + Computer c2 = new Computer(2009, "black", 65); |
| 60 | + TriFunction<Integer, String, Integer, Computer> c6Function = Computer::new; |
| 61 | + Computer c3 = c6Function.apply(2008, "black", 90); |
| 62 | + |
| 63 | + List<Computer> inventory = Arrays.asList(c1, c2, c3); |
| 64 | + inventory.forEach(ComputerUtils::repair); |
| 65 | + |
| 66 | + Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testInstanceMethodArbitraryObjectParticularType() { |
| 71 | + |
| 72 | + Computer c1 = new Computer(2015, "white", 35); |
| 73 | + Computer c2 = new MacbookPro(2009, "black", 65); |
| 74 | + List<Computer> inventory = Arrays.asList(c1, c2); |
| 75 | + inventory.forEach(Computer::turnOnPc); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testSuperMethodReference() { |
| 81 | + |
| 82 | + final TriFunction<Integer, String, Integer, MacbookPro> integerStringIntegerObjectTriFunction = MacbookPro::new; |
| 83 | + final MacbookPro macbookPro = integerStringIntegerObjectTriFunction.apply(2010, "black", 100); |
| 84 | + Double initialValue = new Double(999.99); |
| 85 | + final Double actualValue = macbookPro.calculateValue(initialValue); |
| 86 | + Assert.assertEquals(766.659, actualValue, 0.0); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments