本文整理汇总了Java中io.github.benas.randombeans.api.Randomizer类的典型用法代码示例。如果您正苦于以下问题:Java Randomizer类的具体用法?Java Randomizer怎么用?Java Randomizer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Randomizer类属于io.github.benas.randombeans.api包,在下文中一共展示了Randomizer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newInstance
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> Randomizer<T> newInstance(final Class<T> type, final RandomizerArgument[] randomizerArguments) {
try {
if (notEmpty(randomizerArguments)) {
Optional<Constructor<?>> matchingConstructor = asList(type.getConstructors())
.stream()
.filter(constructor -> hasSameArgumentNumber(constructor, randomizerArguments) &&
hasSameArgumentTypes(constructor, randomizerArguments))
.findFirst();
if (matchingConstructor.isPresent()) {
return (Randomizer<T>) matchingConstructor.get().newInstance(convertArguments(randomizerArguments));
}
}
return (Randomizer<T>) type.newInstance();
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
throw new ObjectGenerationException(format("Could not create Randomizer of type: %s with constructor arguments: %s", type, Arrays.toString(randomizerArguments)), e);
}
}
示例2: populateField
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException {
Randomizer<?> randomizer = getRandomizer(field);
if (randomizer instanceof SkipRandomizer) {
return;
}
context.pushStackItem(new RandomizationContextStackItem(target, field));
if(!context.hasExceededRandomizationDepth()) {
Object value;
if (randomizer != null) {
value = randomizer.getRandomValue();
} else {
try {
value = generateRandomValue(field, context);
} catch (ObjectGenerationException e) {
String exceptionMessage = String.format("Unable to create type: %s for field: %s of class: %s",
field.getType().getName(), field.getName(), target.getClass().getName());
throw new ObjectGenerationException(exceptionMessage, e);
}
}
setProperty(target, field, value);
}
context.popStackItem();
}
示例3: getRandomPrimitiveArray
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
Object getRandomPrimitiveArray(final Class<?> primitiveType) {
final int randomSize = abs((byte) enhancedRandom.nextInt());
final Randomizer<?> randomizer = randomizerProvider.getRandomizerByType(primitiveType);
final Object result = Array.newInstance(primitiveType, randomSize);
for (int index = 0; index < randomSize; index++) {
Array.set(result, index, randomizer.getRandomValue());
}
return result;
}
示例4: OptionalRandomizer
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
/**
* Create a new {@link OptionalRandomizer} with a delegate randomizer and an optional percent threshold.
*
* @param delegate The delegate to use to retrieve a random value
* @param optionalPercent The percent of randomized value to return (between 0 and 100)
*/
public OptionalRandomizer(final Randomizer<T> delegate, final int optionalPercent) {
this.delegate = delegate;
if (optionalPercent > MAX_PERCENT) {
this.optionalPercent = MAX_PERCENT;
} else if (optionalPercent < 0) {
this.optionalPercent = 0;
} else {
this.optionalPercent = optionalPercent;
}
}
示例5: getRandomizer
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
/**
* Retrieves a randomizer for the given field.
*
* @param field the field for which a randomizer was registered
* @return the randomizer registered for the given field
*/
@Override
public Randomizer<?> getRandomizer(Field field) {
if (field.isAnnotationPresent(io.github.benas.randombeans.annotation.Randomizer.class)) {
io.github.benas.randombeans.annotation.Randomizer randomizer = field.getAnnotation(io.github.benas.randombeans.annotation.Randomizer.class);
Class<?> type = randomizer.value();
RandomizerArgument[] arguments = randomizer.args();
return ReflectionUtils.newInstance(type, arguments);
}
return null;
}
示例6: getRandomizer
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
@Override
public Randomizer<?> getRandomizer(Field field) {
for (FieldDefinition<?, ?> fieldDefinition : customFieldRandomizersRegistry.keySet()) {
if (hasName(field, fieldDefinition.getName()) &&
isDeclaredInClass(field, fieldDefinition.getClazz()) &&
hasType(field, fieldDefinition.getType()) &&
isAnnotatedWithOneOf(field, fieldDefinition.getAnnotations()) &&
hasAllModifiers(field, fieldDefinition.getModifiers())) {
return customFieldRandomizersRegistry.get(fieldDefinition);
}
}
return getRandomizer(field.getType());
}
示例7: getRandomizer
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Randomizer<?> getRandomizer(Field field) {
for (FieldDefinition<?, ?> fieldDefinition : fieldDefinitions) {
if (hasName(field, fieldDefinition.getName()) &&
isDeclaredInClass(field, fieldDefinition.getClazz()) &&
hasType(field, fieldDefinition.getType()) &&
isAnnotatedWithOneOf(field, fieldDefinition.getAnnotations()) &&
hasAllModifiers(field, fieldDefinition.getModifiers())) {
return new SkipRandomizer();
}
}
return null;
}
示例8: asRandomizer
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
/**
* Create a dynamic proxy that adapts the given {@link Supplier} to a {@link Randomizer}.
* @param supplier to adapt
* @return the proxy randomizer
*/
static Randomizer<?> asRandomizer(final Supplier<?> supplier) {
return (Randomizer<?>) Proxy.newProxyInstance(
Randomizer.class.getClassLoader(),
new Class[]{Randomizer.class},
new RandomizerProxy(supplier));
}
示例9: getRandomizer
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
private Randomizer<?> getRandomizer(Field field) {
// issue 241: if there is no custom randomizer by field, then check by type
Randomizer<?> randomizer = randomizerProvider.getRandomizerByField(field);
if (randomizer == null) {
randomizer = randomizerProvider.getRandomizerByType(field.getType());
}
return randomizer;
}
示例10: getRandomizer
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
private Randomizer<?> getRandomizer(final Provider provider) {
return registries.stream()
.map(provider::getRandomizer)
.filter(Objects::nonNull)
.sorted(priorityComparator)
.findFirst().orElse(null);
}
示例11: theProviderShouldReturnTheSameRandomizerRegisteredForTheGivenField
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
@Test
public void theProviderShouldReturnTheSameRandomizerRegisteredForTheGivenField() throws NoSuchFieldException {
// Given
Field field = Foo.class.getDeclaredField("bar");
when(randomizerRegistry.getRandomizer(field)).thenReturn(randomizer);
// When
Randomizer<?> actual = randomizerProvider.getRandomizerByField(field);
// Then
assertThat(actual).isEqualTo(randomizer);
}
示例12: theProviderShouldReturnTheSameRandomizerRegisteredForTheGivenType
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
@Test
public void theProviderShouldReturnTheSameRandomizerRegisteredForTheGivenType() {
// Given
Class<String> type = String.class;
when(randomizerRegistry.getRandomizer(type)).thenReturn(randomizer);
// When
Randomizer<?> actual = randomizerProvider.getRandomizerByType(type);
// Then
assertThat(actual).isEqualTo(randomizer);
}
示例13: generatedNumberShouldNotBeNull
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
@TestTemplate
@UseDataProvider("generateRandomizers")
public void generatedNumberShouldNotBeNull(Randomizer<?> randomizer) {
// when
Object randomNumber = randomizer.getRandomValue();
then(randomNumber).isNotNull();
}
示例14: shouldGenerateTheSameValueForTheSameSeed
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
@TestTemplate
@UseDataProvider("generateSeededRandomizersAndTheirExpectedValues")
public void shouldGenerateTheSameValueForTheSameSeed(Randomizer<?> randomizer, Object expected) {
//when
Object actual = randomizer.getRandomValue();
then(actual).isEqualTo(expected);
}
示例15: generatedTimeShouldNotBeNull
import io.github.benas.randombeans.api.Randomizer; //导入依赖的package包/类
@TestTemplate
@UseDataProvider("generateRandomizers")
public void generatedTimeShouldNotBeNull(Randomizer<?> randomizer) {
// when
Object randomNumber = randomizer.getRandomValue();
then(randomNumber).isNotNull();
}