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

Add ignores to getter setter rules and testers #66

Open
drdamour opened this issue Oct 7, 2015 · 9 comments
Open

Add ignores to getter setter rules and testers #66

drdamour opened this issue Oct 7, 2015 · 9 comments

Comments

@drdamour
Copy link

drdamour commented Oct 7, 2015

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.

@rcriosbr
Copy link

rcriosbr commented Sep 7, 2016

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...

@drdamour
Copy link
Author

drdamour commented Sep 7, 2016

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

@hwaastad
Copy link

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

@stevensouza
Copy link

stevensouza commented Feb 28, 2019

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 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...

@sualeh
Copy link

sualeh commented Feb 28, 2019

@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.

@stevensouza
Copy link

@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.

@sualeh
Copy link

sualeh commented Feb 28, 2019

@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.

@jlczuk
Copy link

jlczuk commented May 6, 2021

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.

@patrickhuy
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants