From 8066d2d006655333d3978af80f377627c5b33dd0 Mon Sep 17 00:00:00 2001 From: Stewart Bissett Date: Fri, 14 Sep 2018 09:28:16 +0100 Subject: [PATCH] Extend testing for constructor based creation of random instances --- .../org/exparity/stub/bean/BeanBuilder.java | 54 +--- .../core/NoDefaultConstructorException.java | 15 ++ .../exparity/stub/core/ValueFactories.java | 10 +- .../exparity/stub/random/RandomBuilder.java | 117 ++++++++- .../org/exparity/stub/stub/StubBuilder.java | 14 +- .../exparity/stub/bean/BeanBuilderTest.java | 19 +- .../stub/core/ValueFactoriesTest.java | 14 +- .../stub/random/RandomBuilderTest.java | 43 ++++ .../type/ConstructorOnlyAllTypes.java | 234 ++++++++++++++++++ .../type/ConstructorOnlyCollectionTypes.java | 55 ++++ .../type/ConstructorOnlyEnumTypes.java | 19 ++ .../testutils/type/ConstructorOnlyNested.java | 14 ++ .../type/ConstructorOnlyObjectTypes.java | 132 ++++++++++ .../type/ConstructorOnlyPrimitiveTypes.java | 71 ++++++ 14 files changed, 731 insertions(+), 80 deletions(-) create mode 100644 src/main/java/org/exparity/stub/core/NoDefaultConstructorException.java create mode 100644 src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyAllTypes.java create mode 100644 src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyCollectionTypes.java create mode 100644 src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyEnumTypes.java create mode 100644 src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyNested.java create mode 100644 src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyObjectTypes.java create mode 100644 src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyPrimitiveTypes.java diff --git a/src/main/java/org/exparity/stub/bean/BeanBuilder.java b/src/main/java/org/exparity/stub/bean/BeanBuilder.java index 14f2b0f..1517235 100644 --- a/src/main/java/org/exparity/stub/bean/BeanBuilder.java +++ b/src/main/java/org/exparity/stub/bean/BeanBuilder.java @@ -6,8 +6,6 @@ import static org.exparity.stub.core.ValueFactories.*; import java.lang.reflect.Array; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.ParameterizedType; import java.math.BigDecimal; import java.time.Instant; @@ -18,8 +16,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -35,6 +31,8 @@ import org.exparity.beans.core.TypeProperty; import org.exparity.beans.core.naming.ForceRootNameNamingStrategy; import org.exparity.beans.core.naming.LowerCaseNamingStrategy; +import org.exparity.stub.core.NoDefaultConstructorException; +import org.exparity.stub.core.ValueFactories; import org.exparity.stub.core.ValueFactory; import org.exparity.stub.random.RandomBuilder; import org.slf4j.Logger; @@ -560,27 +558,6 @@ private void populateProperty(final Object instance, } } - private Object[] createArguments(final java.lang.reflect.Type[] types) { - if (types.length == 0) { - return new Object[0]; - } else { - Object[] args = new Object[types.length]; - for (int i = 0; i < types.length; ++i) { - Type type = Type.type(getActualType(types[i], 0)); - if (type.is(Map.class)) { - args[i] = Collections.emptyMap(); - } else if (type.is(Set.class)) { - args[i] = Collections.emptySet(); - } else if (type.is(List.class) || type.is(Collection.class)) { - args[i] = Collections.emptyList(); - } else { - args[i] = createValue(getActualType(types[i], 0)); - } - } - return args; - } - } - private Class getActualType(final java.lang.reflect.Type type, final int typeOrdinal) { if (type instanceof Class) { return (Class) type; @@ -674,7 +651,7 @@ private E createValue(final Class type) { } else if (type.isEnum()) { return createValue(aRandomEnum(type), type); } else { - return createValue(aNewInstanceOf(type), type); + return createValue(ValueFactories.anEmptyInstanceOf(type), type); } } case EMPTY: { @@ -684,7 +661,7 @@ private E createValue(final Class type) { } else if (type.isEnum()) { return null; } else { - return createValue(aNewInstanceOf(type), type); + return createValue(ValueFactories.anEmptyInstanceOf(type), type); } } default: @@ -775,28 +752,15 @@ private Map createMap(final Class keyType, private T createNewInstance() { try { - Constructor constructor = findConstructor(); - java.lang.reflect.Type[] params = constructor.getGenericParameterTypes(); - Object[] initargs = createArguments(params); - return constructor.newInstance(initargs); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + return ValueFactories.anEmptyInstanceOf(type).createValue(); + } catch (NoDefaultConstructorException e) { + throw e; + } catch (Exception e) { throw new BeanBuilderException("Failed to instantiate '" + this.type + "'. Error [" + e.getMessage() + "]", e); } } - private Constructor findConstructor() { - List> constructors = Arrays.asList((Constructor[]) this.type.getConstructors()); - Collections.sort(constructors, new Comparator>() { - - @Override - public int compare(final Constructor o1, final Constructor o2) { - return Integer.valueOf(o1.getParameterCount()).compareTo(o2.getParameterCount()); - } - }); - return constructors.get(0); - } - private int collectionSize(final BeanPropertyPath path) { return selectNotNull(this.collectionSizeForPaths.get(path.fullPath()), this.collectionSizeForPaths.get(path.fullPathWithNoIndexes()), @@ -811,7 +775,7 @@ private String propertyName(final BeanPropertyPath path) { private List> createInstanceOfFactoriesForTypes(final Class... subtypes) { List> factories = new ArrayList<>(); for (Class subtype : subtypes) { - factories.add((ValueFactory) aNewInstanceOf(subtype)); + factories.add((ValueFactory) ValueFactories.anEmptyInstanceOf(subtype)); } return factories; } diff --git a/src/main/java/org/exparity/stub/core/NoDefaultConstructorException.java b/src/main/java/org/exparity/stub/core/NoDefaultConstructorException.java new file mode 100644 index 0000000..fe00ccf --- /dev/null +++ b/src/main/java/org/exparity/stub/core/NoDefaultConstructorException.java @@ -0,0 +1,15 @@ +package org.exparity.stub.core; + +/** + * Typed exception so it's explicit when failure is due to a missing default constructor + * + * @author Stewart Bissett + */ +public class NoDefaultConstructorException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public NoDefaultConstructorException(final Class type, final InstantiationException e) { + super("Class '" + type.getSimpleName() + "'has no default constructor", e); + } +} diff --git a/src/main/java/org/exparity/stub/core/ValueFactories.java b/src/main/java/org/exparity/stub/core/ValueFactories.java index 6756eb2..3cee208 100644 --- a/src/main/java/org/exparity/stub/core/ValueFactories.java +++ b/src/main/java/org/exparity/stub/core/ValueFactories.java @@ -16,11 +16,11 @@ import java.util.Date; import java.util.List; -import org.exparity.stub.bean.BeanBuilder; import org.exparity.stub.random.RandomBuilder; + /** - * Static factory for creating instances of {@link ValueFactory} and {@link ArrayFactory} for use in the {@link BeanBuilder} and {@link RandomBuilder} + * Static factory for creating instances of {@link ValueFactory} and {@link ArrayFactory} for use in the {@link org.exparity.stub.bean.BeanBuilder} and {@link RandomBuilder} * * @author Stewart Bissett */ @@ -216,10 +216,12 @@ public static ArrayFactory aRandomArrayOf(final ValueFactory typeFacto * @param type the type to create a new instance of * @return an {@link ArrayFactory} which returns a new unpopulated instance of the given type. */ - public static ValueFactory aNewInstanceOf(final Class type) { + public static ValueFactory anEmptyInstanceOf(final Class type) { return () -> { try { return type.newInstance(); + } catch (InstantiationException e) { + throw new NoDefaultConstructorException(type, e); } catch (Exception e) { throw new ValueFactoryException( "Failed to instantiate instance of '" + type.getCanonicalName() + "'", @@ -244,7 +246,7 @@ public static ValueFactory oneOf(final ValueFactory... factories) { * @return an {@link ValueFactory} which returns a random instance of the given type by using one of the supplied {@link ValueFactory} instances. */ public static ValueFactory oneOf(final Collection> factories) { - return () -> new ArrayList>(factories).get(nextInt(factories.size())).createValue(); + return () -> new ArrayList<>(factories).get(nextInt(factories.size())).createValue(); } /** diff --git a/src/main/java/org/exparity/stub/random/RandomBuilder.java b/src/main/java/org/exparity/stub/random/RandomBuilder.java index 0d772a9..835b866 100644 --- a/src/main/java/org/exparity/stub/random/RandomBuilder.java +++ b/src/main/java/org/exparity/stub/random/RandomBuilder.java @@ -1,10 +1,20 @@ package org.exparity.stub.random; +import static java.util.Collections.singleton; +import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; +import static java.util.Comparator.comparing; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.apache.commons.lang.math.RandomUtils.*; import static org.apache.commons.lang.time.DateUtils.addSeconds; +import static org.exparity.stub.core.ValueFactories.anEmptyInstanceOf; +import static org.exparity.stub.core.ValueFactories.theValue; +import java.lang.reflect.Array; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.ParameterizedType; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; @@ -20,7 +30,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Stream; +import org.exparity.beans.Type; import org.exparity.stub.bean.BeanBuilder; import org.exparity.stub.bean.BeanBuilderException; import org.exparity.stub.core.ValueFactories; @@ -72,6 +86,12 @@ public static interface RandomRestriction { put(String.class, ValueFactories.aRandomString()); put(BigDecimal.class, ValueFactories.aRandomDecimal()); put(Date.class, ValueFactories.aRandomDate()); + put(Date.class, ValueFactories.aRandomDate()); + put(LocalDate.class, ValueFactories.aRandomLocalDate()); + put(LocalTime.class, ValueFactories.aRandomLocalTime()); + put(LocalDateTime.class, ValueFactories.aRandomLocalDateTime()); + put(ZonedDateTime.class, ValueFactories.aRandomZonedDateTime()); + put(Instant.class, ValueFactories.aRandomInstant()); } }; @@ -802,14 +822,105 @@ public static

RandomRestriction collectionSizeForProperty(final String prope private static ValueFactory instanceFactoryFor(final Class type, final RandomRestriction... restrictions) { ValueFactory factory = RANDOM_FACTORIES.get(type); - if (factory == null) { + if (factory != null) { + return factory; + } else if (type.isEnum()) { + return ValueFactories.aRandomEnum(type); + } else if (isBean(type)) { BeanBuilder builder = BeanBuilder.aRandomInstanceOf(type); for (RandomRestriction restriction : restrictions) { restriction.applyTo(builder); } - factory = ValueFactories.theValue(builder.build()); + return ValueFactories.theValue(builder.build()); + } else { + Optional> constructor = findConstructor(type); + if (constructor.isPresent()) { + return theValue(createInstance(constructor.get())); + } else { + return theValue(anEmptyInstanceOf(type).createValue()); + } + } + } + + private static boolean isBean(final Class type) { + // TODO Look for default constructor and get/set of properties + return hasDefaultConstructor(type); + } + + private static boolean hasDefaultConstructor(final Class type) { + return Stream.of(type.getConstructors()).map(Constructor::getParameterCount).anyMatch(count -> count == 0); + } + + @SuppressWarnings("unchecked") + private static Optional> findConstructor(final Class type) { + return Stream.of((Constructor[]) type.getConstructors()).min(comparing(Constructor::getParameterCount)); + } + + private static T createInstance(final Constructor constructor) { + try { + java.lang.reflect.Type[] params = constructor.getGenericParameterTypes(); + Object[] initargs = createArguments(params); + return constructor.newInstance(initargs); + } catch (InstantiationException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException e) { + throw new RandomBuilderException("Could not instantiate type " + constructor.getDeclaringClass().getName() + + " using " + + constructor, e); + } + } + + private static Object[] createArguments(final java.lang.reflect.Type[] types) { + if (types.length == 0) { + return new Object[0]; + } else { + Object[] args = new Object[types.length]; + for (int i = 0; i < types.length; ++i) { + Class rawType = getRawType(types[i]); + Type type = Type.type(rawType); + if (type.isArray()) { + // TODO use size restrictions to populate correct size + Class arrayType = rawType.getComponentType(); + Object value = Array.newInstance(arrayType, 0); + args[i] = value; + } else if (type.is(Map.class)) { + // TODO use size restrictions to populate correct size + args[i] = singletonMap(getActualType(types[i], 0), getActualType(types[i], 1)); + } else if (type.is(Set.class)) { + // TODO use size restrictions to populate correct size + args[i] = singleton(aRandomInstanceOf(getActualType(types[i], 0))); + } else if (type.is(List.class) || type.is(Collection.class)) { + // TODO use size restrictions to populate correct size + args[i] = singletonList(aRandomInstanceOf(getActualType(types[i], 0))); + } else { + args[i] = aRandomInstanceOf(rawType); + } + } + return args; + } + } + + @SuppressWarnings("unchecked") + private static Class getActualType(final java.lang.reflect.Type type, final int typeOrdinal) { + if (type instanceof Class) { + return (Class) type; + } else if (type instanceof ParameterizedType) { + return getActualType(((ParameterizedType) type).getActualTypeArguments()[typeOrdinal], 0); + } else { + throw new IllegalArgumentException("Unknown type subclass '" + type.getClass()); + } + } + + @SuppressWarnings("unchecked") + private static Class getRawType(final java.lang.reflect.Type type) { + if (type instanceof Class) { + return (Class) type; + } else if (type instanceof ParameterizedType) { + return getRawType(((ParameterizedType) type).getRawType()); + } else { + throw new IllegalArgumentException("Unknown type subclass '" + type.getClass()); } - return factory; } } diff --git a/src/main/java/org/exparity/stub/stub/StubBuilder.java b/src/main/java/org/exparity/stub/stub/StubBuilder.java index f486074..5f12cec 100644 --- a/src/main/java/org/exparity/stub/stub/StubBuilder.java +++ b/src/main/java/org/exparity/stub/stub/StubBuilder.java @@ -1,6 +1,6 @@ package org.exparity.stub.stub; -import static org.exparity.stub.core.ValueFactories.aNewInstanceOf; +import static org.exparity.stub.core.ValueFactories.anEmptyInstanceOf; import static org.exparity.stub.core.ValueFactories.oneOf; import java.lang.reflect.Type; @@ -33,7 +33,7 @@ public static StubBuilder aRandomStubOf(final Class type) { throw new IllegalArgumentException( "Use StubBuilder.aRandomStubOf(final TypeReference typeRef) method to create prototypes for generic types. See javadocs on method for example."); } - return new StubBuilder(type); + return new StubBuilder<>(type); } /** @@ -63,18 +63,18 @@ public static StubBuilder aRandomStubOf(final Class type) { * @param type the type to return the {@link StubBuilder} for */ public static StubBuilder aRandomStubOf(final TypeReference type) { - return new StubBuilder(type.getType()); + return new StubBuilder<>(type.getType()); } private final StubDefinition definition; private final StubFactory factory = new StubFactory(); private StubBuilder(final Type type) { - this.definition = new StubDefinition(type); + this.definition = new StubDefinition<>(type); } private StubBuilder(final Class type) { - this.definition = new StubDefinition(type); + this.definition = new StubDefinition<>(type); } /** @@ -200,9 +200,9 @@ public T build() { } private List> createInstanceOfFactoriesForTypes(final Class... subtypes) { - List> factories = new ArrayList>(); + List> factories = new ArrayList<>(); for (Class subtype : subtypes) { - factories.add((ValueFactory) aNewInstanceOf(subtype)); + factories.add((ValueFactory) anEmptyInstanceOf(subtype)); } return factories; } diff --git a/src/test/java/org/exparity/stub/bean/BeanBuilderTest.java b/src/test/java/org/exparity/stub/bean/BeanBuilderTest.java index fe8fcc0..d69f5f3 100644 --- a/src/test/java/org/exparity/stub/bean/BeanBuilderTest.java +++ b/src/test/java/org/exparity/stub/bean/BeanBuilderTest.java @@ -13,13 +13,13 @@ import org.exparity.beans.core.BeanProperty; import org.exparity.beans.core.BeanPropertyException; import org.exparity.beans.core.BeanVisitor; +import org.exparity.stub.core.NoDefaultConstructorException; import org.exparity.stub.core.ValueFactory; import org.exparity.stub.testutils.type.AllTypes; import org.exparity.stub.testutils.type.Car; import org.exparity.stub.testutils.type.Circle; import org.exparity.stub.testutils.type.Employee; import org.exparity.stub.testutils.type.Engine; -import org.exparity.stub.testutils.type.Immutable; import org.exparity.stub.testutils.type.Manager; import org.exparity.stub.testutils.type.NoDefaultConstructor; import org.exparity.stub.testutils.type.Person; @@ -259,19 +259,10 @@ public void canSetOneOrMoreSubTypes() { assertThat(shapeSorter.getShape(), anyOf(instanceOf(Square.class), instanceOf(Circle.class))); } - @Test - public void canBuildARandomInstanceWithNoDefaultConstructor() { - NoDefaultConstructor instance = aRandomInstanceOf(NoDefaultConstructor.class).build(); - assertThat(instance, any(NoDefaultConstructor.class)); - assertThat(instance.getValue(), notNullValue()); - } - - @Test - public void canBuildARandomInstanceOfImmutableType() { - Immutable instance = aRandomInstanceOf(Immutable.class).build(); - assertThat(instance, any(Immutable.class)); - assertThat(instance.getValue(), notNullValue()); - } + @Test(expected = NoDefaultConstructorException.class) + public void canNotCreateAnInstanceWithNoDefaultConstructor() { + aRandomInstanceOf(NoDefaultConstructor.class).build(); + } @Test public void canCreateBigDecimalsWithAPrecisionLessThan10() { diff --git a/src/test/java/org/exparity/stub/core/ValueFactoriesTest.java b/src/test/java/org/exparity/stub/core/ValueFactoriesTest.java index c3ee696..a084129 100644 --- a/src/test/java/org/exparity/stub/core/ValueFactoriesTest.java +++ b/src/test/java/org/exparity/stub/core/ValueFactoriesTest.java @@ -22,20 +22,20 @@ /** * Unit test for the {@link ValueFactories} class - * + * * @author Stewart Bissett */ @SuppressWarnings("unchecked") public class ValueFactoriesTest { @Test - public void canCreateANewInstanceOf() { - checkResult(aNewInstanceOf(Car.class), any(Car.class)); + public void canCreateAnEmptyInstanceOf() { + checkResult(anEmptyInstanceOf(Car.class), any(Car.class)); } - @Test(expected = ValueFactoryException.class) - public void canFailToCreateANewInstanceOfWithNoDefaultConstructir() { - checkResult(aNewInstanceOf(NoDefaultConstructor.class), any(NoDefaultConstructor.class)); + @Test(expected = NoDefaultConstructorException.class) + public void canFailToCreateANewInstanceOfWithNoDefaultConstructor() { + checkResult(anEmptyInstanceOf(NoDefaultConstructor.class), any(NoDefaultConstructor.class)); } @Test @@ -84,7 +84,7 @@ public void canCreateARandomLocalDate() { public void canCreateARandomLocalDateTime() { checkResult(aRandomLocalDateTime(), any(LocalDateTime.class)); } - + @Test public void canCreateARandomZonedDateTime() { checkResult(aRandomZonedDateTime(), any(ZonedDateTime.class)); diff --git a/src/test/java/org/exparity/stub/random/RandomBuilderTest.java b/src/test/java/org/exparity/stub/random/RandomBuilderTest.java index 888f38c..f588763 100644 --- a/src/test/java/org/exparity/stub/random/RandomBuilderTest.java +++ b/src/test/java/org/exparity/stub/random/RandomBuilderTest.java @@ -22,6 +22,12 @@ import org.exparity.stub.testutils.type.Car; import org.exparity.stub.testutils.type.Circle; import org.exparity.stub.testutils.type.CollectionOfGenerics; +import org.exparity.stub.testutils.type.ConstructorOnlyAllTypes; +import org.exparity.stub.testutils.type.ConstructorOnlyCollectionTypes; +import org.exparity.stub.testutils.type.ConstructorOnlyEnumTypes; +import org.exparity.stub.testutils.type.ConstructorOnlyNested; +import org.exparity.stub.testutils.type.ConstructorOnlyObjectTypes; +import org.exparity.stub.testutils.type.ConstructorOnlyPrimitiveTypes; import org.exparity.stub.testutils.type.Employee; import org.exparity.stub.testutils.type.EmptyEnum; import org.exparity.stub.testutils.type.Engine; @@ -251,6 +257,43 @@ public void canBuildARandomInstanceOfImmutableType() { assertThat(instance.getValue(), notNullValue()); } + @Test + public void canBuildARandomInstanceOfAllTypesViaConstructor() { + ConstructorOnlyAllTypes instance = aRandomInstanceOf(ConstructorOnlyAllTypes.class); + assertThat(instance, any(ConstructorOnlyAllTypes.class)); + } + + @Test + public void canBuildARandomInstanceOfNestedViaConstructor() { + ConstructorOnlyNested instance = aRandomInstanceOf(ConstructorOnlyNested.class); + assertThat(instance, any(ConstructorOnlyNested.class)); + assertThat(instance.getNested().getValue(), notNullValue()); + } + + @Test + public void canBuildARandomInstanceOfPrimitivesViaConstructor() { + ConstructorOnlyPrimitiveTypes instance = aRandomInstanceOf(ConstructorOnlyPrimitiveTypes.class); + assertThat(instance, any(ConstructorOnlyPrimitiveTypes.class)); + } + + @Test + public void canBuildARandomInstanceOfEnumsViaConstructor() { + ConstructorOnlyEnumTypes instance = aRandomInstanceOf(ConstructorOnlyEnumTypes.class); + assertThat(instance, any(ConstructorOnlyEnumTypes.class)); + } + + @Test + public void canBuildARandomInstanceOfCollectionsViaConstructor() { + ConstructorOnlyCollectionTypes instance = aRandomInstanceOf(ConstructorOnlyCollectionTypes.class); + assertThat(instance, any(ConstructorOnlyCollectionTypes.class)); + } + + @Test + public void canBuildARandomInstanceOfObjectsViaConstructor() { + ConstructorOnlyObjectTypes instance = aRandomInstanceOf(ConstructorOnlyObjectTypes.class); + assertThat(instance, any(ConstructorOnlyObjectTypes.class)); + } + @Test public void canBuildARandomInstanceAndSetProperty() { BigDecimal capacity = new BigDecimal("1.8"); diff --git a/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyAllTypes.java b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyAllTypes.java new file mode 100644 index 0000000..6b55150 --- /dev/null +++ b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyAllTypes.java @@ -0,0 +1,234 @@ +package org.exparity.stub.testutils.type; + +import java.math.BigDecimal; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZonedDateTime; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class ConstructorOnlyAllTypes { + + public static enum EnumValues { + VALUE_1, VALUE_2 + }; + + private final ConstructorOnlyAllTypes.EnumValues enumValue; + private final String stringValue; + private final Integer integerObjectValue; + private final int integerValue; + private final Long longObjectValue; + private final long longValue; + private final Short shortObjectValue; + private final short shortValue; + private final Double doubleObjectValue; + private final double doubleValue; + private final Float floatObjectValue; + private final float floatValue; + private final Character charObjectValue; + private final char charValue; + private final Byte byteObjectValue; + private final byte byteValue; + private final Date dateValue; + private final LocalDate localDateValue; + private final LocalTime localTimeValue; + private final LocalDateTime localDateTimeValue; + private final ZonedDateTime zonedlDateTimeValue; + private final Instant instantValue; + private final BigDecimal bigDecimalValue; + private final int[] array; + private final Collection collection; + private final List list; + private final Set set; + private final Map map; + private final boolean booleanValue; + private final Boolean booleanObjectValue; + + public ConstructorOnlyAllTypes(final EnumValues enumValue, + final String stringValue, + final Integer integerObjectValue, + final int integerValue, + final Long longObjectValue, + final long longValue, + final Short shortObjectValue, + final short shortValue, + final Double doubleObjectValue, + final double doubleValue, + final Float floatObjectValue, + final float floatValue, + final Character charObjectValue, + final char charValue, + final Byte byteObjectValue, + final byte byteValue, + final Date dateValue, + final LocalDate localDateValue, + final LocalTime localTimeValue, + final LocalDateTime localDateTimeValue, + final ZonedDateTime zonedlDateTimeValue, + final Instant instantValue, + final BigDecimal bigDecimalValue, + final int[] array, + final Collection collection, + final List list, + final Set set, + final Map map, + final boolean booleanValue, + final Boolean booleanObjectValue) { + this.enumValue = enumValue; + this.stringValue = stringValue; + this.integerObjectValue = integerObjectValue; + this.integerValue = integerValue; + this.longObjectValue = longObjectValue; + this.longValue = longValue; + this.shortObjectValue = shortObjectValue; + this.shortValue = shortValue; + this.doubleObjectValue = doubleObjectValue; + this.doubleValue = doubleValue; + this.floatObjectValue = floatObjectValue; + this.floatValue = floatValue; + this.charObjectValue = charObjectValue; + this.charValue = charValue; + this.byteObjectValue = byteObjectValue; + this.byteValue = byteValue; + this.dateValue = dateValue; + this.localDateValue = localDateValue; + this.localTimeValue = localTimeValue; + this.localDateTimeValue = localDateTimeValue; + this.zonedlDateTimeValue = zonedlDateTimeValue; + this.instantValue = instantValue; + this.bigDecimalValue = bigDecimalValue; + this.array = array; + this.collection = collection; + this.list = list; + this.set = set; + this.map = map; + this.booleanValue = booleanValue; + this.booleanObjectValue = booleanObjectValue; + } + + public ConstructorOnlyAllTypes.EnumValues getEnumValue() { + return enumValue; + } + + public String getStringValue() { + return stringValue; + } + + public Integer getIntegerObjectValue() { + return integerObjectValue; + } + + public int getIntegerValue() { + return integerValue; + } + + public Long getLongObjectValue() { + return longObjectValue; + } + + public long getLongValue() { + return longValue; + } + + public Short getShortObjectValue() { + return shortObjectValue; + } + + public short getShortValue() { + return shortValue; + } + + public Double getDoubleObjectValue() { + return doubleObjectValue; + } + + public double getDoubleValue() { + return doubleValue; + } + + public Float getFloatObjectValue() { + return floatObjectValue; + } + + public float getFloatValue() { + return floatValue; + } + + public Character getCharObjectValue() { + return charObjectValue; + } + + public char getCharValue() { + return charValue; + } + + public Byte getByteObjectValue() { + return byteObjectValue; + } + + public byte getByteValue() { + return byteValue; + } + + public Date getDateValue() { + return dateValue; + } + + public LocalDate getLocalDateValue() { + return localDateValue; + } + + public LocalTime getLocalTimeValue() { + return localTimeValue; + } + + public LocalDateTime getLocalDateTimeValue() { + return localDateTimeValue; + } + + public ZonedDateTime getZonedlDateTimeValue() { + return zonedlDateTimeValue; + } + + public Instant getInstantValue() { + return instantValue; + } + + public BigDecimal getBigDecimalValue() { + return bigDecimalValue; + } + + public int[] getArray() { + return array; + } + + public Collection getCollection() { + return collection; + } + + public List getList() { + return list; + } + + public Set getSet() { + return set; + } + + public Map getMap() { + return map; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public Boolean getBooleanObjectValue() { + return booleanObjectValue; + } + +} \ No newline at end of file diff --git a/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyCollectionTypes.java b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyCollectionTypes.java new file mode 100644 index 0000000..b898852 --- /dev/null +++ b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyCollectionTypes.java @@ -0,0 +1,55 @@ +package org.exparity.stub.testutils.type; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class ConstructorOnlyCollectionTypes { + + private int[] array; + private final String[] stringArray; + private final Collection collection; + private final List list; + private final Set set; + private final Map map; + + public ConstructorOnlyCollectionTypes(final int[] array, + final String[] stringArray, + final Collection collection, + final List list, + final Set set, + final Map map) { + this.array = array; + this.stringArray = stringArray; + this.collection = collection; + this.list = list; + this.set = set; + this.map = map; + } + + public int[] getArray() { + return array; + } + + public String[] getStringArray() { + return stringArray; + } + + public Collection getCollection() { + return collection; + } + + public List getList() { + return list; + } + + public Set getSet() { + return set; + } + + public Map getMap() { + return map; + } + +} \ No newline at end of file diff --git a/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyEnumTypes.java b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyEnumTypes.java new file mode 100644 index 0000000..389cc22 --- /dev/null +++ b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyEnumTypes.java @@ -0,0 +1,19 @@ +package org.exparity.stub.testutils.type; + +public class ConstructorOnlyEnumTypes { + + public static enum EnumValues { + VALUE_1, VALUE_2 + }; + + private final ConstructorOnlyEnumTypes.EnumValues enumValue; + + public ConstructorOnlyEnumTypes(final EnumValues enumValue) { + this.enumValue = enumValue; + } + + public ConstructorOnlyEnumTypes.EnumValues getEnumValue() { + return enumValue; + } + +} \ No newline at end of file diff --git a/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyNested.java b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyNested.java new file mode 100644 index 0000000..d058a6d --- /dev/null +++ b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyNested.java @@ -0,0 +1,14 @@ +package org.exparity.stub.testutils.type; + +public class ConstructorOnlyNested { + + private final NoDefaultConstructor nested; + + public ConstructorOnlyNested(final NoDefaultConstructor nested) { + this.nested = nested; + } + + public NoDefaultConstructor getNested() { + return nested; + } +} diff --git a/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyObjectTypes.java b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyObjectTypes.java new file mode 100644 index 0000000..84ad9c1 --- /dev/null +++ b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyObjectTypes.java @@ -0,0 +1,132 @@ +package org.exparity.stub.testutils.type; + +import java.math.BigDecimal; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZonedDateTime; +import java.util.Date; + +public class ConstructorOnlyObjectTypes { + + public static enum EnumValues { + VALUE_1, VALUE_2 + }; + + private final String stringValue; + private final Integer integerObjectValue; + private final Long longObjectValue; + private final Short shortObjectValue; + private final Double doubleObjectValue; + private final Float floatObjectValue; + private final Character charObjectValue; + private final Byte byteObjectValue; + private final Date dateValue; + private final LocalDate localDateValue; + private final LocalTime localTimeValue; + private final LocalDateTime localDateTimeValue; + private final ZonedDateTime zonedlDateTimeValue; + private final Instant instantValue; + private final BigDecimal bigDecimalValue; + private final Boolean booleanObjectValue; + + public ConstructorOnlyObjectTypes(final String stringValue, + final Integer integerObjectValue, + final Long longObjectValue, + final Short shortObjectValue, + final Double doubleObjectValue, + final Float floatObjectValue, + final Character charObjectValue, + final Byte byteObjectValue, + final Date dateValue, + final LocalDate localDateValue, + final LocalTime localTimeValue, + final LocalDateTime localDateTimeValue, + final ZonedDateTime zonedlDateTimeValue, + final Instant instantValue, + final BigDecimal bigDecimalValue, + final Boolean booleanObjectValue) { + this.stringValue = stringValue; + this.integerObjectValue = integerObjectValue; + this.longObjectValue = longObjectValue; + this.shortObjectValue = shortObjectValue; + this.doubleObjectValue = doubleObjectValue; + this.floatObjectValue = floatObjectValue; + this.charObjectValue = charObjectValue; + this.byteObjectValue = byteObjectValue; + this.dateValue = dateValue; + this.localDateValue = localDateValue; + this.localTimeValue = localTimeValue; + this.localDateTimeValue = localDateTimeValue; + this.zonedlDateTimeValue = zonedlDateTimeValue; + this.instantValue = instantValue; + this.bigDecimalValue = bigDecimalValue; + this.booleanObjectValue = booleanObjectValue; + } + + public String getStringValue() { + return stringValue; + } + + public Integer getIntegerObjectValue() { + return integerObjectValue; + } + + public Long getLongObjectValue() { + return longObjectValue; + } + + public Short getShortObjectValue() { + return shortObjectValue; + } + + public Double getDoubleObjectValue() { + return doubleObjectValue; + } + + public Float getFloatObjectValue() { + return floatObjectValue; + } + + public Character getCharObjectValue() { + return charObjectValue; + } + + public Byte getByteObjectValue() { + return byteObjectValue; + } + + public Date getDateValue() { + return dateValue; + } + + public LocalDate getLocalDateValue() { + return localDateValue; + } + + public LocalTime getLocalTimeValue() { + return localTimeValue; + } + + public LocalDateTime getLocalDateTimeValue() { + return localDateTimeValue; + } + + public ZonedDateTime getZonedlDateTimeValue() { + return zonedlDateTimeValue; + } + + public Instant getInstantValue() { + return instantValue; + } + + public BigDecimal getBigDecimalValue() { + return bigDecimalValue; + } + + public Boolean getBooleanObjectValue() { + return booleanObjectValue; + } + +} \ No newline at end of file diff --git a/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyPrimitiveTypes.java b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyPrimitiveTypes.java new file mode 100644 index 0000000..1047ca7 --- /dev/null +++ b/src/test/java/org/exparity/stub/testutils/type/ConstructorOnlyPrimitiveTypes.java @@ -0,0 +1,71 @@ +package org.exparity.stub.testutils.type; + +public class ConstructorOnlyPrimitiveTypes { + + private final int integerValue; + private final long longValue; + private final short shortValue; + private final double doubleValue; + private final float floatValue; + private final char charValue; + private final byte byteValue; + private final int[] array; + private final boolean booleanValue; + + public ConstructorOnlyPrimitiveTypes(final int integerValue, + final long longValue, + final short shortValue, + final double doubleValue, + final float floatValue, + final char charValue, + final byte byteValue, + final int[] array, + final boolean booleanValue) { + this.integerValue = integerValue; + this.longValue = longValue; + this.shortValue = shortValue; + this.doubleValue = doubleValue; + this.floatValue = floatValue; + this.charValue = charValue; + this.byteValue = byteValue; + this.array = array; + this.booleanValue = booleanValue; + } + + public int getIntegerValue() { + return integerValue; + } + + public long getLongValue() { + return longValue; + } + + public short getShortValue() { + return shortValue; + } + + public double getDoubleValue() { + return doubleValue; + } + + public float getFloatValue() { + return floatValue; + } + + public char getCharValue() { + return charValue; + } + + public byte getByteValue() { + return byteValue; + } + + public int[] getArray() { + return array; + } + + public boolean isBooleanValue() { + return booleanValue; + } + +} \ No newline at end of file