Skip to content

Commit

Permalink
test-arranger-62 MAX_RANDOMIZATION_DEPTH configurable through arrange…
Browse files Browse the repository at this point in the history
…r.properties
  • Loading branch information
mjureczko committed Apr 22, 2024
1 parent b63bf6f commit 131ed93
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ If you create `arranger.properties` file and save it in the root of classpath (u
Not always it is what you need in the tests, especially, when there is a convention in the project to initialize fields with empty values.
Fortunately, you can force test-arranger to overwrite the defaults with random values.
Set `arranger.overridedefaults` to true to override the default initialization.
* `arranger.maxRandomizationDepth`
Some test data structures can generate any length chains of objects that reference each other.
However, to effectively use them in a test case, it's crucial to control the length of these chains.
By default, Test-arranger stops creating new objects at the 4th level of nesting depth.
If this default setting does not suit your project test cases, it can be adjusted using this parameter.

## The challenges it solves

Expand Down
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.4.11</version>
<version>1.4.12</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 @@ -28,7 +28,6 @@ class ArrangersConfigurer {
static final int STRING_MIN_LENGTH = 9;
static final int STRING_MAX_LENGTH = 16;
static final int CACHE_SIZE = 15;
static final int MAX_RANDOMIZATION_DEPTH = 4;
static final AtomicBoolean defaultInitialized = new AtomicBoolean(false);
private static ArrangersConfigurer instance;

Expand All @@ -52,7 +51,7 @@ static ArrangersConfigurer instance() {
static EasyRandomParameters getEasyRandomDefaultParameters() {
return sharedParameters()
.collectionSizeRange(1, 4)
.randomizationDepth(MAX_RANDOMIZATION_DEPTH)
.randomizationDepth(PropertiesWrapper.getMaxRandomizationDepth())
.stringLengthRange(STRING_MIN_LENGTH, STRING_MAX_LENGTH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class NestingSafeExecutor {

public static <T> T execute(Class<T> type, Supplier<T> supplier) {
Integer nestingDepth = nestingLevel.get();
if (nestingDepth < ArrangersConfigurer.MAX_RANDOMIZATION_DEPTH) {
if (nestingDepth < PropertiesWrapper.getMaxRandomizationDepth()) {
try {
nestingLevel.set(++nestingDepth);
return supplier.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class PropertiesWrapper {
private static final String defaultRandomSeed = "false";
private static final String overrideDefaults = "arranger.overridedefaults";
private static final String defaultOverrideDefaults = "false";
static final String maxRandomizationDepth = "arranger.maxRandomizationDepth";
private static final String defaultMaxRandomizationDepth = "4";
private static final PropertiesFromFile propertiesFromFile = new PropertiesFromFile();

public static String getRootPackage() {
Expand All @@ -43,6 +45,10 @@ public static boolean getOverrideDefaults() {
return Boolean.parseBoolean(getPropertyValue(overrideDefaults, defaultOverrideDefaults));
}

public static int getMaxRandomizationDepth() {
return Integer.parseInt(getPropertyValue(maxRandomizationDepth, defaultMaxRandomizationDepth));
}

private static String getPropertyValue(String key, String defaultValue) {
String value = System.getProperties().getProperty(key);
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;


public class ArrangerTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public class ArrangerTestNestedObjects {

static final String FIXED_TEXT = "text";

@Test
public void shouldRespectConfiguredRandomizationDepth() {
//when
RecursiveObject actual = Arranger.some(RecursiveObject.class);

//then
assertThat(actual.array[0].array[0].array[0].array.length)
.as("Default randomization depth is 4, so the 4th array should be empty")
.isEqualTo(0);
}

@Test
public void avoidNullsInNestedObjectOnDeepestLevel() {
//when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.ocadotechnology.gembus.test;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -25,12 +26,36 @@ public class PropertiesWrapperTest {
private final String rootKey = "arranger.root";
private final String cacheKey = "arranger.cache.enable";
final static String overrideKey = "arranger.overridedefaults";
private final String maxDepthKey = "arranger.maxRandomizationDepth";

@AfterEach
public void cleanupProperties() {
System.getProperties().remove(rootKey);
System.getProperties().remove(cacheKey);
System.getProperties().remove(overrideKey);
System.getProperties().remove(maxDepthKey);
}

@Test
void shouldFindMaxDepthInSystemProperties() {
//given
int expected = Arranger.somePositiveInt(10);
System.setProperty(maxDepthKey, String.valueOf(expected));

//when
int actual = PropertiesWrapper.getMaxRandomizationDepth();

//then
assertEquals(expected, actual);
}

@Test
void shouldUseDefaultMaxDepth_whenNotSetInPropertiesFile() {
//when
int actual = PropertiesWrapper.getMaxRandomizationDepth();

//then
assertEquals(4, actual);
}

@Test
Expand Down

0 comments on commit 131ed93

Please sign in to comment.