From c8a34fb7e9b899200271aeeafbebaba73295c58c Mon Sep 17 00:00:00 2001 From: Mousa Al Bateh Date: Thu, 3 Oct 2024 20:06:42 +0200 Subject: [PATCH] Minor code changes are the following: -Removed unused variables from few files. -Added null assertions in some tests. -Added suppression of unused warning alongside the comments. -Removed unnecessary throw exceptions. --- .../samples/petclinic/owner/VisitController.java | 8 ++++---- .../samples/petclinic/vet/Vet.java | 4 ---- .../samples/petclinic/MySqlIntegrationTests.java | 2 +- .../petclinic/PetClinicIntegrationTests.java | 2 +- .../petclinic/PostgresIntegrationTests.java | 16 +++++++++++++++- .../petclinic/owner/PetTypeFormatterTests.java | 2 +- .../petclinic/service/ClinicServiceTests.java | 2 -- .../petclinic/system/CrashControllerTests.java | 2 +- .../samples/petclinic/vet/VetTests.java | 5 +++++ 9 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java b/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java index 6c7a8dd1e..25c4c1354 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java @@ -71,15 +71,15 @@ class VisitController { return visit; } - // Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is - // called + @SuppressWarnings("unused") // Spring MVC calls method loadPetWithVisit(...) before + // initNewVisitForm is called @GetMapping("/owners/{ownerId}/pets/{petId}/visits/new") public String initNewVisitForm() { return "pets/createOrUpdateVisitForm"; } - // Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is - // called + @SuppressWarnings("unused") // Spring MVC calls method loadPetWithVisit(...) before + // processNewVisitForm is called @PostMapping("/owners/{ownerId}/pets/{petId}/visits/new") public String processNewVisitForm(@ModelAttribute Owner owner, @PathVariable int petId, @Valid Visit visit, BindingResult result, RedirectAttributes redirectAttributes) { diff --git a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java index 7a70155c3..d8a1fc84d 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java @@ -57,10 +57,6 @@ public class Vet extends Person { return this.specialties; } - protected void setSpecialtiesInternal(Set specialties) { - this.specialties = specialties; - } - @XmlElement public List getSpecialties() { List sortedSpecs = new ArrayList<>(getSpecialtiesInternal()); diff --git a/src/test/java/org/springframework/samples/petclinic/MySqlIntegrationTests.java b/src/test/java/org/springframework/samples/petclinic/MySqlIntegrationTests.java index 300ac359f..31231d79e 100644 --- a/src/test/java/org/springframework/samples/petclinic/MySqlIntegrationTests.java +++ b/src/test/java/org/springframework/samples/petclinic/MySqlIntegrationTests.java @@ -58,7 +58,7 @@ class MySqlIntegrationTests { private RestTemplateBuilder builder; @Test - void testFindAll() throws Exception { + void testFindAll() { vets.findAll(); vets.findAll(); // served from cache } diff --git a/src/test/java/org/springframework/samples/petclinic/PetClinicIntegrationTests.java b/src/test/java/org/springframework/samples/petclinic/PetClinicIntegrationTests.java index 6472a1212..6d9820696 100644 --- a/src/test/java/org/springframework/samples/petclinic/PetClinicIntegrationTests.java +++ b/src/test/java/org/springframework/samples/petclinic/PetClinicIntegrationTests.java @@ -44,7 +44,7 @@ public class PetClinicIntegrationTests { private RestTemplateBuilder builder; @Test - void testFindAll() throws Exception { + void testFindAll() { vets.findAll(); vets.findAll(); // served from cache } diff --git a/src/test/java/org/springframework/samples/petclinic/PostgresIntegrationTests.java b/src/test/java/org/springframework/samples/petclinic/PostgresIntegrationTests.java index 18945a570..82e7a6a87 100644 --- a/src/test/java/org/springframework/samples/petclinic/PostgresIntegrationTests.java +++ b/src/test/java/org/springframework/samples/petclinic/PostgresIntegrationTests.java @@ -17,6 +17,7 @@ package org.springframework.samples.petclinic; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeTrue; import java.util.Arrays; @@ -114,7 +115,20 @@ public class PostgresIntegrationTests { Arrays.sort(names); for (String name : names) { String resolved = environment.getProperty(name); - String value = source.getProperty(name).toString(); + + if (resolved == null) { + fail("resolved property named {0} was expecting a string but is null.", name); + return; + } + + Object sourceProperty = source.getProperty(name); + + if (sourceProperty == null || sourceProperty.toString() == null) { + fail("source property named {0} was expecting a string value but is null.", name); + return; + } + + String value = sourceProperty.toString(); if (resolved.equals(value)) { log.info(name + "=" + resolved); } diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java index dabade7b9..0295b4788 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -68,7 +68,7 @@ class PetTypeFormatterTests { } @Test - void shouldThrowParseException() throws ParseException { + void shouldThrowParseException() { given(this.pets.findPetTypes()).willReturn(makePetTypes()); Assertions.assertThrows(ParseException.class, () -> { petTypeFormatter.parse("Fish", Locale.ENGLISH); diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java index 2cbf7af26..2f3d00ce6 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -205,8 +205,6 @@ class ClinicServiceTests { owner6.addVisit(pet7.getId(), visit); this.owners.save(owner6); - owner6 = this.owners.findById(6); - assertThat(pet7.getVisits()) // .hasSize(found + 1) // .allMatch(value -> value.getId() != null); diff --git a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java index 09773aec8..cc2ad6746 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java @@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; // luck ((plain(st) UNIT test)! :) class CrashControllerTests { - CrashController testee = new CrashController(); + final CrashController testee = new CrashController(); @Test void testTriggerException() { diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java index b29c6d100..8ada6f87a 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java @@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test; import org.springframework.util.SerializationUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; /** * @author Dave Syer @@ -33,6 +34,10 @@ class VetTests { vet.setId(123); @SuppressWarnings("deprecation") Vet other = (Vet) SerializationUtils.deserialize(SerializationUtils.serialize(vet)); + if (other == null) { + fail("other expected value but is null."); + return; + } assertThat(other.getFirstName()).isEqualTo(vet.getFirstName()); assertThat(other.getLastName()).isEqualTo(vet.getLastName()); assertThat(other.getId()).isEqualTo(vet.getId());