six2six / fixture-factory

Generator fake objects from a template
Apache License 2.0
445 stars 88 forks source link

Fixtures Factory does not generate values for Short type #41

Closed marcello1975 closed 10 years ago

marcello1975 commented 10 years ago

Boa tarde, Outro detalhe que não sei se interessa a vocês, é que para o caso da RandomFunction, se o cara informar um range fora dos valores MAX/MIN do tipo (Short por exemplo) ele retorna com perda de precisão, por exemplo, se o cara informar 99999 como Range máximo para um Short quando é dado "this.range.getEnd().shortValue()" ele retorna um valor negativo (-31073) por que esse número não cabe nos bits de um Short. Eu entendo que "poxa se o cara está pedindo um Short porque vai colocar um range fora do tamanho do tipo???", mas caso queiram tratar isso seria tipo uma cereja no bolo... Talvez já no momento da criação do template lançar uma exceção tipo "RangeOutBoundException"... ai o cara nem consegue criar o template errado... pode ser preciosismo de minha parte, mas sei lá vejam ai que acham da ideia..

Att, Marcello Ribeiro

I do have a simple java Object which has a property of Type Short.

What happens is that a simple "add/random" for this Short type property does not take any effect and the property is still null after gimme is called;

    Fixture.of(Trajeto.class).addTemplate("valid", new Rule() {
        {
            add("codPeriodo", random(Short.class, range(1, 99999)));
        }

    });
    Trajeto trajeto = Fixture.from(Trajeto.class).gimme("valid");

Does anyone have any words on this? or is it a really bug in the API?

marcello1975 commented 10 years ago

Same behaviour for BigDecimal and others...

marcello1975 commented 10 years ago

Problem is in this method when it relies that most of Number sons are assignable from Integer, Long and others:

@Override @SuppressWarnings("unchecked") public T generateValue() { Object result = null; Random random = new Random();

    if (this.dataset != null && this.dataset.length > 0) {
        result = this.dataset[random.nextInt(this.dataset.length)];

    } else if (this.functions != null && this.functions.length > 0) { 
        result = this.functions[random.nextInt(this.functions.length)].generateValue();

    } else if (this.type.isEnum()) {
        result = this.type.getEnumConstants()[random.nextInt(this.type.getEnumConstants().length)];

    } else if (this.type.isAssignableFrom(Integer.class)) {
        result = this.range == null ? random.nextInt() : (this.range.getStart().intValue() + (int)(Math.random() * (this.range.getEnd().intValue() - this.range.getStart().intValue()) + 1));

    } else if (this.type.isAssignableFrom(Long.class)) {
        result = this.range == null ? random.nextLong() : (this.range.getStart().longValue() + (long)(Math.random() * (this.range.getEnd().longValue() - this.range.getStart().longValue()) + 1));

    } else if (this.type.isAssignableFrom(Float.class)) {
        result = this.range == null ? random.nextFloat() : (this.range.getStart().floatValue() + (float)(Math.random() * (this.range.getEnd().floatValue() - this.range.getStart().floatValue()) + 1));

    } else if (this.type.isAssignableFrom(Double.class)) {
        result = this.range == null ? random.nextDouble() : (this.range.getStart().doubleValue() + (double)(Math.random() * (this.range.getEnd().doubleValue() - this.range.getStart().doubleValue()) + 1));

    } else if (this.type.isAssignableFrom(Boolean.class)) {
        result = random.nextBoolean();  
    }

    return (T) result;
}
ahirata commented 10 years ago

@marcello1975 thank you for reporting this!