Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit 138dbc0

Browse files
committed
Add more test methods to cover 100% of the source code
1 parent 4625f1b commit 138dbc0

File tree

2 files changed

+190
-5
lines changed

2 files changed

+190
-5
lines changed

src/controllers/AppStoreAPI.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,23 @@ public void save() throws Exception {
299299
out.close();
300300
}
301301

302+
private String xmlFileName = "apps.xml";
303+
304+
/**
305+
* <strong>Test Only</strong>
306+
* <p>DO NOT CALL THIS METHOD UNLESS YOU WANT TO TEST EXCEPTION</p>
307+
* @param fileName String parameter passed to change the xmlFileName field
308+
*/
309+
public void setFileName(String fileName) {
310+
this.xmlFileName = fileName;
311+
}
312+
313+
@SuppressWarnings("ResultOfMethodCallIgnored")
302314
public String fileName() {
303-
File xmlFile = new File("apps.xml");
315+
File xmlFile = new File(xmlFileName);
304316
if(!xmlFile.exists()) { // If the file doesn't exist then create it
305317
try {
306-
xmlFile.createNewFile();
318+
xmlFile.createNewFile(); // The return value cannot be false
307319
ObjectOutputStream out = new XStream(new DomDriver()).createObjectOutputStream(new FileWriter("apps.xml"));
308320
out.writeObject(apps);
309321
out.close();

test/AppStoreAPITest.java

Lines changed: 176 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.jupiter.api.Nested;
66
import org.junit.jupiter.api.Test;
77

8+
import java.io.File;
9+
810
import static org.junit.jupiter.api.Assertions.*;
911

1012
public class AppStoreAPITest {
@@ -188,17 +190,119 @@ void listRecommendedAppsReturnsRecommendedAppsWhenTheyExist() {
188190
assertTrue(apps.contains("WeDo"));
189191
}
190192

193+
@Test
194+
void listAllSummaryOfAllAppsReturnsNoAppsStoredWhenArrayListIsEmpty() {
195+
assertEquals(0, emptyAppStore.numberOfApps());
196+
assertTrue(emptyAppStore.listSummaryOfAllApps().toLowerCase().contains("no apps"));
197+
}
191198

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+
}
193206

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+
}
196218

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+
}
197249
}
198250

199251
@Nested
200252
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+
}
201290

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+
}
202306
}
203307

204308
@Nested
@@ -233,6 +337,75 @@ void sortByNameAscendingDoesntCrashWhenListIsEmpty() {
233337

234338
}
235339

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+
236409
//--------------------------------------------
237410
// Helper Methods
238411
//--------------------------------------------

0 commit comments

Comments
 (0)