Skip to content

Commit

Permalink
Add TypeDefinition API
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Feb 21, 2019
1 parent 0fe66af commit 4a91de9
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ public <T, F> EnhancedRandomBuilder exclude(FieldDefinition<T, F> fieldDefinitio
return this;
}

/**
* Exclude a type from being populated.
*
* @param typeDefinition definition of the type to exclude
* @return a pre configured {@link EnhancedRandomBuilder} instance
*/
public EnhancedRandomBuilder exclude(TypeDefinition typeDefinition) {
exclusionRandomizerRegistry.addTypeDefinition(typeDefinition);
return this;
}

/**
* Exclude types from being populated.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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 lombok.Value;

/**
* Defines attributes used to identify a type.
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
@Value
public class TypeDefinition {

private final String package_;

/**
* Create a new {@link TypeDefinition}.
*
* @param package_ prefix of the package name. Example: "java.util"
*/
public TypeDefinition(String package_) {
this.package_ = package_;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 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;

/**
* Builder for {@link FieldDefinition}.
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public class TypeDefinitionBuilder {

private String package_;

/**
* Create a new {@link TypeDefinitionBuilder}.
*
* @return a new {@link TypeDefinitionBuilder}
*/
public static TypeDefinitionBuilder type() {
return new TypeDefinitionBuilder();
}

/**
* Specify the package name.
* The {@code name} can be a prefix like "java.util" or "com.sun".
*
* @param package_ the (prefix of) package name
* @return the configured {@link TypeDefinitionBuilder}
*/
public TypeDefinitionBuilder inPackage(String package_) {
this.package_ = package_;
return this;
}

/**
* Create a new {@link TypeDefinition}.
*
* @return a new {@link TypeDefinition}
*/
public TypeDefinition get() {
return new TypeDefinition(package_);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,15 @@ protected boolean isAnnotatedWithOneOf(final Field field, final Set<Class<? exte
protected boolean hasAllModifiers(final Field field, final Integer modifiers){
return modifiers == null || (modifiers & field.getModifiers()) == modifiers;
}

/**
* Check if a class is defined in a given package.
*
* @param clazz to check
* @param p enclosing package
* @return true if the class is defined in the package, false otherwise
*/
protected boolean isInPackage(Class<?> clazz, String p) {
return p == null || clazz.getPackage().getName().startsWith(p);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package io.github.benas.randombeans.randomizers.registry;

import io.github.benas.randombeans.FieldDefinition;
import io.github.benas.randombeans.TypeDefinition;
import io.github.benas.randombeans.annotation.Exclude;
import io.github.benas.randombeans.annotation.Priority;
import io.github.benas.randombeans.api.EnhancedRandomParameters;
Expand All @@ -46,6 +47,7 @@
public class ExclusionRandomizerRegistry extends AbstractRandomizerRegistry implements RandomizerRegistry {

private Set<FieldDefinition<?, ?>> fieldDefinitions = new HashSet<>();
private Set<TypeDefinition> typeDefinitions = new HashSet<>();

/**
* {@inheritDoc}
Expand Down Expand Up @@ -77,6 +79,11 @@ public Randomizer<?> getRandomizer(Field field) {
*/
@Override
public Randomizer<?> getRandomizer(Class<?> clazz) {
for (TypeDefinition typeDefinition : typeDefinitions) {
if (isInPackage(clazz, typeDefinition.getPackage_())) {
return new SkipRandomizer();
}
}
return null;
}

Expand All @@ -89,4 +96,13 @@ public void addFieldDefinition(final FieldDefinition<?, ?> fieldDefinition) {
fieldDefinitions.add(fieldDefinition);
}

/**
* Add a type definition.
*
* @param typeDefinition to add
*/
public void addTypeDefinition(final TypeDefinition typeDefinition) {
typeDefinitions.add(typeDefinition);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.benas.randombeans;

import io.github.benas.randombeans.api.EnhancedRandom;
import io.github.benas.randombeans.beans.Address;
import io.github.benas.randombeans.beans.Website;
import lombok.Data;
import org.junit.jupiter.api.Test;

import static io.github.benas.randombeans.TypeDefinitionBuilder.type;
import static org.assertj.core.api.Assertions.assertThat;

public class TypeExclusionTest {

@Test
void testPackageExclusion() {
// given
EnhancedRandom enhancedRandom = new EnhancedRandomBuilder()
.exclude(type().inPackage("io.github.benas.randombeans.beans").get())
.build();

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

// then
assertThat(foo).isNotNull();
// types from "io.github.benas.randombeans.beans" package should be excluded
assertThat(foo.getAddress()).isNull();
assertThat(foo.getWebsite()).isNull();
}

@Data
static class Foo {
private String name;
private Address address;
private Website website;
}
}

0 comments on commit 4a91de9

Please sign in to comment.