Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strategy interface for field/type exclusion
- Loading branch information
1 parent
a213600
commit 8ae7150
Showing
5 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
easy-random-core/src/main/java/org/jeasy/random/api/ExclusionPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2019, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jeasy.random.api; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* Strategy interface for field/type exclusion. | ||
* | ||
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) | ||
* @since 4.0 | ||
*/ | ||
public interface ExclusionPolicy { | ||
|
||
/** | ||
* Given the current randomization context, should the field be excluded from being randomized? | ||
* | ||
* @param field the field to check | ||
* @param context the current randomization context | ||
* @return true if the field should be excluded, false otherwise | ||
*/ | ||
boolean shouldBeExcluded(final Field field, final RandomizerContext context); | ||
|
||
/** | ||
* Given the current randomization context, should the type be excluded from being randomized? | ||
* | ||
* @param type the type to check | ||
* @param context the current randomization context | ||
* @return true if the type should be excluded, false otherwise | ||
*/ | ||
boolean shouldBeExcluded(final Class<?> type, final RandomizerContext context); | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
easy-random-core/src/test/java/org/jeasy/random/parameters/ExclusionPolicyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2019, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jeasy.random.parameters; | ||
|
||
import org.jeasy.random.EasyRandom; | ||
import org.jeasy.random.EasyRandomParameters; | ||
import org.jeasy.random.api.ExclusionPolicy; | ||
import org.jeasy.random.api.RandomizerContext; | ||
import org.jeasy.random.beans.Address; | ||
import org.jeasy.random.beans.Person; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ExclusionPolicyTests { | ||
|
||
@Test | ||
void testCustomExclusionPolicy() { | ||
// given | ||
EasyRandomParameters parameters = new EasyRandomParameters() | ||
.exclusionPolicy(new ExclusionPolicy() { | ||
@Override | ||
public boolean shouldBeExcluded(Field field, RandomizerContext context) { | ||
return field.getName().equals("birthDate"); | ||
} | ||
|
||
@Override | ||
public boolean shouldBeExcluded(Class<?> type, RandomizerContext context) { | ||
return type.isAssignableFrom(Address.class); | ||
} | ||
}); | ||
EasyRandom easyRandom = new EasyRandom(parameters); | ||
|
||
// when | ||
Person person = easyRandom.nextObject(Person.class); | ||
|
||
// then | ||
assertThat(person).isNotNull(); | ||
assertThat(person.getName()).isNotNull(); | ||
assertThat(person.getEmail()).isNotNull(); | ||
assertThat(person.getPhoneNumber()).isNotNull(); | ||
|
||
assertThat(person.getBirthDate()).isNull(); | ||
assertThat(person.getAddress()).isNull(); | ||
} | ||
} |