Skip to content

Commit

Permalink
refactor: check for pre-defined specimen in factory
Browse files Browse the repository at this point in the history
Refs: #98
  • Loading branch information
akutschera committed Oct 21, 2023
1 parent 568a241 commit 495e251
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public <T> ISpecimen<T> build(final SpecimenType<T> type) {
}

if (type.isEnum()) {
return new EnumSpecimen<>(type, context);
return new EnumSpecimen<>(type );
}

if (type.isCollection()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.nylle.javafixture.specimen;

import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.CustomizationContext;
import com.github.nylle.javafixture.ISpecimen;
import com.github.nylle.javafixture.SpecimenType;
Expand All @@ -12,9 +11,8 @@ public class EnumSpecimen<T> implements ISpecimen<T> {

private final SpecimenType<T> type;
private final Random random;
private final Context context;

public EnumSpecimen(final SpecimenType<T> type, final Context context) {
public EnumSpecimen(final SpecimenType<T> type ) {

if (type == null) {
throw new IllegalArgumentException("type: null");
Expand All @@ -24,17 +22,12 @@ public EnumSpecimen(final SpecimenType<T> type, final Context context) {
throw new IllegalArgumentException("type: " + type.getName());
}

if (context == null) {
throw new IllegalArgumentException("context: null");
}

this.type = type;
this.random = new Random();
this.context = context;
}

@Override
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
return context.preDefined(type, type.getEnumConstants()[random.nextInt(type.getEnumConstants().length)]);
return type.getEnumConstants()[random.nextInt(type.getEnumConstants().length)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class PrimitiveSpecimen<T> implements ISpecimen<T> {
private final SpecimenType<T> type;
private final PseudoRandom pseudoRandom;
private final Configuration configuration;
private final Context context;

public PrimitiveSpecimen(final SpecimenType<T> type, final Context context) {

Expand All @@ -35,46 +34,45 @@ public PrimitiveSpecimen(final SpecimenType<T> type, final Context context) {
this.type = type;
this.pseudoRandom = new PseudoRandom();
this.configuration = context.getConfiguration();
this.context = context;
}

@Override
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
if (type.asClass().equals(String.class)) {
StringConstraints constraints = getStringConstraints(annotations);
return context.preDefined(type, (T) pseudoRandom.nextString(constraints));
return (T) pseudoRandom.nextString(constraints);
}

if (type.asClass().equals(Boolean.class) || type.asClass().equals(boolean.class)) {
return context.preDefined(type, (T) pseudoRandom.nextBool());
return (T) pseudoRandom.nextBool();
}

if (type.asClass().equals(Character.class) || type.asClass().equals(char.class)) {
return context.preDefined(type, (T) pseudoRandom.nextChar());
return (T) pseudoRandom.nextChar();
}

if (type.asClass().equals(Byte.class) || type.asClass().equals(byte.class)) {
return context.preDefined(type, (T) pseudoRandom.nextByte());
return (T) pseudoRandom.nextByte();
}

if (type.asClass().equals(Short.class) || type.asClass().equals(short.class)) {
return context.preDefined(type, (T) pseudoRandom.nextShort(configuration.usePositiveNumbersOnly()));
return (T) pseudoRandom.nextShort(configuration.usePositiveNumbersOnly());
}

if (type.asClass().equals(Integer.class) || type.asClass().equals(int.class)) {
return context.preDefined(type, (T) pseudoRandom.nextInt(configuration.usePositiveNumbersOnly()));
return (T) pseudoRandom.nextInt(configuration.usePositiveNumbersOnly());
}

if (type.asClass().equals(Long.class) || type.asClass().equals(long.class)) {
return context.preDefined(type, (T) pseudoRandom.nextLong(configuration.usePositiveNumbersOnly()));
return (T) pseudoRandom.nextLong(configuration.usePositiveNumbersOnly());
}

if (type.asClass().equals(Float.class) || type.asClass().equals(float.class)) {
return context.preDefined(type, (T) pseudoRandom.nextFloat(configuration.usePositiveNumbersOnly()));
return (T) pseudoRandom.nextFloat(configuration.usePositiveNumbersOnly());
}

if (type.asClass().equals(Double.class) || type.asClass().equals(double.class)) {
return context.preDefined(type, (T) pseudoRandom.nextDouble(configuration.usePositiveNumbersOnly()));
return (T) pseudoRandom.nextDouble(configuration.usePositiveNumbersOnly());
}

throw new SpecimenException("Unsupported type: " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,41 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
if (Temporal.class.isAssignableFrom(type.asClass())) {
try {
Method now = type.asClass().getMethod("now", Clock.class);
return context.preDefined(type, (T) now.invoke(null, context.getConfiguration().getClock()));
return (T) now.invoke(null, context.getConfiguration().getClock());
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new SpecimenException("Unsupported type: " + type.asClass());
}
}

if (type.asClass().equals(java.util.Date.class)) {
return context.preDefined(type, (T) java.sql.Timestamp.valueOf(LocalDateTime.now(context.getConfiguration().getClock())));
return (T) java.sql.Timestamp.valueOf(LocalDateTime.now(context.getConfiguration().getClock()));
}

if (type.asClass().equals(java.sql.Date.class)) {
return context.preDefined(type, (T) java.sql.Date.valueOf(LocalDateTime.now(context.getConfiguration().getClock()).toLocalDate()));
return (T) java.sql.Date.valueOf(LocalDateTime.now(context.getConfiguration().getClock()).toLocalDate());
}

if (type.asClass().equals(MonthDay.class)) {
return context.preDefined(type, (T) MonthDay.now(context.getConfiguration().getClock()));
return (T) MonthDay.now(context.getConfiguration().getClock());
}

if (type.asClass().equals(JapaneseEra.class)) {
return context.preDefined(type, (T) JapaneseEra.values()[random.nextInt(JapaneseEra.values().length)]);
return (T) JapaneseEra.values()[random.nextInt(JapaneseEra.values().length)];
}

if (type.asClass().equals(ZoneOffset.class)) {
return context.preDefined(type, (T) ZoneOffset.ofHours(new Random().nextInt(19)));
return (T) ZoneOffset.ofHours(new Random().nextInt(19));
}
if (type.asClass().equals(Duration.class)) {
return context.preDefined(type, (T) Duration.ofDays(random.nextInt()));
return (T) Duration.ofDays(random.nextInt());
}

if (type.asClass().equals(Period.class)) {
return context.preDefined(type, (T) Period.ofDays(random.nextInt()));
return (T) Period.ofDays(random.nextInt());
}

if (type.asClass().equals(ZoneId.class)) {
return context.preDefined(type, (T) ZoneId.of(ZoneId.getAvailableZoneIds().iterator().next()));
return (T) ZoneId.of(ZoneId.getAvailableZoneIds().iterator().next());
}

throw new SpecimenException("Unsupported type: " + type.asClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,46 @@
package com.github.nylle.javafixture.specimen;

import com.github.nylle.javafixture.Configuration;
import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.SpecimenType;
import com.github.nylle.javafixture.testobjects.TestEnum;
import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.util.Map;

import static com.github.nylle.javafixture.Configuration.configure;
import static com.github.nylle.javafixture.CustomizationContext.noContext;
import static com.github.nylle.javafixture.Fixture.fixture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class EnumSpecimenTest {

@Test
void typeIsRequired() {
assertThatThrownBy(() -> new EnumSpecimen<>(null, new Context(configure())))
assertThatThrownBy(() -> new EnumSpecimen<>(null ))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("type: null");
}

@Test
void onlyEnumTypes() {
assertThatThrownBy(() -> new EnumSpecimen<>(SpecimenType.fromClass(Object.class), new Context(configure())))
assertThatThrownBy(() -> new EnumSpecimen<>(SpecimenType.fromClass(Object.class) ))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("type: " + Object.class.getName());
}

@Test
void contextIsRequired() {
assertThatThrownBy(() -> new EnumSpecimen<>(SpecimenType.fromClass(TestEnum.class), null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("context: null");
void contextIsNotRequired() {
assertThatCode(() -> new EnumSpecimen<>(SpecimenType.fromClass(TestEnum.class) ))
.doesNotThrowAnyException();
}

@Test
void createEnum() {
var sut = new EnumSpecimen<>(SpecimenType.fromClass(TestEnum.class), new Context(configure()));
var sut = new EnumSpecimen<>(SpecimenType.fromClass(TestEnum.class) );

var actual = sut.create(noContext(), new Annotation[0]);

assertThat(actual).isInstanceOf(TestEnum.class);
assertThat(actual.toString()).isIn("VALUE1", "VALUE2", "VALUE3");
}

@Test
void canBePredefined() {
var expected = fixture().create(TestEnum.class);

var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(TestEnum.class), expected));

var sut = new EnumSpecimen<>(SpecimenType.fromClass(TestEnum.class), context);

var actual = sut.create(noContext(), new Annotation[0]);

assertThat(actual).isSameAs(expected);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.github.nylle.javafixture.specimen;

import com.github.nylle.javafixture.Configuration;
import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.SpecimenType;
import com.github.nylle.javafixture.annotations.testcases.TestCase;
import com.github.nylle.javafixture.annotations.testcases.TestWithCases;
import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.util.Map;

import static com.github.nylle.javafixture.Configuration.configure;
import static com.github.nylle.javafixture.CustomizationContext.noContext;
import static com.github.nylle.javafixture.Fixture.fixture;
import static com.github.nylle.javafixture.SpecimenType.fromClass;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -130,26 +127,4 @@ void createDouble(boolean positiveOnly, double min, double max) {
assertThat(actual).isBetween(min, max);
}

@TestWithCases
@TestCase(class1 = String.class)
@TestCase(class1 = Boolean.class)
@TestCase(class1 = Character.class)
@TestCase(class1 = Byte.class)
@TestCase(class1 = Short.class)
@TestCase(class1 = Integer.class)
@TestCase(class1 = Long.class)
@TestCase(class1 = Float.class)
@TestCase(class1 = Double.class)
void canBePredefined(Class type) {
var expected = fixture().create(type);

var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(type), expected));

var sut = new PrimitiveSpecimen<>(SpecimenType.fromClass(type), context);

var actual = sut.create(noContext(), new Annotation[0]);

assertThat(actual).isSameAs(expected);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.Map;

import static com.github.nylle.javafixture.CustomizationContext.noContext;
import static com.github.nylle.javafixture.Fixture.fixture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -130,37 +129,4 @@ void createWithClock() {
assertThat(actual).isEqualTo(Instant.MIN);
}

@TestWithCases
@TestCase(class1 = Instant.class)
@TestCase(class1 = HijrahDate.class)
@TestCase(class1 = JapaneseDate.class)
@TestCase(class1 = LocalDate.class)
@TestCase(class1 = LocalDateTime.class)
@TestCase(class1 = LocalTime.class)
@TestCase(class1 = MinguoDate.class)
@TestCase(class1 = OffsetDateTime.class)
@TestCase(class1 = OffsetTime.class)
@TestCase(class1 = ThaiBuddhistDate.class)
@TestCase(class1 = Year.class)
@TestCase(class1 = YearMonth.class)
@TestCase(class1 = ZonedDateTime.class)
@TestCase(class1 = java.sql.Date.class)
@TestCase(class1 = java.util.Date.class)
@TestCase(class1 = Duration.class)
@TestCase(class1 = JapaneseEra.class)
@TestCase(class1 = MonthDay.class)
@TestCase(class1 = Period.class)
@TestCase(class1 = ZoneId.class)
@TestCase(class1 = ZoneOffset.class)
void canBePredefined(Class type) {
var expected = fixture().create(type);

var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(type), expected));

var sut = new TimeSpecimen<>(SpecimenType.fromClass(type), context);

var actual = sut.create(noContext(), new Annotation[0]);

assertThat(actual).isSameAs(expected);
}
}

0 comments on commit 495e251

Please sign in to comment.