From f868b27f4d56c9ec376a03b37ac8bcc73ac93904 Mon Sep 17 00:00:00 2001 From: voidbert Date: Tue, 27 May 2025 17:20:19 +0100 Subject: [PATCH 1/4] Port project to Java 8 --- build.gradle.kts | 5 +- .../MakeItFit/queries/MostDoneActivity.java | 25 ++++++-- .../trainingPlan/TrainingPlanManager.java | 25 +++++--- src/main/java/MakeItFit/users/User.java | 28 ++++----- .../java/MakeItFit/utils/ExtendedRandom.java | 32 ++++++++++ .../java/MakeItFit/views/MakeItFitView.java | 62 ++++++++++++------- 6 files changed, 121 insertions(+), 56 deletions(-) create mode 100644 src/main/java/MakeItFit/utils/ExtendedRandom.java diff --git a/build.gradle.kts b/build.gradle.kts index e8988d2..cad10af 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -32,9 +32,8 @@ dependencies { } java { - toolchain { - languageVersion = JavaLanguageVersion.of(21) - } + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 } application { 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."); From 5b34ba65351671a3b677a6cfbe16615b861281a3 Mon Sep 17 00:00:00 2001 From: voidbert Date: Wed, 28 May 2025 02:09:12 +0100 Subject: [PATCH 2/4] Add EvoSuite runtime to Gradle config --- build.gradle.kts | 66 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index cad10af..0567e38 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,6 +14,24 @@ * 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") { + jvmVersion = JavaVersion.VERSION_1_8 +} + +if (JavaVersion.current() != jvmVersion) { + throw GradleException( + "Wrong java version for this task: use Java 8 for EvoSuite and 21 for everything else" + ) +} + // Project configuration plugins { @@ -27,8 +45,16 @@ 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") + testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.12.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.2.0") } java { @@ -40,38 +66,23 @@ 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() + useJUnitPlatform { + includeEngines("junit-jupiter", "junit-vintage") + } finalizedBy(tasks.jacocoTestReport) } -// 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'") +jacoco { + toolVersion = "0.8.13" } -// PIT Mutation Testing pitest { junit5PluginVersion.set("1.2.2") pitestVersion.set("1.19.1") @@ -83,3 +94,14 @@ pitest { outputFormats.set(setOf("HTML")) timestampedReports.set(false) } + +// 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'") +} From a25569ee35702cf4cd18d229fde667a91a8325ae Mon Sep 17 00:00:00 2001 From: voidbert Date: Wed, 28 May 2025 03:57:13 +0100 Subject: [PATCH 3/4] Add EvoSuite test generation to Gradle config --- build.gradle.kts | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0567e38..6e03229 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,7 +22,9 @@ if (project.hasProperty("testDir")) { } var jvmVersion = JavaVersion.VERSION_21 -if (testTarget == "evosuitetests") { +if (testTarget == "evosuitetests" || + gradle.startParameter.taskNames.contains("generateEvoSuiteTests")) { + jvmVersion = JavaVersion.VERSION_1_8 } @@ -32,6 +34,12 @@ if (JavaVersion.current() != jvmVersion) { ) } +// Build script configuration + +val evoSuiteDownload by configurations.creating { + isTransitive = true +} + // Project configuration plugins { @@ -54,7 +62,8 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.12.2") // EvoSuite Runtime - testImplementation("org.evosuite:evosuite-standalone-runtime:1.2.0") + testImplementation("org.evosuite:evosuite-standalone-runtime:1.0.6") + evoSuiteDownload("org.evosuite:evosuite-master:1.0.6") } java { @@ -95,6 +104,28 @@ pitest { timestampedReports.set(false) } +tasks.register("generateEvoSuiteTests") { + classpath = evoSuiteDownload + mainClass = "org.evosuite.EvoSuite" + args = listOf( + "-target", + layout.buildDirectory.dir("classes/java/main").get().asFile.path, + "-seed", + "1" + ) + + doLast { + project.delete(files("evosuite-report")) + File("src/evosuite-tests").mkdirs() + File("evosuite-tests").renameTo(File("src/evosuite-tests/main")) + ProcessBuilder(listOf("sh", "-c", + "find src/evosuite-tests/main -type f | xargs -n1 sed -i '/^@RunWith/d'" + )).start().waitFor() + } + + finalizedBy(tasks.named("format")) +} + // Other tasks tasks.named("run") { @@ -103,5 +134,7 @@ tasks.named("run") { 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'") + commandLine = listOf("sh", "-c", + "find src -type f | xargs -n1 sh -c 'clang-format -i $0; sed -i s/\\\\r//g $0'" + ) } From 8fa1ce4c6dc3ac39b498a422a4c743fcbeeca4bb Mon Sep 17 00:00:00 2001 From: voidbert Date: Wed, 28 May 2025 14:42:03 +0100 Subject: [PATCH 4/4] Fix EvoSuite crashing runtime and wrong test directory --- build.gradle.kts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 6e03229..2786744 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,6 +28,11 @@ if (testTarget == "evosuitetests" || 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" @@ -55,7 +60,6 @@ repositories { dependencies { // JUnit 4 testImplementation("junit:junit:4.13.2") - testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.12.2") // JUnit 5 testImplementation("org.junit.jupiter:junit-jupiter:5.12.2") @@ -82,9 +86,10 @@ java.sourceSets["test"].java { } tasks.named("test") { - useJUnitPlatform { - includeEngines("junit-jupiter", "junit-vintage") + if (junitVersion == 5) { + useJUnitPlatform () } + finalizedBy(tasks.jacocoTestReport) } @@ -110,17 +115,16 @@ tasks.register("generateEvoSuiteTests") { args = listOf( "-target", layout.buildDirectory.dir("classes/java/main").get().asFile.path, + "-Duse_separate_classloader=false", "-seed", "1" ) doLast { project.delete(files("evosuite-report")) - File("src/evosuite-tests").mkdirs() - File("evosuite-tests").renameTo(File("src/evosuite-tests/main")) - ProcessBuilder(listOf("sh", "-c", - "find src/evosuite-tests/main -type f | xargs -n1 sed -i '/^@RunWith/d'" - )).start().waitFor() + project.delete(files("src/evosuitetests/java")) + File("src/evosuitetests/java").mkdirs() + File("evosuite-tests").renameTo(File("src/evosuitetests/java")) } finalizedBy(tasks.named("format"))