Manufacturing a pojo with a java.sql.Timestamp throws java.lang.IllegalArgumentException: nanos > 999999999 or < 0
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
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;
}
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
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.
Autounboxing is taking care of it. Could you please elaborate what exactly is not working?
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.
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
Yes!
Please show your code.
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;
}
}
`
Should this line
podamFactory.getStrategy().addOrReplaceTypeManufacturer(Integer.class, manufacturer);
actually look like this?
podamFactory.getStrategy().addOrReplaceTypeManufacturer(int.class, manufacturer);
Thanks it worked! But in earlier scenario, upon failing shouldn't it try to resort to external factory? (I tried adding one).
Execution is halted by an exception, there is no resorting really.
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.?
This is an offtopic in this issue. Please, ask questions on StackOverflow and use Google group to discuss things.