Skip to content

Commit

Permalink
Add validation for empty reference name
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietrorossellini committed Apr 28, 2016
1 parent 56e2452 commit 34fa092
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ abstract class AddReferenceCommand extends Command {
private void setReferenceName(Reference reference) {
this.fieldValidator = new FieldValidator(reference);
while(true) {
String referenceName = io.readLine("Refererence id");
if (fieldValidator.ReferenceNameIsUnique(
referenceName, app.listReferences())) {
String referenceName = io.readLine("Reference id");
if (!fieldValidator.referenceNameIsUnique(referenceName, app.listReferences())) {
io.print("There already exists a reference with the name " + referenceName
+ "\nReference name must be unique.");
}
else if (!fieldValidator.referenceNameIsNotEmpty(referenceName)) {
io.print("Reference name should not be empty.");
}
else {
reference.setReferenceName(referenceName);
break;
}
io.print("There already exists a reference with the name " + referenceName
+ "\nReference name must be unique.");
}
}
}
/**
* Prompts for reference fields, checks for duplicate reference names and stores the reference.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ public boolean fieldValueIsValid(String fieldValue) {
return true;
}

public boolean ReferenceNameIsUnique(String referenceName, List<Reference> referenceList) {
public boolean referenceNameIsUnique(String referenceName, List<Reference> referenceList) {
for (Reference reference : referenceList) {
if(reference.getReferenceName().equalsIgnoreCase(referenceName)) return false;
}
return true;
}

public boolean referenceNameIsNotEmpty(String referenceName) {
return referenceName.trim().length() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ public void testFieldValueIsValid() {
public void testRefereceNameIsUnique() {
ArrayList<Reference> references = new ArrayList();
references.add(reference);
assertEquals(true, fieldValidator.ReferenceNameIsUnique("newBook", references));
assertEquals(false, fieldValidator.ReferenceNameIsUnique("book", references));
assertEquals(true, fieldValidator.referenceNameIsUnique("newBook", references));
assertEquals(false, fieldValidator.referenceNameIsUnique("book", references));
}

@Test
public void testReferenceNameIsNotEmpty() {
String empty = "", whitespace = " ";
assertFalse(fieldValidator.referenceNameIsNotEmpty(empty));
assertFalse(fieldValidator.referenceNameIsNotEmpty(whitespace));
}
}

0 comments on commit 34fa092

Please sign in to comment.