Skip to content

Commit

Permalink
fix naming of MyBeanTestData -> MyBeanTestDataFactory
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Hechtl <[email protected]>
  • Loading branch information
stefanhechtltng committed May 24, 2024
1 parent 758bf6c commit b222f59
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tngtech.valueprovider;

import com.google.common.collect.Iterables;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
Expand All @@ -10,21 +12,12 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.google.common.collect.Iterables;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.asList;
Expand Down Expand Up @@ -953,14 +946,14 @@ public final <T> T oneOfExcluding(Iterable<T> elements, T... elementsToExclude)
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* static class MyBeanTestDataFactory {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.listOfContaining(MyBeanTestData::myBean, myBean(), myBean(), myBean()); // -> List[myBean_1, myBean_2, myBean_random, myBean_3, myBean_random]
* vp.listOfContaining(MyBeanTestDataFactory::myBean, myBean(), myBean(), myBean()); // -> List[myBean_1, myBean_2, myBean_random, myBean_3, myBean_random]
* </pre>
* </p>
*
Expand Down Expand Up @@ -1007,14 +1000,14 @@ private <T> int maxNumberOfRandomElements(Collection<T> containedElements) {
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* static class MyBeanTestDataFactory {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.nonEmptyListOf(MyBeanTestData::myBean); // -> List[myBean_generated_1, myBean_generated_2]
* vp.nonEmptyListOf(MyBeanTestDataFactory::myBean); // -> List[myBean_generated_1, myBean_generated_2]
* </pre>
* </p>
*
Expand All @@ -1031,15 +1024,15 @@ public <T> List<T> nonEmptyListOf(Function<VP, T> generator) {
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* static class MyBeanTestDataFactory {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.listOf(MyBeanTestData::myBean); // -> List[myBean_generated_1, myBean_generated_2]
* vp.listOf(MyBeanTestData::myBean); // -> List[]
* vp.listOf(MyBeanTestDataFactory::myBean); // -> List[myBean_generated_1, myBean_generated_2]
* vp.listOf(MyBeanTestDataFactory::myBean); // -> List[]
* </pre>
* </p>
*
Expand All @@ -1056,14 +1049,14 @@ public <T> List<T> listOf(Function<VP, T> generator) {
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* static class MyBeanTestDataFactory {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.listOf(MyBeanTestData::myBean, 4); // -> List[myBean_generated_1, myBean_generated_2, myBean_generated_3, myBean_generated_4]
* vp.listOf(MyBeanTestDataFactory::myBean, 4); // -> List[myBean_generated_1, myBean_generated_2, myBean_generated_3, myBean_generated_4]
* </pre>
* </p>
*
Expand Down Expand Up @@ -1092,14 +1085,14 @@ public boolean booleanValue() {
* Generates an {@link Optional} of {@code <T>} by means of {@code generator}.
* Example:
* <pre>
* static class MyBeanTestData {
* static class MyBeanTestDataFactory {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.optionalOf(MyBeanTestData::myBean);
* vp.optionalOf(MyBeanTestDataFactory::myBean);
* vp.optionalOf(provider -> provider.randomString(10));
* </pre>
* </p>
Expand Down
75 changes: 23 additions & 52 deletions core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.tngtech.valueprovider;

import lombok.Data;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -9,56 +14,22 @@
import java.net.UnknownHostException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Duration;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Duration;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Duration;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import lombok.Data;

import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import static com.tngtech.valueprovider.ValueProvider.Builder;
import static com.tngtech.valueprovider.ValueProviderInitialization.createRandomInitialization;
import static com.tngtech.valueprovider.ValueProviderInitialization.createReproducibleInitialization;
import static com.tngtech.valueprovider.ValueProviderTest.MethodInvocation.assertDifferentResultAsFarAsPossible;
import static com.tngtech.valueprovider.ValueProviderTest.MethodInvocation.assertEqualResult;
import static com.tngtech.valueprovider.ValueProviderTest.MethodInvocation.invoke;
import static com.tngtech.valueprovider.ValueProviderTest.MyBeanTestData.myBeanContained;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.EIGHT;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.ELEVEN;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.FIVE;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.FOUR;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.NINE;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.ONE;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.SEVEN;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.SIX;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.TEN;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.THREE;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.TWELVE;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.TWO;
import static com.tngtech.valueprovider.ValueProviderTest.MethodInvocation.*;
import static com.tngtech.valueprovider.ValueProviderTest.MyBeanTestDataFactory.myBeanContained;
import static com.tngtech.valueprovider.ValueProviderTest.TestEnum.*;
import static java.time.LocalDateTime.now;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -109,8 +80,8 @@ private static Collection<MethodInvocation> allMethodInvocations() {
int max = random.intNumber(1001, 2000);
LocalDate start = random.localDateBetweenYears(min, max);
LocalDate end = random.localDateBetweenYears(
random.intNumber(3000, 4000),
random.intNumber(4001, 10000)
random.intNumber(3000, 4000),
random.intNumber(4001, 10000)
);
Duration duration = Duration.ofDays(Long.valueOf(min));
LocalTime time = random.localTime();
Expand Down Expand Up @@ -617,15 +588,15 @@ void localDateInBetween_should_generate_value_in_between() {
LocalDate start = LocalDate.of(2000, 1, 1);
LocalDate end = LocalDate.of(2040, 12, 31);
assertThat(withRandomValues().localDateBetween(start, end)).isAfterOrEqualTo(start)
.isBeforeOrEqualTo(end);
.isBeforeOrEqualTo(end);
}

@Test
void localDateInBetween_should_reject_temporal_inconsistency() {
LocalDate start = LocalDate.of(2000, 1, 1);
LocalDate end = LocalDate.of(2040, 12, 31);
Throwable thrown = assertThrows(IllegalArgumentException.class,
() -> withRandomValues().localDateBetween(end, start));
() -> withRandomValues().localDateBetween(end, start));
assertThat(thrown.getMessage()).contains("must be before or equal end ");
}

Expand All @@ -635,7 +606,7 @@ void localDateInPast_should_return_value_in_allowed_range() {
Duration duration = Duration.ofDays(300);
LocalDate pastDate = today.minusDays(duration.toDays());
assertThat(withRandomValues().localDateInPast(duration)).isAfterOrEqualTo(pastDate)
.isBeforeOrEqualTo(today);
.isBeforeOrEqualTo(today);
}

@Test
Expand All @@ -644,24 +615,24 @@ void localDateInFuture_should_return_value_in_allowed_range() {
Duration duration = Duration.ofDays(300);
LocalDate futureDate = today.plusDays(duration.toDays());
assertThat(withRandomValues().localDateInFuture(duration)).isAfterOrEqualTo(today)
.isBeforeOrEqualTo(futureDate);
.isBeforeOrEqualTo(futureDate);
}

@Test
void localDateTimeInBetween_should_generate_value_in_between() {
LocalDate start = LocalDate.of(2000, 1, 1);
LocalDate end = LocalDate.of(2040, 12, 31);
assertThat(withRandomValues().localDateTimeBetween(start, end))
.isAfterOrEqualTo(LocalDateTime.of(start, LocalTime.MIN))
.isBeforeOrEqualTo(LocalDateTime.of(end, LocalTime.MAX));
.isAfterOrEqualTo(LocalDateTime.of(start, LocalTime.MIN))
.isBeforeOrEqualTo(LocalDateTime.of(end, LocalTime.MAX));
}

@Test
void localDateTimeInBetween_should_reject_temporal_inconsistency() {
LocalDate start = LocalDate.of(2000, 1, 1);
LocalDate end = LocalDate.of(2040, 12, 31);
Throwable thrown = assertThrows(IllegalArgumentException.class,
() -> withRandomValues().localDateTimeBetween(end, start));
() -> withRandomValues().localDateTimeBetween(end, start));
assertThat(thrown.getMessage()).contains("must be before or equal end ");
}

Expand Down Expand Up @@ -832,7 +803,7 @@ void listOf_should_return_provided_number_of_elements() {
int numberOfElements = random.intNumber(5, 10);

// when
List<MyBean> myBeans = random.listOf(MyBeanTestData::myBean, numberOfElements);
List<MyBean> myBeans = random.listOf(MyBeanTestDataFactory::myBean, numberOfElements);

// then
assertThat(myBeans).hasSize(numberOfElements);
Expand All @@ -844,7 +815,7 @@ void listOf_should_return_a_sensible_number_of_elements() {
ValueProvider random = withRandomValues();

// when
List<MyBean> myBeans = random.listOf(MyBeanTestData::myBean);
List<MyBean> myBeans = random.listOf(MyBeanTestDataFactory::myBean);

// then
assertThat(myBeans).hasSizeLessThanOrEqualTo(5); // 5 is the default
Expand All @@ -856,7 +827,7 @@ void nonEmptyListOf_should_return_at_least_one_element() {
ValueProvider random = withRandomValues();

// when
List<MyBean> myBeans = random.nonEmptyListOf(MyBeanTestData::myBean);
List<MyBean> myBeans = random.nonEmptyListOf(MyBeanTestDataFactory::myBean);

// then
assertThat(myBeans).isNotEmpty()
Expand All @@ -869,7 +840,7 @@ void listOfContaining_should_return_the_provided_elements_plus_some_randomly_gen
ValueProvider random = withRandomValues();

// when
List<MyBean> myBeans = random.listOfContaining(MyBeanTestData::myBean, myBeanContained(1), myBeanContained(2), myBeanContained(3));
List<MyBean> myBeans = random.listOfContaining(MyBeanTestDataFactory::myBean, myBeanContained(1), myBeanContained(2), myBeanContained(3));

// then
assertThat(myBeans).hasSizeLessThanOrEqualTo(7)
Expand Down Expand Up @@ -937,7 +908,7 @@ enum TestEnum {
enum EmptyTestEnum {
}

static class MyBeanTestData {
static class MyBeanTestDataFactory {
public static MyBean myBean(ValueProvider valueProvider) {
return new MyBean("randomly generated" + valueProvider.intNumber(0, Integer.MAX_VALUE));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.tngtech.valueprovider.example.customprovider;

import java.util.Random;

import com.tngtech.valueprovider.ValueProviderExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static com.tngtech.valueprovider.example.customprovider.CustomValueProviderFactoryTest.MyBeanTestData.myBean;
import java.util.Random;

import static com.tngtech.valueprovider.example.customprovider.CustomValueProviderFactoryTest.MyBeanTestDataFactory.myBean;
import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("JoinAssertThatStatements")
Expand Down Expand Up @@ -44,10 +44,10 @@ void createReproducibleValueProvider_should_create_providers_that_return_same_se
void type_inference_should_work_for_listOf_in_a_custom_ValueProvider() {
CustomValueProvider customValueProvider = CustomValueProviderFactory.createRandomValueProvider();
customValueProvider.listOf(vp -> myBean());
customValueProvider.listOf(MyBeanTestData::myBean);
customValueProvider.listOf(MyBeanTestDataFactory::myBean);
}

static class MyBeanTestData {
static class MyBeanTestDataFactory{
public static MyBean myBean() {
return new MyBean("not using VP");
}
Expand Down

0 comments on commit b222f59

Please sign in to comment.