Skip to content

Commit

Permalink
test-arranger-19 Extend features delivered by easy-random-bean-valida…
Browse files Browse the repository at this point in the history
…tion - refactoring
  • Loading branch information
mjureczko committed Nov 5, 2021
1 parent e22932c commit 916e8de
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ocadotechnology.gembus</groupId>
<artifactId>test-arranger</artifactId>
<version>1.3</version>
<version>1.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-arranger</name>
<description>A tool for arranging test data with pseudo-random values.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static EasyRandomParameters getEasyRandomDefaultParameters() {
.objectPoolSize(calculateObjectPoolSize())
.objectFactory(new DecoratedObjectFactory(PropertiesWrapper.getCacheEnable()))
.stringLengthRange(STRING_MIN_LENGTH, STRING_MAX_LENGTH)
.seed(calculateSeed());
.seed(SeedHelper.calculateSeed());
}

static EasyRandomParameters getEasyRandomSimplifiedParameters() {
Expand All @@ -62,15 +62,7 @@ static EasyRandomParameters getEasyRandomSimplifiedParameters() {
.objectPoolSize(calculateObjectPoolSize())
.objectFactory(new DecoratedObjectFactory(PropertiesWrapper.getCacheEnable()))
.stringLengthRange(5, 10)
.seed(calculateSeed());
}

static long calculateSeed() {
long seed = EasyRandomParameters.DEFAULT_SEED;
if (PropertiesWrapper.getRandomSeedEnabled()) {
seed = System.nanoTime();
}
return seed;
.seed(SeedHelper.calculateSeed());
}

EnhancedRandom defaultRandom() {
Expand All @@ -85,7 +77,7 @@ EnhancedRandom randomForGivenConfiguration(Class<?> type, Map<Class<?>, CustomAr
EnhancedRandom.Builder randomBuilder = new EnhancedRandom.Builder(parametersSupplier);
CustomArranger<?> arrangerToUpdate = arrangers.get(type);
if (arrangerToUpdate == null) {
return randomBuilder.build(arrangers, calculateSeed());
return randomBuilder.build(arrangers, SeedHelper.calculateSeed());
} else {
return randomWithoutSelfReferenceThroughArranger(arrangers, randomBuilder, type);
}
Expand All @@ -104,16 +96,12 @@ private EnhancedRandom randomWithArrangers(Map<Class<?>, CustomArranger<?>> arra
EnhancedRandom random = randomWithoutSelfReferenceThroughArranger(arrangers, randomBuilder, clazz);
customArranger.setEnhancedRandom(random);
});
return randomBuilder.build(arrangers, calculateSeed());
return randomBuilder.build(arrangers, SeedHelper.calculateSeed());
}

private EnhancedRandom randomWithoutSelfReferenceThroughArranger(Map<Class<?>, CustomArranger<?>> arrangers, EnhancedRandom.Builder enhancedRandomBuilder, Class<?> type) {
final HashMap<Class<?>, CustomArranger<?>> forCustomArranger = new HashMap<>(arrangers);
forCustomArranger.remove(type);
return enhancedRandomBuilder.build(forCustomArranger, customArrangerTypeSpecificSeedRespectingRandomSeedSetting(type));
}

private long customArrangerTypeSpecificSeedRespectingRandomSeedSetting(Class<?> type) {
return calculateSeed() + type.getName().hashCode();
return enhancedRandomBuilder.build(forCustomArranger, SeedHelper.customArrangerTypeSpecificSeedRespectingRandomSeedSetting(type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public abstract class CustomArranger<T> {

protected EnhancedRandom enhancedRandom = new EnhancedRandom.Builder(ArrangersConfigurer::getEasyRandomDefaultParameters).build(new HashMap<>(), ArrangersConfigurer.calculateSeed());
protected EnhancedRandom enhancedRandom = new EnhancedRandom.Builder(ArrangersConfigurer::getEasyRandomDefaultParameters).build(new HashMap<>(), SeedHelper.calculateSeed());
protected final Class<T> type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright © 2020 Ocado (marian.jureczko@ocado.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ocadotechnology.gembus.test;

import org.jeasy.random.EasyRandomParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private EnhancedRandom(Map<Class<?>, CustomArranger<?>> arrangers, Supplier<Easy
this.parametersSupplier = parametersSupplier;
EasyRandomParameters parameters = parametersSupplier.get();
parameters.seed(seed);
addRandomizersToParameters(Optional.empty(), parameters, arrangers, seed);
addRandomizersToParameters(Optional.empty(), parameters, arrangers);
this.easyRandom = new EasyRandom(parameters);
}

Expand Down Expand Up @@ -104,15 +104,16 @@ private EasyRandomParameters addExclusionToParameters(Set<String> fields) {
return parameters;
}

private void addRandomizersToParameters(Optional<Class> typeToSkip, EasyRandomParameters parameters, Map<Class<?>, CustomArranger<?>> customArrangers, long seed) {
private void addRandomizersToParameters(Optional<Class> typeToSkip, EasyRandomParameters parameters, Map<Class<?>, CustomArranger<?>> customArrangers) {
for (Map.Entry<Class<?>, CustomArranger<?>> entry : customArrangers.entrySet()) {
if (entry.getKey() != typeToSkip.orElse(null)) {
final Class key = entry.getKey();
final Randomizer randomizer = customArrangerToRandomizer(entry.getValue());
parameters.randomize(key, randomizer);
}
}
parameters.randomizerRegistry(new CustomArrangerRandomizerRegistry(seed));
long newSeed = parameters.getSeed() + SeedHelper.customArrangerTypeSpecificSeedRespectingRandomSeedSetting(typeToSkip.orElse(CustomArranger.class));
parameters.randomizerRegistry(new CustomArrangerRandomizerRegistry(newSeed));
}

private Randomizer<?> customArrangerToRandomizer(CustomArranger arranger) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/ocadotechnology/gembus/test/SeedHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright © 2020 Ocado (marian.jureczko@ocado.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ocadotechnology.gembus.test;

import org.jeasy.random.EasyRandomParameters;

public class SeedHelper {

static long calculateSeed() {
long seed = EasyRandomParameters.DEFAULT_SEED;
if (PropertiesWrapper.getRandomSeedEnabled()) {
seed = System.nanoTime();
}
return seed;
}

static long customArrangerTypeSpecificSeedRespectingRandomSeedSetting(Class<?> type) {
return calculateSeed() + type.getName().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright © 2020 Ocado (marian.jureczko@ocado.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ocadotechnology.gembus.test;

import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit 916e8de

Please sign in to comment.