Skip to content

Commit 729a7e3

Browse files
committed
issue #280 : exclude fields of type Class from being randomized
1 parent 59c9c73 commit 729a7e3

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

random-beans/src/main/java/io/github/benas/randombeans/randomizers/registry/InternalRandomizerRegistry.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.github.benas.randombeans.api.RandomizerRegistry;
3030
import io.github.benas.randombeans.randomizers.misc.BooleanRandomizer;
3131
import io.github.benas.randombeans.randomizers.misc.LocaleRandomizer;
32+
import io.github.benas.randombeans.randomizers.misc.SkipRandomizer;
3233
import io.github.benas.randombeans.randomizers.misc.UUIDRandomizer;
3334
import io.github.benas.randombeans.randomizers.net.UriRandomizer;
3435
import io.github.benas.randombeans.randomizers.net.UrlRandomizer;
@@ -103,6 +104,8 @@ public void init(EnhancedRandomParameters parameters) {
103104
randomizers.put(URI.class, new UriRandomizer(seed));
104105
randomizers.put(Locale.class, new LocaleRandomizer(seed));
105106
randomizers.put(UUID.class, new UUIDRandomizer(seed));
107+
// issue #280: skip fields of type Class
108+
randomizers.put(Class.class, new SkipRandomizer());
106109
}
107110

108111
@Override

random-beans/src/test/java/io/github/benas/randombeans/EnhancedRandomImplTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Set;
4949
import java.util.stream.Stream;
5050

51+
import io.github.benas.randombeans.beans.*;
5152
import org.junit.jupiter.api.BeforeEach;
5253
import org.junit.jupiter.api.Disabled;
5354
import org.junit.jupiter.api.Test;
@@ -58,16 +59,6 @@
5859
import io.github.benas.randombeans.api.EnhancedRandom;
5960
import io.github.benas.randombeans.api.ObjectGenerationException;
6061
import io.github.benas.randombeans.api.Randomizer;
61-
import io.github.benas.randombeans.beans.AbstractBean;
62-
import io.github.benas.randombeans.beans.Address;
63-
import io.github.benas.randombeans.beans.Gender;
64-
import io.github.benas.randombeans.beans.Human;
65-
import io.github.benas.randombeans.beans.ImmutableBean;
66-
import io.github.benas.randombeans.beans.Node;
67-
import io.github.benas.randombeans.beans.Person;
68-
import io.github.benas.randombeans.beans.Street;
69-
import io.github.benas.randombeans.beans.TestData;
70-
import io.github.benas.randombeans.beans.TestEnum;
7162
import io.github.benas.randombeans.randomizers.misc.ConstantRandomizer;
7263
import io.github.benas.randombeans.util.ReflectionUtils;
7364

@@ -272,6 +263,17 @@ public void nextEnumShouldNotAlwaysReturnTheSameValue() {
272263
assertThat(distinctEnumBeans.size()).isGreaterThan(1);
273264
}
274265

266+
@Test
267+
public void fieldsOfTypeClassShouldBeSkipped() {
268+
try {
269+
TestBean testBean = enhancedRandom.nextObject(TestBean.class);
270+
assertThat(testBean.getException()).isNotNull();
271+
assertThat(testBean.getClazz()).isNull();
272+
} catch (Exception e) {
273+
fail("Should skip fields of type Class");
274+
}
275+
}
276+
275277
private void validatePerson(final Person person) {
276278
assertThat(person).isNotNull();
277279
assertThat(person.getEmail()).isNotEmpty();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright (c) 2019, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package io.github.benas.randombeans.beans;
25+
26+
public class TestBean {
27+
private Exception exception; // contains a field of type Class via StackTraceElement
28+
private Class<?> clazz;
29+
30+
public Exception getException() {
31+
return exception;
32+
}
33+
34+
public void setException(Exception exception) {
35+
this.exception = exception;
36+
}
37+
38+
public Class<?> getClazz() {
39+
return clazz;
40+
}
41+
42+
public void setClazz(Class<?> clazz) {
43+
this.clazz = clazz;
44+
}
45+
}

0 commit comments

Comments
 (0)