diff --git a/build.gradle.kts b/build.gradle.kts index e8988d2..2786744 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,6 +14,37 @@ * limitations under the License. */ +// Comamnd-line arguments + +var testTarget = "unittests" +if (project.hasProperty("testDir")) { + testTarget = project.property("testDir").toString() +} + +var jvmVersion = JavaVersion.VERSION_21 +if (testTarget == "evosuitetests" || + gradle.startParameter.taskNames.contains("generateEvoSuiteTests")) { + + jvmVersion = JavaVersion.VERSION_1_8 +} + +var junitVersion = 5 +if (testTarget == "evosuitetests") { + junitVersion = 4 +} + +if (JavaVersion.current() != jvmVersion) { + throw GradleException( + "Wrong java version for this task: use Java 8 for EvoSuite and 21 for everything else" + ) +} + +// Build script configuration + +val evoSuiteDownload by configurations.creating { + isTransitive = true +} + // Project configuration plugins { @@ -27,52 +58,45 @@ repositories { } dependencies { - testImplementation("org.junit.jupiter:junit-jupiter:5.12.1") - testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.12.1") + // JUnit 4 + testImplementation("junit:junit:4.13.2") + + // JUnit 5 + testImplementation("org.junit.jupiter:junit-jupiter:5.12.2") + testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.12.2") + + // EvoSuite Runtime + testImplementation("org.evosuite:evosuite-standalone-runtime:1.0.6") + evoSuiteDownload("org.evosuite:evosuite-master:1.0.6") } java { - toolchain { - languageVersion = JavaLanguageVersion.of(21) - } + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 } application { mainClass = "MakeItFit.Main" } -jacoco { - toolVersion = "0.8.13" -} - // Test configuration -var testTarget = "unittests" -if (project.hasProperty("testDir")) { - testTarget = project.property("testDir").toString() -} - java.sourceSets["test"].java { srcDir("src/${testTarget}/java") } tasks.named("test") { - useJUnitPlatform() - finalizedBy(tasks.jacocoTestReport) -} - -// Other tasks + if (junitVersion == 5) { + useJUnitPlatform () + } -tasks.named("run") { - standardInput = System.`in` + finalizedBy(tasks.jacocoTestReport) } -tasks.register("format") { - workingDir = file(rootDir) - commandLine = listOf("sh", "-c", "find src -type f | xargs -n1 sh -c 'clang-format -i $0; sed -i s/\\\\r//g $0'") +jacoco { + toolVersion = "0.8.13" } -// PIT Mutation Testing pitest { junit5PluginVersion.set("1.2.2") pitestVersion.set("1.19.1") @@ -84,3 +108,37 @@ pitest { outputFormats.set(setOf("HTML")) timestampedReports.set(false) } + +tasks.register("generateEvoSuiteTests") { + classpath = evoSuiteDownload + mainClass = "org.evosuite.EvoSuite" + args = listOf( + "-target", + layout.buildDirectory.dir("classes/java/main").get().asFile.path, + "-Duse_separate_classloader=false", + "-seed", + "1" + ) + + doLast { + project.delete(files("evosuite-report")) + project.delete(files("src/evosuitetests/java")) + File("src/evosuitetests/java").mkdirs() + File("evosuite-tests").renameTo(File("src/evosuitetests/java")) + } + + finalizedBy(tasks.named("format")) +} + +// Other tasks + +tasks.named("run") { + standardInput = System.`in` +} + +tasks.register("format") { + workingDir = file(rootDir) + commandLine = listOf("sh", "-c", + "find src -type f | xargs -n1 sh -c 'clang-format -i $0; sed -i s/\\\\r//g $0'" + ) +} diff --git a/src/main/java/MakeItFit/queries/MostDoneActivity.java b/src/main/java/MakeItFit/queries/MostDoneActivity.java index 7d9cf51..406c610 100644 --- a/src/main/java/MakeItFit/queries/MostDoneActivity.java +++ b/src/main/java/MakeItFit/queries/MostDoneActivity.java @@ -43,13 +43,26 @@ public String executeQuery(UserManager userManager) { } } int maxIndex = max(count); - return switch (maxIndex) { - case 0 -> "DistanceWithAltimetry"; - case 1 -> "Distance"; - case 2 -> "Repetitions"; - case 3 -> "RepetitionsWithWeights"; - default -> "No activities"; + + String mostDoneActivity; + switch (maxIndex) { + case 0: + mostDoneActivity = "DistanceWithAltimetry"; + break; + case 1: + mostDoneActivity = "Distance"; + break; + case 2: + mostDoneActivity = "Repetitions"; + break; + case 3: + mostDoneActivity = "RepetitionsWithWeights"; + break; + default: + mostDoneActivity = "No activities"; + break; }; + return mostDoneActivity; } /** diff --git a/src/main/java/MakeItFit/trainingPlan/TrainingPlanManager.java b/src/main/java/MakeItFit/trainingPlan/TrainingPlanManager.java index 2118ef1..618a42a 100644 --- a/src/main/java/MakeItFit/trainingPlan/TrainingPlanManager.java +++ b/src/main/java/MakeItFit/trainingPlan/TrainingPlanManager.java @@ -8,6 +8,7 @@ import MakeItFit.activities.HardInterface; import MakeItFit.activities.implementation.*; import MakeItFit.exceptions.EntityDoesNotExistException; +import MakeItFit.utils.ExtendedRandom; import MakeItFit.utils.MakeItFitDate; import MakeItFit.utils.MyTuple; @@ -82,7 +83,7 @@ public TrainingPlan constructTrainingPlanByObjectives(TrainingPlan trainingPlan, ActivityManager activityManager = new ActivityManager(); MakeItFitDate currentDate = trainingPlan.getStartDate(); - Random random = new Random(); + ExtendedRandom random = new ExtendedRandom(); Set differentActivityTypes = new HashSet<>(); @@ -97,7 +98,7 @@ public TrainingPlan constructTrainingPlanByObjectives(TrainingPlan trainingPlan, } switch (activityType) { - case "PushUp" -> { + case "PushUp": { int expectedDuration = random.nextInt(10, 40); int repetitions = random.nextInt(5, 20); int series = random.nextInt(1, 5); @@ -108,8 +109,9 @@ public TrainingPlan constructTrainingPlanByObjectives(TrainingPlan trainingPlan, activityType, repetitions, series); - } - case "Running" -> { + } break; + + case "Running": { int expectedDuration = random.nextInt(10, 40); double distance = random.nextInt(500, 5000); double speed = random.nextInt(5, 20); @@ -120,8 +122,9 @@ public TrainingPlan constructTrainingPlanByObjectives(TrainingPlan trainingPlan, activityType, distance, speed); - } - case "Trail" -> { + } break; + + case "Trail": { int expectedDuration = random.nextInt(10, 40); int distance = random.nextInt(500, 5000); int elevationGain = random.nextInt(0, 800); @@ -136,8 +139,9 @@ public TrainingPlan constructTrainingPlanByObjectives(TrainingPlan trainingPlan, elevationGain, elevationLoss, trailType); - } - case "WeightSquat" -> { + } break; + + case "WeightSquat": { int expectedDuration = random.nextInt(10, 40); int repetitions = random.nextInt(5, 20); int series = random.nextInt(1, 5); @@ -150,8 +154,11 @@ public TrainingPlan constructTrainingPlanByObjectives(TrainingPlan trainingPlan, repetitions, series, weight); + } break; + + default: { + throw new IllegalArgumentException("Invalid type."); } - default -> throw new IllegalArgumentException("Invalid type."); } if (!hardActivities && activity instanceof HardInterface) { diff --git a/src/main/java/MakeItFit/users/User.java b/src/main/java/MakeItFit/users/User.java index 23546bd..c04f1e0 100644 --- a/src/main/java/MakeItFit/users/User.java +++ b/src/main/java/MakeItFit/users/User.java @@ -382,21 +382,19 @@ public float calculateIndex(float weight, int height, int bpm) { @Override public String toString() { // clang-format off - return String.format(""" - == (User details) == - Code: %s - Name: %s - Age: %d - Gender: %s - Weight: %.2f kg - Height: %d cm - Bpm: %d - Level: %s - Address: %s - Phone: %s - Email: %s - Activities: %s - """, + return String.format(" == (User details) ==\n" + + " Code: %s\n" + + " Name: %s\n" + + " Age: %d\n" + + " Gender: %s\n" + + " Weight: %.2f kg\n" + + " Height: %d cm\n" + + " Bpm: %d\n" + + " Level: %s\n" + + " Address: %s\n" + + " Phone: %s\n" + + " Email: %s\n" + + " Activities: %s\n", this.code, this.name, this.age, diff --git a/src/main/java/MakeItFit/utils/ExtendedRandom.java b/src/main/java/MakeItFit/utils/ExtendedRandom.java new file mode 100644 index 0000000..03d6e60 --- /dev/null +++ b/src/main/java/MakeItFit/utils/ExtendedRandom.java @@ -0,0 +1,32 @@ +package MakeItFit.utils; + +import java.util.Random; + +/** + * Class to implement new java features to java.util.Random. + * + * @author Humberto Gomes (A104348) + * @version (27052025) + */ +public class ExtendedRandom extends Random { + /** + * Creates a new random number generator with a random seed. + */ + public ExtendedRandom() { + super(); + } + + /** + * Creates a new random number generator with a set seed. + */ + public ExtendedRandom(long seed) { + super(seed); + } + + /** + * Generates a random integer between origin (inclusive) and bound (exclusive) + */ + public int nextInt(int origin, int bound) { + return this.nextInt(bound - origin) + origin; + } +} diff --git a/src/main/java/MakeItFit/views/MakeItFitView.java b/src/main/java/MakeItFit/views/MakeItFitView.java index 003b6c8..d7f6a24 100644 --- a/src/main/java/MakeItFit/views/MakeItFitView.java +++ b/src/main/java/MakeItFit/views/MakeItFitView.java @@ -77,7 +77,7 @@ public void createUser() throws InvalidTypeException { String phone = scanner.nextLine(); switch (this.userType) { - case "Amateur" -> + case "Amateur": { this.makeItFitController.createUser(name, age, gender, @@ -89,7 +89,10 @@ public void createUser() throws InvalidTypeException { phone, -1, this.userType); - case "Occasional", "Professional" -> { + } break; + + case "Occasional": + case "Professional": { System.out.print("[APP] Frequency: "); int frequency = scanner.nextInt(); scanner.nextLine(); // Consume the remaining newline @@ -105,10 +108,11 @@ public void createUser() throws InvalidTypeException { phone, frequency, this.userType); - } - default -> { + } break; + + default: { System.out.println("[APP] Invalid type."); - } + } break; } System.out.println("[" + this.makeItFitController.getName() + "] User created successfully."); @@ -396,7 +400,7 @@ public void addActivityToUser() { String designation = scanner.nextLine(); switch (this.activityType) { - case "PushUp" -> { + case "PushUp": { System.out.print("[APP] Repetitions: "); int repetitions = scanner.nextInt(); System.out.print("[APP] Series: "); @@ -409,8 +413,9 @@ public void addActivityToUser() { this.activityType, repetitions, series); - } - case "Running" -> { + } break; + + case "Running": { System.out.print("[APP] Distance (meters): "); double distance = scanner.nextInt(); scanner.nextLine(); // Consume the remaining newline @@ -424,8 +429,9 @@ public void addActivityToUser() { this.activityType, distance, speed); - } - case "Trail" -> { + } break; + + case "Trail": { System.out.print("[APP] Distance (meters): "); double distance = scanner.nextInt(); scanner.nextLine(); // Consume the remaining newline @@ -447,8 +453,9 @@ public void addActivityToUser() { elevationGain, elevationLoss, trailType); - } - case "WeightSquat" -> { + } break; + + case "WeightSquat": { System.out.print("[APP] Repetitions: "); int repetitions = scanner.nextInt(); System.out.print("[APP] Series: "); @@ -465,8 +472,11 @@ public void addActivityToUser() { repetitions, series, weight); - } - default -> System.out.println("[APP] Invalid type."); + } break; + + default: { + System.out.println("[APP] Invalid type."); + } break; } } catch (Exception e) { System.out.println("[" + this.makeItFitController.getName() + "] Invalid input."); @@ -608,7 +618,7 @@ public void addActivityToTrainingPlan() { String designation = scanner.nextLine(); switch (this.activityType) { - case "PushUp" -> { + case "PushUp": { System.out.print("[APP] Repetitions: "); int repetitions = scanner.nextInt(); System.out.print("[APP] Series: "); @@ -622,8 +632,9 @@ public void addActivityToTrainingPlan() { repetitions, series, iterations); - } - case "Running" -> { + } break; + + case "Running": { System.out.print("[APP] Distance (meters): "); double distance = scanner.nextInt(); scanner.nextLine(); // Consume the remaining newline @@ -638,8 +649,9 @@ public void addActivityToTrainingPlan() { distance, speed, iterations); - } - case "Trail" -> { + } break; + + case "Trail": { System.out.print("[APP] Distance (meters): "); double distance = scanner.nextInt(); scanner.nextLine(); // Consume the remaining newline @@ -662,8 +674,9 @@ public void addActivityToTrainingPlan() { elevationLoss, trailType, iterations); - } - case "WeightSquat" -> { + } break; + + case "WeightSquat": { System.out.print("[APP] Repetitions: "); int repetitions = scanner.nextInt(); System.out.print("[APP] Series: "); @@ -681,8 +694,11 @@ public void addActivityToTrainingPlan() { series, weight, iterations); - } - default -> System.out.println("[APP] Invalid type."); + } break; + + default: { + System.out.println("[APP] Invalid type."); + } break; } } catch (Exception e) { System.out.println("[" + this.makeItFitController.getName() + "] Invalid input.");