From 916e8defdd42ee53b46d601f2dee2c8e90c1396d Mon Sep 17 00:00:00 2001 From: Marian Jureczko Date: Fri, 5 Nov 2021 13:16:42 +0100 Subject: [PATCH] test-arranger-19 Extend features delivered by easy-random-bean-validation - refactoring --- pom.xml | 2 +- .../gembus/test/ArrangersConfigurer.java | 22 +++---------- .../gembus/test/CustomArranger.java | 2 +- .../CustomArrangerRandomizerRegistry.java | 15 +++++++++ .../gembus/test/EnhancedRandom.java | 7 ++-- .../gembus/test/SeedHelper.java | 33 +++++++++++++++++++ .../ArrangerBeanValidationSupportTest.java | 15 +++++++++ 7 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/ocadotechnology/gembus/test/SeedHelper.java diff --git a/pom.xml b/pom.xml index f4d8fcc..1d0b763 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ 4.0.0 com.ocadotechnology.gembus test-arranger - 1.3 + 1.4-SNAPSHOT jar test-arranger A tool for arranging test data with pseudo-random values. diff --git a/src/main/java/com/ocadotechnology/gembus/test/ArrangersConfigurer.java b/src/main/java/com/ocadotechnology/gembus/test/ArrangersConfigurer.java index a88d449..67c1ef7 100644 --- a/src/main/java/com/ocadotechnology/gembus/test/ArrangersConfigurer.java +++ b/src/main/java/com/ocadotechnology/gembus/test/ArrangersConfigurer.java @@ -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() { @@ -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() { @@ -85,7 +77,7 @@ EnhancedRandom randomForGivenConfiguration(Class type, Map, 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); } @@ -104,16 +96,12 @@ private EnhancedRandom randomWithArrangers(Map, 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, CustomArranger> arrangers, EnhancedRandom.Builder enhancedRandomBuilder, Class type) { final HashMap, 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)); } } diff --git a/src/main/java/com/ocadotechnology/gembus/test/CustomArranger.java b/src/main/java/com/ocadotechnology/gembus/test/CustomArranger.java index be533e1..6678da7 100644 --- a/src/main/java/com/ocadotechnology/gembus/test/CustomArranger.java +++ b/src/main/java/com/ocadotechnology/gembus/test/CustomArranger.java @@ -32,7 +32,7 @@ */ public abstract class CustomArranger { - 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 type = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; /** diff --git a/src/main/java/com/ocadotechnology/gembus/test/CustomArrangerRandomizerRegistry.java b/src/main/java/com/ocadotechnology/gembus/test/CustomArrangerRandomizerRegistry.java index d7d5c72..d7581c0 100644 --- a/src/main/java/com/ocadotechnology/gembus/test/CustomArrangerRandomizerRegistry.java +++ b/src/main/java/com/ocadotechnology/gembus/test/CustomArrangerRandomizerRegistry.java @@ -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; diff --git a/src/main/java/com/ocadotechnology/gembus/test/EnhancedRandom.java b/src/main/java/com/ocadotechnology/gembus/test/EnhancedRandom.java index 4b7e593..9734160 100644 --- a/src/main/java/com/ocadotechnology/gembus/test/EnhancedRandom.java +++ b/src/main/java/com/ocadotechnology/gembus/test/EnhancedRandom.java @@ -50,7 +50,7 @@ private EnhancedRandom(Map, CustomArranger> arrangers, Supplier fields) { return parameters; } - private void addRandomizersToParameters(Optional typeToSkip, EasyRandomParameters parameters, Map, CustomArranger> customArrangers, long seed) { + private void addRandomizersToParameters(Optional typeToSkip, EasyRandomParameters parameters, Map, CustomArranger> customArrangers) { for (Map.Entry, CustomArranger> entry : customArrangers.entrySet()) { if (entry.getKey() != typeToSkip.orElse(null)) { final Class key = entry.getKey(); @@ -112,7 +112,8 @@ private void addRandomizersToParameters(Optional typeToSkip, EasyRandomPa 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) { diff --git a/src/main/java/com/ocadotechnology/gembus/test/SeedHelper.java b/src/main/java/com/ocadotechnology/gembus/test/SeedHelper.java new file mode 100644 index 0000000..aad14f2 --- /dev/null +++ b/src/main/java/com/ocadotechnology/gembus/test/SeedHelper.java @@ -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(); + } +} diff --git a/src/test/java/com/ocadotechnology/gembus/test/ArrangerBeanValidationSupportTest.java b/src/test/java/com/ocadotechnology/gembus/test/ArrangerBeanValidationSupportTest.java index a10733b..1544d02 100644 --- a/src/test/java/com/ocadotechnology/gembus/test/ArrangerBeanValidationSupportTest.java +++ b/src/test/java/com/ocadotechnology/gembus/test/ArrangerBeanValidationSupportTest.java @@ -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;