com.flextrade.jfixture.jodatime.customisation.BasePartialRelay Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfixture-jodatime Show documentation
Show all versions of jfixture-jodatime Show documentation
A joda time customisation for handling Joda Time classes in JFixture.
package com.flextrade.jfixture.jodatime.customisation;
import com.flextrade.jfixture.NoSpecimen;
import com.flextrade.jfixture.SpecimenBuilder;
import com.flextrade.jfixture.SpecimenContext;
import com.flextrade.jfixture.utility.SpecimenType;
import org.joda.time.base.BasePartial;
import java.util.Date;
public class BasePartialRelay implements SpecimenBuilder {
@Override
public Object create(Object request, SpecimenContext context) {
if (!(request instanceof SpecimenType)) {
return new NoSpecimen();
}
SpecimenType type = (SpecimenType) request;
if (!BasePartial.class.isAssignableFrom(type.getRawType())) {
return new NoSpecimen();
}
try {
Date date = (Date) context.resolve(Date.class);
long instant = date.getTime();
return type.getRawType().getDeclaredConstructor(long.class).newInstance(instant);
} catch (Exception e) {
e.printStackTrace();
return new NoSpecimen();
}
}
}