-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fe66af
commit 4a91de9
Showing
6 changed files
with
186 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
random-beans/src/main/java/io/github/benas/randombeans/TypeDefinition.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,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_; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
random-beans/src/main/java/io/github/benas/randombeans/TypeDefinitionBuilder.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,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_); | ||
} | ||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
random-beans/src/test/java/io/github/benas/randombeans/TypeExclusionTest.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,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; | ||
} | ||
} |