diff options
| author | jrshi <jrs9538@g.rit.edu> | 2025-11-16 16:02:38 -0500 |
|---|---|---|
| committer | jrshi <jrs9538@g.rit.edu> | 2025-11-16 16:02:38 -0500 |
| commit | 0b43e258054b450f5007ef4d4fa34dacba2d8a9c (patch) | |
| tree | af8f64e4bdb6074fe2fb34c993850b6d81ce61de | |
| parent | 343d0baaaf718bfc9959484d187c4df1e171335e (diff) | |
| parent | af9f559a2ee427905c39363643bac2e7878fb10c (diff) | |
| download | designproject-design-6-0b43e258054b450f5007ef4d4fa34dacba2d8a9c.tar.gz designproject-design-6-0b43e258054b450f5007ef4d4fa34dacba2d8a9c.tar.bz2 designproject-design-6-0b43e258054b450f5007ef4d4fa34dacba2d8a9c.zip | |
Merge branch 'league-play-refactoring' of https://github.com/RIT-SWEN-262/designproject-design-6 into league-play-refactoringleague-play-refactoring
Merging?
36 files changed, 1393 insertions, 196 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json index e0f15db..9bd06c2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "java.configuration.updateBuildConfiguration": "automatic" + "java.configuration.updateBuildConfiguration": "automatic", + "java.debug.settings.onBuildFailureProceed": true }
\ No newline at end of file diff --git a/data/personaldb.json b/data/personaldb.json index 9b9709d..2aa4ec7 100644 --- a/data/personaldb.json +++ b/data/personaldb.json @@ -1,5 +1,19 @@ [ { + "clubs": [], + "nextClubId": 1, + "username": "test", + "passwordHash": 3556498, + "fullName": "test", + "courses": [ + 999 + ], + "rounds": [], + "invites": [], + "joinedTeam": null, + "team": null + }, + { "clubs": [ { "id": 1, @@ -61,8 +75,8 @@ "clubUsed": 2 } ], - "swingCount": 3, - "distance": 106 + "distance": 106, + "swingCount": 3 }, { "holeNumber": 2, @@ -76,17 +90,17 @@ "clubUsed": 1 } ], - "swingCount": 2, - "distance": 1002 + "distance": 1002, + "swingCount": 2 } ], "currentHoleIndex": 2, - "totalSwings": 5, - "totalDistance": 1108.0, "currentHole": { "number": 3, "par": 4 - } + }, + "totalSwings": 5, + "totalDistance": 1108.0 }, { "course": 1, @@ -112,20 +126,22 @@ "clubUsed": 1 } ], - "swingCount": 1, - "distance": 204 + "distance": 204, + "swingCount": 1 } ], "currentHoleIndex": 9, - "totalSwings": 1, - "totalDistance": 204.0, "currentHole": { "number": 10, "par": 3 - } + }, + "totalSwings": 1, + "totalDistance": 204.0 } ], - "invites": [] + "invites": [], + "joinedTeam": null, + "team": null }, { "clubs": [], @@ -135,6 +151,8 @@ "fullName": "tyler f", "courses": [], "rounds": [], - "invites": [] + "invites": [], + "joinedTeam": null, + "team": null } ]
\ No newline at end of file @@ -42,6 +42,11 @@ <artifactId>jackson-datatype-jdk8</artifactId> <version>2.20.0</version> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-xml</artifactId> + <version>2.20.0</version> + </dependency> </dependencies> <build> <plugins> @@ -67,8 +72,8 @@ <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.11</version> <configuration> - <destfile>/target/coverage-reports/jacoco-unit.exec</destfile> - <datafile>/target/coverage-reports/jacoco-unit.exec</datafile> + <dataFile>/target/coverage-reports/jacoco-unit.exec</dataFile> + <dataFile>/target/coverage-reports/jacoco-unit.exec</dataFile> </configuration> <executions> <execution> diff --git a/src/main/java/design/controller/userinput/menus/CourseSearch.java b/src/main/java/design/controller/userinput/menus/CourseSearch.java index 4dd2cad..8fd1963 100644 --- a/src/main/java/design/controller/userinput/menus/CourseSearch.java +++ b/src/main/java/design/controller/userinput/menus/CourseSearch.java @@ -80,6 +80,10 @@ public class CourseSearch extends Menu { var name = String.format("%s, %s, Difficulty: %s, %s holes, %s total par", c.getName(), c.getLocation(), c.getDifficultyRating(), c.getHoleCount(), c.getTotalPar()); menuOptions.add(new MenuOption(name, () -> { + if(Session.isGuest()) { + System.out.println("\nYou cannot add Courses as a Guest. Please login for this functionality."); + new MainMenu().present(); + } UndoManager.instance().capture(golfer, "Add course " + c.getName()); // add the course, try to save to DB. diff --git a/src/main/java/design/controller/userinput/menus/HistoryMenu.java b/src/main/java/design/controller/userinput/menus/HistoryMenu.java new file mode 100644 index 0000000..226b331 --- /dev/null +++ b/src/main/java/design/controller/userinput/menus/HistoryMenu.java @@ -0,0 +1,30 @@ +package design.controller.userinput.menus; + +import design.controller.userinput.Menu; +import design.controller.userinput.MenuOption; +import design.controller.userinput.UndoActions; + +import java.util.List; + +public class HistoryMenu extends Menu { + + @Override + public String getTitle() { + return "History"; + } + + @Override + public List<MenuOption> getMenuOptions() { + return List.of( + new MenuOption("return to main menu", () -> new MainMenu().present()), + new MenuOption("undo", () -> { + UndoActions.undoWithSave(); + this.present(); + }), + new MenuOption("redo", () -> { + UndoActions.redoWithSave(); + this.present(); + }) + ); + } +} diff --git a/src/main/java/design/controller/userinput/menus/ImportExportMenu.java b/src/main/java/design/controller/userinput/menus/ImportExportMenu.java new file mode 100644 index 0000000..21ffa6c --- /dev/null +++ b/src/main/java/design/controller/userinput/menus/ImportExportMenu.java @@ -0,0 +1,81 @@ +package design.controller.userinput.menus; + +import design.controller.userinput.Menu; +import design.controller.userinput.MenuOption; +import design.persistence.*; +import design.persistence.importexport.DataHandler; +import design.persistence.importexport.DataSource; +import design.persistence.importexport.JSONHandler; +import design.persistence.importexport.XMLHandler; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Scanner; + +public class ImportExportMenu extends Menu { + + @Override + public String getTitle() { + return "import export menu"; + } + + @Override + public List<MenuOption> getMenuOptions() { + List<MenuOption> opts = new java.util.ArrayList<>(); + + + opts.add(new MenuOption("return to main menu", () -> new MainMenu().present())); + + opts.add(new MenuOption("import leagues...", () -> promptForPath(true, false))); + opts.add(new MenuOption("export leagues...", () -> promptForPath(false, false))); + opts.add(new MenuOption("import profiles...", () -> promptForPath(true, true))); + opts.add(new MenuOption("export profiles...", () -> promptForPath(false, true))); + + return opts; + } + + private void promptForPath(boolean isImporting, boolean isPersonalProfile) + { + System.out.print("Enter file path: "); + Scanner sc = new Scanner(System.in); + String filePath = sc.nextLine(); + File file = new File(filePath); + + DataSource source; + if (isPersonalProfile) { + source = PersonalDatabase.instance(); + } else { + source = LeagueDatabase.instance(); + } + + DataHandler handler; + String ext = getExtension(filePath); + switch (ext) { + case "json" -> handler = new JSONHandler(source); + case "xml" -> handler = new XMLHandler(source); + default -> { + System.out.println("Unsupported file type: " + ext); + this.present(); + return; + } + } + + try { + if (isImporting) { + handler.importData(file); + } else { + handler.exportData(file); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + this.present(); + } + + private static String getExtension(String fileName) { + int i = fileName.lastIndexOf('.'); + return (i >= 0) ? fileName.substring(i + 1).toLowerCase() : "unknown"; + } +} diff --git a/src/main/java/design/controller/userinput/menus/MainMenu.java b/src/main/java/design/controller/userinput/menus/MainMenu.java index 75ad88f..3f520e4 100644 --- a/src/main/java/design/controller/userinput/menus/MainMenu.java +++ b/src/main/java/design/controller/userinput/menus/MainMenu.java @@ -2,7 +2,6 @@ package design.controller.userinput.menus; import design.controller.userinput.Menu; import design.controller.userinput.MenuOption; -import design.controller.userinput.UndoActions; import design.runtime.Session; import java.util.List; @@ -15,32 +14,21 @@ public class MainMenu extends Menu { @Override public List<MenuOption> getMenuOptions() { - boolean guest = Session.isGuest(); - if (guest) { - return List.of( - new MenuOption("quit", () -> System.exit(0)), - new MenuOption("guest settings...", () -> new UserSettings().present()), - new MenuOption("browse courses...", () -> new SearchMenu().present()) - - ); + List<MenuOption> options = new java.util.ArrayList<>(); + options.add(new MenuOption("quit", () -> System.exit(0))); + if (Session.isGuest()) { + options.add(new MenuOption("guest settings...", () -> new UserSettings().present())); + options.add(new MenuOption("browse courses...", () -> new SearchMenu().present())); } else { - return List.of( - new MenuOption("quit", () -> System.exit(0)), - new MenuOption("undo", () -> { - UndoActions.undoWithSave(); - this.present(); - }), - new MenuOption("redo", () -> { - UndoActions.redoWithSave(); - this.present(); - }), - new MenuOption("user settings...", () -> new UserSettings().present()), - new MenuOption("manage courses...", () -> new ManageCourses().present()), - new MenuOption("manage clubs...", () -> new ManageClubs().present()), - new MenuOption("statistics...", () -> new StatisticsMenu().present()), - new MenuOption("log round...", () -> new HolePlayMenu().present()), - new MenuOption("League play...", () -> new SelectLeague().present()) - ); + options.add(new MenuOption("manage history...", () -> new HistoryMenu().present())); + options.add(new MenuOption("user settings...", () -> new UserSettings().present())); + options.add(new MenuOption("manage courses...", () -> new ManageCourses().present())); + options.add(new MenuOption("manage clubs...", () -> new ManageClubs().present())); + options.add(new MenuOption("statistics...", () -> new StatisticsMenu().present())); + options.add(new MenuOption("log round...", () -> new HolePlayMenu().present())); + options.add(new MenuOption("league play...", () -> new SelectLeague().present())); + options.add(new MenuOption("manage data...", () -> new ImportExportMenu().present())); } + return options; } } diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 3b4770d..4fb521e 100644 --- a/src/main/java/design/model/Golfer.java +++ b/src/main/java/design/model/Golfer.java @@ -1,6 +1,7 @@ package design.model; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import java.util.ArrayList; @@ -21,6 +22,8 @@ public class Golfer implements Originator { private final List<Invite> invites; private Team joinedTeam; + + @JsonCreator private Golfer(String username, int passwordHash, String fullName, List<Course> courses, List<Round> rounds, List<Club> clubs, List<Invite> invites, Team joinedTeam) { diff --git a/src/main/java/design/persistence/JSONLeagueDatabase.java b/src/main/java/design/persistence/JSONLeagueDatabase.java index 6f3f2d2..6446b34 100644 --- a/src/main/java/design/persistence/JSONLeagueDatabase.java +++ b/src/main/java/design/persistence/JSONLeagueDatabase.java @@ -1,14 +1,7 @@ package design.persistence; -import com.fasterxml.jackson.annotation.JsonAutoDetect; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.core.StreamReadFeature; import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import design.model.Course; import design.model.Golfer; import design.model.League; @@ -30,6 +23,12 @@ public class JSONLeagueDatabase implements LeagueDatabase { return INSTANCE; } + // static instance strictly for testing, to not add data to leaguedb.json + static JSONLeagueDatabase testInstance(String filename) { + INSTANCE = new JSONLeagueDatabase(filename); + return INSTANCE; + } + private final Map<Integer, League> cache; private final ObjectMapper mapper; private final File file; @@ -38,20 +37,10 @@ public class JSONLeagueDatabase implements LeagueDatabase { public JSONLeagueDatabase(String filename) { this.file = new File(filename); this.cache = new HashMap<>(); - this.mapper = JsonMapper.builder().enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION).build(); - - SimpleModule module = new SimpleModule(); - module.addDeserializer(Course.class, new Serializers.CourseIdDeserializer()); - module.addSerializer(Course.class, new Serializers.CourseIdSerializer()); - module.addDeserializer(Golfer.class, new Serializers.GolferUsernameDeserializer()); - module.addSerializer(Golfer.class, new Serializers.GolferUsernameSerializer()); - module.addSerializer(Map.class, new Serializers.MapListSerializer()); - module.addDeserializer(Map.class, new Serializers.MapListDeserializer()); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - mapper.registerModule(module); - mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); - mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); - mapper.registerModule(new JavaTimeModule()); + this.mapper = new ObjectMapper(); + + Serializers.configureMapper(mapper); + mapper.registerModule(this.getJacksonModule()); try { load(); @@ -101,4 +90,35 @@ public class JSONLeagueDatabase implements LeagueDatabase { cache.put(league.getId(), league); save(); } + + @Override + public void importData(Object rawData) throws IOException { + if (!(rawData instanceof League[] data)) { + throw new ClassCastException(); + } + cache.clear(); + for (League league : data) { + cache.put(league.getId(), league); + } + save(); + } + + @Override + public League[] exportData() { + return getLeagues(); + } + + @Override + public SimpleModule getJacksonModule() { + return new SimpleModule() + .addSerializer(Course.class, new Serializers.CourseIdSerializer()) + .addDeserializer(Course.class, new Serializers.CourseIdDeserializer()) + .addSerializer(Golfer.class, new Serializers.GolferUsernameSerializer()) + .addDeserializer(Golfer.class, new Serializers.GolferUsernameDeserializer()); + } + + @Override + public Class<?> getTargetClass() { + return League[].class; + } } diff --git a/src/main/java/design/persistence/JSONPersonalDatabase.java b/src/main/java/design/persistence/JSONPersonalDatabase.java index a2908f3..2f021d7 100644 --- a/src/main/java/design/persistence/JSONPersonalDatabase.java +++ b/src/main/java/design/persistence/JSONPersonalDatabase.java @@ -1,14 +1,7 @@ package design.persistence; -import com.fasterxml.jackson.annotation.JsonAutoDetect; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.core.StreamReadFeature; import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import design.model.Course; import design.model.Golfer; import design.model.League; @@ -29,6 +22,12 @@ public class JSONPersonalDatabase implements PersonalDatabase { return INSTANCE; } + // static instance strictly for testing, to not add data to personaldb.json + static JSONPersonalDatabase testInstance(String filename) { + INSTANCE = new JSONPersonalDatabase(filename); + return INSTANCE; + } + private final Map<String, Golfer> cache; private final ObjectMapper mapper; private final File file; @@ -36,23 +35,10 @@ public class JSONPersonalDatabase implements PersonalDatabase { private JSONPersonalDatabase(String filename) { this.file = new File(filename); this.cache = new HashMap<>(); - this.mapper = JsonMapper.builder().enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION).build(); - - // TODO: Once the saved JSON matches the model, consider removing. - // TEMP: tolerate unknown props while the model stabilizes - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - SimpleModule module = new SimpleModule(); - module.addDeserializer(Course.class, new Serializers.CourseIdDeserializer()); - module.addSerializer(Course.class, new Serializers.CourseIdSerializer()); - module.addSerializer(League.class, new Serializers.LeagueIDSerializer()); - module.addDeserializer(League.class, new Serializers.LeagueIDDeserializer()); - module.addSerializer(Map.class, new Serializers.MapListSerializer()); - module.addDeserializer(Map.class, new Serializers.MapListDeserializer()); - mapper.registerModule(module); - mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); - mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); - mapper.registerModule(new JavaTimeModule()); + this.mapper = new ObjectMapper(); + + Serializers.configureMapper(mapper); + mapper.registerModule(this.getJacksonModule()); try { load(); @@ -102,4 +88,35 @@ public class JSONPersonalDatabase implements PersonalDatabase { cache.put(golfer.getUsername(), golfer); save(); } + + @Override + public void importData(Object rawData) throws IOException { + if (!(rawData instanceof Golfer[] data)) { + throw new ClassCastException(); + } + cache.clear(); + for (Golfer golfer : data) { + cache.put(golfer.getUsername(), golfer); + } + save(); + } + + @Override + public Golfer[] exportData() { + return getGolfers(); + } + + @Override + public SimpleModule getJacksonModule() { + return new SimpleModule() + .addSerializer(Course.class, new Serializers.CourseIdSerializer()) + .addDeserializer(Course.class, new Serializers.CourseIdDeserializer()) + .addSerializer(League.class, new Serializers.LeagueIDSerializer()) + .addDeserializer(League.class, new Serializers.LeagueIDDeserializer()); + } + + @Override + public Class<?> getTargetClass() { + return Golfer[].class; + } } diff --git a/src/main/java/design/persistence/LeagueDatabase.java b/src/main/java/design/persistence/LeagueDatabase.java index 953624d..ed6ddae 100644 --- a/src/main/java/design/persistence/LeagueDatabase.java +++ b/src/main/java/design/persistence/LeagueDatabase.java @@ -1,10 +1,11 @@ package design.persistence; import design.model.League; +import design.persistence.importexport.DataSource; import java.io.IOException; -public interface LeagueDatabase { +public interface LeagueDatabase extends DataSource { static LeagueDatabase instance() { return JSONLeagueDatabase.instance(); } @@ -18,4 +19,10 @@ public interface LeagueDatabase { void removeLeague(League league) throws IOException; void updateLeague(League league) throws IOException; + + @Override + void importData(Object data) throws IOException; + + @Override + Object exportData() throws IOException; } diff --git a/src/main/java/design/persistence/PersonalDatabase.java b/src/main/java/design/persistence/PersonalDatabase.java index adb865d..70dd37d 100644 --- a/src/main/java/design/persistence/PersonalDatabase.java +++ b/src/main/java/design/persistence/PersonalDatabase.java @@ -1,9 +1,11 @@ package design.persistence; import design.model.Golfer; +import design.persistence.importexport.DataSource; + import java.io.IOException; -public interface PersonalDatabase { +public interface PersonalDatabase extends DataSource { static PersonalDatabase instance() { return JSONPersonalDatabase.instance(); @@ -18,4 +20,10 @@ public interface PersonalDatabase { void removeGolfer(Golfer golfer) throws IOException; void updateGolfer(Golfer golfer) throws IOException; + + @Override + void importData(Object data) throws IOException; + + @Override + Object exportData() throws IOException; } diff --git a/src/main/java/design/persistence/Serializers.java b/src/main/java/design/persistence/Serializers.java index d00dcbf..225ec7c 100644 --- a/src/main/java/design/persistence/Serializers.java +++ b/src/main/java/design/persistence/Serializers.java @@ -1,21 +1,36 @@ package design.persistence; +import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.util.DefaultIndenter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.fasterxml.jackson.databind.JsonSerializer; -import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import design.model.Course; import design.model.Golfer; import design.model.League; import java.io.IOException; import java.util.Map; +import java.time.LocalDateTime; +import java.util.List; public class Serializers { + public static void configureMapper(ObjectMapper mapper) { + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); + mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); + mapper.configOverride(List.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY)); + mapper.registerModule(new JavaTimeModule()); + mapper.registerModule(new SimpleModule() + .addSerializer(Map.class, new Serializers.MapListSerializer()) + .addDeserializer(Map.class, new Serializers.MapListDeserializer()) + ); + } + public static class CustomPrettyPrinter extends DefaultPrettyPrinter { public CustomPrettyPrinter() { super._arrayIndenter = new DefaultIndenter(); @@ -78,6 +93,13 @@ public class Serializers { } } + public static class DateTimeStringSerializer extends JsonSerializer<LocalDateTime> { + @Override + public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeString(value.toString()); + } + } + @SuppressWarnings("rawtypes") public static class MapListSerializer extends JsonSerializer<Map> { diff --git a/src/main/java/design/persistence/importexport/DataHandler.java b/src/main/java/design/persistence/importexport/DataHandler.java new file mode 100644 index 0000000..59df9be --- /dev/null +++ b/src/main/java/design/persistence/importexport/DataHandler.java @@ -0,0 +1,11 @@ +package design.persistence.importexport; + +import java.io.File; +import java.io.IOException; + +public interface DataHandler { + + void importData(File file) throws IOException; + + void exportData(File file) throws IOException; +} diff --git a/src/main/java/design/persistence/importexport/DataSource.java b/src/main/java/design/persistence/importexport/DataSource.java new file mode 100644 index 0000000..e735c7e --- /dev/null +++ b/src/main/java/design/persistence/importexport/DataSource.java @@ -0,0 +1,16 @@ +package design.persistence.importexport; + +import com.fasterxml.jackson.databind.module.SimpleModule; + +import java.io.IOException; + +public interface DataSource { + + void importData(Object data) throws IOException; + + Object exportData() throws IOException; + + SimpleModule getJacksonModule(); + + Class<?> getTargetClass(); +}
\ No newline at end of file diff --git a/src/main/java/design/persistence/importexport/JSONHandler.java b/src/main/java/design/persistence/importexport/JSONHandler.java new file mode 100644 index 0000000..9c04281 --- /dev/null +++ b/src/main/java/design/persistence/importexport/JSONHandler.java @@ -0,0 +1,35 @@ +package design.persistence.importexport; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.json.JsonMapper; +import design.persistence.Serializers; + +import java.io.File; +import java.io.IOException; + +public class JSONHandler implements DataHandler { + + private final DataSource dataSource; + private final ObjectMapper jsonMapper = new JsonMapper(); + + public JSONHandler(DataSource dataSource) { + this.dataSource = dataSource; + + Serializers.configureMapper(jsonMapper); + jsonMapper.registerModule(dataSource.getJacksonModule()); + } + + @Override + public void importData(File file) throws IOException { + Object data = jsonMapper.readValue(file, dataSource.getTargetClass()); + dataSource.importData(data); + } + + @Override + public void exportData(File file) throws IOException{ + Object data = dataSource.exportData(); + jsonMapper.writer(new Serializers.CustomPrettyPrinter()) + .writeValue(file, data); + } +} + + diff --git a/src/main/java/design/persistence/importexport/XMLHandler.java b/src/main/java/design/persistence/importexport/XMLHandler.java new file mode 100644 index 0000000..0a07d6e --- /dev/null +++ b/src/main/java/design/persistence/importexport/XMLHandler.java @@ -0,0 +1,38 @@ +package design.persistence.importexport; +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; + +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import design.persistence.Serializers; + +public class XMLHandler implements DataHandler { + + private final DataSource dataSource; + private final XmlMapper xmlMapper = new XmlMapper(); + + public XMLHandler(DataSource dataSource) { + this.dataSource = dataSource; + + Serializers.configureMapper(xmlMapper); + SimpleModule module = dataSource.getJacksonModule(); + module.addSerializer(LocalDateTime.class, new Serializers.DateTimeStringSerializer()); + xmlMapper.registerModule(module); + } + + @Override + public void importData(File file) throws IOException { + Object data = xmlMapper.readValue(file, dataSource.getTargetClass()); + dataSource.importData(data); + } + + @Override + public void exportData(File file) throws IOException { + Object data = dataSource.exportData(); + xmlMapper.writerWithDefaultPrettyPrinter() + .writeValue(file, data); + } +} + + diff --git a/src/test/java/design/TestCourseList.java b/src/test/java/design/TestCourseList.java deleted file mode 100644 index f943852..0000000 --- a/src/test/java/design/TestCourseList.java +++ /dev/null @@ -1,86 +0,0 @@ -// package design; - -// import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; - -// import org.junit.jupiter.api.Test; -// import design.model.course_search.*; -// import design.model.*; - -// import java.util.List; - -// class TestCourseList { - -// // A dummy Course implementation for testing -// private static class DummyCourse implements ICourse { -// private final String name; -// private final float difficulty; - -// public DummyCourse(String name, float difficulty) { -// this.name = name; -// this.difficulty = difficulty; -// } - -// @Override -// public String getName() { return name; } - -// @Override -// public float getDifficultyRating() { return difficulty; } - -// @Override -// public String getLocation() { return ""; } - -// @Override -// public int getTotalPar() { return 0; } - -// @Override -// public int getHoleCount() { return 0; } - -// @Override -// public List<Hole> getHoles() { return null; } -// } - -// // A simple sorter that sorts courses by difficulty -// private static class SortByDifficultyTest implements CourseSorter { -// @Override -// public void sortCourses(List<ICourse> courses) { -// courses.sort((c1, c2) -> Float.compare(c1.getDifficultyRating(), c2.getDifficultyRating())); -// } -// } - -// @Test -// public void testAddAndRemoveCourses() { -// CourseList courseList = new CourseList(); -// ICourse course1 = new DummyCourse("Course A", 2.0f); -// ICourse course2 = new DummyCourse("Course B", 5.0f); - -// courseList.add(course1); -// courseList.add(course2); - -// assertEquals(2, courseList.getCourses().size(), "Should have 2 courses after adding"); -// assertTrue(courseList.getCourses().contains(course1), "Course A should be in the list"); -// assertTrue(courseList.getCourses().contains(course2), "Course B should be in the list"); - -// courseList.remove(course1); -// assertEquals(1, courseList.getCourses().size(), "Should have 1 course after removal"); -// assertTrue(!courseList.getCourses().contains(course1), "Course A should no longer be in the list"); -// } - -// @Test -// public void testSortCourses() { -// CourseList courseList = new CourseList(); -// courseList.add(new DummyCourse("Course A", 3.0f)); -// courseList.add(new DummyCourse("Course B", 1.0f)); -// courseList.add(new DummyCourse("Course C", 2.0f)); - -// // Set sorting strategy -// courseList.setSorter(new SortByDifficultyTest()); - -// courseList.sort(); - -// List<ICourse> sorted = courseList.getCourses(); -// assertEquals("Course B", sorted.get(0).getName(), "First course should have lowest difficulty"); -// assertEquals("Course C", sorted.get(1).getName(), "Second course should have medium difficulty"); -// assertEquals("Course A", sorted.get(2).getName(), "Last course should have highest difficulty"); -// } -// }
\ No newline at end of file diff --git a/src/test/java/design/model/ClubTest.java b/src/test/java/design/model/ClubTest.java index 9ac8130..f47405f 100644 --- a/src/test/java/design/model/ClubTest.java +++ b/src/test/java/design/model/ClubTest.java @@ -1,6 +1,13 @@ package design.model; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -17,13 +24,23 @@ public class ClubTest { { Club testClub = new Club("John Doe", "The Slammer", ClubType.DRIVER); - assertEquals(0, testClub.getId()); + assertEquals(-1, testClub.getId()); assertEquals("John Doe", testClub.getManufacture()); assertEquals("The Slammer", testClub.getNickname()); assertEquals(ClubType.DRIVER, testClub.getClubType()); } @Test + void testPrivateConstructor() throws Exception + { + Constructor<Club> constructor = Club.class.getDeclaredConstructor(int.class, String.class, String.class, ClubType.class); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + Club testClub = constructor.newInstance(0, "John Doe", "The Slammer", ClubType.DRIVER); + assertNotNull(testClub); + } + + @Test void testGetClubType() { Club testClub = new Club("John Doe", "The Slammer", ClubType.DRIVER); @@ -52,10 +69,18 @@ public class ClubTest { } @Test + void testSetId() + { + Club testClub = new Club("John Doe", "The Slammer", ClubType.DRIVER); + testClub.setId(10); + assertThrows(AssertionError.class, () -> testClub.setId(5)); + } + + @Test void testToString() { Club testClub = new Club("John Doe", "The Slammer", ClubType.DRIVER); - String expectedString = "#0 The Slammer - John Doe (DRIVER)"; + String expectedString = "#-1 The Slammer - John Doe (DRIVER)"; assertEquals(expectedString, testClub.toString()); } } diff --git a/src/test/java/design/model/GolferTest.java b/src/test/java/design/model/GolferTest.java index 65229f3..83ca17b 100644 --- a/src/test/java/design/model/GolferTest.java +++ b/src/test/java/design/model/GolferTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import design.model.Club.ClubType; +import design.model.undo.Memento; import java.lang.reflect.Constructor; import java.time.LocalDateTime; @@ -35,10 +36,10 @@ public class GolferTest { @Test void testPrivateConstructor() throws Exception { - Constructor<Golfer> constructor = Golfer.class.getDeclaredConstructor(String.class, int.class, String.class, List.class, List.class, List.class); + Constructor<Golfer> constructor = Golfer.class.getDeclaredConstructor(String.class, int.class, String.class, List.class, List.class, List.class, List.class, Team.class); assertTrue(Modifier.isPrivate(constructor.getModifiers())); constructor.setAccessible(true); - Golfer testGolfer = constructor.newInstance("testUser", 12345, "Test Golfer", new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); + Golfer testGolfer = constructor.newInstance("testUser", 12345, "Test Golfer", new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null); assertNotNull(testGolfer); } @@ -137,7 +138,16 @@ public class GolferTest { assertEquals(expectedString, testGolfer.toString()); } + @Test + void testMemento() + { + Golfer testGolfer = new Golfer("John Doe", "jdoesgolf2", "weback4321"); + Memento memento = testGolfer.createMemento(); + testGolfer.setFullName("Joe Doe"); + assertEquals("Joe Doe", testGolfer.getFullName()); + testGolfer.restore(memento); + assertEquals("John Doe", testGolfer.getFullName()); + } -} - +}
\ No newline at end of file diff --git a/src/test/java/design/model/HoleTest.java b/src/test/java/design/model/HoleTest.java new file mode 100644 index 0000000..58aff49 --- /dev/null +++ b/src/test/java/design/model/HoleTest.java @@ -0,0 +1,55 @@ +package design.model; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** Tests for the hole model class. + * @author Willem Dalton + */ +@Tag("Model-tier") +public class HoleTest { + + @Test + void testValidEquals() + { + Hole hole1 = new Hole(0, 10); + Hole hole2 = new Hole(0, 10); + assertTrue(hole1.equals(hole2)); + } + + @Test + void testNotAHole() + { + Hole hole1 = new Hole(0, 10); + String notAHole = "ImNotAHole!"; + assertFalse(hole1.equals(notAHole)); + } + + @Test + void testNotEquals() + { + Hole hole1 = new Hole(0, 10); + Hole hole2 = new Hole(10, 999); + assertFalse(hole1.equals(hole2)); + } + + @Test + void testNotEqualPar() + { + Hole hole1 = new Hole(0, 10); + Hole hole2 = new Hole(0, 999); + assertFalse(hole1.equals(hole2)); + } + + @Test + void testNotEqualNumber() + { + Hole hole1 = new Hole(0, 10); + Hole hole2 = new Hole(10, 10); + assertFalse(hole1.equals(hole2)); + } +} + diff --git a/src/test/java/design/model/InviteTest.java b/src/test/java/design/model/InviteTest.java new file mode 100644 index 0000000..29388a2 --- /dev/null +++ b/src/test/java/design/model/InviteTest.java @@ -0,0 +1,38 @@ +package design.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.Date; + +/** Unit Tests for the Invite Class. + * @author Willem Dalton + **/ +@Tag("Model-tier") +public class InviteTest { + @Test + void testConstructor() + { + Golfer testOwner = new Golfer("Jane Doe", "j_doe_rocks", "1234"); + Team testTeam = new Team("A Team", testOwner); + Date testDate = new Date(123456); + Invite testInvite = new Invite(testTeam, testDate); + + assertEquals(testTeam, testInvite.getTeam()); + assertEquals(testDate, testInvite.getSendDate()); + } + + @Test + void testAccept() + { + Golfer testOwner = new Golfer("Jane Doe", "j_doe_rocks", "1234"); + Golfer testInvitee = new Golfer("James Doe", "j_doe_is_cool", "54321"); + Team testTeam = new Team("A Team", testOwner); + Date testDate = new Date(123456); + Invite testInvite = new Invite(testTeam, testDate); + testInvite.accept(testInvitee); + assertEquals(testInvitee, testTeam.getMembers()[0]); + } +} diff --git a/src/test/java/design/model/MatchTest.java b/src/test/java/design/model/MatchTest.java new file mode 100644 index 0000000..c775c11 --- /dev/null +++ b/src/test/java/design/model/MatchTest.java @@ -0,0 +1,59 @@ +package design.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +import java.util.Date; +import java.time.LocalDateTime; + +/** Unit Tests for the Match class. + * @author Willem Dalton + **/ +@Tag("Model-tier") +public class MatchTest { + + @Test + void testConstructor() + { + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester. NY", 9, 30, new ArrayList<Hole>()); + Date testDate = new Date(1234567); + LocalDateTime now = LocalDateTime.now(); + Match testMatch = new Match(testCourse, testDate, now, now, 9); + assertEquals(testCourse, testMatch.getCourse()); + assertEquals(testDate, testMatch.getDateScheduled()); + assertEquals(now, testMatch.getStart()); + assertEquals(9, testMatch.getHoleCount()); + } + + @Test + void testPrivateConstructor() throws Exception + { + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester. NY", 9, 30, new ArrayList<Hole>()); + Constructor<Match> constructor = Match.class.getDeclaredConstructor(Course.class, Date.class, LocalDateTime.class, LocalDateTime.class, int.class, List.class); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + Match testMatch = constructor.newInstance(testCourse, new Date(1234), LocalDateTime.now(), LocalDateTime.now(), 0, new ArrayList<>()); + assertNotNull(testMatch); + } + + @Test + void testAddRound() + { + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester. NY", 9, 30, new ArrayList<Hole>()); + Date testDate = new Date(1234567); + LocalDateTime now = LocalDateTime.now(); + Match testMatch = new Match(testCourse, testDate, now, now, 9); + Round testRound = new Round(testCourse, now, new Hole(0, 5)); + testMatch.addRound(testRound); + assertEquals(testRound, testMatch.getRounds()[0]); + assertEquals(1, testMatch.getRounds().length); + } +} diff --git a/src/test/java/design/model/PlayTest.java b/src/test/java/design/model/PlayTest.java new file mode 100644 index 0000000..0a27076 --- /dev/null +++ b/src/test/java/design/model/PlayTest.java @@ -0,0 +1,43 @@ +package design.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import design.model.Club.ClubType; + +import java.util.ArrayList; + +/** Unit Tests for the Play class. + * @author Willem Dalton + **/ +@Tag("Model-tier") +public class PlayTest { + @Test + void testConstructor() + { + Play testPlay = new Play(0); + assertEquals(0, testPlay.getHoleNumber()); + } + + @Test + void testConstructorNull() + { + Play testPlay2 = new Play(0, null); + assertEquals(0, testPlay2.getHoleNumber()); + assertEquals(0, testPlay2.getSwings().length); + } + + @Test + void testConstructorNotNull() + { + Club newClub = new Club("John Doe Inc", "The Slammer", ClubType.DRIVER); + Swing newSwing = new Swing(100, newClub); + ArrayList<Swing> swings = new ArrayList<Swing>(); + swings.add(newSwing); + Play testPlay3 = new Play(0, swings); + assertEquals(0, testPlay3.getHoleNumber()); + assertEquals(swings.get(0), testPlay3.getSwings()[0]); + } +} diff --git a/src/test/java/design/model/RoundTest.java b/src/test/java/design/model/RoundTest.java index d472f5b..76d8f47 100644 --- a/src/test/java/design/model/RoundTest.java +++ b/src/test/java/design/model/RoundTest.java @@ -1,12 +1,19 @@ package design.model; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import design.model.Club.ClubType; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.List; /** Unit Tests for the Round class. * @author Willem Dalton @@ -27,6 +34,18 @@ public class RoundTest { } @Test + void testPrivateConstructor() throws Exception + { + Course testCourse = new Course(0, "Rolling Waves", 62, "Rochester, NY", 9, 20, new ArrayList<Hole>()); + Constructor<Round> constructor = Round.class.getDeclaredConstructor(Course.class, LocalDateTime.class, Hole.class, List.class); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + Round testRound = constructor.newInstance(testCourse, LocalDateTime.now(), new Hole(0, 0), new ArrayList<Play>()); + assertNotNull(testRound); + } + + + @Test void testHolePlay() { Course testCourse = new Course(0, "Rolling Waves", 62, "Rochester, NY", 9, 20, new ArrayList<Hole>()); @@ -36,7 +55,38 @@ public class RoundTest { Play testPlay = new Play(0); testRound.addPlay(testPlay); + assertEquals(1, testRound.getPlays().length); assertEquals(testPlay, testRound.getPlays()[0]); + assertEquals(0, testRound.getTotalSwings()); + + Club newClub = new Club("John Doe Inc", "The Slammer", ClubType.DRIVER); + Swing newSwing = new Swing(200, newClub); + + // try swinging, expect to see another swing added. + testPlay.addSwing(newSwing); + + assertEquals(1, testRound.getTotalSwings()); + assertEquals(200, testRound.getTotalDistance()); + } + + @Test + void testProgressHole() + { + Hole testHole = new Hole(0,3); + Hole nextHole = new Hole(1, 5); + + ArrayList<Hole> testHoles = new ArrayList<Hole>(); + testHoles.add(testHole); + testHoles.add(nextHole); + + Course testCourse = new Course(0, "Rolling Waves", 62, "Rochester, NY", 9, 20, testHoles); + LocalDateTime testTime = LocalDateTime.now(); + Round testRound = new Round(testCourse, testTime, testHole); + + // progress a Hole and check value. + assertEquals(testHole, testRound.getCurrentHole()); + testRound.nextHole(); + assertEquals(nextHole, testRound.getCurrentHole()); } } diff --git a/src/test/java/design/model/ScrambleLeagueTest.java b/src/test/java/design/model/ScrambleLeagueTest.java new file mode 100644 index 0000000..e8fe63c --- /dev/null +++ b/src/test/java/design/model/ScrambleLeagueTest.java @@ -0,0 +1,89 @@ +package design.model; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** Tests for the scramble league model class. + * @author Willem Dalton + */ +@Tag("Model-tier") +public class ScrambleLeagueTest { + + @Test + void testConstructor() + { + Date testStart = new Date(1234567); + Date testRegistration = new Date(1234568); + Date testEnd = new Date(12345679); + Golfer testOwner = new Golfer("Bobby", "bobby123", "iloveswen262"); + ScrambleLeague testLeague = new ScrambleLeague("Bobby's Band", testRegistration, testStart, testEnd, testOwner); + assertEquals(-1, testLeague.getId()); + assertEquals("Bobby's Band", testLeague.getName()); + assertEquals(testRegistration, testLeague.getRegistrationDate()); + assertEquals(testStart, testLeague.getStartDate()); + assertEquals(testEnd, testLeague.getEndDate()); + assertEquals(testOwner, testLeague.getOwner()); + } + + @Test + void testPrivateConstructor() throws Exception + { + Golfer testOwner = new Golfer("Bobby", "bobby123", "iloveswen262"); + Constructor<ScrambleLeague> constructor = ScrambleLeague.class.getDeclaredConstructor(int.class, String.class, Date.class, Date.class, Date.class, Golfer.class, List.class, List.class); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + ScrambleLeague testLeague = constructor.newInstance(1, "Bobby's Band", new Date(), new Date(), new Date(), testOwner, new ArrayList<Team>(), new ArrayList<Match>()); + assertNotNull(testLeague); + } + + @Test + void testParticipants() throws Exception + { + Date testStart = new Date(1234567); + Date testRegistration = new Date(1234568); + Date testEnd = new Date(12345679); + Golfer testOwner = new Golfer("Bobby", "bobby123", "iloveswen262"); + ScrambleLeague testLeague = new ScrambleLeague("Bobby's Band", testRegistration, testStart, testEnd, testOwner); + Team testTeam = new Team("The A Team", testOwner); + + // add and remove participant + testLeague.addParticipants(testTeam); + assertEquals(testTeam, testLeague.getParticipants()[0]); + assertEquals(1, testLeague.getParticipants().length); + testLeague.removeParticipants(testTeam); + assertEquals(0, testLeague.getParticipants().length); + } + + @Test + void testLocateTeam() throws Exception + { + Date testStart = new Date(1234567); + Date testRegistration = new Date(1234568); + Date testEnd = new Date(12345679); + Golfer testOwner = new Golfer("Bobby", "bobby123", "iloveswen262"); + ScrambleLeague testLeague = new ScrambleLeague("Bobby's Band", testRegistration, testStart, testEnd, testOwner); + + Team testTeam = new Team("The A Team", testOwner); + Golfer teamMember = new Golfer("Jane Doe", "jane_doe", "weback"); + Golfer nonMember = new Golfer("NAN doe", "nan_doe", "wenotback"); + + // expect a result if member is part of team. expect null if they are not. + testTeam.addMember(teamMember); + testLeague.addParticipants(testTeam); + Team result = testLeague.locateTeam(teamMember); + assertEquals(result, testTeam); + result = testLeague.locateTeam(nonMember); + assertNull(result); + } +} diff --git a/src/test/java/design/model/StrokeLeagueTest.java b/src/test/java/design/model/StrokeLeagueTest.java new file mode 100644 index 0000000..8af7561 --- /dev/null +++ b/src/test/java/design/model/StrokeLeagueTest.java @@ -0,0 +1,65 @@ +package design.model; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** Tests for the stroke league model class. + * @author Willem Dalton + */ +@Tag("Model-tier") +public class StrokeLeagueTest { + + @Test + void testConstructor() + { + Date testStart = new Date(1234567); + Date testRegistration = new Date(1234568); + Date testEnd = new Date(12345679); + Golfer testOwner = new Golfer("Bobby", "bobby123", "iloveswen262"); + StrokeLeague testLeague = new StrokeLeague("Bobby's Band", testRegistration, testStart, testEnd, testOwner); + assertEquals(-1, testLeague.getId()); + assertEquals("Bobby's Band", testLeague.getName()); + assertEquals(testRegistration, testLeague.getRegistrationDate()); + assertEquals(testStart, testLeague.getStartDate()); + assertEquals(testEnd, testLeague.getEndDate()); + assertEquals(testOwner, testLeague.getOwner()); + } + + @Test + void testPrivateConstructor() throws Exception + { + Golfer testOwner = new Golfer("Bobby", "bobby123", "iloveswen262"); + Constructor<StrokeLeague> constructor = StrokeLeague.class.getDeclaredConstructor(int.class, String.class, Date.class, Date.class, Date.class, Golfer.class, List.class, List.class); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + StrokeLeague testLeague = constructor.newInstance(1, "Bobby's Band", new Date(), new Date(), new Date(), testOwner, new ArrayList<Golfer>(), new ArrayList<Match>()); + assertNotNull(testLeague); + } + + @Test + void testParticipants() throws Exception + { + Date testStart = new Date(1234567); + Date testRegistration = new Date(1234568); + Date testEnd = new Date(12345679); + Golfer testOwner = new Golfer("Bobby", "bobby123", "iloveswen262"); + StrokeLeague testLeague = new StrokeLeague("Bobby's Band", testRegistration, testStart, testEnd, testOwner); + + // add and remove participant + testLeague.addParticipants(testOwner); + assertEquals(testOwner, testLeague.getParticipants()[0]); + assertEquals(1, testLeague.getParticipants().length); + testLeague.removeParticipants(testOwner); + assertEquals(0, testLeague.getParticipants().length); + } +} diff --git a/src/test/java/design/model/TeamTest.java b/src/test/java/design/model/TeamTest.java new file mode 100644 index 0000000..c81aa1e --- /dev/null +++ b/src/test/java/design/model/TeamTest.java @@ -0,0 +1,42 @@ +package design.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** Unit Tests for the Team Class. + * @author Willem Dalton + **/ +@Tag("Model-tier") +public class TeamTest { + @Test + void testConstructor() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe_golfs", "ilovegolf123"); + Team testTeam = new Team("A Team", testGolfer); + assertEquals("A Team", testTeam.getName()); + assertEquals(testGolfer, testTeam.getOwner()); + } + + @Test + void testSetName() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe_golfs", "ilovegolf123"); + Team testTeam = new Team("A Team", testGolfer); + testTeam.setName("B Team"); + assertEquals("B Team", testTeam.getName()); + } + + @Test + void testRemoveMember() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe_golfs", "ilovegolf123"); + Golfer newGolfer = new Golfer("Jane Doe", "j_doe_golfs2", "ilovegolf321"); + Team testTeam = new Team("A Team", testGolfer); + testTeam.addMember(newGolfer); + assertEquals(1, testTeam.getMembers().length); + testTeam.removeMember(newGolfer); + assertEquals(0, testTeam.getMembers().length); + } +} diff --git a/src/test/java/design/model/coursesearch/CourseListTest.java b/src/test/java/design/model/coursesearch/CourseListTest.java index 7538ba5..6bc5529 100644 --- a/src/test/java/design/model/coursesearch/CourseListTest.java +++ b/src/test/java/design/model/coursesearch/CourseListTest.java @@ -1,18 +1,19 @@ package design.model.coursesearch; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.ArrayList; +import design.model.course_search.ICourse; import design.model.course_search.*; import design.model.Course; -/** Unit Tests for the Club class. +/** Unit Tests for the Course List class. * @author Willem Dalton **/ @Tag("Model-tier") @@ -22,6 +23,61 @@ public class CourseListTest { void testConstructor() { CourseList testList = new CourseList(); + assertNotNull(testList); + } + + @Test + void testSetCourses() + { + CourseList testList = new CourseList(); + Course testCourse1 = new Course(0, "Rolling Waves", 10, "Rochester, NY", 0, 0, null); + Course testCourse2 = new Course(1, "Arcadia Hills", 10, "Rochester, NY", 0, 0, null); + ArrayList<ICourse> courses = new ArrayList<ICourse>(); + courses.add(testCourse1); + courses.add(testCourse2); + testList.setCourses(courses); + assertEquals(courses, testList.getCourses()); + } + @Test + void testSort() + { + CourseList testList = new CourseList(); + Course testCourse1 = new Course(0, "Rolling Waves", 10, "Rochester, NY", 0, 0, null); + Course testCourse2 = new Course(1, "Arcadia Hills", 10, "Rochester, NY", 0, 0, null); + ArrayList<ICourse> courses = new ArrayList<ICourse>(); + courses.add(testCourse1); + courses.add(testCourse2); + testList.setCourses(courses); + SortByName sorter = new SortByName(); + testList.setSorter(sorter); + testList.sort(); + assertEquals(testCourse2, testList.getCourses().get(0)); + assertEquals(testCourse1, testList.getCourses().get(1)); + } + + @Test + void testAddRemove() + { + CourseList testList = new CourseList(); + Course testCourse1 = new Course(0, "Rolling Waves", 10, "Rochester, NY", 0, 0, null); + testList.add(testCourse1); + assertEquals(testCourse1, testList.getCourses().get(0)); + assertEquals(1, testList.getCourses().size()); + testList.remove(testCourse1); + assertEquals(0, testList.getCourses().size()); + } + + @Test + void testOverrides() + { + CourseList testList = new CourseList(); + assertEquals("Course List", testList.getName()); + assertEquals("Course List", testList.toString()); + assertEquals(0, testList.getDifficultyRating()); + assertEquals("", testList.getLocation()); + assertEquals(0, testList.getTotalPar()); + assertEquals(0, testList.getHoleCount()); + assertNull(testList.getHoles()); } } diff --git a/src/test/java/design/model/coursesearch/CurrentSearchQueryTest.java b/src/test/java/design/model/coursesearch/CurrentSearchQueryTest.java new file mode 100644 index 0000000..10f3f87 --- /dev/null +++ b/src/test/java/design/model/coursesearch/CurrentSearchQueryTest.java @@ -0,0 +1,27 @@ +package design.model.coursesearch; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import java.util.ArrayList; + +import design.model.course_search.*; +import design.model.Course; + +/** Unit Tests for the Current Search Query Class + * @author Willem Dalton + **/ +@Tag("Model-tier") +public class CurrentSearchQueryTest { + + // @Test + // void testReset() + // { + // CurrentSearchQuery query + // } + + +} diff --git a/src/test/java/design/model/coursesearch/sortStrategyTest.java b/src/test/java/design/model/coursesearch/sortStrategyTest.java index aecbeff..35fa335 100644 --- a/src/test/java/design/model/coursesearch/sortStrategyTest.java +++ b/src/test/java/design/model/coursesearch/sortStrategyTest.java @@ -11,10 +11,11 @@ import java.util.ArrayList; import design.model.course_search.*; import design.model.Course; -/** Unit Tests for the Difficulty Sorter class +/** Unit Tests for the Sorter Strategy classes * @author Willem Dalton **/ -public class sortStrategyTest { +@Tag("Model-tier") +public class SortStrategyTest { Course test1 = new Course(0, "Rolling Waves", 67, "Rochester, NY", 9, 30, null); Course test2 = new Course(1, "Balls in the Rough", 60, "Buffalo, NY", 18, 60, null); @@ -25,7 +26,7 @@ public class sortStrategyTest { void testDifficultySort() { SortByDifficulty sorter = new SortByDifficulty(); - ArrayList<ICourse> courses = new ArrayList(); + ArrayList<ICourse> courses = new ArrayList<ICourse>(); courses.add(test1); courses.add(test2); sorter.sortCourses(courses); @@ -38,7 +39,7 @@ public class sortStrategyTest { void testNameSort() { SortByName sorter = new SortByName(); - ArrayList<ICourse> courses = new ArrayList(); + ArrayList<ICourse> courses = new ArrayList<ICourse>(); courses.add(test1); courses.add(test4); sorter.sortCourses(courses); @@ -51,7 +52,7 @@ public class sortStrategyTest { void testLocationSort() { SortByLocation sorter = new SortByLocation(); - ArrayList<ICourse> courses = new ArrayList(); + ArrayList<ICourse> courses = new ArrayList<ICourse>(); courses.add(test1); courses.add(test4); sorter.sortCourses(courses); @@ -64,7 +65,7 @@ public class sortStrategyTest { void testParSort() { SortByPar sorter = new SortByPar(); - ArrayList<ICourse> courses = new ArrayList(); + ArrayList<ICourse> courses = new ArrayList<ICourse>(); courses.add(test1); courses.add(test4); sorter.sortCourses(courses); @@ -77,7 +78,7 @@ public class sortStrategyTest { void testHoleSort() { SortByHoles sorter = new SortByHoles(); - ArrayList<ICourse> courses = new ArrayList(); + ArrayList<ICourse> courses = new ArrayList<ICourse>(); courses.add(test1); courses.add(test4); sorter.sortCourses(courses); diff --git a/src/test/java/design/model/holeplay/HolePlayContextTest.java b/src/test/java/design/model/holeplay/HolePlayContextTest.java index de0f1e9..c2ca619 100644 --- a/src/test/java/design/model/holeplay/HolePlayContextTest.java +++ b/src/test/java/design/model/holeplay/HolePlayContextTest.java @@ -1,5 +1,74 @@ package design.model.holeplay; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.Date; +import java.time.LocalDateTime; +import java.util.ArrayList; + +import design.model.Golfer; +import design.model.Round; +import design.model.Club.ClubType; +import design.persistence.PersonalDatabase; +import design.model.Course; +import design.model.Hole; +import design.model.Club; + +/** Unit Tests for the HolePlayContext Class. + * @author Willem Dalton + **/ +@Tag("Model-tier") public class HolePlayContextTest { + @Test + void testConstructor() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + HolePlayContext testContext = new HolePlayContext(testGolfer, testRound, PersonalDatabase.instance()); + assertEquals(testGolfer, testContext.getGolfer()); + assertEquals(testRound, testContext.getRound()); + } + + @Test + void testPlay() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + HolePlayContext testContext = new HolePlayContext(testGolfer, testRound, PersonalDatabase.instance()); + testContext.beginNewPlay(0); + Club testClub = new Club("John Doe Inc", "The Slammer", ClubType.DRIVER); + testContext.addSwing(testClub, 100); + assertEquals(1, testContext.getCurrentPlay().getSwingCount()); + assertEquals(100, testContext.getCurrentPlay().getDistance()); + } + + @Test + void testPlayNull() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + HolePlayContext testContext = new HolePlayContext(testGolfer, testRound, PersonalDatabase.instance()); + testContext.beginNewPlay(0); + Club testClub = new Club("John Doe Inc", "The Slammer", ClubType.DRIVER); + testContext.addSwing(testClub, null); + assertEquals(1, testContext.getCurrentPlay().getSwingCount()); + assertEquals(0, testContext.getCurrentPlay().getDistance()); + } + + // @Test + // void testHolePlay() + // { + // Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + // Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + // Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + // HolePlayContext testContext = new HolePlayContext(testGolfer, testRound, PersonalDatabase.instance()); + // testContext.startHole(); + // } }
\ No newline at end of file diff --git a/src/test/java/design/model/statistics/StatisticsTest.java b/src/test/java/design/model/statistics/StatisticsTest.java new file mode 100644 index 0000000..e68796a --- /dev/null +++ b/src/test/java/design/model/statistics/StatisticsTest.java @@ -0,0 +1,180 @@ +package design.model.statistics; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; + +import design.model.Course; +import design.model.Golfer; +import design.model.Hole; +import design.model.Round; +import design.model.Club.ClubType; +import design.model.Play; +import design.model.Club; +import design.model.Swing; + +/** Unit Tests for the Statistics Subsystem + * @author Willem Dalton + **/ +@Tag("Model-tier") +public class StatisticsTest { + + /* base stats wrapper for testing purposes */ + private static class StubStatistics implements Statistics { + private final Golfer golfer; + + public StubStatistics(Golfer golfer) { + this.golfer = golfer; + } + + @Override + public Round[] getRounds() { + return golfer.getRounds(); + } + + @Override + public int get_score() { + return Arrays.stream(getRounds()) + .mapToInt(Round::getTotalSwings) + .sum(); + } + + @Override + public double get_distance() { + return Arrays.stream(getRounds()) + .mapToDouble(Round::getTotalDistance) + .sum(); + } + } + + @Test + void testLifeTime() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); + LifetimeStats stats = new LifetimeStats(testGolfer); + + assertNotNull(stats); + assertEquals(0, stats.get_score()); + assertEquals(0, stats.get_distance()); + + ArrayList<Swing> testSwings = new ArrayList<Swing>(); + testSwings.add(testSwing); + Play testPlay = new Play(0, testSwings); + testRound.addPlay(testPlay); + testGolfer.addRound(testRound); + + assertEquals(1, stats.get_score()); + assertEquals(100, stats.get_distance()); + } + + @Test + void testYearly() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); + + Statistics wrapper = new StubStatistics(testGolfer); + YearlyStats stats = new YearlyStats(wrapper, 2025); + + assertNotNull(stats); + assertEquals(0, stats.get_score()); + assertEquals(0, stats.get_distance()); + + ArrayList<Swing> testSwings = new ArrayList<Swing>(); + testSwings.add(testSwing); + Play testPlay = new Play(0, testSwings); + testRound.addPlay(testPlay); + testGolfer.addRound(testRound); + + assertEquals(1, stats.get_score()); + assertEquals(100, stats.get_distance()); + } + + @Test + void testCourse() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); + + Statistics wrapper = new StubStatistics(testGolfer); + CourseStats stats = new CourseStats(wrapper, testCourse); + + assertNotNull(stats); + assertEquals(0, stats.get_score()); + assertEquals(0, stats.get_distance()); + + ArrayList<Swing> testSwings = new ArrayList<Swing>(); + testSwings.add(testSwing); + Play testPlay = new Play(0, testSwings); + testRound.addPlay(testPlay); + testGolfer.addRound(testRound); + + assertEquals(1, stats.get_score()); + assertEquals(100, stats.get_distance()); + } + + @Test + void testRound() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); + + Statistics wrapper = new StubStatistics(testGolfer); + RoundStats stats = new RoundStats(wrapper, testRound); + + assertNotNull(stats); + assertEquals(0, stats.get_score()); + assertEquals(0, stats.get_distance()); + + ArrayList<Swing> testSwings = new ArrayList<Swing>(); + testSwings.add(testSwing); + Play testPlay = new Play(0, testSwings); + testRound.addPlay(testPlay); + testGolfer.addRound(testRound); + + assertEquals(1, stats.get_score()); + assertEquals(100, stats.get_distance()); + } + + @Test + void testHole() + { + Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); + Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList<Hole>()); + Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); + Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); + Hole testHole = new Hole(0, 10); + + Statistics wrapper = new StubStatistics(testGolfer); + HoleStats stats = new HoleStats(wrapper, testHole); + + assertNotNull(stats); + assertEquals(0, stats.get_score()); + assertEquals(0, stats.get_distance()); + + ArrayList<Swing> testSwings = new ArrayList<Swing>(); + testSwings.add(testSwing); + Play testPlay = new Play(0, testSwings); + testRound.addPlay(testPlay); + testGolfer.addRound(testRound); + + assertEquals(1, stats.get_score()); + assertEquals(100, stats.get_distance()); + assertEquals(0, stats.getRounds().length); + } +} diff --git a/src/test/java/design/persistence/CSVMasterDatabaseTest.java b/src/test/java/design/persistence/CSVMasterDatabaseTest.java new file mode 100644 index 0000000..4c78800 --- /dev/null +++ b/src/test/java/design/persistence/CSVMasterDatabaseTest.java @@ -0,0 +1,33 @@ +package design.persistence; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** Unit Tests for the CSV Master Database Singleton class. + * @author Willem Dalton + **/ +@Tag("Persistence-tier") +public class CSVMasterDatabaseTest { + + @Test + void testInstance() + { + CSVMasterDatabase instance = CSVMasterDatabase.instance(); + assertNotNull(instance); + CSVMasterDatabase instance2 = CSVMasterDatabase.instance(); + assertNotNull(instance2); + } + + @Test + void testGetCourses() + { + CSVMasterDatabase instance = CSVMasterDatabase.instance(); + String expectedResult = "Mountain View Links (Mobile, AL) | Holes: 18 | Total Par: 70 | Difficulty: 73.0"; + assertEquals(1000, instance.getCourses().length); + assertNotNull(instance.getCourseList()); + assertEquals(expectedResult, instance.getCourse(0).toString()); + } +} diff --git a/src/test/java/design/persistence/JSONLeagueDatabaseTest.java b/src/test/java/design/persistence/JSONLeagueDatabaseTest.java new file mode 100644 index 0000000..f211670 --- /dev/null +++ b/src/test/java/design/persistence/JSONLeagueDatabaseTest.java @@ -0,0 +1,75 @@ +package design.persistence; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Date; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import design.model.League; +import design.model.StrokeLeague; +import design.model.Course; +import design.model.Golfer; +import design.model.Hole; +import design.model.Match; + +/** Unit Tests for the JSON Personal Database Singleton + * @author Willem Dalton + **/ +@Tag("Persistence-tier") +public class JSONLeagueDatabaseTest { + Path tempDB; + + @BeforeEach + void clearDB() throws IOException + { + tempDB = Files.createTempFile("testleaguedb", ".json"); + Files.writeString(tempDB, "[]"); + JSONLeagueDatabase.testInstance(tempDB.toString()); + } + + @Test + void testInstance() + { + JSONLeagueDatabase instance = JSONLeagueDatabase.instance(); // makes new instance + assertNotNull(instance); + JSONLeagueDatabase instance2 = JSONLeagueDatabase.instance(); // instance already exists + assertNotNull(instance2); + } + + @Test + void testAddRemove() throws IOException + { + JSONLeagueDatabase instance = JSONLeagueDatabase.testInstance(tempDB.toString()); // makes new instance + Golfer testOwner = new Golfer("Jamie Doe", "joe_cool", "12345"); + League testLeague = new StrokeLeague("The A Team", new Date(1234), new Date(123), new Date(12345), testOwner); + instance.addLeague(testLeague); + assertEquals(1, instance.getLeagues().length); + assertEquals(testLeague, instance.getLeagues()[0]); + instance.removeLeague(testLeague); + assertEquals(0, instance.getLeagues().length); + } + + @Test + void testUpdateGolfer() throws IOException + { + JSONLeagueDatabase instance = JSONLeagueDatabase.testInstance(tempDB.toString()); // makes new instance + Golfer testOwner = new Golfer("Jamie Doe", "joe_cool", "12345"); + League testLeague = new StrokeLeague("The A Team", new Date(1234), new Date(123), new Date(12345), testOwner); + instance.addLeague(testLeague); + Course testCourse = new Course(0, "Rolling Waves", 62, "Rochester, NY", 9, 20, new ArrayList<Hole>()); + Match testMatch = new Match(testCourse, new Date(123), LocalDateTime.now(), LocalDateTime.now(), 3); + testLeague.addMatchToSchedule(testMatch); + instance.updateLeague(testLeague); + assertEquals(testLeague, instance.getLeague(testLeague.getId())); + } +} diff --git a/src/test/java/design/persistence/JSONPersonalDatabaseTest.java b/src/test/java/design/persistence/JSONPersonalDatabaseTest.java new file mode 100644 index 0000000..21cc366 --- /dev/null +++ b/src/test/java/design/persistence/JSONPersonalDatabaseTest.java @@ -0,0 +1,62 @@ +package design.persistence; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import design.model.Golfer; + +/** Unit Tests for the JSON Personal Database Singleton + * @author Willem Dalton + **/ +@Tag("Persistence-tier") +public class JSONPersonalDatabaseTest { + Path tempDB; + + @BeforeEach + void clearDB() throws IOException + { + tempDB = Files.createTempFile("testdb", ".json"); + Files.writeString(tempDB, "[]"); + JSONPersonalDatabase.testInstance(tempDB.toString()); + } + + @Test + void testInstance() + { + JSONPersonalDatabase instance = JSONPersonalDatabase.instance(); // makes new instance + assertNotNull(instance); + JSONPersonalDatabase instance2 = JSONPersonalDatabase.instance(); // instance already exists + assertNotNull(instance2); + } + + @Test + void testAddRemove() throws IOException + { + JSONPersonalDatabase instance = JSONPersonalDatabase.testInstance(tempDB.toString()); // makes new instance + Golfer testGolfer = new Golfer("Jamie Doe", "joe_cool", "12345"); + instance.addGolfer(testGolfer); + assertEquals(testGolfer, instance.getGolfer("joe_cool")); + instance.removeGolfer(testGolfer); + assertEquals(null, instance.getGolfer("joe_cool")); + } + + @Test + void testUpdateGolfer() throws IOException + { + JSONPersonalDatabase instance = JSONPersonalDatabase.testInstance(tempDB.toString()); // makes new instance + Golfer testGolfer = new Golfer("Jamie Doe", "joe_cool", "12345"); + instance.addGolfer(testGolfer); + testGolfer.setUsername("joe_super_cool"); + instance.updateGolfer(testGolfer); + assertEquals(testGolfer, instance.getGolfer("joe_super_cool")); + } +} |
