podam icon indicating copy to clipboard operation
podam copied to clipboard

Podam doesn't correctly handle collection types when memoization is enabled

Open mtedone opened this issue 11 years ago • 2 comments

For the following mojo

package uk.co.jemos.podam.test.dto.issue123;

import java.util.List;
import java.util.Vector;

/**
 * Created by tedonema on 03/10/2015.
 */
public class GenericCollectionsConstructorPojo {

    private final List<Long> list1;
    private final List<String> list2;
    private final List<Integer> list3;

    /**
     * Full constructor
     * @param list1 First List
     * @param list2 Second List
     */
    public GenericCollectionsConstructorPojo(Vector<Long> list1, Vector<String> list2, Vector<Integer> list3) {
        this.list1  = new Vector<Long>(list1);
        this.list2 = new Vector<String>(list2);
        this.list3 = new Vector<Integer>(list3);
    }

    public Vector<Long> getList1() {
        return new Vector<Long>(list1);
    }

    public Vector<String> getList2() {
        return new Vector<String>(list2);
    }

    public Vector<Integer> getList3() {
        return new Vector<Integer>(list3);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("GenericCollectionsConstructorPojo{");
        sb.append("list1=").append(list1);
        sb.append(", list2=").append(list2);
        sb.append(", list3=").append(list3);
        sb.append('}');
        return sb.toString();
    }
}

The following test will fail:

   @Test
    @Title("Podam should correctly handle generic collections in constructor with memoization enabled")
    public void podamShouldHandleGenericCollectionsInConstructorWithMemoizationEnabled() throws Exception {

        PodamFactory podamFactory = podamFactorySteps.givenAStandardPodamFactoryWithMemoizationEnabled();

        GenericCollectionsConstructorPojo pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass
                (GenericCollectionsConstructorPojo.class, podamFactory);

        podamValidationSteps.theObjectShouldNotBeNull(pojo);
        podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndContainElementsOfType(pojo.getList1(), Long.class);
        podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndContainElementsOfType(pojo.getList2(), String.class);
        podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndContainElementsOfType(pojo.getList3(), Integer
                .class);

    }

But the following test passes:

   @Test
    @Title("Podam should correctly handle generic collections in constructor with memoization disabled")
    public void podamShouldHandleGenericCollectionsInConstructorWithMemoizationDisabled() throws Exception {

        PodamFactory podamFactory = podamFactorySteps.givenAStandardPodamFactory();

        GenericCollectionsConstructorPojo pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass
                (GenericCollectionsConstructorPojo.class, podamFactory);

        podamValidationSteps.theObjectShouldNotBeNull(pojo);
        podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndContainElementsOfType(pojo.getList1(), Long.class);
        podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndContainElementsOfType(pojo.getList2(), String.class);
        podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndContainElementsOfType(pojo.getList3(), Integer
                .class);

    }

mtedone avatar Oct 04 '15 09:10 mtedone

I don't think there is anything wrong with memoization. The error happens when constructor has type with generic wildcard https://github.com/daivanov/podam/commit/0b5101f821c08a6d5a48ab04df7d3645daae5075

daivanov avatar Oct 04 '15 10:10 daivanov

Fixed with commit 9d5e15671adff10b607e425908799649185ecd82

daivanov avatar Aug 19 '16 10:08 daivanov