/
GenericPetCageTests.java
96 lines (70 loc) · 2.23 KB
/
GenericPetCageTests.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2015, 2016, Malpeza and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
package com.malpeza.solid.lsp;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class GenericPetCageTests {
@Test
public void allows() {
Animal a = new Animal();
Cat c = new Cat();
Dog d = new Dog();
GenericPetCage<Animal> cage = new GenericPetCage<>();
assertTrue(cage.allows(a));
assertTrue(cage.allows(c));
assertTrue(cage.allows(d));
GenericPetCage<Cat> catCage = new GenericPetCage<>();
// GenericPetCage<Cat> catCage2 = new GenericPetCage<Animal>(); /*Does not compile*/
assertTrue(catCage.allows(c));
/* These lines do not compile */
// assertFalse(catCage.allows(a));
// assertFalse(catCage.allows(d));
}
@Test
public void empty() {
GenericPetCage<Animal> cage = new GenericPetCage<>();
GenericPetCage<Cat> catCage = new GenericPetCage<>();
assertTrue(cage.isEmpty());
assertTrue(catCage.isEmpty());
Animal a = new Animal();
Cat c = new Cat();
cage.enter(a);
catCage.enter(c);
assertFalse(cage.isEmpty());
assertFalse(catCage.isEmpty());
cage.takeOut();
catCage.takeOut();
assertTrue(cage.isEmpty());
assertTrue(catCage.isEmpty());
}
@Test(expected = IllegalStateException.class)
public void takeOut_from_empty_animal_cage() {
GenericPetCage<Animal> cage = new GenericPetCage<>();
cage.takeOut();
}
@Test(expected = IllegalStateException.class)
public void takeOut_from_empty_cat_cage() {
GenericPetCage<Cat> catCage = new GenericPetCage<>();
catCage.takeOut();
}
@Test
public void enter_cat_sub_type() {
GenericPetCage<Cat> catCage = new GenericPetCage<>();
PersianCat pc = new PersianCat();
catCage.enter(pc);
assertFalse(catCage.isEmpty());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void invalid_up_cast() {
GenericPetCage catCage = new GenericPetCage<Cat>();
GenericPetCage<Animal> cage = (GenericPetCage<Animal>)catCage;
Dog d = new Dog();
assertTrue(cage.allows(d));
cage.enter(d);
assertFalse(cage.isEmpty());
}
}