podam icon indicating copy to clipboard operation
podam copied to clipboard

Manufacturing a pojo with a java.sql.Timestamp throws java.lang.IllegalArgumentException: nanos > 999999999 or < 0

Open KnechtRootrecht opened this issue 10 years ago • 14 comments

The nanos field of a Timestamp object must be within 0 and 999999999. Podam seems to ignore this constraint.

As a workaround I can provide an implementation of AttributeStrategy and simply return something like new Timestamp(System.currentTimeMillis()), in some cases annotations adn an additional class just for testing purposes may be not the most elegant solution.

KnechtRootrecht avatar Jul 12 '16 12:07 KnechtRootrecht

There is no way how Podam can know that Nanos must be less than 999999999.

The only way to deal with it is to inherit a custom AttributeStrategy from AbstractRandomDataProviderStrategy and then override

@Override
public Integer getInteger(AttributeMetadata attributeMetadata) {

    log(attributeMetadata);
    Integer retValue;
    do {
        retValue = RANDOM.nextInt(1000000000);
    } while (retValue.intValue() == 0);
    return retValue;
}

daivanov avatar Jul 16 '16 11:07 daivanov

In the 7.0 Podam one can make custom integer manufacturer to fill java.sql.Timestamp properly, for example, like this: https://github.com/devopsfolks/podam/blob/1d77cc6c0229eefb242f1c5375fe2b91c4777cda/src/test/java/uk/co/jemos/podam/test/unit/steps/PodamFactorySteps.java#L65

daivanov avatar Dec 02 '16 14:12 daivanov

Still doesn't works! Timestamp function public void setNanos(int n) { if (n > 999999999 || n < 0) { throw new IllegalArgumentException("nanos > 999999999 or < 0"); } nanos = n; } expects a int rather than Integer. Only annotation based strategy is working.

simarpreets avatar Dec 28 '16 13:12 simarpreets

Autounboxing is taking care of it. Could you please elaborate what exactly is not working?

daivanov avatar Dec 28 '16 13:12 daivanov

I have used the mentioned code snippet to add type manufacturer of Integer.

TypeManufacturer<Integer> manufacturer = new IntTypeManufacturerImpl() {

        @Override
        public Integer getInteger(AttributeMetadata attributeMetadata) {

            if (attributeMetadata.getPojoClass() == Timestamp.class) {
                return PodamUtils.getIntegerInRange(0, 999999999);
            } else {
                return super.getInteger(attributeMetadata);
            }
        }
    };

But it still gives me same error :- Caused by: uk.co.jemos.podam.exceptions.PodamMockeryException at uk.co.jemos.podam.api.PodamFactoryImpl.doManufacturePojo(PodamFactoryImpl.java:444) at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojo(PodamFactoryImpl.java:148) at in.cleartax.gst.data.mock.MockDataGenerator.generateMockData(MockDataGenerator.java:101) at in.cleartax.gst.data.mock.MockDataGenerator.lambda$generateData$1(MockDataGenerator.java:78) ... 9 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at uk.co.jemos.podam.api.PodamFactoryImpl.populatePojoInternal(PodamFactoryImpl.java:693) at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:533) at uk.co.jemos.podam.api.PodamFactoryImpl.manufactureAttributeValue(PodamFactoryImpl.java:884) at uk.co.jemos.podam.api.PodamFactoryImpl.populatePojoInternal(PodamFactoryImpl.java:686) at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:533) at uk.co.jemos.podam.api.PodamFactoryImpl.doManufacturePojo(PodamFactoryImpl.java:437) ... 12 more Caused by: java.lang.IllegalArgumentException: nanos > 999999999 or < 0 at java.sql.Timestamp.setNanos(Timestamp.java:389) ... 22 more

Only @PodamStrategyValue works. Also tried adding type manufacturer for Timestamp.class explicitly, that also doesn't work.

Off topic, but where can i find proper usage documentation to Podam.

simarpreets avatar Dec 28 '16 13:12 simarpreets

And did you register your type manufacturer to your factory's strategy? https://github.com/devopsfolks/podam/blob/1d77cc6c0229eefb242f1c5375fe2b91c4777cda/src/test/java/uk/co/jemos/podam/test/unit/steps/PodamFactorySteps.java#L89

daivanov avatar Dec 28 '16 14:12 daivanov

Yes!

simarpreets avatar Dec 28 '16 14:12 simarpreets

Please show your code.

daivanov avatar Dec 28 '16 14:12 daivanov

public class TestMockGenerator {

@Data
public class TestClass {

    private long id;

    private int version;
    

    private Timestamp invoiceDate;

}

public void generate() {

    PodamFactory podamFactory = new PodamFactoryImpl();


    TypeManufacturer<Integer> manufacturer = new IntTypeManufacturerImpl() {

        @Override
        public Integer getInteger(AttributeMetadata attributeMetadata) {

            if (attributeMetadata.getPojoClass() == Timestamp.class) {
                return PodamUtils.getIntegerInRange(0, 999999999);
            } else {
                return super.getInteger(attributeMetadata);
            }
        }
    };

    podamFactory.getStrategy().addOrReplaceTypeManufacturer(Integer.class, manufacturer);

    try {
        TestClass pojo = podamFactory.manufacturePojo(TestClass.class);
        System.out.println(pojo);
    }
    catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

`

simarpreets avatar Dec 28 '16 14:12 simarpreets

Should this line

podamFactory.getStrategy().addOrReplaceTypeManufacturer(Integer.class, manufacturer);

actually look like this?

podamFactory.getStrategy().addOrReplaceTypeManufacturer(int.class, manufacturer);

daivanov avatar Dec 28 '16 14:12 daivanov

Thanks it worked! But in earlier scenario, upon failing shouldn't it try to resort to external factory? (I tried adding one).

simarpreets avatar Dec 28 '16 14:12 simarpreets

Execution is halted by an exception, there is no resorting really.

daivanov avatar Dec 28 '16 14:12 daivanov

Then when does it resort to external factory?

Got to know from code that external factory is only used when there are no constructors available. On another note, how about when we want to have a global Type manufacturer? The object is instantiated using that, but the values are randomly populated using setter. Is there a way to override that.?

simarpreets avatar Dec 28 '16 14:12 simarpreets

This is an offtopic in this issue. Please, ask questions on StackOverflow and use Google group to discuss things.

daivanov avatar Dec 28 '16 16:12 daivanov