mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-19 05:55:51 +00:00
Shadow Read mod
This commit is contained in:
parent
fc12751b2c
commit
1b8913f5ae
4 changed files with 21 additions and 37 deletions
|
@ -149,4 +149,7 @@ public class Owner extends Person {
|
||||||
.append("firstName", this.getFirstName()).append("address", this.address)
|
.append("firstName", this.getFirstName()).append("address", this.address)
|
||||||
.append("city", this.city).append("telephone", this.telephone).toString();
|
.append("city", this.city).append("telephone", this.telephone).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// for testing
|
||||||
|
public Owner() {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,13 +90,6 @@ class OwnerController {
|
||||||
// find owners by last name
|
// find owners by last name
|
||||||
Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
|
Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
|
||||||
|
|
||||||
for (Owner o : results)
|
|
||||||
System.out.println(o.toString());
|
|
||||||
|
|
||||||
// Shadow Read
|
|
||||||
compareResults(results, owner.getLastName());
|
|
||||||
|
|
||||||
|
|
||||||
if (results.isEmpty()) {
|
if (results.isEmpty()) {
|
||||||
// no owners found
|
// no owners found
|
||||||
result.rejectValue("lastName", "notFound", "not found");
|
result.rejectValue("lastName", "notFound", "not found");
|
||||||
|
@ -112,32 +105,6 @@ class OwnerController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void compareResults(Collection<Owner> results, String lastName) {
|
|
||||||
|
|
||||||
String pattern = lastName + "*";
|
|
||||||
|
|
||||||
NewOwnerStore newStore = NewOwnerStore.getInstance(owners);
|
|
||||||
newStore.populateStore();
|
|
||||||
|
|
||||||
HashMap<Integer, StaticOwner> storeMap = newStore.getStore();
|
|
||||||
|
|
||||||
ArrayList<StaticOwner> newOwners = new ArrayList<>();
|
|
||||||
|
|
||||||
for (StaticOwner owner : storeMap.values()) {
|
|
||||||
if (!Pattern.compile(pattern).matcher(owner.getLastName()).find())
|
|
||||||
newOwners.add(owner);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Owner owner : results) {
|
|
||||||
if (newOwners.contains(owner))
|
|
||||||
System.out.println("Found. Good");
|
|
||||||
else {
|
|
||||||
System.out.println("Not Found. Bad");
|
|
||||||
newStore.findAndReplace(owner);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/owners/{ownerId}/edit")
|
@GetMapping("/owners/{ownerId}/edit")
|
||||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||||
Owner owner = this.owners.findById(ownerId);
|
Owner owner = this.owners.findById(ownerId);
|
||||||
|
|
|
@ -27,6 +27,13 @@ public class StaticOwner {
|
||||||
this.setTelephone(telephone);
|
this.setTelephone(telephone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StaticOwner(Integer id, String firstName, String lastName)
|
||||||
|
{
|
||||||
|
this.setId(id);
|
||||||
|
this.setLastName(lastName);
|
||||||
|
this.setFirstName(firstName);
|
||||||
|
}
|
||||||
|
|
||||||
public void setAddress(String address) {
|
public void setAddress(String address) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
@ -98,5 +105,4 @@ public class StaticOwner {
|
||||||
return new StaticOwner(owner.getId(), owner.getLastName(), owner.getFirstName(),
|
return new StaticOwner(owner.getId(), owner.getLastName(), owner.getFirstName(),
|
||||||
owner.getAddress(), owner.getCity(), owner.getTelephone());
|
owner.getAddress(), owner.getCity(), owner.getTelephone());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,17 +68,25 @@ public class NewOwnerStoreTest {
|
||||||
int inconsistencies = compareResults(results, "");
|
int inconsistencies = compareResults(results, "");
|
||||||
|
|
||||||
assertEquals(inconsistencies, 0);
|
assertEquals(inconsistencies, 0);
|
||||||
|
|
||||||
|
// Introduce inconsistency
|
||||||
|
testOwnerStore.getStore().put(1, new StaticOwner(1, "First", "Last"));
|
||||||
|
inconsistencies = compareResults(results, "");
|
||||||
|
assertEquals(inconsistencies, 1);
|
||||||
|
|
||||||
|
|
||||||
|
// Inconsistency should be gone
|
||||||
|
inconsistencies = compareResults(results, "");
|
||||||
|
assertEquals(inconsistencies, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int compareResults(Collection<Owner> results, String lastName) {
|
private int compareResults(Collection<Owner> results, String lastName) {
|
||||||
|
|
||||||
String pattern = "/^" + lastName + "/";
|
String pattern = "/^" + lastName + "/";
|
||||||
|
|
||||||
HashMap<Integer, StaticOwner> storeMap = testOwnerStore.getStore();
|
|
||||||
|
|
||||||
ArrayList<StaticOwner> staticOwners = new ArrayList<>();
|
ArrayList<StaticOwner> staticOwners = new ArrayList<>();
|
||||||
|
|
||||||
for (StaticOwner owner : storeMap.values()) {
|
for (StaticOwner owner : ownerStore.values()) {
|
||||||
if (!Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(owner.getLastName()).find())
|
if (!Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(owner.getLastName()).find())
|
||||||
staticOwners.add(owner);
|
staticOwners.add(owner);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue