-
Notifications
You must be signed in to change notification settings - Fork 0
/
KahahaTest.java
245 lines (213 loc) · 9.08 KB
/
KahahaTest.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
/**
* JUnit test for Kahaha I'a subclass.
* @author Lisa Miller
* @since 3/15/22
*/
public class KahahaTest {
/** constant reproductive mode for this Ia. */
static final String REPRODUCTIVE_MODE = "serial";
/** constant array of what the fish eats. */
static final String[] DIET_ITEMS = {"zooplankton", "algae", "detritus", "invertebrates"};
/** constant maximum length for this Ia. */
static final double MAX_LENGTH = 8.0;
/** constant minimum length for this Ia.
*/
static final double MIN_LENGTH = 4.0;
/** Fixture initialization (common initialization
* for all tests). **/
@Before public void setUp() {
}
/** Constructor with random length test.*/
@Test public void constructorAndGetterTest(){
I_a i = new Kahaha();
String name = i.getName();
String engName = i.getEnglishName();
String sciName = i.getScientificName();
double length = i.getLength();
boolean dietOk = false;
String bodColor = i.getBodyColor();
String finColor = i.getFinColor();
String sex = i.getSex();
Assert.assertTrue("Name is not correct. It is " + name + " but should be Kahaha",
name.equalsIgnoreCase("Kahaha"));
Assert.assertTrue("English name is not correct. It is " + engName + " but should be Striped mullet",
engName.equalsIgnoreCase("Striped mullet"));
Assert.assertTrue("Scientific name is not correct. It is " + sciName + " but should be Mugil Cephalus",
sciName.equalsIgnoreCase("Mugil Cephalus"));
Assert.assertTrue("Length is outside allowed limits for this fish. It is " + length
+ " but should be between " + MIN_LENGTH + " and " + MAX_LENGTH, (length >= MIN_LENGTH && length < MAX_LENGTH));
Assert.assertEquals("Body color is not correct. It is " + bodColor + " but should be silver", bodColor, "silver");
Assert.assertEquals("Fin color is not correct. It is " + finColor + " but should be silver",
finColor, "silver");
Assert.assertTrue("Fish sex is not correct. It is " + sex + " but should be male or female", (sex.equalsIgnoreCase("male") || sex.equalsIgnoreCase("female")));
}
/** Constructor with given length test.*/
@Test public void constructorWLengthAndGetterTest(){
I_a i = new Kahaha(4.0);
String name = i.getName();
String engName = i.getEnglishName();
String sciName = i.getScientificName();
double length = i.getLength();
boolean dietOk = false;
String bodColor = i.getBodyColor();
String finColor = i.getFinColor();
String sex = i.getSex();
Assert.assertTrue("Name is not correct. It is " + name + " but should be Kahaha",
name.equalsIgnoreCase("Kahaha"));
Assert.assertTrue("English name is not correct. It is " + engName + " but should be Striped mullet",
engName.equalsIgnoreCase("Striped mullet"));
//Assert.assertTrue("Scientific name is not correct. It is " + sciName + " but should be Mugil Cephalus",
// sciName.equalsIgnoreCase("Mugil Cephalus"));
Assert.assertTrue("Length is outside allowed limits for this fish. It is " + length
+ " but should be between " + MIN_LENGTH + " and " + MAX_LENGTH, (length >= MIN_LENGTH && length < MAX_LENGTH));
Assert.assertEquals("Body color is not correct. It is " + bodColor + " but should be silver", bodColor, "silver");
Assert.assertEquals("Fin color is not correct. It is " + finColor + " but should be silver",
finColor, "silver");
Assert.assertTrue("Fish sex is not correct. It is " + sex + " but should be male or female", (sex.equalsIgnoreCase("male") || sex.equalsIgnoreCase("female")));
}
/** Constructor with length Exception test.*/
@Test(expected=FishSizeException.class)
public void constructorLengthTooLongTest(){
I_a i = new Kahaha(8.1);
}
/** Constructor with length Exception test.*/
@Test(expected=FishSizeException.class)
public void constructorLengthTooShortTest(){
I_a i = new Kahaha(-0.1);
}
/** setLength Exception test.*/
@Test(expected=FishSizeException.class)
public void setLengthTooLongTest(){
I_a i = new Kahaha();
i.setLength(9.5);
}
/** setLength Exception test.*/
@Test(expected=FishSizeException.class)
public void setLengthTooShortTest(){
I_a i = new Kahaha();
i.setLength(-0.1);
}
/**
Checks fish eats all diet items with no Exception.
*/
@Test public void eatingDietTest(){
I_a a = new Kahaha();
String food = "";
boolean foodListCorrect = true;
String bad = "";
for(int i = 0; i < DIET_ITEMS.length; i++){
food = DIET_ITEMS[i];
try{
a.eat(food);
} catch(FishFoodException fe) {
foodListCorrect = false;
bad = food;
} catch(FishSizeException fs) {
//do nothing food ok but fish got too big
}
}
Assert.assertTrue("Fish diet items are not correct. " + bad
+ " is not a diet item", foodListCorrect);
} //eating test
/**
* Test FishFoodException throwing
*/
@Test(expected=FishFoodException.class)
public void fishFoodExceptionTest()throws FishFoodException {
I_a a = new Kahaha();
a.eat("raisins");
}
/**
* Test grow and eat methods.
*/
@Test public void growthTest(){
I_a a = new Kahaha(4.0);
double newLen = 0;
double oldLen = a.getLength();
a.eat("algae");
newLen = a.getLength();
Assert.assertTrue("Fish does not grow bigger when eating. Start length: " + oldLen
+ ", End length: " + newLen, newLen > oldLen);
}
/**
* Test need to levelUp from eat method.
*/
@Test(expected=FishSizeException.class)
public void growthSizeExceptionTest()throws FishSizeException {
I_a a = new Kahaha(7.999);
boolean tooBig = false;
while(!tooBig) {
a.eat("algae");
}
}
/**
* Test levelUp method.
*/
@Test public void levelUpTest() {
I_a a = new Kahaha(7.999);
boolean tooBig = false;
while(!tooBig) {
try {
a.eat("algae");
} catch (FishSizeException fse) {
tooBig = true;
}
}
I_a i = a.levelUp();
//check that i is next level fish
String name = i.getName();
String engName = i.getEnglishName();
String sciName = i.getScientificName();
double length = i.getLength();
boolean dietOk = false;
String bodColor = i.getBodyColor();
String finColor = i.getFinColor();
String sex = i.getSex();
double leveledUpMaxLength = 12.0;
double leveledUpMinLength = 8.0;
Assert.assertTrue("Name is not correct. It is " + name + " but should be 'Ama'ama",
name.equalsIgnoreCase("'Ama'ama"));
Assert.assertTrue("English name is not correct. It is " + engName + " but should be Striped mullet",
engName.equalsIgnoreCase("Striped mullet"));
Assert.assertTrue("Scientific name is not correct. It is " + sciName + " but should be Mugil Cephalus",
sciName.equalsIgnoreCase("Mugil Cephalus"));
Assert.assertTrue("Length is outside allowed limits for this fish. It is " + length
+ " but should be between " + leveledUpMinLength + " and " + leveledUpMaxLength, (length >= leveledUpMinLength && length < leveledUpMaxLength));
Assert.assertEquals("Body color is not correct. It is " + bodColor + " but should be silver", bodColor, "silver");
Assert.assertEquals("Fin color is not correct. It is " + finColor + " but should be silver",
finColor, "silver");
Assert.assertTrue("Fish sex is not correct. It is " + sex + " but should be male or female", (sex.equalsIgnoreCase("male") || sex.equalsIgnoreCase("female")));
} //end level up test
/************* Fishable methods tests ****************************/
//****************** Check for Fishable Implementation ***********/
@Test public void isFishableTest() {
Kahaha a = new Kahaha();
Assert.assertTrue(a.getName() + " is not Fishable", (a instanceof Fishable));
}
//*********** Not yet completed ************
/** test isLegalSize. */
@Test public void isLegalSizeTest() {
Kahaha a = new Kahaha();
Assert.assertEquals("This fish is never legal size", a.isLegalSize(), false);
}
/** test isBait. */
@Test public void isBaitTest() {
Kahaha a = new Kahaha();
Assert.assertEquals("This fish is never used for bait", a.isBait(), false);
}
/** test isGameFish method. */
@Test public void isGamefishTest() {
Kahaha a = new Kahaha();
Assert.assertEquals("This fish is never used for eating", a.isGamefish(), false);
}
// test getCatchMethods
/** test getCatchMethods method. */
@Test public void getCatchMethodsTest() {
Kahaha a = new Kahaha();
Assert.assertEquals("This fish has no catch methods", a.getCatchMethods(), null);
}
} ///close test clas