Skip to content

Commit

Permalink
Merge pull request #37 from Nylle/JAVAFIXTURE-35_support-EnumSet
Browse files Browse the repository at this point in the history
JAVAFIXTURE-35: Support EnumSet and EnumMap
  • Loading branch information
Nylle authored Mar 31, 2020
2 parents 7da5a83 + 1a4e27f commit 06dba9b
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -29,6 +30,7 @@
import java.util.stream.IntStream;

import static com.github.nylle.javafixture.CustomizationContext.noContext;
import static java.util.stream.Collectors.toList;

public class CollectionSpecimen<T, G> implements ISpecimen<T> {
private final SpecimenType<T> type;
Expand Down Expand Up @@ -56,7 +58,7 @@ public CollectionSpecimen(final SpecimenType<T> type, final Context context, fin
this.type = type;
this.context = context;

if(type.isParameterized()) {
if (type.isParameterized()) {
this.specimen = specimenFactory.build(SpecimenType.fromClass(type.getGenericTypeArgument(0)));
}
}
Expand All @@ -72,7 +74,11 @@ public T create(final CustomizationContext customizationContext) {
return (T) context.cached(type);
}

Collection<G> collection = context.cached(type, type.isInterface() ? createFromInterfaceType(type.asClass()) : createFromConcreteType(type.asClass()));
if (type.asClass().equals(EnumSet.class)) {
return createEnumSet();
}

Collection<G> collection = context.cached(type, type.isInterface() ? createFromInterfaceType(type.asClass()) : createFromConcreteType(type));

IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
.boxed()
Expand All @@ -82,9 +88,19 @@ public T create(final CustomizationContext customizationContext) {
return (T) collection;
}

private Collection<G> createFromConcreteType(final Class<?> type) {
private <G extends Enum> T createEnumSet() {
final List<G> elements = IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
.boxed()
.filter(x -> specimen != null)
.map(x -> (G) specimen.create())
.collect(toList());

return (T) EnumSet.of(elements.get(0), (G[]) elements.stream().skip(1).toArray(size -> new Enum[size]));
}

private Collection<G> createFromConcreteType(final SpecimenType<T> type) {
try {
return (Collection<G>) type.getDeclaredConstructor().newInstance();
return (Collection<G>) type.asClass().getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new SpecimenException("Unable to create collection of type " + type.getName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.github.nylle.javafixture.SpecimenType;

import java.lang.reflect.InvocationTargetException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
Expand Down Expand Up @@ -65,7 +66,7 @@ public T create(final CustomizationContext customizationContext) {
return (T) context.cached(type);
}

Map<K, V> map = context.cached(type, type.isInterface() ? createFromInterfaceType(type.asClass()) : createFromConcreteType(type.asClass()));
Map<K, V> map = context.cached(type, type.isInterface() ? createFromInterfaceType(type.asClass()) : createFromConcreteType(type));

IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
.boxed()
Expand All @@ -75,9 +76,13 @@ public T create(final CustomizationContext customizationContext) {
return (T) map;
}

private Map<K, V> createFromConcreteType(final Class<T> type) {
private Map<K, V> createFromConcreteType(final SpecimenType<T> type) {
if (type.asClass().equals(EnumMap.class)) {
return (Map<K, V>) new EnumMap((Class<K>) type.getGenericTypeArgument(0));
}

try {
return (Map<K, V>) type.getDeclaredConstructor().newInstance();
return (Map<K, V>) type.asClass().getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new SpecimenException("Unable to create map of type " + type.getName(), e);
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/github/nylle/javafixture/FixtureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.github.nylle.javafixture.testobjects.ITestGeneric;
import com.github.nylle.javafixture.testobjects.ITestGenericInside;
import com.github.nylle.javafixture.testobjects.TestEnum;
import com.github.nylle.javafixture.testobjects.TestObjectGeneric;
import com.github.nylle.javafixture.testobjects.TestObjectWithEnumMap;
import com.github.nylle.javafixture.testobjects.TestObjectWithEnumSet;
import com.github.nylle.javafixture.testobjects.TestObjectWithGenericConstructor;
import com.github.nylle.javafixture.testobjects.TestObjectWithGenerics;
import com.github.nylle.javafixture.testobjects.TestObjectWithNestedGenericInterfaces;
Expand Down Expand Up @@ -410,6 +413,30 @@ void canCreateMapsAndLists() {
assertThat(innerList.get(0).get()).isNotEmpty();
}

@Test
void canCreateEnumSets() {

Fixture fixture = new Fixture(new Configuration().collectionSizeRange(2, 2));

var result = fixture.create(TestObjectWithEnumSet.class);

assertThat(result.getId()).isNotBlank();
assertThat(result.getEnums()).isNotEmpty();
assertThat(result.getEnums().iterator().next()).isInstanceOf(TestEnum.class);
}

@Test
void canCreateEnumMaps() {

Fixture fixture = new Fixture(new Configuration().collectionSizeRange(2, 2));

var result = fixture.create(TestObjectWithEnumMap.class);

assertThat(result.getId()).isNotBlank();
assertThat(result.getEnums()).isNotEmpty();
assertThat(result.getEnums().keySet().iterator().next()).isInstanceOf(TestEnum.class);
}

@Test
void canCreateMany() {
Fixture fixture = new Fixture(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.SpecimenFactory;
import com.github.nylle.javafixture.SpecimenType;
import com.github.nylle.javafixture.testobjects.TestEnum;
import com.github.nylle.javafixture.testobjects.TestObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -12,6 +13,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -223,6 +225,16 @@ void createHashSet() {
assertThat(actual.size()).isEqualTo(2);
}

@Test
void createEnumSet() {
var sut = new CollectionSpecimen<>(new SpecimenType<EnumSet<TestEnum>>(){}, context, specimenFactory);

var actual = sut.create();

assertThat(actual).isNotEmpty();
assertThat(actual.iterator().next()).isInstanceOf(TestEnum.class);
}

@Test
void createLinkedBlockingDeque() {
var sut = new CollectionSpecimen<>(new SpecimenType<LinkedBlockingDeque<String>>(){}, context, specimenFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.SpecimenFactory;
import com.github.nylle.javafixture.SpecimenType;
import com.github.nylle.javafixture.testobjects.TestEnum;
import com.github.nylle.javafixture.testobjects.TestObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -167,6 +169,17 @@ void createTreeMap() {
.isTrue();
}

@Test
void createEnumMap() {
var actual = new MapSpecimen<>(new SpecimenType<EnumMap<TestEnum, Integer>>(){}, context, specimenFactory).create();

assertThat(actual).isInstanceOf(EnumMap.class);
assertThat(actual).isNotEmpty();
assertThat(actual.entrySet().stream()
.allMatch(x -> x.getKey().getClass().equals(TestEnum.class) && x.getValue().getClass().equals(Integer.class)))
.isTrue();
}

@Test
void resultIsCached() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.nylle.javafixture.testobjects;

import java.util.EnumMap;

public class TestObjectWithEnumMap {
private String id;
private EnumMap<TestEnum, String> enums;

public String getId() {
return id;
}

public EnumMap<TestEnum, String> getEnums() {
return enums;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.nylle.javafixture.testobjects;

import java.util.EnumSet;

public class TestObjectWithEnumSet {
private String id;
private EnumSet<TestEnum> enums;

public String getId() {
return id;
}

public EnumSet<TestEnum> getEnums() {
return enums;
}
}

0 comments on commit 06dba9b

Please sign in to comment.