openpojo icon indicating copy to clipboard operation
openpojo copied to clipboard

Add ignores to getter setter rules and testers

Open drdamour opened this issue 8 years ago • 9 comments

There's a few cases where sometimes i just want to ignore a specific field. I should be able to do that. This would be a solution to #48 as well.

I suggest adding vararg string params to the constructors of the tester to include a list of ignores. Or you could annotate a field with an annotation...but i don't want to decorate my class just for testing in general so i prefer making the rules/testers a little flexible.

drdamour avatar Oct 07 '15 17:10 drdamour

Take a look at pull request #90. I modified Setter/GetterTester to do it:

SetterTester setterTester = new SetterTester(); setterTester.addField("name");

By doing it, OpenPojo will skip setName test. I just realized that I should give another name to method, instead addField. It may lead to a inverse understanding of it's purpose...

Meanwhile Oshoukry decided if he will incorporate it or not, I wrote my own Setter/Getter extending Tester interface. You could do it...

rcriosbr avatar Sep 07 '16 23:09 rcriosbr

oh we have, but seems like it should be core to the lib

drdamour avatar Sep 07 '16 23:09 drdamour

Hi, I think this is a nice addition....but probably builder pattern on addField would be better(?)

hwaastad avatar Nov 22 '17 13:11 hwaastad

First of all thanks for the useful API.

I also had the need to ignore fields (i would think this would be a common need). Being as #90 or something similar isn't making it into the codebase I implemented a solution that doesn't require a code change to openpojo.

Here is how my implementation works...

// any fieldnames passed to the method will be excluded from the tests
PojoValidator.validateSettersGetters(MyPojo.class, "id", "dateFormatter", "firstName");

The concept is to

  • write a 'decorator' class around PojoClass (easy to do in intellij): public class PojoClassDecorator implements PojoClass
  • All methods in PojoClassDecorator simply call the wrapped PojoClass object, except one...
  • The exception is 'public List<PojoField> getPojoFields()'. The implementation for this method first looks at an exclusion list of fields and only returns fields not in this list. If there is no exclusion list then all fields will be returned (i.e. it will work the way openpojo does now).
  • The other trick is the decorator (PojoClassDecorator) needs to be called by the Tester instead of the underlying PojoClass object
  • To do this I implemented a Tester that uses the decorator object instead of the PojoClass by passing it to a SetterTester, or GetterTester. This object is called the TesterDecorator. See the code below:
public class TesterDecorator implements Tester {
    public TesterDecorator(PojoClassDecorator pojoClassDecorator, Tester tester) {
        this.pojoClassDecorator = pojoClassDecorator;
        this.tester = tester;
    }

    private PojoClassDecorator pojoClassDecorator; 
    private Tester tester;

    public void run(final PojoClass pojoClass) {
        // pojoClassDecorator simply calls the underlying PojoClass for all methods except getPojoFields.  The decorated version of this method only returns methods not on an exclusion list
        tester.run(pojoClassDecorator.decorate(pojoClass));
    }


}

Note in addition to working on Testers this approach can also be used on Rules.

I'm sure there are other ways to do this too...

stevensouza avatar Feb 28 '19 18:02 stevensouza

@stevensouza - thanks for the workaround. It would be really nice to have something in the API itself, but like you say, until then we can use code similar to what you have.

sualeh avatar Feb 28 '19 18:02 sualeh

@stevensouza - thanks for the workaround. It would be really nice to have something in the API itself, but like you say, until then we can use code similar to what you have.

Note, if anyone is interested I can paste more of the code. I didn't because although PojoClassDecorator is simple it is quite long due to the number of methods in PojoClass.

stevensouza avatar Feb 28 '19 19:02 stevensouza

@stevensouza - thanks for the workaround. It would be really nice to have something in the API itself, but like you say, until then we can use code similar to what you have.

Note, if anyone is interested I can paste more of the code. I didn't because although PojoClassDecorator is simple it is quite long due to the number of methods in PojoClass.

Please publish your complete code as a GitHub Gist. Thanks.

sualeh avatar Feb 28 '19 19:02 sualeh

Is this dead? I have a POJO with a setter method that has special considerations. It may set true even if the input is false, depending on certain conditions. I cannot test setters of this class with OpenPojo.

jlczuk avatar May 06 '21 20:05 jlczuk

Please publish your complete code as a GitHub Gist. Thanks.

Here you go: https://gist.github.com/patrickhuy/0392c54f52ad921bcf0152fe3cbb4d7c this shows an example how such an exclude could be implemented. Note that it is a rather naive implementation and does not use caching or anything

patrickhuy avatar May 04 '22 15:05 patrickhuy