Skip to content

Commit 31ab460

Browse files
nomemoryCiobanu, Andrei-Nicolin (UK - EDC)
authored andcommitted
Added mockunit that can cycle through an Iterable for obtaining values.
1 parent e83a252 commit 31ab460

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.andreinc.mockneat.github;
2+
3+
import net.andreinc.mockneat.MockNeat;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* Created by andreinicolinciobanu on 11/05/17.
9+
*/
10+
public class GenerateShufflers {
11+
public static void main(String[] args) {
12+
13+
MockNeat mockNeat = MockNeat.threadLocal();
14+
15+
int[] x = { 1, 2, 3, 4, 5, 6 };
16+
mockNeat.shufflers()
17+
.arrayInt(x)
18+
.stream().val()
19+
.limit(5)
20+
.forEach(arr -> System.out.println(Arrays.toString(arr)));
21+
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.andreinc.mockneat.github;
2+
3+
import net.andreinc.mockneat.MockNeat;
4+
5+
import java.util.List;
6+
7+
import static java.util.stream.Collectors.toList;
8+
import static net.andreinc.mockneat.MockNeat.threadLocal;
9+
10+
/**
11+
* Created by andreinicolinciobanu on 11/05/17.
12+
*/
13+
public class HrSchema {
14+
15+
private static final int REG_START = 0;
16+
private static final int REG_END = 5;
17+
public static final int COUNTRY_NUM = 100;
18+
19+
public static void main(String[] args) {
20+
21+
}
22+
23+
public List<String> generateCountries() {
24+
MockNeat mockNeat = threadLocal();
25+
26+
return mockNeat.fmt("INSERT INTO countries VALUES ('#{code}', '#{name}', #{id}, #{region_id});")
27+
.param("code", mockNeat.countries().iso2())
28+
.param("name", mockNeat.countries().names())
29+
.param("id", mockNeat.longSeq())
30+
.param("region_id", mockNeat.ints().range(REG_START, REG_END))
31+
.set(COUNTRY_NUM).val().stream()
32+
.collect(toList());
33+
}
34+
}

src/main/java/net/andreinc/mockneat/MockNeat.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import net.andreinc.mockneat.unit.regex.Regex;
3333
import net.andreinc.mockneat.unit.seq.IntSeq;
3434
import net.andreinc.mockneat.unit.seq.LongSeq;
35+
import net.andreinc.mockneat.unit.seq.Seq;
3536
import net.andreinc.mockneat.unit.text.*;
3637
import net.andreinc.mockneat.unit.time.Days;
3738
import net.andreinc.mockneat.unit.time.LocalDates;
@@ -230,6 +231,8 @@ public <T, FT> Factory<T, FT> factory(Class<T> targetCls, Class<FT> factoryCls)
230231

231232
public <T> Reflect<T> reflect(Class<T> cls) { return new Reflect<>(this, cls);}
232233

234+
public <T> Seq<T> seq(Iterable<T> iterable) { return new Seq(this, iterable); }
235+
233236
public Shufflers shufflers() { return rShufflers; }
234237

235238
public Strings strings() { return new Strings(this); }
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package net.andreinc.mockneat.unit.seq;
2+
3+
/**
4+
* Copyright 2017, Andrei N. Ciobanu
5+
6+
Permission is hereby granted, free of charge, to any user obtaining a copy of this software and associated
7+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
8+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
9+
persons to whom the Software is furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
12+
Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
15+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17+
OTHERWISE, ARISING FROM, FREE_TEXT OF OR PARAM CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS PARAM THE SOFTWARE.
18+
*/
19+
20+
import net.andreinc.mockneat.MockNeat;
21+
import net.andreinc.mockneat.interfaces.MockUnit;
22+
23+
import java.util.Iterator;
24+
import java.util.function.Supplier;
25+
26+
import static net.andreinc.mockneat.utils.ValidationUtils.IMPOSSIBLE_TO_SEQ_OVER_EMPTY_COLLECTION;
27+
import static net.andreinc.mockneat.utils.ValidationUtils.isTrue;
28+
import static net.andreinc.mockneat.utils.ValidationUtils.notNull;
29+
30+
31+
// TODO - documentation
32+
public class Seq<T> implements MockUnit<T> {
33+
34+
private final MockNeat mockNeat;
35+
36+
private final Iterable<T> iterable;
37+
private Iterator<T> iterator;
38+
39+
private boolean cycle = false;
40+
private T after = null;
41+
42+
public Seq(MockNeat mockNeat, Iterable<T> iterable) {
43+
44+
notNull(mockNeat, "mockNeat");
45+
notNull(iterable, "iterable");
46+
47+
this.mockNeat = mockNeat;
48+
this.iterable = iterable;
49+
this.iterator = iterable.iterator();
50+
51+
isTrue(iterator.hasNext(), IMPOSSIBLE_TO_SEQ_OVER_EMPTY_COLLECTION);
52+
}
53+
54+
public Seq<T> cycle(boolean value) {
55+
this.cycle = value;
56+
return this;
57+
}
58+
59+
public Seq<T> after(T after) {
60+
this.after = after;
61+
return this;
62+
}
63+
64+
@Override
65+
public Supplier<T> supplier() {
66+
return () -> {
67+
if (iterator.hasNext())
68+
return (T) iterator.next();
69+
else
70+
if (cycle) {
71+
this.iterator = iterable.iterator();
72+
return (T) iterator.next();
73+
}
74+
else
75+
return(T) after;
76+
};
77+
}
78+
}

src/main/java/net/andreinc/mockneat/utils/ValidationUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class ValidationUtils {
6868
public static final String INVALID_REGEX_PATTERN = "Invalid regex pattern ('#{pattern}'): ";
6969
public static final String OBJECT_NOT_SERIALIZABLE = "Object is not serializable. Does the object's class extends 'java.io.Serializable' ?";
7070
public static final String PROBABILITY_NOT_NEGATIVE = "Probability '#{prob}' should be bigger than '0.0'.";
71+
public static final String IMPOSSIBLE_TO_SEQ_OVER_EMPTY_COLLECTION = "Impossible to create a Seq from an empty Iterable<T>.";
7172

7273
private ValidationUtils() {}
7374

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package net.andreinc.mockneat.unit.seq;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static java.util.stream.IntStream.range;
9+
import static net.andreinc.mockneat.Constants.M;
10+
import static org.junit.Assert.assertTrue;
11+
12+
/**
13+
* Copyright 2017, Andrei N. Ciobanu
14+
15+
Permission is hereby granted, free of charge, to any user obtaining a copy of this software and associated
16+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
17+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
18+
persons to whom the Software is furnished to do so, subject to the following conditions:
19+
20+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
21+
Software.
22+
23+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
25+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26+
OTHERWISE, ARISING FROM, FREE_TEXT OF OR PARAM CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS PARAM THE SOFTWARE.
27+
*/
28+
29+
public class SeqTest {
30+
31+
@Test(expected = NullPointerException.class)
32+
public void testSeqNullList() throws Exception {
33+
M.seq(null).list(100).val();
34+
}
35+
36+
@Test(expected = IllegalArgumentException.class)
37+
public void testEmptyList() throws Exception {
38+
M.seq(new ArrayList<String>()).list(100).val();
39+
}
40+
41+
@Test
42+
public void testSeq() throws Exception {
43+
List<Integer> arr = M.intSeq().max(100).cycle(false).list(100).val();
44+
List<Integer> seq = M.seq(arr).cycle(false).list(10).val();
45+
46+
seq.forEach(i -> assertTrue(seq.get(i) == i));
47+
}
48+
49+
@Test
50+
public void testTwoElementsCycle() throws Exception {
51+
List<Integer> twoElements = new ArrayList<>();
52+
53+
twoElements.add(1);
54+
twoElements.add(2);
55+
56+
List<Integer> seq = M.seq(twoElements).cycle(true).list(100).val();
57+
58+
range(0, seq.size()).forEach(i -> {
59+
if (i%2==0)
60+
assertTrue(seq.get(i) == 1);
61+
else
62+
assertTrue(seq.get(i) == 2);
63+
});
64+
}
65+
66+
@Test
67+
public void testOneElementNoCycle() throws Exception {
68+
List<Integer> oneElement = new ArrayList<>();
69+
70+
oneElement.add(2);
71+
72+
List<Integer> seq = M.seq(oneElement).list(10).val();
73+
74+
assertTrue(seq.get(0) == 2);
75+
assertTrue(seq.get(1) == null);
76+
}
77+
78+
@Test
79+
public void testOneElementNoCycleAfterNonNull() throws Exception {
80+
List<Integer> oneElement = new ArrayList<>();
81+
82+
oneElement.add(2);
83+
84+
List<Integer> seq = M.seq(oneElement).after(4).list(10).val();
85+
86+
assertTrue(seq.get(0) == 2);
87+
assertTrue(seq.get(1) == 4);
88+
}
89+
}

0 commit comments

Comments
 (0)