Skip to content

Commit

Permalink
Add support for regular expressions when specifying a field name
Browse files Browse the repository at this point in the history
Resolves #322
  • Loading branch information
fmbenhassine committed Feb 12, 2019
1 parent 96b379d commit 5886032
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class FieldDefinition<T, F> {
/**
* Create a new {@link FieldDefinition}.
*
* @param name the field name
* @param name the field name (or regex to match a field name)
* @param type the filed type
* @param clazz the declaring class type
*/
Expand All @@ -63,7 +63,7 @@ public FieldDefinition(String name, Class<F> type, Class<T> clazz) {
/**
* Create a new {@link FieldDefinition}.
*
* @param name the field name
* @param name the field name (or regex to match a field name)
* @param type the filed type
* @param clazz the declaring class type
* @param annotations annotations present on the field
Expand All @@ -75,7 +75,7 @@ public FieldDefinition(String name, Class<F> type, Class<T> clazz, Set<Class <?
/**
* Create a new {@link FieldDefinition}.
*
* @param name the field name
* @param name the field name (or regex to match a field name)
* @param type the filed type
* @param clazz the declaring class type
* @param annotations annotations present on the field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static FieldDefinitionBuilder field() {

/**
* Specify the field name.
* The {@code name} can be a regular expression according to {@link java.util.regex.Pattern#compile(String)}.
*
* @param name the field name
* @return the configured {@link FieldDefinitionBuilder}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ protected boolean hasType(final Field field, final Class<?> type) {

/**
* Check if a {@code field} has the given {@code name}.
* {@code name} can be a regular expression according to {@link java.util.regex.Pattern#compile(String)}.
*
* @param field to check
* @param name of the field
* @return true if the field has the given name, false otherwise
* @return true if the field has the given name (or matches the given regex), false otherwise
*/
protected boolean hasName(final Field field, final String name) {
return name == null || field.getName().equals(name);
return name == null || java.util.regex.Pattern.compile(name).matcher(field.getName()).matches();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* 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 io.github.benas.randombeans;

import io.github.benas.randombeans.api.EnhancedRandom;
import io.github.benas.randombeans.api.Randomizer;
import org.junit.jupiter.api.Test;

import static io.github.benas.randombeans.FieldDefinitionBuilder.field;
import static org.assertj.core.api.Assertions.assertThat;

public class FieldNameMatchingWithRegex {

@lombok.Data
public class Foo {
private String name1;
private String name2;
private String nickname;
private String job;
private Bar bar;
}

@lombok.Data
public class Bar {
private String name;
private String address;
}

@Test
void testFieldDefinitionWithNameAsRegexp() {
// given
EnhancedRandom enhancedRandom = new EnhancedRandomBuilder()
.randomize(
field().named("name.*").ofType(String.class).get(),
(Randomizer<String>) () -> "foo")
.build();

// when
Foo foo = enhancedRandom.nextObject(Foo.class);

// then
assertThat(foo.getName1()).isEqualTo("foo");
assertThat(foo.getName2()).isEqualTo("foo");
assertThat(foo.getBar().getName()).isEqualTo("foo");

assertThat(foo.getNickname()).isNotEqualTo("foo");
assertThat(foo.getJob()).isNotEqualTo("foo");
assertThat(foo.getBar().getAddress()).isNotEqualTo("foo");
}

// non regression test
@Test
void testFieldDefinitionWithNameNotAsRegexp() {
// given
EnhancedRandom enhancedRandom = new EnhancedRandomBuilder()
.randomize(
field().named("name").ofType(String.class).get(),
(Randomizer<String>) () -> "foo")
.build();

// when
Foo foo = enhancedRandom.nextObject(Foo.class);

// then
assertThat(foo.getBar().getName()).isEqualTo("foo");

assertThat(foo.getName1()).isNotEqualTo("foo");
assertThat(foo.getName2()).isNotEqualTo("foo");
assertThat(foo.getNickname()).isNotEqualTo("foo");
assertThat(foo.getJob()).isNotEqualTo("foo");
assertThat(foo.getBar().getAddress()).isNotEqualTo("foo");
}
}

0 comments on commit 5886032

Please sign in to comment.