/
SortableBeerListTest.java
167 lines (140 loc) · 4.47 KB
/
SortableBeerListTest.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package edu.ics211.h03;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import edu.ics211.h01.AbvComparator;
import edu.ics211.h01.Beer;
import edu.ics211.h01.IBrewery;
import edu.ics211.h01.IbuComparator;
import edu.ics211.h01.IndiaPaleAle;
import edu.ics211.h01.ManoaBrewing;
import edu.ics211.h01.Pilsner;
import java.util.Comparator;
import org.junit.Before;
import org.junit.Test;
/**
* Represents a SortableBeerListTest.
*
* @author Cam Moore
*
*/
public class SortableBeerListTest {
private ManoaBrewing brewrey;
private SortableBeerList bitters;
private SortableBeerList strongs;
private Pilsner[] pils;
private IbuComparator ibuC;
private AbvComparator abvC;
/** Sets up the brewer. */
@Before
public void initialize() {
brewrey = ManoaBrewing.getInstance();
ibuC = new IbuComparator();
abvC = new AbvComparator();
pils = new Pilsner[3];
pils[0] = (Pilsner) brewrey.brewPilsner("pils1", 25, 4.2);
pils[1] = (Pilsner) brewrey.brewPilsner("pils2", 30, 5.0);
pils[2] = (Pilsner) brewrey.brewPilsner("pils2", 35, 6.0);
bitters = new SortableBeerList(ibuC);
strongs = new SortableBeerList(abvC);
for (int i = pils.length - 1; i >= 0; i--) {
bitters.add(pils[i]);
strongs.add(brewrey.brewBeer("strong beer" + i, randomBeerType()));
}
}
/**
* Test method for {@link edu.ics211.h03.SortableBeerList#get(int)}.
*/
@Test
public void testGet() {
assertTrue("Wrong Get", pils[0].equals(bitters.get(0)));
assertTrue("Wrong Get", pils[1].equals(bitters.get(1)));
assertTrue("Wrong Get", pils[2].equals(bitters.get(2)));
}
/**
* Test method for {@link edu.ics211.h03.SortableBeerList#set(int, edu.ics211.h01.Beer)}.
*/
@Test
public void testSet() {
bitters.set(0, pils[1]);
assertTrue("Wrong Get", pils[1].equals(bitters.get(0)));
assertTrue("Wrong Get", pils[1].equals(bitters.get(1)));
assertTrue("Wrong Get", pils[2].equals(bitters.get(2)));
}
/**
* Test method for {@link edu.ics211.h03.SortableBeerList#indexOf(java.lang.Object)}.
*/
@Test
public void testIndexOf() {
IndiaPaleAle ipa = (IndiaPaleAle) brewrey.brewBeer("IPA", IBrewery.INDIA_PALE_ALE);
assertEquals("Shouldn't find ipa", bitters.indexOf(ipa), -1);
assertEquals("Shouldn't find ipa", bitters.indexOf(pils[0]), 0);
assertEquals("Shouldn't find ipa", bitters.indexOf(pils[1]), 1);
assertEquals("Shouldn't find ipa", bitters.indexOf(pils[2]), 2);
}
/**
* Test method for {@link edu.ics211.h03.SortableBeerList#size()}.
*/
@Test
public void testSize() {
assertEquals("Wrong size want 3", bitters.size(), 3);
}
/**
* Test method for {@link edu.ics211.h03.SortableBeerList#add(edu.ics211.h01.Beer)}.
*/
@Test
public void testAddBeer() {
IndiaPaleAle ipa = (IndiaPaleAle) brewrey.brewBeer("IPA", IBrewery.INDIA_PALE_ALE);
assertTrue(bitters.add(ipa));
assertTrue("Not Sorted", isSorted(bitters, ibuC));
assertEquals("Wrong size want 4", bitters.size(), 4);
assertTrue("Wrong Get", ipa.equals(bitters.get(3)));
}
/**
* Test method for {@link edu.ics211.h03.SortableBeerList#add(int, edu.ics211.h01.Beer)}.
*/
@Test
public void testAddIntBeer() {
IndiaPaleAle ipa = (IndiaPaleAle) brewrey.brewBeer("IPA", IBrewery.INDIA_PALE_ALE);
bitters.add(2, ipa);
assertTrue("Not Sorted", isSorted(bitters, ibuC));
assertEquals("Wrong size want 4", bitters.size(), 4);
assertTrue("Wrong Get", ipa.equals(bitters.get(3)));
}
/**
* Test method for {@link edu.ics211.h03.SortableBeerList#remove(int)}.
*/
@Test
public void testRemove() {
try {
bitters.remove(-1);
fail("Should throw exception");
} catch (Exception e) {
// expected
}
assertEquals("Wrong size want 3", bitters.size(), 3);
bitters.remove(0);
assertEquals("Wrong size want 2", bitters.size(), 2);
}
private String randomBeerType() {
int val = (int) (Math.random() * 3);
switch (val) {
case 0:
return IBrewery.PILSNER;
case 1:
return IBrewery.BOHEMIAN_PILSNER;
case 2:
return IBrewery.INDIA_PALE_ALE;
default:
return IBrewery.INDIA_PALE_ALE;
}
}
private boolean isSorted(SortableBeerList data, Comparator<Beer> comp) {
for (int i = 0; i < data.size() - 1; i++) {
if (comp.compare(data.get(i), data.get(i + 1)) > 0) {
return false;
}
}
return true;
}
}