-
Notifications
You must be signed in to change notification settings - Fork 3
Description
I noticed that *Specimen-classes have a weird relationship to the SpecimenType-class.
In order to decide whether a specific Specimen can be used for a given SpecimenType, the SpecimenFactory asks the SpecimenType.
Example:
SpecimenFactory:
if (type.isTimeType()) {
return new TimeSpecimen<>(type, context);
}SpecimenType:
public boolean isTimeType() {
if (Temporal.class.isAssignableFrom(asClass())) {
return true;
}
if (TemporalAdjuster.class.isAssignableFrom(asClass())) {
return true;
}
if (TemporalAmount.class.isAssignableFrom(asClass())) {
return true;
}
if (asClass().equals(ZoneId.class)) {
return true;
}
if (asClass().equals(java.util.Date.class)) {
return true;
}
if (asClass().equals(java.sql.Date.class)) {
return true;
}
return false;
}It seems weird to me that a specimen cannot decide for itself. It also introduces some oddly specific "configuration" in the SpecimenType like:
public boolean isSpecialType() {
if (asClass().equals(java.math.BigInteger.class)) {
return true;
}
if (asClass().equals(java.math.BigDecimal.class)) {
return true;
}
if (asClass().equals(java.io.File.class)) {
return true;
}
if (asClass().equals(java.net.URI.class)) {
return true;
}
return false;
}...which in this case covers some classes Fixture cannot instantiate out-of-the-box and hence has to defer to the SpecialSpecimen.
Some helper-methods in SpecimenType seem reasonable as they are simply forwarding class-information, e.g.
public boolean isEnum() {
return asClass().isEnum();
}The above mentioned helpers isTimeType() and isSpecialType() however seem out of place as Specimen-specific information is shared across classes, one of which should not have the responsibility to "know" this information.
I'd like to refactor the code to allow each Specimen to decide on its own whether it can handle a specific type, removing this responsibility from SpecimenType.
This will have the following advantages:
- Specimens are self-contained
SpecimenTypecan be simplified by removing methodsSpecimenFactorycan be simplified by replacing the if-else-chain by a simple loop- It will be easier to register custom Specimens in the future, e.g. for expanded support of KotlinFixture