mtedone / podam

PODAM - POjo DAta Mocker
https://mtedone.github.io/podam
MIT License
323 stars 750 forks source link

@Size annotation is currently ignored on read-only collections and maps #228

Closed NBau closed 7 years ago

NBau commented 7 years ago

If a collection is annotated with @Size, this should impact the number of instances generated for this collection.

daivanov commented 7 years ago

There is a test testing javax.validation.constraints.Size annotation and this test is not failing.

NBau commented 7 years ago

Here is the code that does not work for me (very simplified):

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.Size;

import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

public class Dummy {

    public static void main(String[] args){
        PodamFactory pf = new PodamFactoryImpl();

        Pojo p = pf.manufacturePojo(Pojo.class);
        System.out.println(p.dec.size()<=2);
    }

    public static class Pojo{

        @Size(max=2)
        List<Integer>dec;

        public List<Integer> getDec() {
            if (dec == null) {
                dec = new ArrayList<>();
                }
            return dec;
        }
    }
}

The Pojo is similar to what you get when creating Java classes from an XML Schema using JAXB, so I think this pattern should be very common. Even replacing List with Collection does not seem to fix it.

I did look through the test cases and it seems that there actually is no test that checks the validity of the size of the collection. All I could find were line 50 and 51, where the collections are checked for null. The size should be checked as well.

daivanov commented 7 years ago

You can try Podam's snapshot.

NBau commented 7 years ago

Perfect. Thank you for the good support.

daivanov commented 7 years ago

Released in Podam 7.0.5