Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetterTester and GetterTester improvement #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/com/openpojo/validation/test/impl/GetterTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package com.openpojo.validation.test.impl;

import java.util.HashSet;
import java.util.Set;

import com.openpojo.log.LoggerFactory;
import com.openpojo.random.RandomFactory;
import com.openpojo.reflection.PojoClass;
Expand All @@ -34,9 +37,16 @@
*/
public class GetterTester implements Tester {

private Set<String> skippedFields = new HashSet<String>();

public void run(final PojoClass pojoClass) {
final Object classInstance = ValidationHelper.getBasicInstance(pojoClass);
for (final PojoField fieldEntry : pojoClass.getPojoFields()) {
if (skippedFields.contains(fieldEntry.getName())) {
LoggerFactory.getLogger(this.getClass()).debug("Skipping field [{0}]", fieldEntry);
continue;
}

if (fieldEntry.hasGetter()) {
Object value = fieldEntry.get(classInstance);

Expand All @@ -57,4 +67,20 @@ public void run(final PojoClass pojoClass) {
}
}
}

/**
* Instructs GetterTester to skip the field when running pojoClass
* validation.
*
* @param fieldToBeSkipped
* Field name (e.g. Into Person class, to skip getName, provide
* "name" as a field to be skipped.
*/
public void addField(String fieldToBeSkipped) {
if (fieldToBeSkipped == null || fieldToBeSkipped.trim().isEmpty()) {
throw new IllegalArgumentException("The argument 'fieldToBeSkipped' cannot be null or empty.");
}

this.skippedFields.add(fieldToBeSkipped);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package com.openpojo.validation.test.impl;

import java.util.HashSet;
import java.util.Set;

import com.openpojo.log.LoggerFactory;
import com.openpojo.random.RandomFactory;
import com.openpojo.reflection.PojoClass;
Expand All @@ -34,9 +37,16 @@
*/
public class SetterTester implements Tester {

private Set<String> skippedFields = new HashSet<String>();

public void run(final PojoClass pojoClass) {
final Object classInstance = ValidationHelper.getBasicInstance(pojoClass);
for (final PojoField fieldEntry : pojoClass.getPojoFields()) {
if (skippedFields.contains(fieldEntry.getName())) {
LoggerFactory.getLogger(this.getClass()).debug("Skipping field [{0}]", fieldEntry);
continue;
}

if (fieldEntry.hasSetter()) {
final Object value;

Expand All @@ -56,4 +66,20 @@ public void run(final PojoClass pojoClass) {
}
}
}
}

/**
* Instructs SetterTester to skip the field when running pojoClass
* validation.
*
* @param fieldToBeSkipped
* Field name (e.g. Into Person class, to skip getName, provide
* "name" as a field to be skipped.
*/
public void addField(String fieldToBeSkipped) {
if (fieldToBeSkipped == null || fieldToBeSkipped.trim().isEmpty()) {
throw new IllegalArgumentException("The argument 'fieldToBeSkipped' cannot be null or empty.");
}

this.skippedFields.add(fieldToBeSkipped);
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/openpojo/validation/test/impl/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.openpojo.validation.test.impl;

public class Person {
private String name;

public void setName(String name) {
this.name = "bad programming";
}

public String getName() {
return "bad programming";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.openpojo.validation.test.impl;

import static org.junit.Assert.*;

import org.junit.Test;

import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.validation.PojoValidator;
import com.openpojo.validation.Validator;
import com.openpojo.validation.ValidatorBuilder;
import com.openpojo.validation.rule.impl.GetterMustExistRule;
import com.openpojo.validation.rule.impl.SetterMustExistRule;

public class SetterTesterTest {

@Test
public void test() {
PojoClass personPojo = PojoClassFactory.getPojoClass(Person.class);

SetterTester setterTester = new SetterTester();
Copy link

@konrad7d konrad7d Feb 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about supplying skipped fields via constructor?

SetterTester setterTester = new SetterTester(String... skippedFields);

setterTester.addField("name");

GetterTester getterTester = new GetterTester();
getterTester.addField("name");

Validator validator = ValidatorBuilder.create().with(setterTester).with(getterTester).build();

validator.validate(personPojo);
}
}