Refactor the code smells that were present according to SonarQube

This commit is contained in:
Marty30 2017-06-21 16:41:04 +02:00
parent 29dc4b634f
commit 029fc4ec4e
10 changed files with 34 additions and 48 deletions

View file

@ -28,7 +28,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class PetClinicApplication { public class PetClinicApplication {
public static void main(String[] args) throws Exception { public static void main(String[] args) {
SpringApplication.run(PetClinicApplication.class, args); SpringApplication.run(PetClinicApplication.class, args);
} }

View file

@ -126,13 +126,13 @@ public class Owner extends Person {
* @param name to test * @param name to test
* @return true if pet name is already in use * @return true if pet name is already in use
*/ */
public Pet getPet(String name, boolean ignoreNew) { public Pet getPet(final String name, final boolean ignoreNew) {
name = name.toLowerCase(); String lowerCaseName = name.toLowerCase();
for (Pet pet : getPetsInternal()) { for (Pet pet : getPetsInternal()) {
if (!ignoreNew || !pet.isNew()) { if (!ignoreNew || !pet.isNew()) {
String compName = pet.getName(); String compName = pet.getName();
compName = compName.toLowerCase(); compName = compName.toLowerCase();
if (compName.equals(name)) { if (compName.equals(lowerCaseName)) {
return pet; return pet;
} }
} }

View file

@ -78,7 +78,7 @@ class OwnerController {
} }
@RequestMapping(value = "/owners", method = RequestMethod.GET) @RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) { public String processFindForm(final Owner owner, final BindingResult result, final Map<String, Object> model) {
// allow parameterless GET request for /owners to return all records // allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) { if (owner.getLastName() == null) {
@ -93,8 +93,8 @@ class OwnerController {
return "owners/findOwners"; return "owners/findOwners";
} else if (results.size() == 1) { } else if (results.size() == 1) {
// 1 owner found // 1 owner found
owner = results.iterator().next(); Owner founndOwner = results.iterator().next();
return "redirect:/owners/" + owner.getId(); return "redirect:/owners/" + founndOwner.getId();
} else { } else {
// multiple owners found // multiple owners found
model.put("selections", results); model.put("selections", results);

View file

@ -31,8 +31,7 @@ class CrashController {
@RequestMapping(value = "/oups", method = RequestMethod.GET) @RequestMapping(value = "/oups", method = RequestMethod.GET)
public String triggerException() { public String triggerException() {
throw new RuntimeException( throw new ExampleException("Expected: controller used to showcase what happens when an exception is thrown");
"Expected: controller used to showcase what " + "happens when an exception is thrown");
} }
} }

View file

@ -0,0 +1,15 @@
package org.springframework.samples.petclinic.system;
/**
* @author Martijn
* @since 21-6-2017.
*/
public class ExampleException extends RuntimeException {
public ExampleException(String message) {
super(ExampleException.class.getSimpleName() + ": " + message);
}
protected ExampleException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(ExampleException.class.getSimpleName() + ": " + message, cause, enableSuppression, writableStackTrace);
}
}

View file

@ -31,11 +31,11 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller @Controller
class VetController { class VetController {
private final VetRepository vets; private final VetRepository vetRepository;
@Autowired @Autowired
public VetController(VetRepository clinicService) { public VetController(VetRepository clinicService) {
this.vets = clinicService; this.vetRepository = clinicService;
} }
@RequestMapping(value = { "/vets.html" }) @RequestMapping(value = { "/vets.html" })
@ -43,7 +43,7 @@ class VetController {
// Here we are returning an object of type 'Vets' rather than a collection of Vet // Here we are returning an object of type 'Vets' rather than a collection of Vet
// objects so it is simpler for Object-Xml mapping // objects so it is simpler for Object-Xml mapping
Vets vets = new Vets(); Vets vets = new Vets();
vets.getVetList().addAll(this.vets.findAll()); vets.getVetList().addAll(this.vetRepository.findAll());
model.put("vets", vets); model.put("vets", vets);
return "vets/vetList"; return "vets/vetList";
} }
@ -53,7 +53,7 @@ class VetController {
// Here we are returning an object of type 'Vets' rather than a collection of Vet // Here we are returning an object of type 'Vets' rather than a collection of Vet
// objects so it is simpler for JSon/Object mapping // objects so it is simpler for JSon/Object mapping
Vets vets = new Vets(); Vets vets = new Vets();
vets.getVetList().addAll(this.vets.findAll()); vets.getVetList().addAll(this.vetRepository.findAll());
return vets; return vets;
} }

View file

@ -18,7 +18,6 @@ package org.springframework.samples.petclinic.vet;
import java.util.Collection; import java.util.Collection;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -40,7 +39,7 @@ public interface VetRepository extends Repository<Vet, Integer> {
*/ */
@Transactional(readOnly = true) @Transactional(readOnly = true)
@Cacheable("vets") @Cacheable("vets")
Collection<Vet> findAll() throws DataAccessException; Collection<Vet> findAll();
} }

View file

@ -30,14 +30,14 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement @XmlRootElement
public class Vets { public class Vets {
private List<Vet> vets; private List<Vet> vetList;
@XmlElement @XmlElement
public List<Vet> getVetList() { public List<Vet> getVetList() {
if (vets == null) { if (vetList == null) {
vets = new ArrayList<>(); vetList = new ArrayList<>();
} }
return vets; return vetList;
} }
} }

View file

@ -17,7 +17,6 @@ package org.springframework.samples.petclinic.visit;
import java.util.List; import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.BaseEntity;
@ -38,7 +37,7 @@ public interface VisitRepository extends Repository<Visit, Integer> {
* @param visit the <code>Visit</code> to save * @param visit the <code>Visit</code> to save
* @see BaseEntity#isNew * @see BaseEntity#isNew
*/ */
void save(Visit visit) throws DataAccessException; void save(Visit visit);
List<Visit> findByPetId(Integer petId); List<Visit> findByPetId(Integer petId);

View file

@ -1,26 +0,0 @@
package nl.utwente.bpsd.selenium;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.net.MalformedURLException;
/**
* @author Martijn
* @since 21-6-2017.
*/
public class FailingIT extends SeleniumBaseIT {
public FailingIT() throws MalformedURLException {
super();
}
@Test
@Category(SeleniumBaseIT.class)
@Ignore
public void failIT() {
Assert.fail();
}
}