Repository navigation
Expand file tree
/
Copy pathDecoratorTest.java
More file actions
83 lines (65 loc) · 2.48 KB
/
Copy pathDecoratorTest.java
File metadata and controls
83 lines (65 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package java8project;
import java.util.function.Function;
import org.junit.Test;
import java8.decorator.functions.Packing;
import java8.decorator.functions.Taxes;
import java8.decorator.item.Book;
import java8.decorator.item.GiftPacking;
import java8.decorator.item.InternationalDeilvery;
import java8.decorator.item.Item;
import java8.decorator.item.ItemFunctional;
import junit.framework.Assert;
public class DecoratorTest {
//Implementing Decorator Design pattern
@Test
public void testDecoratorItens(){
Item book = new Book(10);
Assert.assertEquals(10, book.getPrice());
InternationalDeilvery internationalDeilvery = new InternationalDeilvery(book);
Assert.assertEquals(15, internationalDeilvery.getPrice());
InternationalDeilvery internationalDeilvery2 = new InternationalDeilvery(new GiftPacking(book));
Assert.assertEquals(30, internationalDeilvery2.getPrice());
GiftPacking giftPacking = new GiftPacking(new InternationalDeilvery(book));
Assert.assertEquals(30, giftPacking.getPrice());
}
@Test
public void testDecoratorFunctionalItens(){
Item book = new Book(10);
Assert.assertEquals(10, book.getPrice());
Function<Integer, Integer> giftPacking = value -> {
System.out.println("Applying giftPacking");
return 15 + value;
};
Assert.assertEquals(new Integer(25), giftPacking.apply(book.getPrice()));
Function<Integer, Integer> internationalDeilvery = value -> {
System.out.println("Applying internationalDeilvery");
return 5 + value;
};
Assert.assertEquals(new Integer(15), internationalDeilvery.apply(book.getPrice()));
Assert.assertEquals(new Integer(30), giftPacking.andThen(internationalDeilvery).apply(book.getPrice()));
}
@SuppressWarnings("unchecked")
@Test
public void testDecoratorFunctionalItens2(){
ItemFunctional book = new ItemFunctional(10);
Assert.assertEquals(10, book.getPrice());
Function<Integer, Integer> giftPacking = value -> {
System.out.println("Applying giftPacking");
return 15 + value;
};
Function<Integer, Integer> internationalDeilvery = value -> {
System.out.println("Applying internationalDeilvery");
return 5 + value;
};
book.setItemExtras(giftPacking, internationalDeilvery);
Assert.assertEquals(30, book.getPrice());
}
@SuppressWarnings("unchecked")
@Test
public void testDecoratorFunctionalItensMethodReferences(){
ItemFunctional book = new ItemFunctional(10,
Packing::giftPacking,
Taxes::international);
Assert.assertEquals(75, book.getPrice());
}
}