|
5 | 5 | import org.junit.jupiter.api.Nested; |
6 | 6 | import org.junit.jupiter.api.Test; |
7 | 7 |
|
| 8 | +import java.io.File; |
| 9 | + |
8 | 10 | import static org.junit.jupiter.api.Assertions.*; |
9 | 11 |
|
10 | 12 | public class AppStoreAPITest { |
@@ -188,17 +190,119 @@ void listRecommendedAppsReturnsRecommendedAppsWhenTheyExist() { |
188 | 190 | assertTrue(apps.contains("WeDo")); |
189 | 191 | } |
190 | 192 |
|
| 193 | + @Test |
| 194 | + void listAllSummaryOfAllAppsReturnsNoAppsStoredWhenArrayListIsEmpty() { |
| 195 | + assertEquals(0, emptyAppStore.numberOfApps()); |
| 196 | + assertTrue(emptyAppStore.listSummaryOfAllApps().toLowerCase().contains("no apps")); |
| 197 | + } |
191 | 198 |
|
192 | | - } |
| 199 | + @Test |
| 200 | + void listAllSummaryOfAllAppsReturnsAppsStoredWhenArrayListHasAppsStored() { |
| 201 | + assertEquals(12, appStore.numberOfApps()); |
| 202 | + String summary = appStore.listSummaryOfAllApps(); |
| 203 | + assertTrue(summary.contains("Outlook")); |
| 204 | + assertTrue(summary.contains(String.valueOf(2.0))); |
| 205 | + } |
193 | 206 |
|
194 | | - @Nested |
195 | | - class ReportingMethods { |
| 207 | + @Test |
| 208 | + void listAllGameAppsReturnsGameAppsStoredWhenArrayListHasAppsStored() { |
| 209 | + assertEquals(12, appStore.numberOfApps()); |
| 210 | + String summary = appStore.listAllGameApps(); |
| 211 | + assertTrue(summary.contains("Empires")); |
| 212 | + assertTrue(summary.contains("CookOff")); |
| 213 | + assertTrue(summary.contains("Tetris")); |
| 214 | + assertFalse(summary.contains("Outlook")); |
| 215 | + assertFalse(summary.contains("EV3")); |
| 216 | + assertTrue(summary.contains("Genres")); |
| 217 | + } |
196 | 218 |
|
| 219 | + @Test |
| 220 | + void listAllGameAppsReturnsNoGameAppsWhenArrayListIsEmpty() { |
| 221 | + assertEquals(0, emptyAppStore.numberOfApps()); |
| 222 | + assertTrue(emptyAppStore.listAllGameApps().toLowerCase().contains("no game apps")); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + void listAllEducationAppsReturnsEducationAppsStoredWhenArrayListHasAppsStored() { |
| 227 | + assertEquals(12, appStore.numberOfApps()); |
| 228 | + String summary = appStore.listAllEducationApps(); |
| 229 | + assertTrue(summary.contains("Spike")); |
| 230 | + assertTrue(summary.contains("WeDo")); |
| 231 | + assertFalse(summary.contains("CookOff")); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + void listAllProductivityAppsReturnsNoProductivityAppsWhenArrayListIsEmpty() { |
| 236 | + assertEquals(0, emptyAppStore.numberOfApps()); |
| 237 | + assertTrue(emptyAppStore.listAllProductivityApps().toLowerCase().contains("no productivity apps")); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + void listAllProductivityAppsReturnsProductivityAppsStoredWhenArrayListHasAppsStored() { |
| 242 | + assertEquals(12, appStore.numberOfApps()); |
| 243 | + String summary = appStore.listAllProductivityApps(); |
| 244 | + assertTrue(summary.contains("Pages")); |
| 245 | + assertTrue(summary.contains("NoteKeeper")); |
| 246 | + assertFalse(summary.contains("MazeRunner")); |
| 247 | + assertFalse(summary.contains("WeDo")); |
| 248 | + } |
197 | 249 | } |
198 | 250 |
|
199 | 251 | @Nested |
200 | 252 | class SearchingMethods { |
| 253 | + @Test |
| 254 | + void listAllAppsByNameWhenArrayListHasAppsStored() { |
| 255 | + assertEquals(12, appStore.numberOfApps()); |
| 256 | + String list = appStore.listAllAppsByName("Pages"); |
| 257 | + assertTrue(list.contains("Pages")); |
| 258 | + assertFalse(list.contains("NoteKeeper")); |
| 259 | + assertTrue(list.contains("3.5")); |
| 260 | + assertTrue(list.contains("2.99")); |
| 261 | + } |
| 262 | + |
| 263 | + @Test |
| 264 | + void listAllAppsByNameReturnsNoAppsWhenArrayListIsEmpty() { |
| 265 | + assertEquals(0, emptyAppStore.numberOfApps()); |
| 266 | + assertTrue(emptyAppStore.listAllAppsByName("NoteKeeper").toLowerCase().contains("no apps")); |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + void listAllAppsAboveOrEqualAGivenStarRatingReturnsAppsMatchingStarRatingWhenArrayListHasAppsStored() { |
| 271 | + assertEquals(12, appStore.numberOfApps()); |
| 272 | + assertTrue(appStore.listAllAppsAboveOrEqualAGivenStarRating(10).toLowerCase().contains("no apps")); |
| 273 | + assertTrue(appStore.listAllAppsAboveOrEqualAGivenStarRating(0).toLowerCase().contains("no apps")); |
| 274 | + appStore.addApp(setupProductivityAppWithRating(2,3)); |
| 275 | + String list = appStore.listAllAppsAboveOrEqualAGivenStarRating(1); |
| 276 | + assertTrue(list.contains("Evernote")); |
| 277 | + assertTrue(list.contains("John101")); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + void listAllAppsByChosenDeveloperReturnsNoAppsWhenTheDeveloperDoesNotExist() { |
| 282 | + assertTrue(appStore.listAllAppsByChosenDeveloper(developerSphero).toLowerCase().contains("no apps for developer")); |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + void listAllAppsByChosenDeveloperReturnsNoAppsWhenArrayListIsEmpty() { |
| 287 | + assertEquals(0, emptyAppStore.numberOfApps()); |
| 288 | + assertTrue(emptyAppStore.listAllAppsByChosenDeveloper(developerSphero).toLowerCase().contains("no apps for developer")); |
| 289 | + } |
201 | 290 |
|
| 291 | + @Test |
| 292 | + void listAllAppsByChosenDeveloperReturnsAppsMatchingDeveloperWhenArrayListHasAppsStored() { |
| 293 | + assertEquals(12, appStore.numberOfApps()); |
| 294 | + String list = appStore.listAllAppsByChosenDeveloper(developerLego); |
| 295 | + assertTrue(list.contains("WeDo")); |
| 296 | + assertTrue(list.contains("Spike")); |
| 297 | + assertFalse(list.contains("CookOff")); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + void numberOfAppsByChosenDeveloper() { |
| 302 | + assertEquals(12, appStore.numberOfApps()); |
| 303 | + assertEquals(0, emptyAppStore.numberOfAppsByChosenDeveloper(developerMicrosoft)); |
| 304 | + assertEquals(0, appStore.numberOfAppsByChosenDeveloper(developerSphero)); |
| 305 | + } |
202 | 306 | } |
203 | 307 |
|
204 | 308 | @Nested |
@@ -233,6 +337,75 @@ void sortByNameAscendingDoesntCrashWhenListIsEmpty() { |
233 | 337 |
|
234 | 338 | } |
235 | 339 |
|
| 340 | + @Nested |
| 341 | + class PersistenceMethods { |
| 342 | + @Test |
| 343 | + void loadXMLFile() { |
| 344 | + assertEquals(12, appStore.numberOfApps()); |
| 345 | + try { |
| 346 | + appStore.load(); |
| 347 | + } catch (Exception e) { |
| 348 | + throw new RuntimeException(e); |
| 349 | + } |
| 350 | + assertTrue(appStore.numberOfApps() >= 12); |
| 351 | + // Check whether |
| 352 | + } |
| 353 | + |
| 354 | + @Test |
| 355 | + void saveXMLFile() { |
| 356 | + File xmlFile = new File("apps.xml"); |
| 357 | + if(xmlFile.exists()) xmlFile.delete(); |
| 358 | + try { |
| 359 | + appStore.save(); |
| 360 | + } catch (Exception e) { |
| 361 | + throw new RuntimeException(e); |
| 362 | + } |
| 363 | + assertTrue(xmlFile.exists()); |
| 364 | + } |
| 365 | + |
| 366 | + @Test |
| 367 | + void checkXMLFileName() { |
| 368 | + assertEquals("apps.xml", appStore.fileName()); |
| 369 | + File xmlFile = new File("apps.xml"); |
| 370 | + if(xmlFile.exists()) xmlFile.delete(); |
| 371 | + assertEquals("apps.xml", appStore.fileName()); |
| 372 | + // This is a new method introduced in version 5.8 |
| 373 | + // If the IDEA indicates that the method does not exist |
| 374 | + // Please update the JUnit library to the latest version |
| 375 | + assertThrowsExactly(RuntimeException.class, () -> { |
| 376 | + // An exception will be thrown if the fileName is in a nonexistent directory |
| 377 | + appStore.setFileName(xmlFile.getAbsolutePath() + File.separator + "APathThatWillThrowException" + File.separator + "apps.xml"); |
| 378 | + appStore.fileName(); // Should throw an exception |
| 379 | + }); |
| 380 | + } |
| 381 | + } |
| 382 | + |
| 383 | + @Nested |
| 384 | + class ValidationMethods { |
| 385 | + @Test |
| 386 | + void isValidAppName() { |
| 387 | + assertTrue(appStore.isValidAppName("Empires")); |
| 388 | + assertTrue(appStore.isValidAppName("CookOff")); |
| 389 | + assertTrue(appStore.isValidAppName("Tetris")); |
| 390 | + assertTrue(appStore.isValidAppName("Outlook")); |
| 391 | + assertFalse(emptyAppStore.isValidAppName("Outlook")); |
| 392 | + } |
| 393 | + } |
| 394 | + |
| 395 | + @Test |
| 396 | + void checkRandomApp() { |
| 397 | + assertNull(emptyAppStore.randomApp()); |
| 398 | + assertNotNull(appStore.randomApp()); |
| 399 | + } |
| 400 | + |
| 401 | + @Test |
| 402 | + void checkSimulateRatings() { |
| 403 | + appStore.simulateRatings(); |
| 404 | + for (int i = 0; i < appStore.numberOfApps(); ++i) { |
| 405 | + assertFalse(appStore.getAppByIndex(i).getRatings().isEmpty()); |
| 406 | + } |
| 407 | + } |
| 408 | + |
236 | 409 | //-------------------------------------------- |
237 | 410 | // Helper Methods |
238 | 411 | //-------------------------------------------- |
|
0 commit comments